Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  c=a AND b constraint in Java API

    Posted 04/09/18 10:04 PM

    Originally posted by: whitegreen


    Suppose, a, b, and c are binary variables in an integer program. How to add a "c = a AND b" logical constraint via Java API?

    Otherwise I have to use  0<=a+b-2c<=1  instead. 


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: c=a AND b constraint in Java API

    Posted 04/10/18 01:27 AM

    I am not sure I understand you correctly. If you want to have "c = a AND b" then just add constraints "c == a" and "b == 1". That gives what you wrote but I think that is probably not what do meant. Did you mean "IF c == a THEN b == 1"? That would be modeled using the ifThen() function. If that was not what you meant, could you try to rephrase your question?


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: c=a AND b constraint in Java API

    Posted 04/10/18 02:25 AM

    Originally posted by: whitegreen


    Thank you Daneil, I mean c=(a && b ) where && is the logical AND operator, or: c must be 1 if and only if both a==1 and b==1.   

    I am family with ifthen function, but it is not applicable for such constraint. 


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: c=a AND b constraint in Java API

    Posted 04/10/18 03:29 AM

    The IloCplex class has a function called and() (there are several overloads for this, see here). This allows you to create the logical AND of two constraints. There are also functions to create OR constraints. Together with ifThen() you should be able to state your constraint.

    However, for all binary variables it is probably more efficient to just state the constraint yourself, unless you are worried about readability or maintainability of your code.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: c=a AND b constraint in Java API

    Posted 04/10/18 05:26 AM

    Originally posted by: whitegreen


    Thank you again for the answer!

    However, I am still confused about the syntax of using and() after reading the doc saying

    
    IloAnd and(
    IloConstraint con1,  
    IloConstraint con2)
    

    Given three binary variables: IloNumVar a, b, c, Could you please give me a few lines of codes to implement the "c= (a && b)" constraint using and()?


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: c=a AND b constraint in Java API

    Posted 04/13/18 02:54 AM

    Have you tried something like this (untested code):

    IloAnd and = cplex.and(cplex.eq(a, 1.0), cplex.eq(b, 1.0)); // a == 1 && b == 1
    cplex.add(cplex.ifThen(and, cplex.eq(c, 1.0)));             // if a == 1 && b == 1 then c == 1

    The reverse direction is simpler and does not even require a logical AND:

    cplex.add(cplex.ifThen(cplex.eq(c, 1.0), cplex.eq(a, 1.0)));
    cplex.add(cplex.ifThen(cplex.eq(c, 1.0), cplex.eq(b, 1.0)));

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: c=a AND b constraint in Java API

    Posted 04/14/18 01:28 AM

    Originally posted by: whitegreen


    The following codes

    IloAnd and = cplex.and(cplex.eq(a, 1.0), cplex.eq(b, 1.0)); // a == 1 && b == 1
    cplex.add(cplex.ifThen(and, cplex.eq(c, 1.0)));             // if a == 1 && b == 1 then c == 1

     

    can run, however, it does Not impose the " if a == 1 && b == 1 then c == 1" logic.

    I get the results: a=0,b=0, and c=1. Something is wrong.


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: c=a AND b constraint in Java API

    Posted 04/14/18 04:00 PM

    The values a=0, b=0, c=1 do in fact satisfy the constraint "if a == 1 and b == 1 then c == 1". Perhaps you are misstating what you want. Are you actually looking for "c == 1 if and only if a == 1 and  b == 1"?


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: c=a AND b constraint in Java API

    Posted 04/17/18 01:04 AM

    The thing Paul suspects you are missing is the "reverse direction" I mentioned in my post.


    #CPLEXOptimizers
    #DecisionOptimization