Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

Extract constraint in CP optimizer

  • 1.  Extract constraint in CP optimizer

    Posted Wed February 10, 2016 09:18 AM

    Originally posted by: ZoharFeldman


    I am trying to migrate from an old version of cp solver (v1.3) to the latest (cplex 12.6.3).

    I am using the C++ concert API (for Linux)

    Unfortunately, much has changed in terms of interface.

    One of my biggest obstacles is with using constraints.

    In the old version, it was possible to extract IloConstraint into the solver (calling IloConstraintI::use) and then retrieve the corresponding internal IlcConstraint using solver.getConstraint(IloConstraint). Notably, at this point, the constraint was extracted but not yet posted.

    In the new version, these capabilities are deprecated, and this creates a great deal of problems for me:

    1. I could "convert" IloConstraint to IlcConstrainted and call the isFalse() isTrue() methods and now I can't 

    2. I could "convert" IloConstraint to IlcConstrainted and add it to the solver, and by that the constraint was posted

    3. I could "convert" IloConstraint to IlcConstrainted and use post and meta post of the Impl class (very practical in custom constraints)

    4. I could "convert" IloConstraint to IlcConstrainted and pass it to IlcGoal for instantiation and many other cool things

     

    Can someone advise on the best practice to enable the above in the new version?


    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: Extract constraint in CP optimizer

    Posted Thu February 11, 2016 10:45 AM

    Originally posted by: PhilippeLaborie


    Hello,

    The IloCP::getConstraint(IloConstraint) function is indeed deprecated. The reason for that is because after extraction CP Optimizer now performs, during a presolve step, many transformations of the model for efficiency reasons (like constraint aggregation) so that there is no guarantee that there even is an instance of IlcConstraint corresponding to the IloConstraint of the concert model.

    One thing you could do is adding Boolean variables in the model to represent the truth value of the IloConstraint you want to manipulate and then work on these Boolean variables (set them to true/false, post demons, ...).  Unlike constraints, decision variables cannot be transformed by the presolve step so you can always get their Ilc counterpart.

     

     


    #CPOptimizer
    #DecisionOptimization


  • 3.  Re: Extract constraint in CP optimizer

    Posted Thu February 11, 2016 11:15 AM

    Originally posted by: ZoharFeldman


    Thanks for your response, Philippe.

     

    Just to make sure...

    It is clear how this method could be used for use case 1.

    For use case 2,4 (posting), are you saying that I could  add a boolean var to CP and then constrain it to be 1 and it will be equivalent to posting the Iloconstraint?

    For use case 3, if  I use whenValue(...)  of the corresponding bool var, it will be equivalent to IlcConstraintI.post(...)?


    #CPOptimizer
    #DecisionOptimization


  • 4.  Re: Extract constraint in CP optimizer

    Posted Thu February 11, 2016 11:48 AM

    Originally posted by: PhilippeLaborie


    > For use case 2,4 (posting), are you saying that I could  add a boolean var to CP and then constrain it to be 1 and it will be equivalent to posting the Iloconstraint?

    Yes, underneath it should all post()

    > For use case 3, if  I use whenValue(...)  of the corresponding bool var, it will be equivalent to IlcConstraintI.post(...)?

    The demons you attach to the whenValue event will be executed when the boolean variable gets fixed thus when the truth status of the constraint is fixed.

     


    #CPOptimizer
    #DecisionOptimization


  • 5.  Re: Extract constraint in CP optimizer

    Posted Thu February 11, 2016 02:19 PM

    Originally posted by: ZoharFeldman


    I see. That should satisfy my needs.

    Thanks much!


    #CPOptimizer
    #DecisionOptimization


  • 6.  Re: Extract constraint in CP optimizer

    Posted Sun February 14, 2016 03:53 AM

    Originally posted by: ZoharFeldman


    I have another question re. this approach:

    Suppose I create IloNumVar with Some IloConstraint and extract this var to the solver.

    Now, I would like to query at some point if this constraint is extracted to the solver, is it possible to use solver.isExtracted(IloConstraint) or must I query about the corresponding var (that is, solver.isExtracted(IloNumVar))?

     


    #CPOptimizer
    #DecisionOptimization


  • 7.  Re: Extract constraint in CP optimizer

    Posted Tue February 16, 2016 09:12 AM

    Originally posted by: ZoharFeldman


    I think I was over optimistic about my interpretation of the suggested resolution.

    I thought I could simply write:

    IloNumVar(IloConstraint)

    use(cp, IloNumVar);

    IlcIntVar = cp.getIntVar(IloNumVar)

    and from this point work with IlcIntVar.

    I now suspect (due to some failures in the solve) that I should have possibly done something like that

    IloModel.add(IloBoolVar == IloConstraint)

    and only then can I work with the boolean variables.

    Is this correct? 


    #CPOptimizer
    #DecisionOptimization


  • 8.  Re: Extract constraint in CP optimizer

    Posted Wed February 17, 2016 10:41 AM

    Originally posted by: PhilippeLaborie


    Yes, adding (IloBoolVar == IloConstraint) is what I had in mind.


    #CPOptimizer
    #DecisionOptimization


  • 9.  Re: Extract constraint in CP optimizer

    Posted Thu February 18, 2016 02:01 AM

    Originally posted by: ZoharFeldman


    OK. It indeed seem to help.

    One thing I did not think about but is worth mentioning is that using this approach also entails implementing the makeOpposite() of the corresponding IlcConstraintI (as it is now participating in a meta constraint), right?


    #CPOptimizer
    #DecisionOptimization


  • 10.  Re: Extract constraint in CP optimizer

    Posted Fri February 19, 2016 09:59 AM

    Originally posted by: ol


    Hello,

    for your specific case,  if in cp 1.3 you did not need to implement the methods for metaposting a constraint C, it seems to me that you still can avoid it for the migration. For example you can add the IlcConstraint C in a goal. Or am I missing something?

    Regards,

    Olivier


    #CPOptimizer
    #DecisionOptimization


  • 11.  Re: Extract constraint in CP optimizer

    Posted Tue March 01, 2016 08:33 AM

    Originally posted by: ZoharFeldman


    After adding the constraints IloBoolVar == IloConstraint (custom constraint, in this case) instead of just adding the IloConstraint, I got to a point where makeOpposite of the constraint was called (perhaps as part of the refineConflict process?)

    Anyway, the ability to add constraints dynamically seems like my biggest obstacle in the migration to the new version.

    The suggested approach above helps, but only partially. 

    For instance, in the old version I could add a constraint dynamically from the propagation of another constraint. If this constraint can be formulated only during the propagation, for example IloTableConstraint which possible values are revealed only during search (and perhaps even changed dynamically) I couldn't have formulated this constraint as is in the model extraction phase, and therefore I could not use the indicator approach. It could be easier I guess if I could construct IlcTableConstraint and add it to the solver, but this is not possible. I therefore need to find more creative solutions that hopefully will not add more complexity relative to adding the original IloConstraint to the solver.

    I cannot see a way of using a goal in this case for two reasons: (1) I don't have IlcConstraint at this point (only IloConstraint), and (2) I need to add this constraint after some trigger (for instance, a domain change of some variable)

     

    Thanks,

    Zohar


    #CPOptimizer
    #DecisionOptimization


  • 12.  Re: Extract constraint in CP optimizer

    Posted Thu March 03, 2016 12:09 PM

    Originally posted by: ol


    Hello,

    the following methods on IloCP seem to be what you want:

    voidadd(constIlcConstraint constraint) const;

    voidadd(constIlcConstraintArray constraints) const;

    for example, they can be called from the propagate() method of another constraint.

    Regards,

    Olivier


    #CPOptimizer
    #DecisionOptimization


  • 13.  Re: Extract constraint in CP optimizer

    Posted Sun March 06, 2016 07:23 AM

    Originally posted by: ZoharFeldman


    My main difficulty is that I don't have IlcConstraint but IloConstraint. For simple constraints, it is quite simple to formulate the corresponding IlcConstraint.

    For more complex constraints like IloTableConstraint, it is not possible to obtain the IlcConstraint without extracting them  at the beginning. Is this correct?


    #CPOptimizer
    #DecisionOptimization


  • 14.  Re: Extract constraint in CP optimizer

    Posted Mon March 07, 2016 02:55 AM

    Originally posted by: Petr Vilím


    Hello,

    In the header file cpext.h there is a way to create IlcTableConstraint directly:

    http://www.ibm.com/support/knowledgecenter/SSSA5P_12.6.3/ilog.odms.cpo.help/refcppcpoptimizer/html/functions/IlcTableConstraint.html?lang=en

    It is meant to be used if you want to add constraints during the search. As you mentioned, for example during propagation of another constraint.

    And yes, makeOpposite needs to be implemented when a constraint is used in boolVar == constraint expression.

    Best regards, Petr


    #CPOptimizer
    #DecisionOptimization


  • 15.  Re: Extract constraint in CP optimizer

    Posted Mon March 07, 2016 07:00 AM

    Originally posted by: ZoharFeldman


    I don't know how I missed it.

    Thanks, this is very helpful! 

     

    Zohar


    #CPOptimizer
    #DecisionOptimization