Skip to main content

Adding TempConstr and an int

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • 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

Post is closed for comments.