version 1.5.1
merged iron python changes fixed dippy (hopefully)
This commit is contained in:
82
examples/BeerDistributionProblem_resolve.py
Normal file
82
examples/BeerDistributionProblem_resolve.py
Normal file
@@ -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)
|
||||
@@ -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) ):
|
||||
|
||||
Reference in New Issue
Block a user