Error in Setting Objective
回答済みDear Sir,
I am trying to run this code in which I am using an MPS data file. I am extracting the coefficient matrix, the right-hand side vector, and the cost vector for an optimization problem from the MPS file. It shows some errors in setting the objective function. I do not understand the errors in the objective function and the constraints. Please help me in this regard. I will be incredibly grateful to you. The error is as follows:

from gurobipy import *
from numpy import genfromtxt
import numpy as np
import scipy as sp
import pandas as pd
from sklearn.preprocessing import normalize
linear = Model("LP")
m = read("mpsfile.mps")
B = m.getA()
B1 = np.array(B)
r = m.getAttr("rhs", m.getConstrs())
b = np.array(r)
obj = m.getAttr("Obj", m.getVars())
c = np.array(obj)
N = len(c)
sen = m.getAttr("Sense", m.getConstrs())
optr = np.array(sen)
x = m.addMVar(N, vtype=GRB.CONTINUOUS, lb=0, name='x')
obj_linear = (c @ x)
linear.setObjective(obj_linear, sense=GRB.MINIMIZE)
linear.addMConstr(B1, x, optr, b)
m.optimize()
m.update()
-
正式なコメント
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. -
Hi Prachi,
The issue you encounter is because you are adding the variables to Model \(\texttt{m}\) instead of Model \(\texttt{linear}\). The variable addition step should read
x = linear.addMVar(N, vtype=GRB.CONTINUOUS, lb=0, name='x')
Moreover, the matrix returned by the getA method is already of type \(\texttt{scipy.sparse}\) in CSR format. Thus, you don't need the line where you convert it to an \(\texttt{np.array}\)
linear = Model("LP")
m = read("myLP.lp")
B = m.getA()
[...]
linear.addMConstr(B, x, optr, b)Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント