我已经使用 cplex IBM studio 将我的线性程序写入 .mod 文件,如下所示:
int I = ...;
int J = ...;
int ds[1..I]=...;
int maxnint[1..J]=...;
dvar int E[1..I][1..J][1..6];
dvar boolean V[1..I];
minimize sum(i in 1..I)(E[i,J,maxnint[J]]-ds[i]*V[i]);
subject to{
forall(i in 1..I, j in 1..J, mu in 1..maxnint[j]) E[i,j,mu]>=0;
forall(i in 1..I)( t_max*V[i] >=E[i,J,maxnint[J]]-ds[i]);
forall(i in 1..I)(-t_max*(1-V[i]) <=E[i,J,maxnint[J]]-ds[i]);
}
我想将这个文件导入到python中,并使用我在python中读取的数据来解决它。 数据应该是这样的:
import numpy as np
I = range(10)
J = range(6)
ra = np.array([240, 400, 264, 390, 200, 440])
maxnint = np.array([1, 1, 3, 3, 1, 4])
ds = np.array([14, 19, 16, 21, 28,
14, 21, 19, 15, 14
])
有可能吗?我尽量避免在 python 中编写约束,因为我有很多约束和许多索引应该更难在 python 中实现。
你可以使用doopl。小例子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")
或者如果您不想使用 doopl,您可以从 python 调用 oplrun,如在 https://github.com/AlexFleischerParis/howtowithopl/blob/master/callexternalprogram.mod
另见