【问题标题】:Filling a tuple from excel with the execute block用执行块从excel填充一个元组
【发布时间】:2021-04-09 19:00:10
【问题描述】:

我必须为我的 CPLEX CPL 模型填充以下元组:

tuple Assignment{
       int id; //ID of the Operation O_ij, connects opearation and assignment
       int worker; //worker l for operation O_ij
       int machine; //machine k for operation O_ij
       int time; //processing time of the operation O_ij
     }

现在的问题是元组的每个元素都应该以不同的方式填充,即“id”将来自迭代步骤,对于“worker”,我必须从单个元素中获取一组整数Excel 中的单元格,对于“机器”,我必须检查 Excel 中的单元格是否为空,最后对于“时间”,我必须将一些 Excel 单元格相乘。

我最初的想法是在执行块中使用一堆 for 循环来完成,因为我必须根据迭代移动单元格范围。问题是我不能在我的 .mod 文件中使用 SheetConnection,我也不能在 .data 文件中使用执行块。

有什么方法可以让我无法从 .mod 文件访问 Excel 或在 .data 文件中使用执行块?

【问题讨论】:

    标签: excel cplex opl


    【解决方案1】:

    https://www.linkedin.com/pulse/javascript-within-opl-cplex-alex-fleischer/所写

    在 OPL 中,我们可以使用脚本进行预处理和后处理,例如 流控,还可以在数据文件.dat内处理数据。

    https://github.com/AlexFleischerParis/oplscripting/blob/main/zooscriptingindata.dat 中的示例

    prepare {                                      
       function priceDecrease(s, name) {  
          writeln("before price decrease");
          writeln("s=",s);                
          for(i=0;i<s.size;i++) Opl.item(s,i).cost-=100;
          writeln("after price decrease");
          writeln("s=",s); 
          return true;
       }   
    }
    
    nbKids= 300;
    
    buses={<40,500>,<30,400>} invoke priceDecrease;
    

    您也可以在不使用 SheetRead 的情况下使用 SheetRead,如 https://github.com/AlexFleischerParis/oplexcel/blob/main/readwithoutsheetread.mod 中所示

    // Read from an Excel spreadsheet without SheetRead
    // which means you can use this on non Windows platform
    
    execute
        {
    
        function read_excel(filename,sheetname,skiprows,nrows,cols,datfilename,resname)
        {
            var quote="\"";
            
            var python=new IloOplOutputFile("c:\\temp\\readexcel.py");
            
            python.writeln("import pandas as pd");
            python.writeln("import xlrd");
            python.writeln("df=pd.read_excel('"+filename+"'"+",sheet_name = '"+sheetname+"'"+
            ",skiprows = "+skiprows+  ",nrows= "+nrows+ ","
            +"header=None,usecols = '"+cols+"')");
            python.writeln("print(df)");
            
            
            
            python.writeln("res = open(",quote,datfilename,quote,",",quote,"w",quote,")");
            python.writeln("res.write(",quote,resname,"=[",quote,")");
            python.writeln("res.write(",quote,"\\","n",quote,")");
            python.writeln("for i, row in enumerate(df.values):");
           
            python.writeln("   res.write(",quote,"[",quote,")");
            
            python.writeln("   for j in row:");
           
            python.writeln("      if (j==j):");
            python.writeln("         res.write(str(j))");
            python.writeln("         res.write(\",\")");
           
            python.writeln("   res.write(\"],\")    ");
            python.writeln("   res.write(",quote,"\\","n",quote,")");
            python.writeln("res.write(\"];\")");
            python.writeln("res.close()");
            python.close();
           
            python.close();
            
            IloOplExec("C:\\Python36\\python.exe c:\\temp\\readexcel.py",true);
            
        }
        read_excel("c:\\\\temp\\\\read2Darray.xls","Sheet1",0,2,"B:D","c:\\\\temp\\\\resexcel","res");
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多