You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CPLEX 12.6中基于同一CP模型运行多数据集实例?

适配CPLEX约束规划(CP)的OPL主脚本修改方案

问题描述

我正尝试在CPLEX 12.6中使用using CP编写迭代代码。此前该脚本用于OPL模型,请问需对下方的main.mod代码做哪些修改才能运行CP模型?

原main.mod代码

{string} datafile = ...;
int timeLimit = ... ;

main {  
  
  thisOplModel.generate();
  var Data = thisOplModel.dataElements;  

  var fResult = new IloOplOutputFile();
  fResult.open("D_Forte_Var_faible_m_2.csv");

 function computeSubMIP(s) {
       var subSource = new IloOplModelSource("MIP (nouveau).mod");
       var subDef = new IloOplModelDefinition(subSource);
       var subCplex = new IloCplex();
       var subDataSource = new IloOplDataSource(s);
       // Parametrage Cplex
       subCplex.tilim = Data.timeLimit;
       // Imports des donnees dans modele OPL
       var Opl0 = new IloOplModel(subDef, subCplex);
       Opl0.addDataSource(subDataSource);
       Opl0.generate();
      if ( subCplex.solve() ) {
          //fResult.write(subCplex.getBestObjValue(), " ; ");
          fResult.write(subCplex.getObjValue(), " ; ");
          fResult.writeln(subCplex.getSolvedTime(), " ; "); 
          Opl0.postProcess();             
       } else {
          writeln("ERROR IN SUB MIP COMPUTATION !!");
        }
     subDataSource.end();  
     Opl0.end();
     subCplex.end();
     subSource.end();
}
  for ( var s in Data.datafile){  
     fResult.write(s, " ; "); 
     computeSubMIP(s);
 }
 fResult.close();
}

修改方案

要将原本适配MIP的脚本改成支持CP(约束规划)的版本,需要做以下核心修改:

1. 替换求解器实例:从IloCplex改为IloCP

原代码创建的是MIP求解器,CP模型需要使用约束规划专属的求解器实例,修改对应行:

// 原代码
var subCplex = new IloCplex();
// 修改后
var subCP = new IloCP();

后续所有引用subCplex的地方都要同步替换为subCP。

2. 调整求解器参数设置

CP求解器的时间限制参数名与MIP不同,需修改参数赋值语句:

// 原代码
subCplex.tilim = Data.timeLimit;
// 修改后
subCP.timeLimit = Data.timeLimit;

3. 更新模型源文件路径

原脚本加载的是MIP模型文件,需替换为你的CP模型文件(该文件开头必须包含using CP;声明):

// 原代码
var subSource = new IloOplModelSource("MIP (nouveau).mod");
// 修改为你的CP模型文件路径
var subSource = new IloOplModelSource("你的CP模型文件名.mod");

4. 调整解的获取方法

CP求解器的目标值、求解时间的获取属性与MIP不同,修改结果写入逻辑:

// 原代码
fResult.write(subCplex.getObjValue(), " ; ");
fResult.writeln(subCplex.getSolvedTime(), " ; ");
// 修改后
fResult.write(subCP.objectiveValue, " ; ");
fResult.writeln(subCP.solveTime, " ; ");

5. 修正求解错误提示

将错误提示改为贴合CP场景的表述:

// 原代码
writeln("ERROR IN SUB MIP COMPUTATION !!");
// 修改后
writeln("ERROR IN SUB CP COMPUTATION !!");

6. 同步资源清理的变量名

确保资源释放部分的变量名与修改后的求解器实例一致:

// 原代码
Opl0.end();
subCplex.end();
// 修改后
Opl0.end();
subCP.end();

修改后的完整main.mod示例

{string} datafile = ...;
int timeLimit = ... ;

main {  
  
  thisOplModel.generate();
  var Data = thisOplModel.dataElements;  

  var fResult = new IloOplOutputFile();
  fResult.open("D_Forte_Var_faible_m_2.csv");

 function computeSubCP(s) {
       var subSource = new IloOplModelSource("你的CP模型文件名.mod");
       var subDef = new IloOplModelDefinition(subSource);
       var subCP = new IloCP();
       var subDataSource = new IloOplDataSource(s);
       // Parametrage CP
       subCP.timeLimit = Data.timeLimit;
       // Imports des donnees dans modele OPL
       var Opl0 = new IloOplModel(subDef, subCP);
       Opl0.addDataSource(subDataSource);
       Opl0.generate();
      if ( subCP.solve() ) {
          fResult.write(subCP.objectiveValue, " ; ");
          fResult.writeln(subCP.solveTime, " ; "); 
          Opl0.postProcess();             
       } else {
          writeln("ERROR IN SUB CP COMPUTATION !!");
        }
     subDataSource.end();  
     Opl0.end();
     subCP.end();
     subSource.end();
}
  for ( var s in Data.datafile){  
     fResult.write(s, " ; "); 
     computeSubCP(s);
 }
 fResult.close();
}

内容的提问来源于stack exchange,提问作者Meriem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 07:09:12