From d182a03ad090edeffcf6ca2c92900719535e5752 Mon Sep 17 00:00:00 2001 From: Stuart Mitchell Date: Mon, 27 Feb 2012 09:37:20 +1300 Subject: [PATCH] version 1.5.1 merged iron python changes fixed dippy (hopefully) --- VERSION | 2 +- examples/BeerDistributionProblem_resolve.py | 82 +++++++++++++++++++++ src/pulp/pulp.py | 14 +--- 3 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 examples/BeerDistributionProblem_resolve.py diff --git a/VERSION b/VERSION index bc80560..26ca594 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.5.0 +1.5.1 diff --git a/examples/BeerDistributionProblem_resolve.py b/examples/BeerDistributionProblem_resolve.py new file mode 100644 index 0000000..7561133 --- /dev/null +++ b/examples/BeerDistributionProblem_resolve.py @@ -0,0 +1,82 @@ +""" +The Beer Distribution Problem for the PuLP Modeller +Illustrates changing a constraint and solving + +Authors: Antony Phillips, Dr Stuart Mitchell 2007 +""" + +# Import PuLP modeler functions +from pulp import * + +# Creates a list of all the supply nodes +Warehouses = ["A", "B"] + +# Creates a dictionary for the number of units of supply for each supply node +supply = {"A": 1000, + "B": 4000} + +# Creates a list of all demand nodes +Bars = ["1", "2", "3", "4", "5"] + +# Creates a dictionary for the number of units of demand for each demand node +demand = {"1":500, + "2":900, + "3":1800, + "4":200, + "5":700,} + +# Creates a list of costs of each transportation path +costs = [ #Bars + #1 2 3 4 5 + [2,4,5,2,1],#A Warehouses + [3,1,3,2,3] #B + ] + +# The cost data is made into a dictionary +costs = makeDict([Warehouses,Bars],costs,0) + +# Creates the 'prob' variable to contain the problem data +prob = LpProblem("Beer Distribution Problem",LpMinimize) + +# Creates a list of tuples containing all the possible routes for transport +Routes = [(w,b) for w in Warehouses for b in Bars] + +# A dictionary called 'Vars' is created to contain the referenced variables(the routes) +vars = LpVariable.dicts("Route",(Warehouses,Bars),0,None,LpInteger) + +# The objective function is added to 'prob' first +prob += lpSum([vars[w][b]*costs[w][b] for (w,b) in Routes]), "Sum_of_Transporting_Costs" + +# The supply maximum constraints are added to prob for each supply node (warehouse) +for w in Warehouses: + prob += lpSum([vars[w][b] for b in Bars])<=supply[w], "Sum_of_Products_out_of_Warehouse_%s"%w + +# The demand minimum constraints are added to prob for each demand node (bar) +# These constraints are stored for resolve later +bar_demand_constraint = {} +for b in Bars: + constraint = lpSum([vars[w][b] for w in Warehouses])>=demand[b] + prob += constraint, "Sum_of_Products_into_Bar_%s"%b + bar_demand_constraint[b] = constraint + +# The problem data is written to an .lp file +prob.writeLP("BeerDistributionProblem.lp") +for demand in range(500, 601, 10): + # reoptimise the problem by increasing demand at bar '1' + # note the constant is stored as the LHS constant not the RHS of the constraint + bar_demand_constraint['1'].constant = - demand + #or alternatively, + #prob.constraints["Sum_of_Products_into_Bar_1"].constant = - demand + + # The problem is solved using PuLP's choice of Solver + prob.solve() + + # The status of the solution is printed to the screen + print "Status:", LpStatus[prob.status] + + # Each of the variables is printed with it's resolved optimum value + for v in prob.variables(): + print v.name, "=", v.varValue + + # The optimised objective function value is printed to the screen + print "Total Cost of Transportation = ", value(prob.objective) diff --git a/src/pulp/pulp.py b/src/pulp/pulp.py index c2ddf55..9c06f0b 100755 --- a/src/pulp/pulp.py +++ b/src/pulp/pulp.py @@ -1,6 +1,6 @@ #! /usr/bin/env python # PuLP : Python LP Modeler -# Version 1.4.8 +# Version 1.5.1 # Copyright (c) 2002-2005, Jean-Sebastien Roy (js@jeannot.org) # Modifications Copyright (c) 2007- Stuart Anthony Mitchell (s.mitchell@auckland.ac.nz) @@ -152,14 +152,14 @@ class LpElement(object): """ #to remove illegal characters from the names trans = string.maketrans("-+[] ->/","________") - def setname(self,name): + def setName(self,name): if name: self.__name = str(name).translate(self.trans) else: self.__name = None - def getname(self): + def getName(self): return self.__name - name = property(fget = getname,fset = setname) + name = property(fget = getName,fset = setName) def __init__(self, name): self.name = name @@ -351,9 +351,6 @@ class LpVariable(LpElement): return d dict = classmethod(dict) - def getName(self): - return self.name - def getLb(self): return self.lowBound @@ -830,9 +827,6 @@ class LpConstraint(LpAffineExpression): self.sense = sense self.modified = True - def getName(self): - return self.name - def getLb(self): if ( (self.sense == LpConstraintGE) or (self.sense == LpConstraintEQ) ):