GoBlue_MathMan
GoBlue_MathMan

Reputation: 1148

Conditional Constraint Linear Program in Python's Pulp Module

Is there a way to add a constraint within pulp in python that would count all the variables above 5 and require that count to be greater than lets say 10? I currently have the following code but it only calls the function once;

def min_qty_constraint(vars):
    count = 0
    for v in vars:
        if v.varValue > 5 :
            count += 1
    print count
    return -count

prob = pulp.LpProblem('problem',pulp.LpMaximize)
prob +=  min_qty_constraint(vars) <= 10

so for example if i had vars = [x1,x2,..,x20] where xi is a pulp.Lpvariable and x1,..,x10 = 6 then then 10 of these variables are greater than 5 thus fulfilling the contraitns that at least 10 variables have values greater than 5.

Upvotes: 1

Views: 1728

Answers (1)

Erwin Kalvelagen
Erwin Kalvelagen

Reputation: 16724

This is not linear. There is a way doing this with additional binary variables y(i):

x(i) >= 5 y(i)
sum(i, y(i)) >= 10

(Note: I assumed x(i) are non-negative variables).

Upvotes: 3

Related Questions