Defining a binary decision variable in java using cplex

前端 未结 2 1831
抹茶落季
抹茶落季 2021-01-28 05:52

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 o

相关标签:
2条回答
  • 2021-01-28 05:58

    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 ...
    }
    
    0 讨论(0)
  • 2021-01-28 06:23

    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);
        } 
    
    0 讨论(0)
提交回复
热议问题