Search code examples
cplexopl

Cplex pass decision variable to another model using ILOG Script


I have two separate Cplex models, where the second model depends on the solution of the first one.

I want to solve the models using ILOG Script within the Optimization Studio.

So far I am at the following for my main file, which controls the execution of both models.

// Stage 1
    var source = new IloOplModelSource("TwoStage_Stage1.mod");
    var cplex = new IloCplex();
    var def = new IloOplModelDefinition(source);
    var modelInstance = new IloOplModel(def, cplex);
    var data = new IloOplDataSource("TwoStage.dat");
    modelInstance.addDataSource(data);
    modelInstance.generate();
    cplex.solve();

    modelInstance.postProcess();

// Stage 2
    var source2 = new IloOplModelSource("TwoStage_Stage2.mod");
    var cplex2 = new IloCplex();
    var def2 = new IloOplModelDefinition(source2);
    var modelInstance2 = new IloOplModel(def2, cplex2);
    var x_fromStage1 = new IloOplDataElements(); 
    var y_fromStage1 = new IloOplDataElements(); 

    x_fromStage1.xbest = modelInstance.xbest;
    y_fromStage1.ybest = modelInstance.ybest;

    modelInstance2.addDataSource(x_fromStage1);
    modelInstance2.addDataSource(y_fromStage1);
    var data2 = new IloOplDataSource("TwoStage.dat");
    modelInstance2.addDataSource(data2);

    modelInstance2.generate();
    cplex2.solve();

In stage 2, I tried to read in the solution of the decision variables xbest and ybest from the first modelInstance. xbest and ybest are not the actual decision variables from the first model, but a copy which I have made in the postprocessing in the first model as suggested in several other threads. xbest is a two dimensional array and ybest a three dimensional one. I have declared both these variables in the .mod-file for the second model as

int xbest[set1][set2];
int ybest[set3][set4][set5];

I get the following error messages:

  • the element xbest is not defined
  • invalid initialization expression for xbest

Am I on the right track to pass variables from one model to another one or does it work completely different?

Thanks already a lot.


Solution

  • if you write the result into a .dat then you should replace

    int xbest[set1][set2];
    int ybest[set3][set4][set5];
    

    by

    int xbest[set1][set2]=...;
    int ybest[set3][set4][set5]=...;
    

    to read the .dat in the second model