Decision Optimization

 View Only
Expand all | Collapse all

How to use if_then for 2 expression in constraint programming using DOCPLEX

  • 1.  How to use if_then for 2 expression in constraint programming using DOCPLEX

    Posted Fri August 02, 2024 05:23 PM
    I am building a bidding VRP model using constraint programming with DOCPLEX. In this model, I have the following constraint:
     
    IF {presentOf(xtime[k]) == 0}, THEN {sum(b in B)z[k][b] == 0 - forall k in KN}
     
    and
     
    IF {sum(b in B)z[k][b] == 0} - forall k in KN, THEN {presentOf(xtime[k]) == 0}
    This is my code: 
    for k in KN:
        model.add(model.if_then(sum(z[k -NberExistConstract- 1][b-1] for b in B) == 0, model.presence_of(xtime[k-NberExistConstract-1])==0))
        model.add(model.if_then(model.presence_of(xtime[k-NberExistConstract-1])==0, sum(z[k -NberExistConstract- 1][b-1] for b in B) == 0))
    
    I have tried using if_then as shown in the [link](https://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.modeler.py.html?highlight=logical#docplex.cp.modeler.if_then),
    But it is not works; the results are still incorrect. For example wrong solution:
    xtime_19: absent 
    but: z_19_1 = 1; z_19_2 = 0; z_19_3 = 0 => sum(z_19_1; z_19_2; z_19_3) = 1.
     
    It should be:
     
    xtime_19: absent 
    but: z_19_1 = 0; z_19_2 = 0; z_19_3 = 0 => sum(z_19_1; z_19_2; z_19_3) = 0

     
    I have try this example: 
     
     
     
     
    But it does not works and still give the wrong solution
    Please help me, thanks


    ------------------------------
    Son Nguyen Hoang
    ------------------------------


  • 2.  RE: How to use if_then for 2 expression in constraint programming using DOCPLEX

    Posted Wed August 07, 2024 08:44 AM

    A way to check that your model is giving you the solutions that you expect is to use a small data set to obtain a tiny model where it will be easier to understand a potential problem. Then  you can export the file to a .cpo model file format. For that you must use oplrun this way :
    oplrun -e model.cpo input_model.mod  input_data.dat
    Looking at the .cpo model you can check that the constraints are as they should be and maybe point what could be an index error.