problem with adding constraint to the model
回答済みHi
I have a model which contains variables and parameters in this way below
with gp.Model ( "masterproblem" ) as model:
Z_var = model.addVar ( name="Z")
y = {}
for a in range ( 1, n + 1 ):
y[a] = model.addVar ( lb=0, ub=1, name="y%d" % (a))
alpha = defaultdict ( lambda: 1, {num_Cuts: 1, } )
for (i, j) in E:
if i != s and j != s:
beta = defaultdict ( lambda: 1, {(num_Cuts, i, j): 1, } )
for v in range ( 1, n_prim + 1 ):
for w in range ( 1, n_prim + 1 ):
delta = defaultdict ( lambda: 1, {(num_Cuts, v, w): 1, } )
for v in range ( 1, n_prim + 1 ):
segma = defaultdict ( lambda: 1, {(num_Cuts, v): 1, } )
for (i, j) in E:
if i != s and j != s:
mu = defaultdict ( lambda: 1, {(num_Cuts, i, j): 1, } )
# Set global sense for ALL objectives
model.ModelSense = GRB.MINIMIZE
# Set up primary objective
model.setObjective ( Z_var )
model.write ( 'MP.lp' )
now I have a while loop and every time I am adding a constraint to the model as follows :
while "condition":
MP_model = gp.read ( "MP.lp" )
k=1
lhs=Z_var
rhs= (2 * alpha[k] + quicksum ( y[i] * beta[k, i, j] for (i, j) in E if i != s and j != s )
+ 2 * quicksum ( y[v] * delta[k, v, v] for v in range ( i, n_prim + 1 ) if v != s )
+ 2 * quicksum ( y[v] * segma[k, v] for v in range ( 1, n_prim + 1 ) if v != s )
+ 2 * quicksum ( mu[k, i, j] for (i, j) in E if i != s and j != s ))
sense=GRB.GREATER_EQUAL
name=k
MP_model.addConstr ( lhs, sense, rhs, name )
MP_model.update ()
k+=1
but I got an error message that
MP_model.addConstr ( lhs, sense, rhs, name )
File "src\gurobipy\model.pxi", line 3376, in gurobipy.Model.addConstr
File "src\gurobipy\model.pxi", line 3206, in gurobipy.Model.__addConstr
gurobipy.GurobiError: Variable not in model
-
正式なコメント
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 why not try our AI Gurobot?. -
Hi Ahmad,
Variables \(\texttt{Z_var}\) and \(\texttt{y}\) are part of the \(\texttt{model}\) object and not of the \(\texttt{MP_model}\). In order to access them in the most convenient way, you could add
MP_model = gp.read ( "MP.lp" )
y = MP_model.getVars() # get all vars as list
Z_var = 0
for v in y:
# this only works because all variable are named y# except for one Z
if v.VarName == 'Z':
Z_var = v # save variable Z to Z_var
y.remove(v) # remove variable Z from the list of y variables
breakTo be 100% certain, you could also check whether the \(\texttt{y}\) list is in the correct order.
Best regards,
Jaromił1 -
Dear Jaromil
Thank you so much for your help, it's working perfectly :)
I have another question if you don't mind, I have a model and I am solving the model if it is infeasible, I want to get the unbounded value of each constraint, I do the following :
SP_model.getConstrByName ( 'one' ).FarkasDual
and if there is a solution I want the dual value of the constraint :
SP_model.getConstrByName ( 'one' ).Pi
I don't know if this is the right way because for Pi it is working normally but with Farkas dual, I got this error message :
alpha[k] = SP_model.getConstrByName ( 'one' ).FarkasDual
File "src\gurobipy\constr.pxi", line 67, in gurobipy.Constr.__getattr__
File "src\gurobipy\constr.pxi", line 95, in gurobipy.Constr.getAttr
File "src\gurobipy\attrutil.pxi", line 100, in gurobipy.__getattr
AttributeError: Unable to retrieve attribute 'FarkasDual'Thanks a lot
0 -
Hi Ahmed,
FarkasDual values are only available if you set the parameter InfUnbdInfo to 1. This is done to avoid the rather expensive computations connected to FarkasDual values. In you particular case, you will have to set
SP_model.setParam("InfUnbdInfo",1)
SP_model.optimize()Best regards,
Jaromił1
投稿コメントは受け付けていません。
コメント
4件のコメント