简体   繁体   English

从其他类访问cplex java变量

[英]cplex java variable access from other class

I'm using cplex in Java and I want to access the value of a variable from another class (after solving). 我在Java中使用cplex,并且想从另一个类访问变量的值(求解后)。

My program configuration class which should print the variable p15[i][j][q] looks like this: 我的程序配置类应显示变量p15 [i] [j] [q]如下所示:

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. 不幸的是,配置类中出现错误消息:“ p15无法解析为变量”。 Is that because the variable is initialized in another class? 那是因为变量是在另一个类中初始化的吗?

What would be the most elegant way to solve the problem? 解决问题的最优雅方法是什么?

You can access a static variable declared in another class using the syntax OtherClass.p15 . 您可以使用语法OtherClass.p15访问在另一个类中声明的static变量。 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: 如果变量是实例变量(声明中没有static关键字),则您需要另一个类的实例来访问变量:

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. 当方法buildModel返回时,局部变量p15不再存在(超出范围)。 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: 我认为最简单的更改是将MainTSP类修改为如下所示:

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 然后,在您的Ausführung类的main方法中,您可以像访问

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

There doesn't seem to be any reason to make p15 an instance field. 似乎没有任何理由使p15成为实例字段。 (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(); .) (如果由于其他原因需要将其作为实例字段,则需要使用MainTSP tsp = new MainTSP();类的东西来创建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: 一种替代方法是使buildModel返回p15 ,然后可以将返回值分配给Ausführung类中另一个名为p15变量:

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.) (也许这就是您计划使用w参数执行的操作?也许只需在buildModel中用w替换p15 buildModel解决您的问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM