【问题标题】:How to SheetRead in Cplex with variable column length?如何在具有可变列长度的 Cplex 中进行 SheetRead?
【发布时间】:2021-06-25 17:16:53
【问题描述】:

在我的 .dat 文件中,我想用 Excel 工作表中的列值填充一个数组。根据问题,值的数量不同,这意味着可能存在来自 A1:A10 或来自 A1:A20 的值。该数字在另一个 Excel 单元格中指定,也用作模型中的变量,因此也可以读取。我现在如何以正确的大小读取数组?也许字符串连接会起作用。我搜索类似:

numberRows from SheetRead(DataSource, "Data!B1");
input = "Data!A1:A" + numberRows;
variable from SheetRead(DataSource, input);

在 CPLEX 中是否有类似的可能?

【问题讨论】:

    标签: cplex opl


    【解决方案1】:

    OPL EXCEL 中的示例:https://github.com/AlexFleischerParis/oplexcel/blob/main/mainvariablesheetreadstring.mod

    主要思想是依赖一个 main 来更新将在 SheetRead 中使用的字符串。

    main
    {
      var source = new IloOplModelSource("variablesheetreadstring.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
      var opl = new IloOplModel(def,cplex);
      var data1=new IloOplDataElements();
      
      data1.paramread="params!A2";
      data1.parambuses="buses!A2:B3";
      data1.paramwrite="buses!E2:F3";
      
      opl.addDataSource(data1);
      
      var data2 = new IloOplDataSource("variablesheetreadstring.dat");
      opl.addDataSource(data2);
      opl.generate();
      if (cplex.solve()) {
         writeln("OBJ = " + cplex.getObjValue());
         opl.postProcess();
    
      } else {
         writeln("No solution");
      }
      
    
      opl.end();
      
      
    }
    

    或者你可能依赖一个不那么微妙的想法:读取所有单元格然后解析。

    Parse all strings from a spreadsheet

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-28
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多