【问题标题】:How to access to a multidimensional decision variable array in Ilog Cplex using Java如何使用 Java 访问 Ilog Cplex 中的多维决策变量数组
【发布时间】:2018-11-29 16:49:44
【问题描述】:

求解模型后,我需要访问四维十进制变量的值。这是我在 Java 中的 decion 变量的声明:

this.y = new IloIntVar[this.Ncd][][][]; 
        for(int i=0; i<this.y.length;i++) { 
            this.y[i]= new IloIntVar[this.Ncd][][]; 
            for(int j=0; j<this.y[i].length;j++) {
                this.y[i][j]= new IloIntVar[this.Nbv+1][];
                for(int k=1; k<this.y[i][j].length; k++) {
                    this.y[i][j][k]= new IloIntVar[this.T+1];

                        this.y[i][j][k]= this.cplex.boolVarArray(this.T+1);

            }
        }
    }

我真的需要你的帮助才能使用我的模型的模拟结果。

期待您的来信。

扎卡利亚

【问题讨论】:

  • 顺便说一句,在您的代码 sn-p 中,您设置了两次 this.y[i][j][k]。一次使用new IloIntVar[this.T+1],再次使用this.cplex.boolVarArray(this.T+1)。您可能应该摆脱前者,因为它没有做任何有成效的事情。
  • 在最里面的for循环中以k=1开头似乎也有点奇怪。这是故意的吗?
  • @rkersh 谢谢你的回答.. 我声称我从 k = 1 开始,因为它涉及所有车辆。关于您对报告结构的评论,我想知道我是否可以使用以下语句:this.y = new IloIntVar [this.Ncd] [this.Ncd] [this.Nbv + 1] [this.T + 1]; for (int i = 0; i &lt;this.Ncd; i ++) { for (int j = 0; j &lt;this.Ncd; j ++) { for (int k = 1; k &lt;= this.Nbv; k ++) { for (int t = 1; t &lt;this.T + 1; t ++) { this.y [i] [j] [k] [t] = this.cplex.boolVar (); } } } }?
  • 如果你想要的话,可以使用k=1;您的多维数组中会有一些空槽,您只需要注意这一点。没有必要停止使用boolVarArray,否则。
  • @rkersh 如果我理解正确,最后这句话是否正确?

标签: java cplex ilog


【解决方案1】:

听起来您已经成功构建并求解了模型。正确的?假设这是真的,您应该能够通过以下方式访问解决方案值:

 for (int i = 0; i < this.y.length; i++) {
    for (int j = 0; j < this.y[i].length; j++) {
       for (int k = 1; k < this.y[i][j].length; k++) {
          double[] x = cplex.getValues(y[i][j][k]);
          for (int l = 0; l < this.y[i][j][k].length; ++l) {
             System.out.printf("Variable [%d][%d][%d][%d] = %f%n",
                               i, j, k, l, x[l]);
          }
       }
    }
 }

【讨论】:

  • 我尝试了您的代码 sn-p,但编译器向我显示此错误:线程“主”ilog.cplex.IloCplex$UnknownObjectException 中的异常:CPLEX 错误:IloCplex 未知对象ilog.cplex.IloCplex.getValues(IloCplex.java:6055) 在 ilog.cplex.IloCplex.getValues(IloCplex.java:5996) 在 MyCplexModel.solve(MyCplexModel.java:190) 在 MyCplexModel.main(MyCplexModel.java:239) ) 请问,我该如何解决这个问题?可能是由于错误声明约束之一造成的吗?
  • 当您尝试在不属于模型的对象上使用getValues 时,通常会发生此错误。您应该检查要为其获取解值的每个变量是否包含在约束或目标中,并已添加到模型中。
  • 我可以把我的java模型发给你,给我你的cmets和建议吗,因为我真的很迷茫,尤其是我认为我做的一切都是正确的。
  • 我建议使用getValues 而不是getValue,因为批量查询变量通常比一次查询一个更快。但是,出于调试目的,您可以将x[l] 替换为cplex.getValue(y[i][j][k][l])。我还会在上面添加一个打印语句,以便您可以在失败之前轻松查看索引是什么(或使用 try/catch 语句)。无论如何,这应该可以很容易地追踪导致问题的变量。
  • Itried 你的提议:(...) System.out.printf("Variable [%d][%d][%d][%d] = %f%n", i, j, k, l, this.cplex.getValue(this.y[i][j][k][l])); (...) 但同样的错误: ilog.cplex.IloCplex.getValues(IloCplex.java:6054) 处 ilog.cplex.IloCplex.getValues(IloCplex.java:5996) 的线程“主”java.lang.NullPointerException 中的异常) 在 MyCplexModel.solve(MyCplexModel.java:190) 在 MyCplexModel.main(MyCplexModel.java:239) MyCplexModel.java:190 对应于行:double[] x = cplex.getValues(y[i][j][k ]);
【解决方案2】:

https://www.ibm.com/developerworks/community/forums/html/topic?id=1865f18c-0176-48b8-8dbe-daa7f88773c4&ps=25#repliesPg=0的类似问题

我会推荐 CPLEX_Studio128\cplex\examples\src\java 中的示例 facility.java

你会看到

// 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);

【讨论】:

  • 感谢您的回答。如果我理解正确,我对决策变量的声明有问题?
猜你喜欢
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多