python - 如何使用 python 运行 .mod 文件(CPLEX)?
问题描述
我正在尝试使用 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
推荐阅读
- android - 我如何在颤动的新容器上放置单词
- python - 如何在调用者的 try/except 之前拦截被调用模块函数中的错误
- wordpress - 如何获取任何 WordPress 主题和插件的挂钩
- net-snmp - NET-SNMP 中的异步调用并不是真正的异步
- c++ - 在保留顺序函数的同时删除向量中的重复项的逻辑错误
- hyperledger-fabric - Cli 服务可以存在于安装在不同机器(Hyperledger Fabric)中的每个组织上吗?
- java - 如何模拟 IMPLY 门?
- neo4j - 由于 Neo4J 中缺少属性,连接不起作用
- amazon-web-services - S3 和 CloudFront 上的静态网站——如何隐藏网站的非产品版本
- python - 在某些迭代中在整个列表中迭代元素时,旧元素保留在新列表中