IC SunsetThe developerWorks Connections Platform is now in read-only mode and content is only available for viewing. No new wiki pages, posts, or messages may be added. Please see our FAQ for more information. The developerWorks Connections platform will officially shut down on March 31, 2020 and content will no longer be available. More details available on our FAQ. (Read in Japanese.)
Topic
  • No replies
AlexFleischer
AlexFleischer
4044 Posts

Pinned topic How to take some information from a first model and use that as input for a second model

‏2017-09-29T11:42:03Z | control flow howto model multi

Hi,

 

I often get this question so let me give you a tiny example.

 

sub.mod

float maxOfx = ...;
    dvar float x;
    dvar float y;

    maximize y;
    subject to {
      ct:x<=maxOfx;
      y==2*x;
    }

    execute
    {
    writeln("x= ",x);
    writeln("y= ",y);
    }

can be read  "multiply by 2"

sub2.mod

float maxOfx = ...;
    dvar float x;
    dvar float y;

    maximize y;
    subject to {
      ct:x<=maxOfx;
      y==3*x;
    }

    execute
    {
    writeln("x= ",x);
    writeln("y= ",y);
    }

can be read  "multiply by 3"

And then

main {
  var source1 = new IloOplModelSource("sub.mod");
  var cplex = new IloCplex();
  var def1 = new IloOplModelDefinition(source1);
  var opl1 = new IloOplModel(def1,cplex);
  var data1= new IloOplDataElements();
  data1.maxOfx=5;
  opl1.addDataSource(data1);
  opl1.generate();
  if (cplex.solve()) {
     writeln("OBJ = " + cplex.getObjValue());
     opl1.postProcess();

  } else {
     writeln("No solution");
  }
   var source2 = new IloOplModelSource("sub2.mod");
  var cplex = new IloCplex();
  var def2 = new IloOplModelDefinition(source2);
  var opl2 = new IloOplModel(def2,cplex);
  var data2= new IloOplDataElements();
  data2.maxOfx=opl1.y.solutionValue; // transfer solution of model1 to input for model2
  opl2.addDataSource(data2);
  opl2.generate();

  if (cplex.solve()) {
     opl2.postProcess();
  } else {
     writeln("No solution");
  }

  opl1.end();
  opl2.end();
 
}

will give


x= 5
y= 10
x= 10
y= 30

sub.mod multiplies 5 by 2 and gives 10 which is the input for sub2.mod that will multiply 10 by 3

regards

 

 

Updated on 2017-10-04T09:41:04Z at 2017-10-04T09:41:04Z by AlexFleischer