c++ - 在 CPLEX 中使用 bool 变量编写 if 语句的问题

标签 c++ cplex

这是我的复杂代码的一部分。我将 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/

相关文章:

c++ - 根据机器上安装的库依赖编译不同的代码

c++ - 如何使用 C++ 在 Windows 和 Linux 中清除控制台

c++ - QuickFIX C++ 库 - 关于 ThreadedSocketInitiator 的一般问题

c++ - 没有得到 ASCII 数字 =7 的字符串打印输出

c++ - 由于 CPLEX 中的 iostream.h 而出错

c++ - C++ 中的 Cplex : cannot open this source file

c++ - 带参数的线程调用库函数

C++ - FindFirstFile 的一个简单例子有问题

optimization - CPLEX 是否能降低 MIP 的成本?

mathematical-optimization - 为什么AMPL无法解决优化失败