Variable not in model when setting objective
AnsweredSo I'm running a model - I successfully solved it using naive approach but I want to try and use a more general model form. However when running it I get the variable not in error for my set objective
#Model set-up
m1 = Model('Nutrition')
#Create Variables
x_1 = m.addVars(N, name = 'fruits')
print(x_1)
#Set objective
m1.setObjective(quicksum(cost[i]*x_1[i] for i in range(N)), GRB.MINIMIZE)#Add Constraints
m1.addConstrs(matrix_fruits[i,j]*x_1[j] for j in range(N)>= min_nutrition[i] for i in range(M))
m1.addConstrs(matrix_fruits[i,j]*x_1[j] for j in range(N)<= max_nutrition[i] for i in range(M))# Print optimal solutions and optimal value
print('-------------------------------------')for v in m.getVars():
print(v.VarName, v.x1)
print('Obj:', m1.objVal)
is the code
N in this case represents the 5 variables I have
-
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 why not try our AI Gurobot?. -
Hi,
The line where you add the variable is adding them to a wrong model \(\texttt{m}\) instead of your Nutrition model \(\texttt{m1}\). The addVars line should read
x_1 = m1.addVars(N, name = 'fruits')
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments