Adding TempConstr and an int
AnsweredHello, I want to minimize the number of tasks that start at the same time. Here is my attempt with 2 tasks which can start at any day between day 0 and day 4.
from gurobipy import *
# Create a new model
m = gp.Model("very simple LP")
# Create variables
T1 = m.addVar(vtype=gp.GRB.INTEGER)
T2 = m.addVar(vtype=gp.GRB.INTEGER)
requests = [T1,T2]
nb_days = 10
nb_requests= 2
# Set objective
m.setObjective( quicksum( [(r ==i ) for r in requests for i in range(nb_days)] )
, sense= gp.GRB.MINIMIZE)
# Add constraints:
m.addConstr( 1 <= T1 )
m.addConstr( T1 <= 4 )
m.addConstr( 1<= T2 )
m.addConstr( T2 <= 4 )
# Optimize model
m.optimize()
But if I'm launching this program, I have
unsupported operand type(s) for +=: 'gurobipy.LinExpr' and 'TempConstr'
How can I proceed to remove this error ?
Thanks in advance !!
-
Official comment
This post is more than two 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. -
Hey, I've rewritten my code to make it more readable and understand exactly where I'm blocking
from gurobipy import *
# Create a new model
m = gp.Model("task_scheduling")# Create variables
T1 = m.addVar(lb=1, ub=4, vtype=gp.GRB.INTEGER, name='T1_start_time')
T2 = m.addVar(lb=1, ub=4, vtype=gp.GRB.INTEGER, name='T2_start_time')
requests = [T1, T2]
nb_days = 5
nb_requests = 2#For each day i and each task j, x[i, j] represent whether that task starts on that day
x = m.addVars(range(nb_days), range(nb_requests), vtype=gp.GRB.BINARY, name=f'x')
# Set objective: minimize the total number of tasks that start on each day.
m.setObjective(quicksum(x.sum(i) for i in range(nb_days)), sense=gp.GRB.MINIMIZE)
# Add constraints:for i in range(nb_days):
for j in range(nb_requests):
if requests[j] == i:
x[i,j]= 1
else:
x[i,j]= 0
# Optimize model
m.optimize()# Print results
#print(f'Minimum number of tasks starting on the same day: {m.objVal}')
#print(f'Task T1 starts on day {int(T1.x)}')
#print(f'Task T2 starts on day {int(T2.x)}')
#for v in m.getVars():
# print('%s %g' % (v.varName, v.x))I can't write in "gurobi language" the text in bold
0 -
Hi Valentin,
You can use indicator constraints to model such relationships. Please have a look at this guide for further information: How do I model conditional statements in Gurobi?
Cheers,
Matthias0
Post is closed for comments.
Comments
3 comments