Help: GurobiError: Unrecognized argument to getAttr
Answeredimport gurobipy as gp
from gurobipy import GRB
import math
numiter = 10
lambdak = 0
alphak = 1.0
zstar = 0
betak = 2
zstarvals = []
lambdavals = []
zlkprev= math.inf
rediter = 1
m = gp.Model('LagrangianRelaxation')
X1 = m.addVar(vtype=GRB.INTEGER, name='X1')
X2 = m.addVar(vtype=GRB.INTEGER, name='X2')
X3 = m.addVar(vtype=GRB.INTEGER, name='X3')
X4 = m.addVar(vtype=GRB.INTEGER, name='X4')
m.addConstr(X1 + X2 <= 1 , "Constraint1")
m.addConstr(X3 + X4 <= 1 , "Constraint2")
LR = (8*lambdak - 16)*X1 + (2*lambdak - 10)*X2 + lambdak*X3 + (4*lambdak - 4)*X4
m.setObjective(LR , GRB.MAXIMIZE)
for i in range (0,numiter):
print(str(i) + "started")
m.optimize()
if m.status == GRB.OPTIMAL:
X1out = m.getAttr('x', X1)
X2out = m.getAttr('x', X2)
X3out = m.getAttr('x', X3)
X4out = m.getAttr('x', X4)
Hello,
I'm new to optimization/Gurobi and trying to learn. The code block above errors at the getAttr lines with the following message:
GurobiError: Unrecognized argument to getAttr
What is the issue here? I have used the same format in another optimization problem and that works just fine. Can someone help please?
Thanks in advance!
0
-
Official comment
This post is more than three 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 Abhishek,
The Model.getAttr function expects a list or dictionary of gurobipy modelling objects as its second object, you cannot pass a single variable object. The following will return a list of the solution values for X1 -> X4, in order:
m.getAttr('X', [X1, X2, X3, X4])Alternatively, you can access the solution values one by one using the 'X' attribute of the variables themselves:
X1out = X1.X
X2out = X2.X
...0 -
Thank you Simon!
0
Post is closed for comments.
Comments
3 comments