I am running a CPLEX OPL model for 10 entities. The objective is to optimize a certain objective for each entity. Step 1: The CPLEX model first runs for the first entity, optimizing the objective function. Step 2: After solving for the first entity, a non-decision variable or parameter is updated in the main function based on the optimization results obtained. Step 3: The updated data is then used to run the CPLEX model for the second entity. Step 4: This process continues iteratively for all 10 entities, where after each iteration, the necessary variables are updated before solving for the next entity. How to do this cplex opl using main function.
1 Answer
Let me share a small example I wrote :
main {
var source = new IloOplModelSource("subvalue.mod");
var cplex = new IloCplex();
var def = new IloOplModelDefinition(source);
for(var k=1;k<=10;k++)
{
var opl = new IloOplModel(def,cplex);
var data2= new IloOplDataElements();
data2.maxOfx=k;
opl.addDataSource(data2);
opl.generate();
if (cplex.solve()) {
opl.postProcess();
writeln("OBJ = " + cplex.getObjValue());
} else {
writeln("No solution");
}
opl.end();
}
}
which gives
x= 1
OBJ = 1
x= 2
OBJ = 2
x= 3
OBJ = 3
x= 4
OBJ = 4
x= 5
OBJ = 5
x= 6
OBJ = 6
x= 7
OBJ = 7
x= 8
OBJ = 8
x= 9
OBJ = 9
x= 10
OBJ = 10
with subvalue.mod
float maxOfx = ...;
dvar float x;
maximize x;
subject to {
x<=maxOfx;
}
execute
{
writeln("x= ",x);
}
2 Comments
SUBHADARSHINI PANDA
I have an excel sheet having data, which is loaded in example.dat file. Now an optimization code is written in subvalue.mod file. and main script is written in example.mod file. By using IloOplDataSource we are adding the example.dat file. The data, I am reading from the excel file is defined in subvalue.mod file. When I am trying to run the entire model I am getting error as one of the parameter say x is not defined. But I have defined it in subvalue.mod. Please help me in understanding the actual flow of the code.
Alex Fleischer
With regards to excel see github.com/AlexFleischerParis/oplexcel for examples you could get ideas from