使用CPLEX六维变量时出现异常。

huangapple go评论145阅读模式
英文:

Getting exception while using CPLEX six dimentional variables

问题

以下是您提供的代码的翻译:

在尝试打印以下代码时我遇到了以下异常有人能指导一下发生了什么吗

for (int i = 0; i < numberOfNodes; i++) {
  for (int j = 0; j < numberOfNodes; j++) {
    for (int t = 0; t < noOfTimePeriods; t++) {
      for (int k = 0; k < numberOfNodes; k++) {
        for (int l = 0; l < numberOfNodes; l++) {
          for (int t1 = 0; t1 < noOfTimePeriods; t1++) {
            if (cplex.getValue(x[i][j][t][k][l][t1]) != 0)
              System.out.println("x[" + i + "][" + j + "][" + t + "][" + k + "][" + l + "][" + t1 + "] : "
                + cplex.getValue(x[i][j][t][k][l][t1]));
          }
        }
      }
    }
  }
}

异常信息:

ilog.cplex.IloCplex$UnknownObjectException: CPLEX 错误对象未知于 IloCplex
    at ilog.cplex.IloCplex.getValue(IloCplex.java:7594)
    at SCHL.t16Model(SCHL.java:358)
    at SCHL.executeModel(SCHL.java:23)
    at Main.main(Main.java:12)
英文:

I am getting the following exception while trying to print a the following code. Can anyone advise what happened?

for (int i = 0; i &lt; numberOfNodes; i++) {
  for (int j = 0; j &lt; numberOfNodes; j++) {
    for (int t = 0; t &lt; noOfTimePeriods; t++) {
      for (int k = 0; k &lt; numberOfNodes; k++) {
        for (int l = 0; l &lt; numberOfNodes; l++) {
          for (int t1 = 0; t1 &lt; noOfTimePeriods; t1++) {
            if (cplex.getValue(x[i][j][t][k][l][t1]) != 0)
              System.out.println(&quot;x[&quot;+i+&quot;][&quot;+j+&quot;][&quot;+t+&quot;][&quot;+k+&quot;][&quot;+l+&quot;][&quot;+t1+&quot;] : &quot;
                + cplex.getValue(x[i][j][t][k][l][t1]));
          }
        }
      }
    }
  }
}

Exception:

ilog.cplex.IloCplex$UnknownObjectException: CPLEX Error: object is unknown to IloCplex
    at ilog.cplex.IloCplex.getValue(IloCplex.java:7594)
    at SCHL.t16Model(SCHL.java:358)
    at SCHL.executeModel(SCHL.java:23)
    at Main.main(Main.java:12)

答案1

得分: 0

我建议查看位于 CPLEX_Studio1210\cplex\examples\src\java 目录下的 facility.java 示例文件。

在那里,你会看到以下内容:

// 创建变量。我们有变量
// open[j]        表示位置 j 是否开放。
// supply[i][j]]  表示客户 i 从位置 j 获得的供应量。
IloNumVar[] open = cplex.boolVarArray(nbLocations);
IloNumVar[][] supply = new IloNumVar[nbClients][];
for (int i = 0; i < nbClients; i++)
    supply[i] = cplex.numVarArray(nbLocations, 0.0, 1.0);

或者在具有 4 个维度的情况下:

IloCplex mycplex = new IloCplex();
int Ncd = 2;
int Nbv = 6;
int T = 4;
IloNumVar[][][][] y = new IloIntVar[Ncd][][][];
for (int i = 0; i < y.length; i++) {
    y[i] = new IloIntVar[Ncd][][];
    for (int j = 0; j < y[i].length; j++) {
        y[i][j] = new IloIntVar[Nbv + 1][];
        for (int k = 1; k < y[i][j].length; k++) {
            y[i][j][k] = mycplex.boolVarArray(T + 1);
            for (int t = 1; t < T; t++) {
                IloRange myConstraint2 = mycplex.addEq(y[i][j][k][t], 1);
                mycplex.add(myConstraint2);
            }
        }
    }
}
mycplex.solve();
for (int i = 0; i < Ncd; i++) {
    for (int j = 0; j < Ncd; j++) {
        for (int k = 1; k < Nbv; k++) {
            for (int t = 1; t < T; t++)
                System.out.printf("valeur = " + mycplex.getValue(y[i][j][k][t]));
        }
    }
}
英文:

I suggest to have a look at the example facility.java in CPLEX_Studio1210\cplex\examples\src\java.

And there you will see:

// Create variables. We have variables
// open[j]        if location j is open.
// supply[i][j]]  how much client i is supplied from location j
IloNumVar[] open = cplex.boolVarArray(nbLocations);
IloNumVar[][] supply = new IloNumVar[nbClients][];
for (int i = 0; i &lt; nbClients; i++)
    supply[i] = cplex.numVarArray(nbLocations, 0.0, 1.0);

or with 4 dimensions:

IloCplex mycplex = new IloCplex();
int Ncd = 2;
int Nbv = 6;
int T = 4;
IloNumVar[][][][] y = new IloIntVar[Ncd][][][];
for (int i = 0; i &lt; y.length; i++) {
    y[i] = new IloIntVar[Ncd][][];
    for (int j = 0; j &lt; y[i].length; j++) {
        y[i][j] = new IloIntVar[Nbv + 1][];
        for (int k = 1; k &lt; y[i][j].length; k++) {
            y[i][j][k] = mycplex.boolVarArray(T + 1);
            for (int t = 1; t &lt; T; t++) {
                IloRange myConstraint2 = mycplex.addEq(y[i][j][k][t], 1);
                mycplex.add(myConstraint2);
            }
        }
    }
}
mycplex.solve();
for (int i = 0; i &lt; Ncd; i++) {
    for (int j = 0; j &lt; Ncd; j++) {
        for (int k = 1; k &lt; Nbv; k++) {
            for (int t = 1; t &lt; T; t++)
                System.out.printf(&quot;valeur = &quot; + mycplex.getValue(y[i][j][k][t]));
        }
    }
}

huangapple
  • 本文由 发表于 2020年10月9日 07:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64271958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定