Skip to main content

Adding TempConstr and an int

Answered

Comments

2 comments

  • Valentin Guidon
    • Gurobi-versary
    • First Comment
    • First Question

    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
  • Matthias Miltenberger
    • Gurobi Staff

    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,
    Matthias

     

    0

Please sign in to leave a comment.