【问题标题】:How to run multiple instances(datasets) from same CPLEX model?如何从同一个 CPLEX 模型运行多个实例(数据集)?
【发布时间】:2019-08-29 23:55:37
【问题描述】:

我在 CPLEX 中有一个 MIP 模型作为 mod 文件。我在多个“.txt”文件中有数据集。有没有一种方法可以自动化 CPLEX 中的流程来解决许多实例的模型。目前,我为每个实例每次运行模型。我的实例为“M1_D1.txt”、“M2_D1.txt”、....“M100_D10.txt”。但是,要求解的模型是一样的。

在 Matlab 中,自动化过程更容易。但是我在任何地方都找不到如何在 CPLEX 中实现自动化。


   define variables
   retrieve data from .dat file

   define objective function
   define constraints

【问题讨论】:

    标签: database dataset cplex opl


    【解决方案1】:

    您可以使用主块(流控制) 这是一个例子:

    sub.mod

    float maxOfx = ...;
    dvar float x;
    
    maximize x;
    subject to {
      x<=maxOfx;
     }
    
    execute
    {
      writeln("x= ",x);
    }
    

    try1.dat

    maxOfx=1;
    

    try2.dat

    maxOfx=2;
    

    然后

    main.mod

    {string} datFiles=...;
    
    main {
          var source = new IloOplModelSource("sub.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
    
          for(var datFile in thisOplModel.datFiles)
          {
          var opl = new IloOplModel(def,cplex);
    
          var data2= new IloOplDataSource(datFile);
    
          opl.addDataSource(data2);
          opl.generate();
    
          if (cplex.solve()) {  
             opl.postProcess();
             var o=new IloOplOutputFile("res"+datFile+".txt");
             o.writeln("OBJ = " + cplex.getObjValue());
             o.close();
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
         opl.end();
        }  
    
        }
    

    main.dat

    datFiles={"Try1.dat","try2.dat"};
    

    在命令行中 oplrun main.mod main.dat 在 IDE 中,您需要在运行配置中包含 main.mod 和 main.dat

    【讨论】:

    • 非常感谢您,先生。你节省了我很多时间。真的很感激。 :)
    猜你喜欢
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多