如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");
}