【问题标题】:Calling a CPLEX (.mod) file which is linked with Excel调用与 Excel 链接的 CPLEX (.mod) 文件
【发布时间】:2020-10-25 17:14:26
【问题描述】:

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

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

Code

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

import pandas

执行这样的操作(调用与 Excel 链接的 CPLEX (.mod) 文件)是正确还是好的方法?

在链接中,buses 被定义为具有 nbSeatscost 特征的 bus 结构。

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

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

【问题讨论】:

    标签: python cplex


    【解决方案1】:

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

    【讨论】:

    • 我这样做了,现在代码正在运行。它甚至适用于嵌套文件,每个文件都链接到另一个 .dat 文件。谢谢,@Alex Fleischer。
    猜你喜欢
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多