对于“此平台不支持工作表数据”,您可以在OPL and Excel 中看到Read without SheetRead (useful for MacOs and Linux)
// 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");
}
对于导出区间,您可以先将区间转换为元组集,然后导出元组集。
以动物园调度为例:
/*
Model written by Philippe Laborie
https://www.linkedin.com/pulse/zoo-buses-kids-optimization-resource-allocation-philippe-laborie/
*/
using CP;
tuple Bus { string Name; int Seats; int Cost; int Avail; int Outward; int Return; }
{Bus} Buses = {
< "A40", 40, 500, 480, 30, 25 >,
< "B40", 40, 500, 480, 30, 25 >,
< "C40", 40, 500, 480, 30, 25 >,
< "D40", 40, 500, 480, 30, 25 >,
< "E40", 40, 500, 510, 30, 25 >,
< "F30", 30, 400, 480, 25, 20 >,
< "G30", 30, 400, 480, 25, 20 >,
< "H30", 30, 400, 480, 25, 20 >,
< "I30", 30, 400, 480, 25, 20 >,
< "J30", 30, 400, 510, 25, 20 >
};
int Opening = 600; // 10:00
int NbKids = 300;
int NbTeachers = 3;
int NbMaxTrips = 2;
dvar interval outwardTrip[b in Buses][i in 1..NbMaxTrips] optional in b.Avail..Opening size b.Outward;
dvar interval roundTrip [b in Buses][i in 1..NbMaxTrips] optional size b.Outward+b.Return;
minimize sum(b in Buses, i in 1..NbMaxTrips) (presenceOf(outwardTrip[b][i]) * b.Cost);
subject to {
sum(b in Buses, i in 1..NbMaxTrips) (presenceOf(outwardTrip[b][i]) * b.Seats) >= NbKids;
sum(b in Buses, i in 1..NbMaxTrips) pulse(roundTrip[b][i],1) <= NbTeachers;
forall(b in Buses) {
forall(i in 1..NbMaxTrips) {
presenceOf(outwardTrip[b][i]) == presenceOf(roundTrip[b][i]);startAtStart(outwardTrip[b][i], roundTrip[b][i]);
if (i>1) {
presenceOf(roundTrip[b][i]) => presenceOf(roundTrip[b][i-1]);
endBeforeStart(roundTrip[b][i-1], roundTrip[b][i]);
}
}
}
}
tuple result
{
string name;
int seats;
int s;
int e;
}
{result} results=
{<b.Name,b.Seats,startOf(outwardTrip[b][i]),endOf(outwardTrip[b][i])> | b in Buses, i in 1..NbMaxTrips : presenceOf(outwardTrip[b][i])==1};
execute
{
writeln(results);
}