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
Post a Comment