首页 > 解决方案 > 调用与 Excel 链接的 CPLEX (.mod) 文件

问题描述

我想从 Python 调用一个 .mod CPLEX 文件。在下一个链接中,有关于如何在 Python 中调用 CPLEX 的说明:

如何使用 python 运行 .mod 文件(CPLEX)?

代码

但是,就我而言,.mod 文件使用从 Excel 读取的数据。在这种情况下,我是否需要使用:

import pandas

做这样的事情(调用与 Excel 链接的 CPLEX (.mod) 文件)是正确的还是好的方法?

在链接中,总线被定义为具有nbSeats成本特征的总线结构。

但是如果问题中没有这样的结构,只有单独的变量和参数,那我们应该用什么来代替opl.set_input()呢?

例如,如果代码中只定义了nbKids之类的变量或参数,我们如何将其从 Python 传递到 CPLEX .mod 文件?

标签: pythoncplex

解决方案


正如您在https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoocalloplwithdataindatfile.py中看到的

you can use a .dat file with doopl to read / write data with OPL from python:

from doopl.factory import *


# Create an OPL model from a .mod file
with create_opl_model(model="zootupleset.mod",data="zootupleset.dat") as opl:
    

    # 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")

然后在 .dat 中,您可以使用 SheetRead 连接 Excel 电子表格。

https://github.com/AlexFleischerParis/zooopl/blob/master/zooexcel.dat

SheetConnection s("zoo.xlsx");

params from SheetRead(s,"params!A2");
buses from SheetRead(s,"buses!A2:B3");
results to SheetWrite(s,"buses!E2:F3");

推荐阅读