首页 > 解决方案 > 使用具有可变大小的 cplex 在 java 中定义二元决策变量

问题描述

我正在尝试在 java cplex 中定义一个 3D 二进制决策变量,假设 x[i][j][k] 其中 k 的大小是可变的并且取决于 f[i] 的值。我在这样的约束中使用它:forall i forall j Sum{k} x[i][j][k] = 1。

如何定义这个变量?

谢谢您的帮助。

标签: javacplex

解决方案


This is unrelated to CPLEX. You just have to define the 3rd dimension of the array depending on f[i] like so (... has to be filled with your correct dimensions):

IloIntVar[][][] x = new IloIntVar[...][][];
for (int i = 0; i < ...; ++i) {
  x[i] = new IloIntVar[...][];
  for (int j = 0; j < ...; ++j) {
    x[i][j] = new IloIntVar[f[i]];
    for (int k = 0; k < f[i]; ++k) {
      x[i][j][k] = cplex.boolVar();
    }
  }
}

推荐阅读