Decision Optimization

 View Only
Expand all | Collapse all

Conditional constraints in CP using Python

  • 1.  Conditional constraints in CP using Python

    Posted Wed April 15, 2020 02:44 PM

    Originally posted by: KeerthivasanC


    A brief overview of my problem:

    I have 8 projects [A,B,C,D,E,F,G,H] which need to completed in 4 time periods [T1,T2,T3,T4], where I have the data for the no. teams required for each project and the max. number of teams allowed to work in each time period. The cumulative sum of max. no. of teams allowed is greater than the cumulative sum of total no. teams required for the 8 projects. 

    Here is what I have done:

    TP1 = m.integer_var_dict(name = "TP1",keys=project). Basically, I have TP1_A,TP_B,...,TP_H as integer variables. I have done the same for TP2, TP3 and TP4. The constraints added to the model are the teams needed for each project and the max. teams allowed in each time period. Now, this is basically an assignment problem subject to resource constraints. 

    Here is what I need to accomplish:

    Suppose, the model assigns the following values for project A:

    TP1_A = 0

    TP2_A = 4

    TP3_A = 0

    TP4_A = 1, assuming the teams needed for A is 5. 

    I need to add an addition condition where I say:

    If the first assignment for project A by the model is in TP2, then TP3 can't be 0 and so on and so forth until I meet my total teams needed for a project condition. Essentially, when I have assigned a team or teams to a project in one time period, it can't go to 0 in the next time period and back again to some positive team allocation value in the third time period. The project should have non-zero teams assigned to it from start to completion. 

    Can someone help me frame this as a constraint in docplex.cp using Python? I would be very grateful as I am unable to grasp how to do this. Usage of if_then(), perhaps? Thanks in advance. 

     


    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: Conditional constraints in CP using Python

    Posted Thu April 16, 2020 03:15 AM

    Originally posted by: PhilippeLaborie


    Yes, I think that you could use if_then constraints here. Something like:

    if_then( (TP4_A > 0) & (TP2_A > 0), (TP3_A > 0) )
    

    Note that if your real problem contains other types of constraints like precedence constraints between projects and limited maximal duration for each project (whereas the number of time periods can be large), then you may also think of an alternative model in CP Optimizer using interval variables. You would have one interval variable for each project, so for instance one interval variable for project A, that is composed of n sub-interval variables of length 1 (one time unit) and that requires a variable (but >0) amount of teams.


    #CPOptimizer
    #DecisionOptimization


  • 3.  Re: Conditional constraints in CP using Python

    Posted Thu April 16, 2020 11:05 AM

    Originally posted by: KeerthivasanC


    Thanks for the reply, Philippe. I have used your recommendation in my model and the if_then() worked. Can you tell me if nested if_then() is possible, or how I can apply a nested if, else logic to add a constraint to my model? A small code snippet in Python would be amazing. Again, thanks a lot for your help!

    Regards
    Keerthi.


    #CPOptimizer
    #DecisionOptimization