【问题标题】:Defining a directory for calling CPLEX within Python在 Python 中定义用于调用 CPLEX 的目录
【发布时间】:2021-02-08 00:42:53
【问题描述】:

我想从 Python 运行以 OPL 语言 (CPLEX IDE) 编写的 .mod 文件。为此,我使用以下命令:

from doopl.factory import *
with create_opl_model(model=model_file) as model_name:
    model_name.run()

当然,首先,我需要打开名为model_file 的文件,并为此定义一个目录。为此,一开始,我会这样做:

import os
from os.path import dirname, abspath, join

现在,我的问题是:

1.不知道有没有需要abspath, join,或者直接next就够了:

from os.path import dirname

2.我想我需要使用下一个命令来定义目录?

DATADIR = join(dirname(abspath(__file__)))

model_file = join(DATADIR, 'main.mod')

但是我在哪里写目录?而不是 file 或其他地方?

【问题讨论】:

    标签: python cplex opl


    【解决方案1】:

    https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoocallopl.py

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

    我将 .mod 和 python 程序放在同一个目录中

    但如果 .mod 位于 temp2 目录中,该目录与包含 python 程序的 temp 位于同一目录中,那么我将更改

    with create_opl_model(model="zootupleset.mod") as opl:
    

    进入

    with create_opl_model(model="../temp2/zootupleset.mod") as opl:
    

    【讨论】:

    • 感谢您的详细解答。 @Alex Fleischer
    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2011-08-07
    • 2016-09-27
    • 1970-01-01
    相关资源
    最近更新 更多