m.getAttr('X', x) gives warning
AnsweredI'm solving the following MWE:
import gurobipy as gp
from gurobipy import GRB
# Parameter
N = (1, 2)
M = (1, 2)
c = {'x': {1: 2, 2: 1},
'y': {1: 3, 2: -1},}
A = {'x': {(1, 1): 1, (1, 2): 2,
(2, 1): -1, (2, 2): -1,},
'y': {(1, 1): 0, (1, 2): 1,
(2, 1): 2, (2, 2): -1,}}
b = {1: 4,
2: 2}
# Create a new model
m = gp.Model()
# Create variables
x = m.addVars(N, vtype=GRB.CONTINUOUS, name='x')
y = m.addVars(N, vtype=GRB.CONTINUOUS, name='y')
# Set objective
m.setObjective(sum(x[i] * c['x'][i] + y[i] * c['y'][i] for i in N), GRB.MAXIMIZE)
# Add constraints
for j in M:
m.addConstr(sum(A['x'][j, i] * x[i] + A['y'][j, i] * y[i] for i in N) <= b[j],
name=f'Const[{j}]')
# Optimize model
m.optimize()
# Solution
x_sol = m.getAttr('X', x)
y_sol = m.getAttr('X', y)
It works and \(\texttt{x_sol}\) and \(\texttt{y_sol}\) contain the correct solutions for the respective variable. But my editor PyCharm tells me that there is a problem, namely that \(\texttt{m.getAttr('X', x)}\) and \(\texttt{m.getAttr('X', y)}\) contain and "Unexpected argument" and the fix would be to drop \(\texttt{x}\) and \(\texttt{y}\), respectively. If I would do that the returned solution is a list and cannot distinguish between the \(\texttt{x}\)s and \(\texttt{y}\)s.
I was wondering whether this is a PyCharm or Gurobi issue and if it was a Gurobi issue, what would be the proper way to call the solution.
-
I don't see a warning with VSCode. Maybe you should install Gurobi/gurobipy-stubs (github.com) to get the correct type hints?
Providing a set of constraints or variables to the getAttr() method is certainly possible and should not trigger a warning.
Cheers,
Matthias0
Please sign in to leave a comment.
Comments
1 comment