问题描述
我想从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
,还是就像下一个就足够了:
from os.path import dirname
2。。我想我需要使用以下命令来定义目录吗?
DATADIR = join(dirname(abspath(__file__)))
model_file = join(DATADIR,'main.mod')
但是我应该在哪里写目录?代替文件或其他地方?
解决方法
在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目录中,而temp2与包含python程序的temp位于同一目录中,那么我会更改
with create_opl_model(model="zootupleset.mod") as opl:
进入
with create_opl_model(model="../temp2/zootupleset.mod") as opl: