用执行块填充 excel 中的一个元组
Filling a tuple from excel with the execute block
我必须为我的 CPLEX CPL 填写以下元组 model:
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 文件中使用执行块?
写在https://www.linkedin.com/pulse/javascript-within-opl-cplex-alex-fleischer/
In OPL we can use scripting for preprocessing and postprocessing,for
flow control, but also within the data file .dat to process data.
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");
}
我必须为我的 CPLEX CPL 填写以下元组 model:
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 文件中使用执行块?
写在https://www.linkedin.com/pulse/javascript-within-opl-cplex-alex-fleischer/
In OPL we can use scripting for preprocessing and postprocessing,for flow control, but also within the data file .dat to process data.
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");
}