HD2000
HD2000

Reputation: 144

CPLEX C++ constant constraint

I am making a CPLEX model with C++, and I need a function like:

IloConstraint f(...){
  IloConstraint constr;
        
  if(condition1){
    constr = (x+y >= 1);
    return constr;
  }
          
  if(condition2){
    constr = false;
    return constr;
  }
        
  constr = true;
  return constr;
}

I think that I succeded in creating a true and false constraints by

constr = (x==x);  and
constr = IloNot(x==x);

I assume that this approach is not very optimal because it adds extra conditions and variables. Is there a more optimal and more readable way to do this? Something like

constr = IloConstraint(IloFalse); ?

Upvotes: 1

Views: 332

Answers (1)

Daniel Junglas
Daniel Junglas

Reputation: 5930

IloConstraint(IloFalse) will not work since this will be interpreted as IloConstraint((IloConstraintI*)0) (IloFalse just expands to the literal 0 (zero)), which will create a constraint without implementation.

There is no literal for a true or false constraint. You can go without extra variables if you do something like IloExpr(env, 1) == IloExpr(env, 1) (and != for the false constraint). Another option for a constant true constraint would be use an empty IloAnd or an empty IloOr.

However, just going with x == x, 1 >= 2 or things similar to that seems a lot more readable to me. Additional expressions should usually not pose a problem. The engine will remove those in preprocessing.

Another option would be to use IloCplex::ifThen() to create a conditional constraint. Maybe this is even more readable then your function that returns a constraint.

Upvotes: 1

Related Questions