Convert a LP in a form of matrix
AnsweredI try to convert a LP in a form of matrix, i.e., Ax <=b. The code is shown below. However, the optimal solution value is different. What dis I miss?
import gurobipy as gp
from gurobipy import GRB
import numpy as np
m=gp.read('expPP.mps')
m.optimize()
with result
Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)
CPU model: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 6 physical cores, 6 logical processors, using up to 6 threads
Optimize a model with 2992 rows, 2992 columns and 7976 nonzeros
Model fingerprint: 0x364e7d38
Coefficient statistics:
Matrix range [1e+00, 1e+02]
Objective range [1e+00, 2e+01]
Bounds range [1e+00, 5e+03]
RHS range [1e+00, 9e+01]
Presolve removed 1995 rows and 1001 columns
Presolve time: 0.01s
Presolved: 997 rows, 1992 columns, 2988 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 5.6468667e+02 7.066250e+02 0.000000e+00 0s
1215 -2.8126036e+04 0.000000e+00 0.000000e+00 0s
Solved in 1215 iterations and 0.01 seconds (0.01 work units)
Optimal objective -2.812603633e+04
I extract the A, b, and c
A = m.getA()
rhs = m.getAttr("RHS",m.getConstrs())
obj = m.getAttr("Obj",m.getVars())
define a new model with these coefficients
mA = gp.Model()
x = mA.addMVar(shape=A.shape[1], lb=0, vtype=GRB.CONTINUOUS)
mA.addConstr(A @ x <= np.array(rhs))
mA.setObjective(np.array(obj) @ x, GRB.MAXIMIZE)
mA.optimize()
but the result is different
Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)
CPU model: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 6 physical cores, 6 logical processors, using up to 6 threads
Optimize a model with 2992 rows, 2992 columns and 7976 nonzeros
Model fingerprint: 0xa6385b94
Coefficient statistics:
Matrix range [1e+00, 1e+02]
Objective range [1e+00, 2e+01]
Bounds range [0e+00, 0e+00]
RHS range [1e+00, 9e+01]
Presolve removed 2992 rows and 2992 columns
Presolve time: 0.01s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 -5.3220000e+03 0.000000e+00 0.000000e+00 0s
Solved in 0 iterations and 0.01 seconds (0.00 work units)
Optimal objective -5.322000000e+03-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
From the coefficient statistics you can see
Coefficient statistics: Matrix range [1e+00, 1e+02] Objective range [1e+00, 2e+01] Bounds range [1e+00, 5e+03] RHS range [1e+00, 9e+01]
vs
Coefficient statistics: Matrix range [1e+00, 1e+02] Objective range [1e+00, 2e+01] Bounds range [0e+00, 0e+00] RHS range [1e+00, 9e+01]
It looks like you need to set variable bounds.
Best regards,
Jaromił0 -
Thanks a lot. beside, I found that not only the variable bounds is not set, but some row of matrix A and b must be reset according to the corresponding constr sense, the code is
for i in range(len(m.getConstrs())):
if m.getConstrs()[i].sense == '>':
A[i,:]=A[i,:]*(-1)
rhs[i]=rhs[i]*(-1)I thought that gurobi can automatically give the corresponding matrix Ax<=b, but it is not. By the way, it seems that for constr sense of '=', that part of matrix Ax<=b given by gurobi is correct.
0 -
You can make it better by using the addMConstr method. To use it, you have to also extract constraint senses as you did with RHS.
0
Post is closed for comments.
Comments
4 comments