首页 > 解决方案 > 如何在具有可变列长度的 Cplex 中进行 SheetRead?

问题描述

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

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

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

标签: cplexopl

解决方案


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();
  
  
}

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

解析电子表格中的所有字符串


推荐阅读