简体   繁体   English

Python Pulp绝对约束

[英]Python Pulp absolute constraints

Suppose I have a set of decision variables in my PuLP definition: 假设我的PuLP定义中有一组决策变量:

vals = [-v for k, v in (DecisionVars.items())]

And I want to create a constraint related to the absolute value of the sum of all constraints. 我想创建一个与所有约束之和的绝对值有关的约束。 So something like: 所以像这样:

for i in range(len(DecisionVars)):
   prob += lpSum(abs(vals[:i+1])) <= some_limit, "Absolute constraint"

But I cant seem to apply the abs() operator to my constraints? 但是我似乎无法将abs()运算符应用于约束吗?

UPDATE UPDATE

Ok, if I make use of the information in this post sum of absolute values constraint in semi definite programming then my question can be formulated differently. 好的,如果我在半定性编程中利用绝对值总和约束中的信息,那么我的问题可以用不同的方式表述。 I am now trying to evaluate: 我现在正在尝试评估:

abs(x1) + abs(x2) + abs(x3) <= some_limit

As pointed out in the link above, the answer might be to create a 1-norm of the vector x (where x is the vector of decision variables as above). 正如上面的链接所指出的,答案可能是创建向量x的1范数(其中x是上述决策变量的向量)。 I can see that numpy has numpy.linalg.norm but I cannot see how this can recursively create my set of constraints in PuLP. 我可以看到numpy具有numpy.linalg.norm但是我看不到它如何在PuLP中递归地创建我的约束集。 I'm struggling to create the correct syntax using lpSum . 我正在努力使用lpSum创建正确的语法。

right for each variable 正确的每个变量

X1 make two new non negative variables Y1 and Z1 >=0 X1使两个新的非负变量Y1Z1 >=0

then set a constraint 然后设置一个约束

X1 == Y1 - Z1

Then your abs constraint becomes 然后你的腹肌约束变成

Y1 + Z1 +.... <= 10

You will need to have another variable and two sets of constraints for each of your absolute variables. 您将需要有另一个变量和每个绝对变量的两组约束。

m += xn <= tn
m += -xn <= tn

then the sum of tn is the sum of the absolute value of xn. 那么tn的和就是xn的绝对值的和。

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

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