Question

I'm using cplex in Java and I want to access the value of a variable from another class (after solving).

My program configuration class which should print the variable p15[i][j][q] looks like this:

import [...]
public class Ausführung {
public static void main(String[] args) throws IOException {
try{
            String filename = "[...]
            Data data = new Data(filename);

            IloCplex cplex = new IloCplex();
            IloNumVar[][][] w = new IloIntVar[n][n][n]; 

            MainTSP.buildModel(cplex, data, w);

            if(cplex.solve()){
            for(int q=0; q< data.distance1.length-1; q++){
                for(int ii=0; ii<data.distance1.length; ii++){
                    for(int j=0; j<data.distance1.length; j++){
                       if(cplex.getValue(p15[i][j][q]) >= 1) System.out.println("p15");
                    }
                }
            }
            cplex.end();
        }
        [...]
    }
}

The variable is initialized in my main program class like this:

public class MainTSP {
    static void buildModel(IloMPModeler model, Data data, IloNumVar[][][] w) throws IloException{

    IloNumVar[][][] p15 = new IloIntVar[n][n][n];           
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                for(int q=0; q<n; q++){
                    p15[i][j][q] = model.intVar(lb, ub);
                }
            }
        }
[...]

Unfortunatly the error message: "p15 cannot be resolved to a variable" occurs in the configuration class. Is that because the variable is initialized in another class?

What would be the most elegant way to solve the problem?

Was it helpful?

Solution

You can access a static variable declared in another class using the syntax OtherClass.p15. If the variable is an instance variable (no static keyword in the declaration), then you need an instance of the other class to access the variable:

OtherClass thing = . . .;
. . . cplex.getValues(thing.p15[i][j][q]) . . .

In both cases, you will need to ensure that the variable has the right access qualifiers so that the referencing code can access it.

Without knowing more about your overall software structure, it's hard to say what the "most elegant" way would be to address the problem.

EDIT: After your edit, I think I see the problem. The local variable p15 ceases to exist (goes out of scope) when the method buildModel returns. You need to store the array somewhere that stays around. The simplest change, I think, would be to modify your MainTSP class to look like this:

public class MainTSP {
    static IloNumVar[][][] p15;

    static void buildModel(IloMPModeler model, Data data, IloNumVar[][][] w) throws IloException{

        p15 = new IloIntVar[n][n][n];           
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                for(int q=0; q<n; q++){
                    p15[i][j][q] = model.intVar(lb, ub);
                }
            }
        }
[...]

Then in your main method of class Ausführung, you can access it like

. . .
if(cplex.getValue(MainTSP.p15[i][j][q]) >= 1) . . .

There doesn't seem to be any reason to make p15 an instance field. (If for some other reason you needed it to be an instance field, you would then need to create an instance of MainTSP using something like MainTSP tsp = new MainTSP();.)

An alternative would be to make buildModel return p15 and then you could assign the return value to another variable named p15 in your Ausführung class:

public class MainTSP {

    static IloNumVar[][][] buildModel(IloMPModeler model, Data data, IloNumVar[][][] w) throws IloException{

        p15 = new IloIntVar[n][n][n];           
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                for(int q=0; q<n; q++){
                    p15[i][j][q] = model.intVar(lb, ub);
                }
            }
        }
        return p15;
[...]

Then:

IloNumVar[][][] p15 = MainTSP.buildModel(cplex, data, w);

(Perhaps that's what you planned to do with the w parameter? Perhaps simply replace p15 with w in buildModel will solve your problem.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top