Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

ternary operator in concert technology

  • 1.  ternary operator in concert technology

    Posted Thu November 04, 2021 04:15 PM
    Hello

    I am starting with the CP Optimizer C++ API interface and I must confess I find the documentation quite difficult to follow. Lucky there are examples in the installation directory! The reason to start with the C++ API instead of continuing with OPL is to try to find more flexibility when defining a model. Achieve application integration is also a goal. am I too wrong?

    My current problem (one of them at least):
    With the OPL I could use the ternary operator to do something like that (simplified model):
    dexpr int TimeDif = (MaxTT - MinTT < ConstantValue ) ? 0 : MaxTT - MinTT;
    minimize staticLex ( DifMaxTiempoTrabajado, other objectives here )

    MaxTT and MinTT are expressions as well. Basically, what I am trying to achieve is to let the solver know that if MaxTT - MinTT < ConstantValue then it is good enough and should look other objectives less important.

    Is there an equivalente way to do this (the ternary operator) with C++ API ?

    Thanks in advance and sorry if this is a basic question

    ------------------------------
    javier rodrigo
    ------------------------------

    #DecisionOptimization


  • 2.  RE: ternary operator in concert technology

    Posted Fri November 05, 2021 04:47 AM
    Hi,

    at https://www.linkedin.com/pulse/optimization-aka-prescriptive-analytics-should-we-write-fleischer/

    I discussed pros and cons of OPL and C++

    Do you know that from C++ you can call any OPL model and see the OPL objects (constraints, decision variables ...) as C++ objects ?

    Many examples at CPLEX_Studio201\opl\examples\opl_interfaces\cpp

    And the ternary operator is available in C++ and in OPL

    regards


    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: ternary operator in concert technology

    Posted Fri November 05, 2021 06:37 AM
    You could use the constraint ifThenElse(c1,c2,c3) with
    c1: MaxTT - MinTT < ConstantValue
    c2 : TimeDif==0
    c3 : TimeDif==MaxTT - MinTT

    note this requires to define TimeDif as a var (and not an expression)

    The grammar you can use in a CP model is 99% the same calling it from OPL or C++. Note that you can also integrate OPL model in your C++ through API 


    ------------------------------
    David Gravot
    ------------------------------



  • 4.  RE: ternary operator in concert technology

    Posted Sun November 07, 2021 12:40 PM
    Great!

    I finally did this through the definition of the absolute tolerance when setting the objective for the MaxTT - MinTT.

    Any idea how to "translate" the following into the C++ API?

    OPL:
    forall (i in Workers, t in 2..ShiftDuration)
           ((Ocupancy[i][t]==0) && (Ocupancy[i][t-1]==1)) => (sum(s in t-7..t-2 : s>=1) (Ocupancy[i,s]))==6;

    C++:
    for (i = 0; i < numWorkers; i++) {
    for (t = 1; t < ShiftDuration; t++) {
    model.add(IloIfThen(env, Ocupancy[i][t] == 0 && Ocupancy[i][t - 1] == 1, SUM FROM t-2 to t-7 of Ocupancy[i,s] == 6));
    }
    }

    I am trying to force that if a worker starts working (from 0 to 1), it has to do it during 6 slots or more.

    Thanks again for your support

    ------------------------------
    javier rodrigo
    ------------------------------



  • 5.  RE: ternary operator in concert technology

    Posted Sun November 07, 2021 02:40 PM
    I found a way although I don´t know if it is too weird. Basically I build up an array (aux) which I make equal to the slice (6 slots) of Ocupancy that I am interested in sum on every loop. What I don´t like is that I have to build up this aux for every loop even if that the condition Ocupancy[i][t] == 0 && Ocupancy[i][t - 1] == 1 is not satisfied. 

    IloBoolVarArray aux(env, 6);
    for (i = 0; i < numWorkers; i++) {
    for (t = 1; t < ShiftDuration; t++) {
    for (IloInt s=0; s<6 && t + 2 + s < ShiftDuration;s++) aux[s] = Ocupancy[i][t + 2 + s];

    model.add(IloIfThen(env, Ocupancy[i][t] == 0 && Ocupancy[i][t - 1] == 1, IloSum(aux)==6));
    }
    }

    ------------------------------
    javier rodrigo
    ------------------------------



  • 6.  RE: ternary operator in concert technology

    Posted Mon November 15, 2021 08:14 AM
    Hello,

    What is the right syntax to use the CP.IfThenElse Method from the C++ API interface? I don´t manage to make it work

    Regards


    ------------------------------
    javier rodrigo
    ------------------------------



  • 7.  RE: ternary operator in concert technology

    Posted Mon November 15, 2021 12:14 PM
    Dear Rodrigo,

    The documentation describes how IloIfThen can be used:
    https://www.ibm.com/docs/en/icos/20.1.0?topic=classes-iloifthen
    What is exactly the problem that you are facing?
    Cheers,

    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 8.  RE: ternary operator in concert technology

    Posted Mon November 15, 2021 02:51 PM
    Hi, what I need is not the IloIfThen but the IfThenElse which apparently is a method of CP but I don´t know how to use it with the C++ API. What I am trying to achieve is what David Gravot suggested above

    You could use the constraint ifThenElse(c1,c2,c3) with
    c1: MaxTT - MinTT < ConstantValue
    c2 : TimeDif==0
    c3 : TimeDif==MaxTT - MinTT

    and my original problem is what in OPL could be written like this:

    dexpr int TimeDif = (MaxTT - MinTT < ConstantValue ) ? 0 : MaxTT - MinTT;

    So I was looking for the IfThenElse to put it like this:
    TimeDif = IloIfThenElse(env, MaxTT - MinTT <= ConstantValue, MaxTT - MinTT, 0);
    but I don´t know the right syntax

    Regards




    ------------------------------
    javier rodrigo
    ------------------------------



  • 9.  RE: ternary operator in concert technology

    Posted Mon November 15, 2021 03:42 PM
    Dear Javirer,

    I cannot find  IloIfThenElse(env, a, b, c)  in the C++ concert API. But you can express it with IloIfThen(env, a, b) and IloIfThen(env, IloNot(a), c).
    Cheers,

                 Renaud

    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 10.  RE: ternary operator in concert technology

    Posted Tue November 16, 2021 05:29 AM
    Great! Thanks

    ------------------------------
    javier rodrigo
    ------------------------------