简体   繁体   English

循环遍历 PuLP 嵌套变量以获得特定约束

[英]Looping through PuLP nested variables for specific constraints

I have the following constraint:我有以下约束:

sum of (between p = 0 to p = 2) of X_sap <= A_sa

My classes are:我的课程是:

# s
site_classes = [1, 2]
# a
age_classes = [1, 2, 3]
# p
period_classes = [0, 1, 2]

age_areas = {1: {'age_range': '0to10', 1: 3000, 2: 8000},
             2: {'age_range': '11to20', 1: 6000, 2: 4000},
             3: {'age_range': '21to30', 1: 9000, 2: 7000}}

My sap is defined as follow:我的 sap 定义如下:

sap = []
for s in site_classes:
    for a in age_classes:
        for p in period_classes:
            sap.append(f'{s}_{a}_{p}')

Here's how I'm creating my X_sap:这是我创建 X_sap 的方式:

x_vars = lp.LpVariable.dicts("X", sap, lowBound=0, cat='Continuous')

Here's how I'm creating my area constraints这是我创建区域约束的方式

area_c_dict = {}
for s in site_classes:
    for a in age_classes:
        for p in period_classes:
            area_c_dict[f'{s}_{a}_{p}'] = age_areas[a][s]

I'm struggling to generate my constraints to output in this way我正在努力以这种方式生成对输出的约束

X110 + X111 + X112 <= 3,000
X120 + X121 + X122 <= 6,000
X130 + X131 + X132 <= 9,000
X210 + X211 + X212 <= 8,000
X220 + X221 + X222 <= 4,000
X230 + X231 + X232 <= 7,000

My outputted variables are我的输出变量是

area_c_dict = {'1_1_0': 3000, '1_1_1': 3000, '1_1_2': 3000, '1_2_0': 6000, '1_2_1': 6000, '1_2_2': 6000, '1_3_0': 9000, '1_3_1': 9000, '1_3_2': 9000, '2_1_0': 8000, '2_1_1': 8000, '2_1_2': 8000, '2_2_0': 4000, '2_2_1': 4000, '2_2_2': 4000, '2_3_0': 7000, '2_3_1': 7000, '2_3_2': 7000}


x_vars = {'1_1_0': X_1_1_0, '1_1_1': X_1_1_1, '1_1_2': X_1_1_2, '1_2_0': X_1_2_0, '1_2_1': X_1_2_1, '1_2_2': X_1_2_2, '1_3_0': X_1_3_0, '1_3_1': X_1_3_1, '1_3_2': X_1_3_2, '2_1_0': X_2_1_0, '2_1_1': X_2_1_1, '2_1_2': X_2_1_2, '2_2_0': X_2_2_0, '2_2_1': X_2_2_1, '2_2_2': X_2_2_2, '2_3_0': X_2_3_0, '2_3_1': X_2_3_1, '2_3_2': X_2_3_2}

Any help would be greatly appreciated on how I can achieve that.关于我如何实现这一目标的任何帮助将不胜感激。 I'm not sure how to loop through my x_vars without breaking it我不确定如何在不破坏它的情况下遍历我的 x_vars

First, create your decision variables like this:首先,创建您的决策变量,如下所示:

x_vars = lp.LpVariable.dicts("X", [(s,a,p) for s in site_classes for a in age_classes for p in period_classes] , lowBound=0, cat='Continuous')

Then use the lpSum function from the pulp library to make your constraints:然后使用纸浆库中的lpSum函数来进行约束:

for s in site_classes:
    for a in age_classes:
        prob += lp.lpSum(x_vars[(s,a,p)] for p in period_classes) <= age_areas[a][s]

The prob variable is an instance of the LpProblem class prob变量是LpProblem类的一个实例


Demonstration on how to define a decision variable for the sum of variables to be used in other constraints演示如何为要在其他约束中使用的变量总和定义决策变量

define a free variable定义一个自由变量

sum_var = lp.LpVariable.dicts("sum_of_variables", [(s,a) for s in site_classes for a in age_classes] , cat='Continuous')

now add a definition to your free variable.现在为您的自由变量添加一个定义。

for s in site_classes:
    for a in age_classes:
        prob += lp.lpSum(x_vars[(s,a,p)] for p in period_classes) == sum_var[(s,a)] 

Now you can use your free variable in a constraint现在您可以在约束中使用自由变量

for s in site_classes:
    for a in age_classes:
        prob += sum_var[(s,a)] <= age_areas[a][s]

This approach can improve the readability of you linear program and makes it easier to add complexity to the model这种方法可以提高线性程序的可读性,并可以更轻松地向模型添加复杂性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM