Defining a binary decision variable in java using cplex

梦想与她 提交于 2020-08-11 19:34:28

问题


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?

Thanks in advance.

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

回答1:


You are trying to create a 2D array of binary decision variables. What errors are you getting?

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



回答2:


For a binary decision variable you may prefer to use IloBoolVar rather than the IloNumVar or even IloIntVar options.

You do need to declare the dimension in one of two ways, however. You've declared new IloNumVar[stations_start.size()][]; but not specified the second dimension. The easiest approach would be to declare:

z = new IloBoolVar[stations_start.size()][paths.size()];

Alternately you can keep your existing declaration, but in the loop add on the second dimension:

z = new IloBoolVar[stations_start.size()][];
for (int j=0; j<stations_start.size(); j++) {
    z[j] = new IloBoolVar[paths.size()];
    ... existing assignments ...
}


来源:https://stackoverflow.com/questions/38597664/defining-a-binary-decision-variable-in-java-using-cplex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!