Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary integer programming with PULP using vector syntax for variables?

Tags:

python

pulp

New to the python library PULP and I'm finding the documentation somewhat unhelpful, as it does not include examples using lists of variables. I've tried to create an absolutely minimalist example below to illustrate my confusion.

import pulp
IDENTIFIERS = ['A','B','C','D','E']
PRICES      = dict( zip( IDENTIFIERS, [100.0, 99.0, 100.5, 101.5, 200.0 ] ) )
n           = len( IDENTIFIERS )

x     = pulp.LpVariable.dicts( "x", indexs = IDENTIFIERS, lowBound=0, upBound=1, cat='Integer', indexStart=[] )
prob  = pulp.LpProblem( "Minimalist example", pulp.LpMaximize )
prob += pulp.lpSum( [ x[i]*PRICES[i] for i in IDENTIFIERS ]  ), " Objective is sum of prices of selected items "
prob += pulp.lpSum( [ x[i] for i in IDENTIFIERS ] )==2, " Constraint is that we choose two items "
prob.solve()
for ident in IDENTIFIERS:
    if x[ident]==1:
        print ident + " is in the basket "

The output is:

A is in the basket 
B is in the basket 
C is in the basket 
D is in the basket 
E is in the basket

The optimizer is not recognizing the constraint that we only add two values.

like image 424
Peter Cotton Avatar asked Jul 14 '15 15:07

Peter Cotton


People also ask

What is binary integer programming?

Zero-one linear programming (or binary integer programming) involves problems in which the variables are restricted to be either 0 or 1. Any bounded integer variable can be expressed as a combination of binary variables.

What is PuLP programming?

PuLP is a python library which can be used to solve linear programming problems. Linear Programming is used to solve optimization problems and has uses in various industries such as Manufacturing, Transportation, Food Diets etc.

What are the three types of integer programming models?

Integer programming models are often classified as being either mixed-integer programming models, pure-integer programming models, or zero-one integer programming models .

What is LpProblem?

Bases: pulp.pulp.LpProblem. Contains the subproblem generated by converting a fixed constraint. into an elastic constraint.


1 Answers

I'll leave this here in case anyone else is just as silly, but actually the above example works fine. I had merely failed to examine the results correctly. Instead:

def printProb( prob ):
    for v in prob.variables():
       print v.name, "=", v.varValue
    print "Status:", pulp.LpStatus[ prob.status ]

reveals that the solution is correct.

like image 150
Peter Cotton Avatar answered Sep 27 '22 23:09

Peter Cotton