Cplex 使用 ILOG 脚本将决策变量传递给另一个模型
Cplex pass decision variable to another model using ILOG Script
我有两个独立的 Cplex model,其中第二个 model 取决于第一个的解决方案。
我想在 Optimization Studio 中使用 ILOG 脚本解决 models。
到目前为止,我的主文件位于以下位置,它控制两个 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();
在第 2 阶段,我尝试从第一个 modelInstance 中读取决策变量 xbest 和 ybest 的解。 xbest 和 ybest 不是第一个 model 的实际决策变量,而是我在第一个 model 的后处理中制作的副本,如其他几个线程中所建议的那样。 xbest 是一个二维数组,ybest 是一个三维数组。我已经在 .mod 文件中为第二个 model 声明了这两个变量为
int xbest[set1][set2];
int ybest[set3][set4][set5];
我收到以下错误消息:
- 未定义元素 xbest
- xbest 的无效初始化表达式
我是否在正确的轨道上将变量从一个 model 传递到另一个 model 还是它的工作方式完全不同?
非常感谢。
如果将结果写入 .dat,则应替换
int xbest[set1][set2];
int ybest[set3][set4][set5];
来自
int xbest[set1][set2]=...;
int ybest[set3][set4][set5]=...;
读取第二个模型中的 .dat
谢谢。几分钟前我自己想出了这个解决方案。
我找到的解决方案如下。
// 从第一个 model
中检索决策变量的解值
Stage1_opl.x.solutionValue;
Stage1_opl.y.solutionValue;
// 将解决方案值添加到第二个 model
中的数据元素
var Stage2_data = new IloOplDataElements();
Stage2_data.xbest = Stage1_opl.x.solutionValue;
Stage2_data.ybest = Stage1_opl.y.solutionValue;
Stage2_opl.addDataSource(Stage2_data);
正如亚历克斯指出的那样
if you write the result into a .dat then you should replace
int xbest[set1][set2];
int ybest[set3][set4][set5];
来自
int xbest[set1][set2]=...;
int ybest[set3][set4][set5]=...;
to read the .dat in the second model
必须在第二个 model 的 .mod 文件中进行更改。
我有两个独立的 Cplex model,其中第二个 model 取决于第一个的解决方案。
我想在 Optimization Studio 中使用 ILOG 脚本解决 models。
到目前为止,我的主文件位于以下位置,它控制两个 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();
在第 2 阶段,我尝试从第一个 modelInstance 中读取决策变量 xbest 和 ybest 的解。 xbest 和 ybest 不是第一个 model 的实际决策变量,而是我在第一个 model 的后处理中制作的副本,如其他几个线程中所建议的那样。 xbest 是一个二维数组,ybest 是一个三维数组。我已经在 .mod 文件中为第二个 model 声明了这两个变量为
int xbest[set1][set2];
int ybest[set3][set4][set5];
我收到以下错误消息:
- 未定义元素 xbest
- xbest 的无效初始化表达式
我是否在正确的轨道上将变量从一个 model 传递到另一个 model 还是它的工作方式完全不同?
非常感谢。
如果将结果写入 .dat,则应替换
int xbest[set1][set2];
int ybest[set3][set4][set5];
来自
int xbest[set1][set2]=...;
int ybest[set3][set4][set5]=...;
读取第二个模型中的 .dat
谢谢。几分钟前我自己想出了这个解决方案。 我找到的解决方案如下。
// 从第一个 model
中检索决策变量的解值Stage1_opl.x.solutionValue;
Stage1_opl.y.solutionValue;
// 将解决方案值添加到第二个 model
中的数据元素var Stage2_data = new IloOplDataElements();
Stage2_data.xbest = Stage1_opl.x.solutionValue;
Stage2_data.ybest = Stage1_opl.y.solutionValue;
Stage2_opl.addDataSource(Stage2_data);
正如亚历克斯指出的那样
if you write the result into a .dat then you should replace
int xbest[set1][set2];
int ybest[set3][set4][set5];
来自
int xbest[set1][set2]=...;
int ybest[set3][set4][set5]=...;
to read the .dat in the second model
必须在第二个 model 的 .mod 文件中进行更改。