1 回答
TA贡献1780条经验 获得超1个赞
你需要使用元组集,但在
tuple bounds {
int lowerBound;
int upperBound;
}
那不是你所做的。
你应该写
tuple typebounds {
int lowerBound;
int upperBound;
}
{typebounds} bounds=...;
在你的 .mod 文件中
让我分享一个完整的例子:
from doopl.factory import *
# Data
Buses=[
(40,500),
(30,400)
]
MinAndMax=[(1,5)]
# Create an OPL model from a .mod file
with create_opl_model(model="zootuplesetwithminandmax.mod") as opl:
# tuple can be a list of tuples, a pandas dataframe...
opl.set_input("buses", Buses)
opl.set_input("singletonMinAndMax", MinAndMax)
# Generate the problem and solve it.
opl.run()
# Get the names of post processing tables
print("Table names are: "+ str(opl.output_table_names))
# Get all the post processing tables as dataframes.
for name, table in iteritems(opl.report):
print("Table : " + name)
for t in table.itertuples(index=False):
print(t)
# nicer display
for t in table.itertuples(index=False):
print(t[0]," buses ",t[1], "seats")
与zootuplesetwithminandmax.mod
int nbKids=300;
// 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=...;
tuple minandmax
{
int m;
int M;
}
{minandmax} singletonMinAndMax=...;
int minBuses=first(singletonMinAndMax).m;
int maxBuses=first(singletonMinAndMax).M;
// 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] in minBuses..maxBuses;
// objective
minimize
sum(b in buses) b.cost*nbBus[b];
// constraints
subject to
{
sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;
}
tuple solution
{
int nbBus;
int sizeBus;
}
{solution} solutions={<nbBus[b],b.nbSeats> | b in buses};
添加回答
举报