Python linprog最大化目标函数

时间:2015-10-13 13:44:49

标签: python optimization scipy linear-programming

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

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.]

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

现在在其他软件包中,我的最大化目标函数是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)

但是,我收到了消息:&#39;优化失败。无法找到一个可行的起点。&#39;&#34;

1 个答案:

答案 0 :(得分:1)

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

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

您的结果应为-841。

这只是因为:

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