是的,您可以通过一次调用向您的 CPLEX 问题添加多行(约束)CPXaddcols.
只需确保在调用上述函数之后CPXcreateprob和调用CPXaddcols上述函数之前,您已调用适当数量的CPXnewrows以通知 CPLEX 这些约束不为零。
正如 CPLEX 帮助在此处所述
CPXaddcols cannot add coefficients in rows that do not already exist (that is, in rows with index greater than the number returned by CPXgetnumrows);
[...]
The routine CPXnewrows can be used to add empty rows before adding new columns via CPXaddcols.
此外,只需确保在添加变量时colNum实际上是指您将在新列中添加的非零值的数量。
此示例具有每次调用CPXaddcols将变量添加到两个约束的实例。
具体仔细看这部分代码:
/* Add flow variables */
for (j = 0; j < NUMEDGES; j++) {
ind[0] = orig[j]; /* Flow leaves origin */
val[0] = -1.0;
ind[1] = dest[j]; /* Flow arrives at destination */
val[1] = 1.0;
name[0] = buffer;
sprintf(buffer, "x%d%d", orig[j], dest[j]);
status = CPXaddcols (env, lp, 1, 2, &unitcost[j], &zero, ind, val,
NULL, NULL, name);
希望有帮助。