我正在寻找解决不同迭代问题的方法,我需要从同一excel文件中的工作表中读取每个迭代数据,就像我想解决我的模型,例如每次迭代4次从Excel读取4次不同工作表/迭代上的不同数据。我可以在主博客上实现一些棘手的代码,以首先添加这些数据并解决问题吗?
答案 0 :(得分:0)
看来我的模型运行没有任何错误,但没有给我结果,因为我认为没有进行数据传输 像你一样,我不能使用元组,因为我的模型使用数组。
这就是我在模型上添加的内容。
`string readP=...;
string readM=...;
string readI=...;
string readH=...;
string readXmih=...;
string readQpih=...;
string readJmax=...;
string readDmax=...;
string readDpm=...;
`
我将其添加到主模型上
for (var sheet in thisOplModel.sheets)
{
var data0=new IloOplDataElements();
var data1=new IloOplDataElements();
var data2=new IloOplDataElements();
var data3=new IloOplDataElements();
var data4=new IloOplDataElements();
var data5=new IloOplDataElements();
var data6=new IloOplDataElements();
var data7=new IloOplDataElements();
var data8=new IloOplDataElements();
data0.readP=sheet+"!B9";
data1.readM=sheet+"!B10";
data2.readI=sheet+"!B11";
data3.readH=sheet+"!B12";
data4.readXmih=sheet+"!N3:BE12";
data5.readQpih=sheet+"!N19:BE30";
data6.readDpm=sheet+"!B18:K29";
data7.readJmax=sheet+"!E2:E13";
data8.readDmax=sheet+"!E31";
var Opl = new IloOplModel(def,cplex);
Opl.addDataSource(data0);
Opl.addDataSource(data1);
Opl.addDataSource(data2);
Opl.addDataSource(data3);
Opl.addDataSource(data4);
Opl.addDataSource(data5);
Opl.addDataSource(data6);
Opl.addDataSource(data7);
Opl.addDataSource(data8);
Opl.addDataSource(data);
和.dat
SheetConnection my_sheet ("E2.xlsx");
p from SheetRead(my_sheet,readP);
m from SheetRead(my_sheet,readM);
h from SheetRead(my_sheet,readH);
i from SheetRead(my_sheet,readI);
X from SheetRead(my_sheet,readXmih);
Jpmax from SheetRead (my_sheet,readJmax);
Dpm from SheetRead (my_sheet,readDpm);
Dmax from SheetRead (my_sheet,readDmax);
Q from SheetRead (my_sheet,readQpih);
我必须执行与此处相同的操作,但是对于我的数组而不是针对元组,我该怎么做
tuple param
{
int nbKids;
}
{param} params=...;
assert card(params)==1;
int nbKids=first(params).nbKids;
非常感谢您的帮助!
致谢
答案 1 :(得分:-1)
假设您有一个带有3个标签的excel电子表格,其中有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
致谢