入门Python使用CPLEX01——安装并应用示例(只有python基础,并解决问题)

Python使用CPLEX——只有python基础

  • 开篇简介
  • CPLEX介绍
  • python使用CPLEX的方法的中文版介绍

开篇简介

默认会安装python包,了解规划优化的基础知识,楼主的IDLE为pycharm。

CPLEX介绍

CPLEX是IBM出的规划优化求解器,介绍在这里[官网关于python API]。

python使用CPLEX的方法的中文版介绍

1.首先是在知乎中看到一名答主(见本章引用)的简要示例,下载docplex包,运行代码
2.但是RUN的时候提示错误,根据error提示,楼主下载cplex包,模型求解成功。
3.随后输入导出求解结果代码如下

// An highlighted block
opt_df = pd.DataFrame.from_dict(x_vars, orient="index",
                                 columns = ["variable_object"])opt_df.index =
pd.MultiIndex.from_tuples(opt_df.index,
                                names=["column_i", "column_j"])opt_df.reset_index(inplace=True)
#
opt_df["solution_value"] = opt_df["variable_object"].apply(lambda item: item.solution_value)
#
opt_df.drop(columns=["variable_object"], inplace=True)
opt_df.to_csv("./optimization_solution.csv")

https://zhuanlan.zhihu.com/p/124422566

出现提示
入门Python使用CPLEX01——安装并应用示例(只有python基础,并解决问题)_第1张图片
开始一个个解决问题嗷呜。

1.首先解决第一个Unresolved reference问题,没想到一下将第二个unresolved一并解决了。方法和过程见楼主下面的贴子

python导入包后调用提示unresolved reference

2.根据error提示,是声明与格式问题,调整缩进,代码如下。运行成功!
.py文件所在文件夹已经导出求解结果。

opt_df = pd.DataFrame.from_dict(x_vars, orient="index",columns = ["variable_object"])
opt_df.index = pd.MultiIndex.from_tuples(opt_df.index,names=["column_i", "column_j"])
opt_df.reset_index(inplace=True)
#
opt_df["solution_value"] = opt_df["variable_object"].apply(lambda item: item.solution_value)
#
opt_df.drop(columns=["variable_object"], inplace=True)
opt_df.to_csv("./optimization_solution.csv")

你可能感兴趣的:(python,规划优化求解,python,pycharm)