【问题标题】:Reading from Excel iteratively from same excel file but on different sheets从相同的 Excel 文件但在不同的工作表上迭代地从 Excel 中读取
【发布时间】:2018-10-24 03:44:55
【问题描述】:

我正在寻找解决不同迭代的问题,我需要从同一个 Excel 文件中的工作表中读取每个迭代数据,就像我想解决我的模型,让我们说每次迭代 4 次在 4 个不同的工作表/迭代上从 Excel 读取的不同数据。有没有什么棘手的代码可以在我的主博客上实现,首先添加这些数据并解决问题?

【问题讨论】:

    标签: excel cplex


    【解决方案1】:

    假设您有一个 Excel 电子表格,其中包含 3 个选项卡,其中包含 3 个孩子数量选项:

    然后为了对这 3 个选项进行循环:

    你先写

    zooexcelmultisheet.mod

    string paramsread=...;
    
    tuple param
    {
    int nbKids;
    }
    
    {param} params=...;
    
    assert card(params)==1;
    
    int nbKids=first(params).nbKids;
    
    // a tuple is like a struct in C, a class in C++ or a record in Pascal
    tuple bus
    {
    key int nbSeats;
    float cost;
    }
    
    // This is a tuple set
    {bus} buses=...;
    
    // asserts help make sure data is fine
    assert forall(b in buses) b.nbSeats>0;
    assert forall(b in buses) b.cost>0;
    
    // decision variable array
    dvar int+ nbBus[buses];
    
    // objective
    minimize 
     sum(b in buses) b.cost*nbBus[b];
    
    // constraints 
    subject to
    {
     sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;
    }
    
    tuple result
    {
       key int nbSeats;
       int nbBuses;
    }
    
    {result} results={<b.nbSeats,nbBus[b]> | b in buses};
    
    execute
    {
    writeln(results);
    writeln("cost = ",cplex.getObjValue());
    }
    

    然后是zooexcelmultisheet.dat

    SheetConnection s("zoomultisheet.xlsx");
    
    //paramsread="params1!A2";
    params from SheetRead(s,paramsread);
    buses from SheetRead(s,"buses!A2:B3");*
    

    然后您将运行的内容为所有 3 个选项执行循环:

    {string} sheets={"params1","params2","params3"};
    
    
    main { 
      var source = new IloOplModelSource("zooexcelmultisheet.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
      var data = new IloOplDataSource("zooexcelmultisheet.dat");
    
      for(var sheet in thisOplModel.sheets)
      {
      var data0=new IloOplDataElements();
      data0.paramsread=sheet+"!A2"; 
      var opl = new IloOplModel(def,cplex);
      opl.addDataSource(data0);
      opl.addDataSource(data);
      opl.generate();
      if (cplex.solve()) {
    
         opl.postProcess();
      } else {
         writeln("No solution");
      }
    
    
      opl.end();
    }  
      data.end(); 
      def.end(); 
      cplex.end(); 
      source.end(); 
    }
    

    你会得到

    {<40 6> <30 2>}
    cost = 3800
     {<40 7> <30 1>}
    cost = 3900
     {<40 8> <30 0>}
    cost = 4000
    

    【讨论】:

    • 首先非常感谢您的宝贵帮助!真的很感激!我有一个更高级的问题:例如,在相同范围的第二次迭代中,数组(范围)的大小将大于或小于第一次迭代,我如何命令 cplex 从该部分获取信息,万分感谢!问候
    • 你也设置了 excel 的 paramsread 吗?因为我试图将您的方法介绍给我的模型并得到错误异常 IBM ILOG Concert : Excel range is unknown。
    • 您好,您可以在ibm.com/developerworks/community/forums/html/…找到所有文件
    猜你喜欢
    • 2020-08-03
    • 1970-01-01
    • 2019-06-17
    • 2022-01-26
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多