python - 调用与 Excel 链接的 CPLEX (.mod) 文件
问题描述
我想从 Python 调用一个 .mod CPLEX 文件。在下一个链接中,有关于如何在 Python 中调用 CPLEX 的说明:
如何使用 python 运行 .mod 文件(CPLEX)?
但是,就我而言,.mod 文件使用从 Excel 读取的数据。在这种情况下,我是否需要使用:
import pandas
做这样的事情(调用与 Excel 链接的 CPLEX (.mod) 文件)是正确的还是好的方法?
在链接中,总线被定义为具有nbSeats和成本特征的总线结构。
但是如果问题中没有这样的结构,只有单独的变量和参数,那我们应该用什么来代替opl.set_input()
呢?
例如,如果代码中只定义了nbKids之类的变量或参数,我们如何将其从 Python 传递到 CPLEX .mod 文件?
解决方案
正如您在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");
推荐阅读
- reactjs - 在 React 中用逗号分隔价格/数字
- vb.net - 'List (of Strings) 类型的值无法转换为 String()
- swiftui - Assign Custom TextField data in another View SwiftUI
- javascript - React Typescript 自定义钩子排序列表
- gcc - 仅将链接器标志添加到 CMake 中的特定库
- python - 如何在 tkinter 中将按钮与标签链接,而不将它们明确声明为变量
- api - AppHostBase.Instance 在已经工作后已经设置好了
- jquery - 选择相邻班级
- html - 如何将字符串以外的内容放在占位符或值中(vue 属性)
- python - 比较两个坐标数组并根据它们的距离合并数组