使用Cplex在Java中定义二进制决策变量 [英] Defining a binary decision variable in java using cplex
问题描述
我正在尝试使用cplex在Java中定义一个二进制决策变量。那是一个二维变量。这意味着,如果路径从特定站点开始,则应该为1,否则为0。我有一组站点J和一组路径K,我的决策变量为Z_jk。目前,我正在按照以下方式定义代码,但无法正常工作。
I am trying to define a binary decision variable in java using cplex. That's a two dimensional variable. It means that if a path starts from a specific station it should be 1 or otherwise 0. I have a set of station, J and a set of paths, K and my decision variable is Z_jk. Currently I am defining the code like following, but it doesn't work. Could anybody please help me?
预先感谢。
// define variables
z = new IloNumVar[stations_start.size()][];
for (int j=0; j<stations_start.size();j++) {
z[j] = cplex.numVarArray(paths.size(),0,1);
for (int k=0;k<paths.size();k++) {
z[j][k] = cplex.numVar(new_column, 0, 1,"z");
z[j][k].setName("z."+j+"."+k);
}
}
推荐答案
您正在尝试创建二进制决策变量的二维数组。您会遇到什么错误?
You are trying to create a 2D array of binary decision variables. What errors are you getting?
尝试查看CPLEX随附的一些示例代码。参见例如transport.java,其中包括一些这样声明和初始化的变量二维数组:
Try looking at some of the sample code provided with CPLEX. See for example transport.java which includes some 2D arrays of variables declared and initialised like this:
IloNumVar[][] x = new IloNumVar[nbSupply][];
IloNumVar[][] y = new IloNumVar[nbSupply][];
for (int i = 0; i < nbSupply; i++) {
x[i] = cplex.numVarArray(nbDemand, 0., Double.MAX_VALUE);
y[i] = cplex.numVarArray(nbDemand, 0., Double.MAX_VALUE);
}
这篇关于使用Cplex在Java中定义二进制决策变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!