首页 > 解决方案 > 纸浆中的二维决策变量

问题描述

我是 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)}

谢谢!

标签: pulpinteger-programming

解决方案


欢迎来到 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]

推荐阅读