这是我的复杂代码的一部分。我将 PreReq 定义为 bool 变量,但在约束中我得到 PreReq 的错误“表达式必须具有 bool 类型”我做错了什么?
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();
}
最佳答案
您宁愿使用 IloIfThen(逻辑约束)
CPLEX 中的示例 documentation :
IloIfThen(env, (x >= y && x >= z), IloNot(x <= 300 || y >= 700))
关于c++ - 在 CPLEX 中使用 bool 变量编写 if 语句的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57812589/