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