如何在 C++ 中命名约束

How to name constraints in C++

我用 C++ 编写了代码,我正在从那里调用 CPLEX 来解决 MILP 问题。我遇到了一些错误,为了缩小错误来源的范围,我想为约束指定不同的名称。但是,我无法获得有关如何执行此操作的任何信息。我正在使用 IloExpr 为约束创建表达式,然后将它们添加到模型中。下面显示了其中一个约束的片段。这里,x[i][d] 是一个布尔决策变量。请帮我命名这些约束。

for (i=0;i<I;i++)
    {
        IloExpr not_more_than_one (env);
        for (d=0;d<D;d++)
        {
            not_more_than_one += x[i][d];
        }
        mod.add(not_more_than_one <= 1);
        not_more_than_one.end();
    }

为了设置约束名称,您应该使用 IloRange。例如,您可以替换代码段中的以下行:

mod.add(not_more_than_one <= 1);

与:

IloRange cons(env, not_more_than_one, 1, "name of the constraint");
model.add(cons);