pulp - 纸浆中的二维决策变量
问题描述
我是 PuLP 的新手,我正在尝试运行一个优化问题,其中我的一个决策变量是 2D。我对如何将 2D 决策变量声明为 plp.LpVariable 的一部分感到有些困惑?截至目前,这就是我声明变量的方式
a = { k : plp.LpVariable(name='a', lowBound=np.array([0, 0]), \
upBound=np.array([2, 3]), \
cat=plp.LpContinuous) for k in range(10)}
谢谢!
解决方案
欢迎来到 SO!您正在寻找的是类的dicts
方法LpVariable
。这允许您传入多维索引以创建 M x N 或 M x N x O(等)变量集。
它的用途在解决数独难题的纸浆文档示例中进行了说明:https ://coin-or.github.io/pulp/CaseStudies/a_sudoku_problem.html?highlight=dicts
该方法本身记录在这里: https ://coin-or.github.io/pulp/technical/pulp.html?highlight=dicts#pulp.LpVariable.dicts
据我所知,该方法不能直接接受每个变量不同的上限和下限,因此您需要执行以下操作:
up_bounds = [2,3]
a = pulp.LpVariable.dicts('a', range(2), lowBound=0)
for i in range(2):
prob += a[i] <= up_bounds[i]
推荐阅读
- python - python Django中存储的索引文件示例在哪里?
- javascript - 单击按钮时如何将url放入变量中
- vue.js - Vue $emit 没有在 finally() 中执行
- javascript - JavaScript - 如何从另一个函数访问变量?
- r - 代表错误:尝试复制“闭包”类型的对象(if 语句中的箱线图)
- android - Android ConstraintLayout内的按钮拒绝显示以水平方向居中的多字符文本
- security - 路径攻击的用户名和密码应该是什么
- c# - C# - 在一个方法中,我怎样才能有 2 个参数是一个?
- sql - 具有各自值的多列到行
- kubernetes - 如何在helm中设置环境变量?