简体   繁体   English

LP model 没有给出预期的答案

[英]LP model does not give expected answer

I want to use gurobi instead of scipy, but I am not getting the same answer which I get in scipy, Could someone help me what went wrong here?我想使用 gurobi 而不是 scipy,但我没有得到与 scipy 相同的答案,有人可以帮我这里出了什么问题吗?

import gurobipy as gp
from scipy.optimize import linprog
import numpy as np
 
lp_m = gp.Model()
w = np.array([1., 5., 1.])
halfspaces = np.array([
[1.*w[0], 1.*w[1], 1.*w[2], -10 ],
[ 1., 0., 0., -4],
[ 0., 1., 0., -4],
[ 0., 0., 1., -4],
[-1., 0., 0., 0],
[ 0., -1., 0., 0],
[ 0., 0., -1., 0]
])
 
A = halfspaces[:,0:3]
b = -1*halfspaces[:,-1]
cost = np.zeros(A.shape[1])
 
opt_x = lp_m.addMVar((A.shape[1],), name="x")
lp_m.setObjective(cost@opt_x)
lp_m.addConstr(A@opt_x <= b)
lp_m.optimize()
print(opt_x.X) #  [0. 0. 0.]
 
res = linprog(c=cost, A_ub=A, b_ub=b, method='interior-point')
print(res.x) # [1.65708642 1.040279 1.65708642]

You are using a zero objective here.您在这里使用的是零目标。 Depending on the algorithm that is used, whichever feasible solution is found first will be reported as "the solution".根据所使用的算法,首先找到的可行解决方案将被报告为“解决方案”。 The interior point method will always target a center solution while the simplex will return a vertex solution - they are never going to be exactly equal without any post-processing.内点法将始终以中心解为目标,而单纯形法将返回顶点解——如果没有任何后处理,它们永远不会完全相等。

You should just rerun the test with a non-zero objective to better compare the two solutions.您应该使用非零目标重新运行测试,以更好地比较两种解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM