Python linprog 最大化 objective 函数

Python linprog to maximise objective function

我已经有一段时间没有这样做了,所以我有点生疏,但等式是:

max t(C)*x
s.t. Ax <=b

我有我的 A 约束矩阵,它是 (1448x1359) :

[[ 1.  1.  0. ...,  0.  0.  0.]

 ..., 


 [ 0.  0.  0. ...,  1.  1.  1.]]

然后我有我的绑定 b (1448x1):

[ 1.  1.  7. ...,  2.  1.  2.]

我的 objective 函数要最大化,它是一个向量 (1359,1)。

现在在其他包中,我最大化的 objective 函数是 841,但是使用 linprog:

res = linprog(c=OBJ_N, A_ub=A, b_ub=b, options={"disp": True})

它已成功优化到 -0.0,所以我想知道我是否在 python 中使用了正确的命令并且我的约束是否正确?

编辑:好的,这是有道理的,它试图最小化。我现在已经重写了(交换了 c 和 b 并将 A 转置为最小化)。

# (max t(C)*x s.t. Ax <=b) = min t(b)*x s.t. ATy = c, y ≥ 0
# (i): minimise number of shops no bounds
ID = np.ones(len(w[0]))
print(ID)
print(ID.shape)  #1359

At = A.transpose()

need_divest = (A.dot(ID)) - 1
print(need_divest)
print(need_divest.shape)  #1448

res = linprog(c=need_divest, A_eq=At, b_eq=ID, options={"disp": True})
print(res)

但是,我得到 "message: 'Optimzation failed. Unable to find a feasible starting point.'"

我猜你可能 minimizing 而不是 maximizing 你的 objective 函数。 试试这个(在 objective 函数系数前插入 -):

res = linprog(c=-OBJ_N, A_ub=A, b_ub=b, options={"disp": True})

那么您的结果应该是 -841。

之所以有效,是因为:

min(f(x))=-max(-f(x))