定义用于在 Python 内调用 CPLEX 的目录

Defining a directory for calling CPLEX within Python

我想从 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 或其他地方?

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 位于与包含 python 程序的 temp 相同的目录中的 temp2 目录中,那么我将更改

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

进入

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