如何使用python运行.mod文件(CPLEX)? [英] How to run a .mod file (CPLEX) using python?
本文介绍了如何使用python运行.mod文件(CPLEX)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试比较使用CPLEX的PLNE模型的结果,并且我想使用python来多次运行此CPLEX模型并比较结果.如何在python中运行.mod文件(在CPLEX上创建的文件)?我在python上阅读了有关cplex库的资料,但是,如果我理解正确,它的目的是直接在python上建立模型...我完全是菜鸟,所以任何建议都可以帮忙:)
I'm trying to compare results from a PLNE model using CPLEX, and I want to use python in order to run this CPLEX model several times and compare the results. How can I run .mod files (those made on CPLEX) in python ? I read stuffs about cplex library on python but, if I understood correctly, it is aimed at making models directly on python... I'm a total noob so any advice would help :)
谢谢!
推荐答案
最好是使用 doopl 包
对于实例,
您可以编写zootupleset.mod
You may write 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代码
and then the following python code
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
这篇关于如何使用python运行.mod文件(CPLEX)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文