mohammad khamechian
mohammad khamechian

Reputation: 31

problem with writing if statement using Boolean variable in CPLEX

here is a part of my cplex code. i defined the PreReq as boolean variable but in the constraint i get the error for PreReq that "expression must have bool type" what am i doing wrong?

IloBoolVarArray2 PreReq(env), CoRec(env), Offered(env);

//Constraint 6: if course d is taken and course c is its pre-req, then course c must be taken before d 
IloExpr constraint6a(env);
IloExpr constraint6b(env);
IloExpr constraint6c(env);
IloExpr constraint6d(env);
for (c = 0; c < NumberOfCourses; c++) {
    for (d = 0; d < NumberOfCourses; d++) {
        if (PreReq[c][d] == 1 ) {
            for (s = 0; s < NumberOfSemesters; s++) {
                constraint6a += X[d][s];
                constraint6b += X[c][s];
                constraint6c += s*X[c][s];
                constraint6d += s*X[d][s];
            }
        }
    }
    mod.add(constraint6a <= constraint6b); 
    mod.add(constraint6c <= constraint6d + (NumberOfSemesters)*(1 - onstraint6a) );
    constraint6a.end(); 
    constraint6b.end(); 
    constraint6c.end(); 
    constraint6d.end(); 
}

Upvotes: 0

Views: 525

Answers (2)

Daniel Junglas
Daniel Junglas

Reputation: 5930

Expanding on Alex's answer: your problem is that you are mixing constraints with constraint generation: In the statement if ( PreReq[c][d] == 1 ) the operator== is overloaded to generate a constraint that requires PreReq[c][d] to take the value 1. So the result in this expression is of type IloConstraint, which is not a boolean type (it is a constraint object instead). That is why you get the error message. What you want instead is to add a constraint to your model that requires additional constraints in case PreReq[c][d] is 1.

So you probably want something like this:

for (d = 0; d < NumberOfCourses; d++) {
   for (s = 0; s < NumberOfSemesters; s++) {
      mod.add(IloIfThen(PreReq[c][d] == 1, constraint6a + X[d][s] <= constraint6b + X[c][s]));
      mod.add(IloIfThen(PreReq[c][d] == 0, constraint6a <= constraint6b));
   }
}

Which adds two variants of the constraint, only one of which will ever be active (depending on the value of PreReq[c][d]). Also see the reference documentation of class IloIfThen.

Upvotes: 2

Alex Fleischer
Alex Fleischer

Reputation: 10059

You'd rather use IloIfThen (logical constraints)

The example in CPLEX documentation:

IloIfThen(env, (x >= y && x >= z), IloNot(x <= 300 || y >= 700))

Upvotes: 2

Related Questions