简体   繁体   中英

Python Pulp absolute constraints

Suppose I have a set of decision variables in my PuLP definition:

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?

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). I can see that numpy has numpy.linalg.norm but I cannot see how this can recursively create my set of constraints in PuLP. I'm struggling to create the correct syntax using lpSum .

right for each variable

X1 make two new non negative variables Y1 and Z1 >=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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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