[英]How to define parameters in python and send them to CPLEX mod file?
[英]How to run a .mod file (CPLEX) using python?
我正在嘗試使用 CPLEX 比較 PLNE 模型的結果,並且我想使用 python 多次運行此 CPLEX 模型並比較結果。 如何在 python 中運行 .mod 文件(在 CPLEX 上創建的文件)? 我在 python 上閱讀了關於 cplex 庫的東西,但是,如果我理解正確,它的目的是直接在 python 上制作模型......我是個菜鳥,所以任何建議都會有所幫助:)
謝謝 !
最好是使用doopl包
例如,
你可以寫 zootupleset.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=...;
// 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 solution
{
int nbBus;
int sizeBus;
}
{solution} solutions={<nbBus[b],b.nbSeats> | b in buses};
然后是以下python代碼
from doopl.factory import *
# Data
Buses=[
(40,500),
(30,400)
]
# Create an OPL model from a .mod file
with create_opl_model(model="zootupleset.mod") as opl:
# tuple can be a list of tuples, a pandas dataframe...
opl.set_input("buses", Buses)
# 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")
給
Table names are: ['solutions']
Table : solutions
Pandas(nbBus=6, sizeBus=40)
Pandas(nbBus=2, sizeBus=30)
6 buses 40 seats
2 buses 30 seats
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.