int 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 t_result
{
key int nbSeats;
int nb;
}
{t_result} result={<b.nbSeats,nbBus[b]> | b in buses};
/*
the csv file is
nbseats;cost;
40;500;
30;400;
*/
nbKids=300;
// New feature in CPLEX 20.10
CSVConnection csvinput("zoobusesinput.csv?firstIsHeader=true", "");
buses from CSVRead(csvinput, "");
CSVConnection csvoutput("zoobusesoutput.csv?outputOnly=true&append=false&outputSeparator=;", "");
result to CSVPublish(csvoutput, "");
/*
and the output file
40;6
30;2
*/
1条答案
按热度按时间vngu2lb81#
使用csv读取和csv发布
你可以看看
.mod https://github.com/AlexFleischerParis/zooopl/blob/master/zoocsv.mod
.dat https://github.com/AlexFleischerParis/zooopl/blob/master/zoocsv.dat
或者如果你想在脚本中做所有的事情,请参阅https://github.com/AlexFleischerParis/opltipsandtricks/blob/master/turncsvintodat.mod。