Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Why am I getting this error encoding condition using cplex.ifThen() ?

    Posted 05/08/10 12:03 PM

    Originally posted by: SystemAdmin


    I am trying to model a process and am having some trouble with a particular portion. I have isolated the logic I need to encode (which I believe is linear) and created a test program that fails to run. This portion is built in a subroutine and the output value is later used in the objective function. Based on some other conditions the objective either needs to be maximized or minimized.

    I will include the exception I received when calling the cplex.solve(); below. I am using CPLEX 12.1, any help is appreciated.

    Alex

    import ilog.concert.IloConstraint;
    import ilog.concert.IloException;
    import ilog.concert.IloIntVar;
    import ilog.concert.IloNumVar;
    import ilog.cplex.IloCplex;
     
    public class IfThenTest {
     
            /* if (avail<=5) { * value=100; * } * else { * if ((ready>=3 && ready<=6) || (ready>=9 && ready<=12)) { * value=300; * } * else { * value=200; * } * } * * maximize/minimize value */
            public static void main(String[] _args) throws IloException {
                    /* input */
                    IloCplex cplex=new IloCplex();
                    IloIntVar avail=cplex.intVar(Integer.MIN_VALUE,Integer.MAX_VALUE);
                    IloIntVar ready=cplex.intVar(Integer.MIN_VALUE,Integer.MAX_VALUE);
                    cplex.add(avail);
                    cplex.add(ready);
     
                    /* output */
                    IloNumVar value=cplex.numVar(-Double.MAX_VALUE,Double.MAX_VALUE);
                    cplex.add(value);
     
                    /* if conditions */
                    IloConstraint if1=cplex.le(avail,5);
                    IloConstraint if2=cplex.or(cplex.range(3,ready,6),cplex.range(9,ready,12));
     
                    /* then conditions */
                    IloConstraint then1=cplex.eq(value,100);
                    IloConstraint then2=cplex.eq(value,300);
                    IloConstraint then3=cplex.eq(value,200);
     
                    /* logic */
                    cplex.add(cplex.ifThen(if1,then1));
                    cplex.add(cplex.ifThen(cplex.and(cplex.not(if1),if2),then2));
                    cplex.add(cplex.ifThen(cplex.and(cplex.not(if1),cplex.not(if2)),then3));
     
                    /* objective */
                    cplex.add(cplex.maximize(value));
                    // cplex.add(cplex.minimize(value));
     
                    /* solve */
                    if (cplex.solve()) {
                            System.out.println("avail="+cplex.getValue(avail));
                            System.out.println("ready="+cplex.getValue(ready));
                            System.out.println("value="+cplex.getValue(value));
                    }
            }
     
    };
    


    Exception in thread "main" java.lang.ClassCastException: ilog.cplex.CpxOr cannot be cast to ilog.cplex.CpxRange
            at ilog.cplex.CpxNot.<init>(CpxNot.java:30)
            at ilog.cplex.IloCplexModeler.not(IloCplexModeler.java:1582)
            at ilog.cplex.IloCplexModeler.not(IloCplexModeler.java:1566)
            at IfThenTest.main(IfThenTest.java:47)
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Why am I getting this error encoding condition using cplex.ifThen() ?

    Posted 05/08/10 12:28 PM

    Originally posted by: SystemAdmin


    Looks like an undocumented feature that cplex.not() can only be applied to instances of IloRange.
    To work around this you could explicitly create the not(if1) and not(if2):
    IloConstraint notif1=cplex.ge(avail,6);
    IloConstraint notif2=cplex.or(cplex.or(cplex.le(ready,2), cplex.range(7,ready,8)), cplex.ge(ready,13));
    

    and then use these explicit negations.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Why am I getting this error encoding condition using cplex.ifThen() ?

    Posted 05/08/10 08:15 PM

    Originally posted by: SystemAdmin


    Thanks Daniel,

    I actually found another way to work around this that is more general. Basically, I just created my own and(), or() and not() that create the linear equations that are represented by my condition. I am not sure why CPLEX doesn't simply do this internally. Please let me know if there is a good reason not to use these.

    Alex

    import ilog.concert.IloConstraint;
    import ilog.concert.IloException;
    import ilog.concert.IloIntVar;
    import ilog.concert.IloNumVar;
    import ilog.cplex.IloCplex;
     
    public class IfThenTest {
     
            public static IloConstraint and(IloCplex _cplex,IloConstraint[] _conds) throws IloException {
                    return(_cplex.eq(_cplex.sum(_conds),_conds.length));
            }
     
            public static IloConstraint or(IloCplex _cplex,IloConstraint[] _conds) throws IloException {
                    return(_cplex.ge(_cplex.sum(_conds),1));
            }
     
            public static IloConstraint not(IloCplex _cplex,IloConstraint _cond) throws IloException {
                    return(_cplex.eq(_cond,0));
            }
     
            /* if (avail<=5) { * value=100; * } * else { * if ((ready>=3 && ready<=6) || (ready>=9 && ready<=12)) { * value=300; * } * else { * value=200; * } * } * * maximize/minimize value */
            public static void main(String[] _args) throws IloException {
                    /* input */
                    IloCplex cplex=new IloCplex();
                    IloIntVar avail=cplex.intVar(Integer.MIN_VALUE,Integer.MAX_VALUE);
                    IloIntVar ready=cplex.intVar(Integer.MIN_VALUE,Integer.MAX_VALUE);
                    cplex.add(avail);
                    cplex.add(ready);
     
                    /* output */
                    IloNumVar value=cplex.numVar(-Double.MAX_VALUE,Double.MAX_VALUE);
                    cplex.add(value);
     
                    /* if conditions */
                    IloConstraint if1=cplex.le(avail,5);
                    IloConstraint if2=or(cplex,new IloConstraint[]{
                            and(cplex,new IloConstraint[]{
                                    cplex.ge(ready,3),
                                    cplex.le(ready,6)
                            }),
                            cplex.range(9,ready,12)
                    });
     
                    /* then conditions */
                    IloConstraint then1=cplex.eq(value,100);
                    IloConstraint then2=cplex.eq(value,300);
                    IloConstraint then3=cplex.eq(value,200);
     
                    /* logic */
                    cplex.add(cplex.ifThen(if1,then1));
                    cplex.add(cplex.ifThen(cplex.and(cplex.not(if1),if2),then2));
                    cplex.add(cplex.ifThen(cplex.and(cplex.not(if1),cplex.not(if2)),then3));
     
                    /* objective */
                    cplex.add(cplex.maximize(value));
                    // cplex.add(cplex.minimize(value));
     
                    /* solve */
                    if (cplex.solve()) {
                            System.out.println("avail="+cplex.getValue(avail));
                            System.out.println("ready="+cplex.getValue(ready));
                            System.out.println("value="+cplex.getValue(value));
                    }
            }
     
    };
    


    Tried aggregator 1 time.
    MIP Presolve eliminated 0 rows and 1 columns.
    MIP Presolve modified 3 coefficients.
    Aggregator did 6 substitutions.
    Reduced MIP has 16 rows, 21 columns, and 37 nonzeros.
    Reduced MIP has 13 binaries, 2 generals, 0 SOSs, and 13 indicators.
    Probing fixed 0 vars, tightened 3 bounds.
    Probing time =    0.01 sec.
    Tried aggregator 1 time.
    MIP Presolve eliminated 8 rows and 5 columns.
    Aggregator did 2 substitutions.
    Reduced MIP has 6 rows, 14 columns, and 17 nonzeros.
    Reduced MIP has 11 binaries, 3 generals, 0 SOSs, and 13 indicators.
    Presolve time =    0.01 sec.
    Clique table members: 9.
    MIP emphasis: balance optimality and feasibility.
    MIP search method: dynamic search.
    Parallel mode: deterministic, using up to 2 threads.
    Root relaxation solution time =    0.00 sec.
     
            Nodes                                         Cuts/ 
       Node  Left     Objective  IInf  Best Integer     Best Node    ItCnt     Gap
     
          0     0      unbounded                                          6         
          0     2      unbounded                                          6         
    *     9     3      integral     0      100.0000                      8     --- 
    *    11     2      integral     0      300.0000                      8     --- 
     
    Root node processing (before b&c):
      Real time             =    0.00
    Parallel b&c, 2 threads:
      Real time             =    0.00
      Sync time (average)   =    0.00
      Wait time (average)   =    0.00
                              -------
    Total (root+branch&cut) =    0.00 sec.
    avail=6.0
    ready=3.0
    value=300.0
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Why am I getting this error encoding condition using cplex.ifThen() ?

    Posted 05/11/10 10:27 AM

    Originally posted by: SystemAdmin


    I don't see any obvious technical reason not to use your functions.
    However, I am not sure how their performance on larger models will be.
    You are introducing a lot of implicit binary/indicator variables here. This may backfire one day.
    #CPLEXOptimizers
    #DecisionOptimization