如何产生所需数量的纸浆。Python中的LpVariable(如x11、x12、x21、x22等)并将它们存储在字典中?

How to generate desired number of pulp.LpVariable in Python (such as x11, x12, x21, x22 and so on) and store them in a dictionary?

提问人:Ali E 提问时间:11/9/2023 更新时间:11/9/2023 访问量:16

问:

我想定义一定数量的数学 x 变量。我使用术语“数学 x 变量”,因为这些是我想用 Python 变量表示的数学变量。我需要这些 x 值,以便稍后在 LP(线性规划)问题中使用。

在 Python PuLP 库中,我们可以这样生成多个 x 变量:

x = {i: LpVariable(name=f"x{i}", lowBound=0) for i in range(1, 5)}

然后当我们使用时,我们得到字典:print(x)

{1: x1, 2: x2, 3: x3, 4: x4}

其中每个 x 值(如 x1、x2 等)都是类型。pulp.pulp.LpVariable

但是我想要一个代码来生成 x 的多个分支。我可以用它来生成我想要的尽可能多的分支。

我想将所有 x 值(例如 x11、x12、x21、x22...)存储在字典中。

这是我写的解决方案:

# Defining the decision variables
# Here, we create a nested dictionary to store the x variables
# Using a dictionary also allows us to store the indice values
x = {i: 
         {j: 
              {k: 
                   {l: 
                       LpVariable(name=f"x{i}{j}{k}{l}", lowBound=0, cat="Integer") 
                    for l in range(1, 3) } 
               for k in range(1, 4) } 
          for j in range(1, 4) } 
     for i in range(1, 3) }

此解决方案打印为:

{1: {1: {1: {1: x1111, 2: x1112},
         2: {1: x1121, 2: x1122},
         3: {1: x1131, 2: x1132}},
     2: {1: {1: x1211, 2: x1212},
         2: {1: x1221, 2: x1222},
         3: {1: x1231, 2: x1232}},
     3: {1: {1: x1311, 2: x1312},
         2: {1: x1321, 2: x1322},
         3: {1: x1331, 2: x1332}}},
 2: {1: {1: {1: x2111, 2: x2112},
         2: {1: x2121, 2: x2122},
         3: {1: x2131, 2: x2132}},
     2: {1: {1: x2211, 2: x2212},
         2: {1: x2221, 2: x2222},
         3: {1: x2231, 2: x2232}},
     3: {1: {1: x2311, 2: x2312},
         2: {1: x2321, 2: x2322},
         3: {1: x2331, 2: x2332}}}}

这就是我想要的。现在的问题是,我想自动完成同样的工作。因此,让我们用这样的函数来假设;

def generate_nested_variables(dimension_number, dimension_ranges)

这将生成具有所需范围的所需数量的维度(嵌套)。所以说

def generate_nested_variables(2, [3,5])

会打印出这个:

{1: {1: x11, 2: x12, 3: x13, 4: x14, 5: x15},
 2: {1: x21, 2: x22, 3: x23, 4: x24, 5: x25},
 3: {1: x31, 2: x32, 3: x33, 4: x34, 5: x35}}
python 字典 嵌套 纸浆

评论


答:

0赞 AirSquid 11/9/2023 #1

这看起来真的像是你试图以艰难的方式进行多维约束。

您应该分别为多个约束设置索引集,然后使用该功能来构造变量 - 这要容易得多,而且您不必为字符串标记而烦恼。然后,以后按多维索引中的项目索引变量要容易得多。我不确定你会如何在你的例子中做到这一点。.dicts()pulp

这里有一些想法。如果你在我的个人资料中搜索类似的东西,我相信你会找到其他人。LpVariable.dicts

法典:

import pulp as pl
from itertools import product

# some indexing sets (or dimensions)

d1 = [4, 5, 6]

d2_size = 4

d2 = list(range(1, d2_size + 1))

d3 = ['red', 'green', 'blue']

d123 = list(product(d1, d2, d3))

# declare the LP

prob = pl.LpProblem('example')

# make some variables

x = pl.LpVariable.dicts('x', indices=d1)

y = pl.LpVariable.dicts('y', indices=[(i, j) for i in d1 for j in d2])

z = pl.LpVariable.dicts('z', indices = d123)

# some example constraints

prob += x[4] >= 3.4

# sum over d2 dimension for each i in d1
for i in d1:
    prob += sum(y[i, j] for j in d2) >= 1.2

# sum over all the greens in z
greens = [(i, j, k) for i, j, k in d123 if k=='green']
prob += sum(z[green] for green in greens) <= 4

print(prob)

输出:

example:
MINIMIZE
None
SUBJECT TO
_C1: x_4 >= 3.4

_C2: y_(4,_1) + y_(4,_2) + y_(4,_3) + y_(4,_4) >= 1.2

_C3: y_(5,_1) + y_(5,_2) + y_(5,_3) + y_(5,_4) >= 1.2

_C4: y_(6,_1) + y_(6,_2) + y_(6,_3) + y_(6,_4) >= 1.2

_C5: z_(4,_1,_'green') + z_(4,_2,_'green') + z_(4,_3,_'green')
 + z_(4,_4,_'green') + z_(5,_1,_'green') + z_(5,_2,_'green')
 + z_(5,_3,_'green') + z_(5,_4,_'green') + z_(6,_1,_'green')
 + z_(6,_2,_'green') + z_(6,_3,_'green') + z_(6,_4,_'green') <= 4

VARIABLES
x_4 free Continuous
y_(4,_1) free Continuous
y_(4,_2) free Continuous
y_(4,_3) free Continuous
y_(4,_4) free Continuous
y_(5,_1) free Continuous
y_(5,_2) free Continuous
y_(5,_3) free Continuous
y_(5,_4) free Continuous
y_(6,_1) free Continuous
y_(6,_2) free Continuous
y_(6,_3) free Continuous
y_(6,_4) free Continuous
z_(4,_1,_'green') free Continuous
z_(4,_2,_'green') free Continuous
z_(4,_3,_'green') free Continuous
z_(4,_4,_'green') free Continuous
z_(5,_1,_'green') free Continuous
z_(5,_2,_'green') free Continuous
z_(5,_3,_'green') free Continuous
z_(5,_4,_'green') free Continuous
z_(6,_1,_'green') free Continuous
z_(6,_2,_'green') free Continuous
z_(6,_3,_'green') free Continuous
z_(6,_4,_'green') free Continuous