Gurobi Model Infeasible or unbounded
AnsweredHi all,
I am solving a model consisting of several periods of reward, when I solve the model for one period (T = 1), I can get the result. But once I specify it to 2 periods (T = 2), it is not solvable. The math shouldn't be difficult since the two-period problem is the summation of two single-period problems.
I am struggling with this problem for a long time, then I simplified the original problem just to find what went wrong first. Please find the attached code. Thank you so much for your help! - Ye
import gurobipy as gp
from gurobipy import GRB, abs_
T = 2
try:
m_ML2 = gp.Model("Model")
# define decision variables
x1_1 = m_ML2.addVars(T, name = 'x1_1', lb = 0)
x2_1 = m_ML2.addVars(T, name = 'x2_1', lb = 0)
y1_1 = m_ML2.addVars(T, name = 'y1_1', lb = 0)
y2_1 = m_ML2.addVars(T, name = 'y2_1', lb = 0)
s1_1 = m_ML2.addVars(T, name = 's1_1', lb = 0)
s2_1 = m_ML2.addVars(T, name = 's2_1', lb = 0)
s1_11 = m_ML2.addVar(vtype = GRB.CONTINUOUS, name = 's1_11', lb = 0)
s2_11 = m_ML2.addVar(vtype = GRB.CONTINUOUS, name = 's2_11', lb = 0)
R1 = m_ML2.addVars(T, name = 'R1')
for i in range(T):
s1_1[i] = w1[i]
s2_1[i] = w2[i]
m_ML2.addConstr(s1_11 == s1_1[i])
m_ML2.addConstr(s2_11 == s2_1[i])
#set value for decision variable
m_ML2.addConstr(y2_1[i] == 0)
m_ML2.addConstr(x2_1[i] == s2_11)
m_ML2.addConstr(y1_1[i] == 0 )
m_ML2.addConstr(x1_1[i] == s1_11)
#reward
m_ML2.addConstr(R1[i] == P[0,i]*(y2_1[i]+(1-alpha)*y1_1[i])+P[1,i]*x2_1[i]+P[2,i]*x1_1[i]-P[4,i]*(q-y1_1[i]-y2_1[i])-P[3,i]*(w1[i]+w2[i]))
m_ML2.setObjective(R1.sum(), GRB.MAXIMIZE)
# Optimize model
m_ML2.optimize()
for v in m_ML2.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % m_ML2.objVal)
except gp.GurobiError as e:
print('Error code ' + str(e.errno) + ': ' + str(e))
except AttributeError:
print('Encountered an attribute error')
-
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. -
This is discussed in How to assign a new variable equal to min of two other variable Gurobi in Python.
0
Post is closed for comments.
Comments
2 comments