簡體   English   中英

在Java API中使用OPL模型時出現CPLEX熱啟動錯誤

[英]CPLEX warm start error when using OPL model in Java API

我正在嘗試使用Java API進行一個良好的開端,並且在將初始解決方案傳遞給模型時遇到一些問題。 在我的模型文件(.mod)中,我定義了一個2D決策變量,

range nodes = 1..5;
range vehicles = 1..2;
dvar int service_time[nodes][vehicles];

在我的java文件我建立的模型如下,並試圖通過初步的解決方案使用addMipStart()函數上述決定變量(如描述這里

static public void main(String[] args) throws Exception {


    int status = 127;

    try {

        IloOplFactory.setDebugMode(true);
        IloOplFactory oplF = new IloOplFactory();
        IloOplErrorHandler errHandler = oplF.createOplErrorHandler(System.out);
        IloOplModelSource modelSource = oplF.createOplModelSource(DATADIR + "/myModFile.mod");
        IloOplSettings settings = oplF.createOplSettings(errHandler);
        IloOplModelDefinition def = oplF.createOplModelDefinition(modelSource, settings);

        IloCplex cplex = oplF.createCplex();
        IloOplModel opl = oplF.createOplModel(def, cplex);

        //adding the custom data source
        IloOplDataSource dataSource = new VRPDataSource(oplF);
        opl.addDataSource(dataSource);

        //generating the model
        opl.generate();

        //creating the initial solution
        int i = 5;
        int j = 2;

        IloIntVar[][] var2D = new IloIntVar[i][];
        double[][] var2D_startingVals = new double[i][];

        for(int index1=0; index1 < i; index1++){
            var2D[index1] = new IloIntVar[j];
            var2D_startingVals[index1] = new double[j];

            for(int index2 = 0; index2 < j; index2++){
                String varName = "service_time("+ (index1+1) +")("+ (index2+1) +")";
                var2D[index1][index2] = cplex.intVar(0, 50, varName);

                //lets assume a unit matrix as the starting solution
                var2D_startingVals[index1][index2] = 1;
             }
         }

        //flatten the multi-dimensional IloNumVar and double arrays

        IloNumVar[] flat_var2D = new IloNumVar[i*j];
        double[] flat_var2D_startingVals = new double[i*j];
        for(int index1=0; index1 < i; index1++){
            for(int index2=0; index2 < j; index2++){
                flat_var2D[index1*j + index2] = var2D[index1][index2];
                flat_var2D_startingVals[index1*j + index2] = var2D_startingVals[index1][index2];
            }
        }

        // adding the MIPStart
        cplex.addMIPStart(flat_var2D, flat_var2D_startingVals, IloCplex.MIPStartEffort.Auto, "addMIPStart start");

        if(cplex.solve()){
            // more code
        }else{
            // more code
        }

        // more code

    }catch(Exception ex){
        // more code
    }
}

不幸的是,我在調用cplex.addMIPStart()函數的行中有一個異常,

 [java]     ### CONCERT exception: The referenced IloExtractable has not been extracted by the IloAlgorithm
 [java]     ilog.concert.IloException: The referenced IloExtractable has not been extracted by the IloAlgorithm
 [java]     at ilog.cplex.cppimpl.cplex_wrapJNI.IloCplex_addMIPStart__SWIG_0(Native Method)
 [java]     at ilog.cplex.cppimpl.IloCplex.addMIPStart(IloCplex.java:866)
 [java]     at ilog.cplex.IloCplex.addMIPStart(IloCplex.java:13219)
 [java]     at ilog.cplex.IloCplex.addMIPStart(IloCplex.java:13228)
 [java]     at myJavaClass.myJavaClass.main(myJavaClass.java:412)

我認為錯誤是由於我准備初始解決方案的方式而引起的,是否可以有人幫助我解決該問題。

非常感謝你。

問題在於您正在創建變量,而不是引用模型中的現有變量。 這些新變量在目標,約束等中不存在,因此您將獲得IloException (請參閱技術說明)。

您應該能夠執行以下操作來訪問現有變量( 請注意,此代碼尚未經過測試 ):

    IloIntRange nodes = opl.getElement("nodes").asIntRange();
    IloIntRange vehicles = opl.getElement("vehicles").asIntRange();
    IloIntVarMap serviceTime = opl.getElement("service_time").asIntVarMap();

    final int nbNodes = nodes.getSize();
    final int nbVehicles = vehicles.getSize();

    IloNumVar[] startX = new IloNumVar[nbNodes * nbVehicles];
    double[] startVals = new double[nbNodes * nbVehicles];
    for (int i = 0; i < nbNodes; i++) {
       IloIntVarMap inner = serviceTime.getSub(nodes.getValue(i));
       for (int j = 0; j < nbVehicles; j++) {
          int idx = i * nbVehicles + j;
          startX[idx] = inner.get(vehicles.getValue(j));
          startVals[idx] = 1.0;
       }
    }

    cplex.addMIPStart(startX, startVals);

看一下Iterators.java示例和getElement文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM