【发布时间】:2018-10-24 03:44:55
【问题描述】:
我正在寻找解决不同迭代的问题,我需要从同一个 Excel 文件中的工作表中读取每个迭代数据,就像我想解决我的模型,让我们说每次迭代 4 次在 4 个不同的工作表/迭代上从 Excel 读取的不同数据。有没有什么棘手的代码可以在我的主博客上实现,首先添加这些数据并解决问题?
【问题讨论】:
我正在寻找解决不同迭代的问题,我需要从同一个 Excel 文件中的工作表中读取每个迭代数据,就像我想解决我的模型,让我们说每次迭代 4 次在 4 个不同的工作表/迭代上从 Excel 读取的不同数据。有没有什么棘手的代码可以在我的主博客上实现,首先添加这些数据并解决问题?
【问题讨论】:
假设您有一个 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
【讨论】: