python - Binary integer programming with PULP using vector syntax for variables? -


new python library pulp , i'm finding documentation unhelpful, not include examples using lists of variables. i've tried create absolutely minimalist example below illustrate 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] in identifiers ]  ), " objective sum of prices of selected items " prob += pulp.lpsum( [ x[i] in identifiers ] )==2, " constraint choose 2 items " prob.solve() ident in identifiers:     if x[ident]==1:         print ident + " in basket " 

the output is:

a in basket  b in basket  c in basket  d in basket  e in basket 

the optimizer not recognizing constraint add 2 values.

i'll leave here in case else silly, above example works fine. had merely failed examine results correctly. instead:

def printprob( prob ):     v in prob.variables():        print v.name, "=", v.varvalue     print "status:", pulp.lpstatus[ prob.status ] 

reveals solution correct.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -