调用与 Excel 链接的 CPLEX (.mod) 文件

Calling a CPLEX (.mod) file which is linked with Excel

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

内调用CPLEX的说明

How to run a .mod file (CPLEX) using python?

Code

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

import pandas

做这样的事情(调用 CPLEX (.mod) 文件 link 使用 Excel 编辑)是正确的还是好的方法?

在 link 中,buses 被定义为一个 bus 结构,它有 nbSeats 成本 功能。

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

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

正如您在 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");