我想使用scipy.optimize最小化大量线性不等式上的函数(最终是非线性的)。作为热身,我正在尝试在框0 <= x <= 1、0 <= y <= 1的情况下最小化x + y。按照以下Johnny Drama的建议,我目前正在使用dict-comprehesion生成不等式的字典,但没有得到预期的答案(最小值= 0,最小值为(0,0))。
新的代码部分(当前相关):
import numpy as np
from scipy.optimize import minimize
#Create initial point.
x0=[.1,.1]
#Create function to be minimized
def obj(x):
return x[0]+x[1]
#Create linear constraints lbnd<= A*(x,y)^T<= upbnd
A=np.array([[1,0],[0,1]])
b1=np.array([0,0])
b2=np.array([1,1])
cons=[{"type": "ineq", "fun": lambda x: np.matmul(A[i, :],x) -b1[i]} for i in range(A.shape[0])]
cons2=[{"type": "ineq", "fun": lambda x: b2[i]-np.matmul(A[i, :], x) } for i in range(A.shape[0])]
cons.extend(cons2)
sol=minimize(obj,x0,constraints=cons)
print(sol)
Run Code Online (Sandbox Code Playgroud)
问题的原始版本:
我想在scipy.optimize中使用LinearConstraint对象,如此处的教程中所述:“定义线性约束”
我试图做一个简单的例子,答案很明显:在平方0 <= x <= 1,0 …
我想使用 COIN-CBC(或 PuLP 提供的任何其他免费 MIP 求解器)求解一个小型混合整数程序,但时间限制为 10 秒。但是, maxSeconds 参数似乎对我不起作用。
举个例子,我这样调用没有时间限制的求解器:
prob.solve(pulp.PULP_CBC_CMD())
Run Code Online (Sandbox Code Playgroud)
我这样称呼它有时间限制:
prob.solve(pulp.PULP_CBC_CMD(maxSeconds=10))
Run Code Online (Sandbox Code Playgroud)
前者在 50.89 秒后终止,解值为 15.65287864835175。后者在 53.53 秒后终止,解值为 15.65287864835175。我预计它会在(大约)10 秒内终止,可能具有更高的解值。
(我知道这篇文章:Time limit for mix integerprogramming with Python PuLP。但它的答案参考了 CPLEX 和 GUROBI,我无法使用它们;我需要一个免费的求解器。)
难道我做错了什么?