英文:
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 < 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]));
}
}
}
}
}
}
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 < 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 < 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]));
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论