Warm tip: This article is reproduced from serverfault.com, please click

LP model does not give expected answer

发布于 2020-12-01 09:09:37

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?

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]
Questioner
GPrathap
Viewed
0
mattmilten 2020-12-02 05:31:53

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.