SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
The
                    Optimisation
 University
of Auckland




                     Dr. Stuart Mitchell
              Department of Engineering Science
                    University of Auckland
                        New Zealand
                   s.mitchell@auckland.ac.nz
                December 2008 meeting of NZPUG
Contents of presentation


What is Mathematical Programing
     The Whiskas Problem
             PuLP
    Further PuLP examples
What is Mathematical
            programing
• A simple mathematically precise way of
  stating optimisation problems.
• Use mathematically rigorous ways to find a
  solution
• Examples of Optimisation Problems that
  can be solved with MP:
  – Shortest Path Problem
  – Scheduling Problems (Set partitioning)
  – Knapsack problems
  – Blending Problems
The Whiskas blending problem
• Taken from
  http://130.216.209.237/engsci392/pulp/ABlendingProblem

• Whiskas cat food want to produce their cat
  food products as cheaply as possible while
  ensuring they meet the stated nutritional
  analysis requirements shown on the cans.
• Thus they want to vary the quantities of
  each ingredient used (the main ingredients
  being chicken, beef, mutton, rice, wheat
  and gel) while still meeting their nutritional
  standards.
The Whiskas blending problem



  $/kg
  $13

   $8

  $10

   $2

   $5
   $1
Whiskas blending problem
• We wish to identify decision variables
• Assume we only have chicken and beef
• Let
  – xc = the percentage of chicken meat
  – xb = the percentage of beef used
• Then we wish to
  minimise $13xc + $8xb
Whiskas Blending Problem
• What about the nutritional requirements
• Subject to
  xc+ xb= 100
  0.100xc + 0.200xb >= 8.0
  0.080xc + 0.100xb >= 6.0
  0.001xc + 0.005xb <= 2.0
  0.002xc + 0.005xb <= 0.4
• xc>=0, xb>=0
MP Model
Let : I ∈{c , b , m , w , g}the set of ingredients
x i be the percentage of ingredient i in the cat food i∈ I
C i be the cost of ingredient i i ∈I
Pi be the protien content of ingredient i i ∈I
F i be the fat content of ingredient i i ∈I
Fbi be the fibre content of ingredient i i ∈ I
Si be the salt content of ingredient i i ∈ I

                     min ∑ i∈ I C i x i
                     s.t.
                     ∑i∈ I x i=100
                     ∑ i∈ I F i x i≥8
                     ∑i∈ I Fb i xi ≤2
                     ∑i∈ I Si xi≤0.4
                       x i≥0 ∀ i ∈ I
Ok where is the python
• On google code you can find pulp-or
  http://code.google.com/p/pulp-or/
• This is a python module that allows the
  easy statement and solution of linear
  programing problems.
• Pulp leverages features of python and the
  open source optimisation libraries Coin-or
Whiskas model in python
quot;quot;quot;
The Full Whiskas Model Python Formulation for the PuLP Modeller
Authors: Antony Phillips, Dr Stuart Mitchell 2007
quot;quot;quot;
# Import PuLP modeler functions
from pulp import *
# Creates a list of the Ingredients
Ingredients = ['CHICKEN', 'BEEF', 'MUTTON', 'RICE', 'WHEAT', 'GEL']
# A dictionary of the costs of each of the Ingredients is created
costs = {'CHICKEN': 0.013,
      'BEEF': 0.008,
      'MUTTON': 0.010,
      'RICE': 0.002,
      'WHEAT': 0.005,
      'GEL': 0.001}
# A dictionary of the protein percent in each of the Ingredients is created
proteinPercent = {'CHICKEN': 0.100,
              'BEEF': 0.200,
              'MUTTON': 0.150,
              'RICE': 0.000,
              'WHEAT': 0.040,
              'GEL': 0.000}
# A dictionary of the fat percent in each of the Ingredients is created
fatPercent = {'CHICKEN': 0.080,
          'BEEF': 0.100,
          'MUTTON': 0.110,
          'RICE': 0.010,
          'WHEAT': 0.010,
          'GEL': 0.000}
# A dictionary of the fibre percent in each of the Ingredients is created
fibrePercent = {'CHICKEN': 0.001,
            'BEEF': 0.005,
            'MUTTON': 0.003,
            'RICE': 0.100,
            'WHEAT': 0.150,
# Create the 'prob' variable to contain the problem data
prob = LpProblem(quot;The Whiskas Problemquot;, LpMinimize)

# A dictionary called 'Vars' is created to contain the referenced Variables
vars = LpVariable.dicts(quot;Ingrquot;,Ingredients,0)

# The objective function is added to 'prob' first
prob += lpSum([costs[i]*vars[i] for i in Ingredients]), quot;Total Cost of Ingredients per canquot;

# The five constraints are added to 'prob'
prob += lpSum([vars[i] for i in Ingredients]) == 100, quot;PercentagesSumquot;
prob += lpSum([proteinPercent[i] * vars[i] for i in Ingredients]) >= 8.0, quot;ProteinRequirementquot;
prob += lpSum([fatPercent[i] * vars[i] for i in Ingredients]) >= 6.0, quot;FatRequirementquot;
prob += lpSum([fibrePercent[i] * vars[i] for i in Ingredients]) <= 2.0, quot;FibreRequirementquot;
prob += lpSum([saltPercent[i] * vars[i] for i in Ingredients]) <= 0.4, quot;SaltRequirementquot;

# The problem data is written to an .lp file
prob.writeLP(quot;WhiskasModel2.lpquot;)

# The problem is solved using PuLP's choice of Solver
prob.solve()

# The status of the solution is printed to the screen
print quot;Status:quot;, LpStatus[prob.status]

# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
   print v.name, quot;=quot;, v.varValue

# The optimised objective function value is printed to the screen
print quot;Total Cost of Ingredients per can = quot;, value(prob.objective)
The open source future for OR
• Presently the Computational OR tools
  used, taught within this department, are
  closed source.
  – Excel /Storm
  – AMPL, GAMS
  – CPLEX, EXPRESS, ZIP
• Students can not afford commercial
  licences of this software
• Students cannot see how this software
  works.
The open source future for OR

• Outcomes for students
  – Ability to access free (no cost) software to
    implement their own solutions once they
    graduate
  – Ability to access free (open) source code to
    see how the algorithms are implemented.
    • Imagine the difference to 391??
  – The ability to improve the software they use.
PuLP
• PuLP is a python module that allows the
  easy expression of Mathematical Programs
• PuLP is built to interface with separate
  solvers
• PuLP is similar in style to:
  – AMPL
  – GAMS
  – OPL
  – LINGO
  – FLOPC++ etc.
PuLP
• Why Python?
  – Core Python syntax leads to the concise
    statement of MP's
  – Python is a scripting language so no
    compilation is needed and the code is platform
    independent
  – Python interfaces easily with external solvers
    that do the heavy lifting
  – Python comes with 'batteries included'
    • The Python standard library is huge
PuLP
• Written initially by J. S. Roy
• Now maintained by S. A. Mitchell
• It is available at
  http://pulp-or.google-code.com
• Now available for Windows and Linux
Generating a Yield Frontier

 Total Volume
 of Pulp logs


40m3




           Lines joining
           extreme points
           represent the
           Yield Frontier


                                 Total Volume
                            20m3 of Saw logs
Generating a Yield Frontier

• Using pulp we formulate the bucking
  problem (with a single objective) as a set
  packing problem by log section.
lp = LpProblem(quot;Bucking Modelquot;, LpMaximize)
#set up the logvolume variables
logvol=LpVariable.dicts(quot;LogVolume(quot;%squot;)quot;,logtypes,0)
#objective
lp+=lpSum([l.price * logvol[l] for l in logtypes]), quot;Revenue“
#setup the arc variables
x=LpVariable.dict(quot;x(%s)quot;,f_logs,0,1,LpInteger)
#set up a section set partitioning problem
count = 0
for s in stems:
       slogs = fs_logs[s]
       for i,sec in enumerate(s.sections):
          lp +=( lpSum((x[log] for log in slogs
                    if log.startl <= sec.start
                    if log.endl > sec.start)) <= 1
                    , quot;Stem_Section(quot;%squot;,%i)quot; % (str(s),i))
          count += 1
#add the constraints that link the log volumes
for lt in logtypes:
       lp +=( lpSum((log.volume*x[log]
                for log in fl_logs[lt])) - logvol[lt] == 0
                , quot;Logtype_volume(quot;%squot;)quot; % str(lt))
Generating a Yield Frontier Using Pulp

• We then iteratively solve the problem to find all
  extreme supported solutions on the Yield
  Frontier
• Equivalent to projecting the problem into the log
  volume space
• I added a module to PuLP that implements
  projection using Iterative Hull Methods (Lassez,
  Lassez 1992)
>>> pprob, ppoints = polytope.project(lp, totalvars)
Find Yield Frontier for the Dataset

                 (176, 649)
      (0, 650)      (235, 611)




                                 (737,145)



                                       (823, 35)
     (0, 0)
                                         (838, 0)
Find Yield Frontier for a Single Stem

* Total projected *
Minimize
OBJ: __dummy
Subject To
_C1: DomSaw + 1.11154598826 ex <= 2669.27592955
_C2: DomSaw + 1.34653465347 ex <= 3118.57425743
_C3: 1.00863930886 DomSaw + ex <= 2522.60691145
Bounds
__dummy = 0
End
Travelling tournament problem with PuLP

• This problem models the allocation of
  teams to Home and Away games in a
  tournament
• A full problem description and datasets are
  found at Michael Trick's page
• http://mat.tepper.cmu.edu/TOURN/
Travelling tournament problem with PuLP

• At IFORS 2008 M. Trick presented an
  approach to finding lower bounds to this
  problem using combinatorial benders cuts
• That evening I implemented his algorithm
  using PuLP
• Along the way I also added Powerset,
  Combination and Permutation operators to
  PuLP
•   lp = LpProblem(quot;Travelling tournement Masterquot;, LpMinimize)
    #create variables
    triplist = [Trip(t1,p) for t1 in teams
                  for p in
                  allpermutations([t for t in
                  teams if t !=t1] ,k)
                  if p[0] <= p[-1]]
    tripvars = LpVariable.dict(quot;mastervar quot;,triplist,0,1,LpInteger)
    #objective
    lp += lpSum([t.cost()*tripvars[t]
                       for t in triplist])
    #construct constraints to ensure that all teams visit each other
        for t1 in teams:
           for t2 in teams:
              if t1 != t2:
                  lp += lpSum([tripvars[t] for t in triplist
                                     if t.team == t1
                                     if t2 in a.awayteams]) == 1, 
                                    quot;Team_%s_Visits_%squot;%(t1,t2)
Further examples
• The 392 course has been converted from
  AMPL to PuLP
 http://130.216.209.237/engsci392/pulp/OptimisationWithPuLP
• There you can see a number of different
  ways to construct problems
• Note that new language features can be
  added very easily only needing approval
  from the BDFL

Más contenido relacionado

La actualidad más candente

Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Dr. Volkan OBAN
 
Brief Summary Of C++
Brief Summary Of C++Brief Summary Of C++
Brief Summary Of C++Haris Lye
 
Sleep Period Optimization Model For Layered Video Service Delivery Over eMBMS...
Sleep Period Optimization Model For Layered Video Service Delivery Over eMBMS...Sleep Period Optimization Model For Layered Video Service Delivery Over eMBMS...
Sleep Period Optimization Model For Layered Video Service Delivery Over eMBMS...Andrea Tassi
 
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYAMaulik Borsaniya
 
Scoped dynamic rewrite rules
Scoped dynamic rewrite rulesScoped dynamic rewrite rules
Scoped dynamic rewrite rulesEelco Visser
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Groovy Fly Through
Groovy Fly ThroughGroovy Fly Through
Groovy Fly Throughniklal
 
PyTorch 튜토리얼 (Touch to PyTorch)
PyTorch 튜토리얼 (Touch to PyTorch)PyTorch 튜토리얼 (Touch to PyTorch)
PyTorch 튜토리얼 (Touch to PyTorch)Hansol Kang
 
Module Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RR
Module Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RRModule Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RR
Module Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RRSasmitoh Rahmad Riady
 
Let's make a contract: The art of designing a Java API | DevNation Tech Talk
Let's make a contract: The art of designing a Java API | DevNation Tech TalkLet's make a contract: The art of designing a Java API | DevNation Tech Talk
Let's make a contract: The art of designing a Java API | DevNation Tech TalkRed Hat Developers
 
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7Ono Shigeru
 
Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)c.titus.brown
 
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 6
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 6Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 6
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 6Ono Shigeru
 
Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnnTaiga Nomi
 
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...Ruby Meditation
 

La actualidad más candente (20)

Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
 
Brief Summary Of C++
Brief Summary Of C++Brief Summary Of C++
Brief Summary Of C++
 
Sleep Period Optimization Model For Layered Video Service Delivery Over eMBMS...
Sleep Period Optimization Model For Layered Video Service Delivery Over eMBMS...Sleep Period Optimization Model For Layered Video Service Delivery Over eMBMS...
Sleep Period Optimization Model For Layered Video Service Delivery Over eMBMS...
 
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
 
Scoped dynamic rewrite rules
Scoped dynamic rewrite rulesScoped dynamic rewrite rules
Scoped dynamic rewrite rules
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Groovy intro for OUDL
Groovy intro for OUDLGroovy intro for OUDL
Groovy intro for OUDL
 
Groovy Fly Through
Groovy Fly ThroughGroovy Fly Through
Groovy Fly Through
 
Python GC
Python GCPython GC
Python GC
 
PyTorch 튜토리얼 (Touch to PyTorch)
PyTorch 튜토리얼 (Touch to PyTorch)PyTorch 튜토리얼 (Touch to PyTorch)
PyTorch 튜토리얼 (Touch to PyTorch)
 
4. functions
4. functions4. functions
4. functions
 
Scala to assembly
Scala to assemblyScala to assembly
Scala to assembly
 
Module Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RR
Module Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RRModule Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RR
Module Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RR
 
Let's make a contract: The art of designing a Java API | DevNation Tech Talk
Let's make a contract: The art of designing a Java API | DevNation Tech TalkLet's make a contract: The art of designing a Java API | DevNation Tech Talk
Let's make a contract: The art of designing a Java API | DevNation Tech Talk
 
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7
 
Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)
 
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 6
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 6Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 6
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 6
 
Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnn
 
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
 

Similar a Stuart Mitchell - Pulp Optimisation

Making the most of 2.2
Making the most of 2.2Making the most of 2.2
Making the most of 2.2markstory
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010singingfish
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computingGo Asgard
 
Pythonlearn-03-Conditional.pptx
Pythonlearn-03-Conditional.pptxPythonlearn-03-Conditional.pptx
Pythonlearn-03-Conditional.pptxVigneshChaturvedi1
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School ProgrammersSiva Arunachalam
 
Adventures in infrastructure as code
Adventures in infrastructure as codeAdventures in infrastructure as code
Adventures in infrastructure as codeJulian Simpson
 
What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?Christophe Porteneuve
 
Getting more out of Matplotlib with GR
Getting more out of Matplotlib with GRGetting more out of Matplotlib with GR
Getting more out of Matplotlib with GRJosef Heinen
 
Exact Real Arithmetic for Tcl
Exact Real Arithmetic for TclExact Real Arithmetic for Tcl
Exact Real Arithmetic for Tclke9tv
 
Multiprocessing with python
Multiprocessing with pythonMultiprocessing with python
Multiprocessing with pythonPatrick Vergain
 
Översättning av django-program
Översättning av django-programÖversättning av django-program
Översättning av django-programmikaelmoutakis
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Engine Yard
 
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Find it. Fix it. Real-World SQL Tuning Cases with Karen MortonFind it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Find it. Fix it. Real-World SQL Tuning Cases with Karen MortonEmbarcadero Technologies
 
Procedures, the Pop-11 stack and debugging
Procedures, the Pop-11 stack and debuggingProcedures, the Pop-11 stack and debugging
Procedures, the Pop-11 stack and debuggingRich Price
 
Call Execute For Everyone
Call Execute For EveryoneCall Execute For Everyone
Call Execute For EveryoneDaniel Boisvert
 
Perl Teach-In (part 1)
Perl Teach-In (part 1)Perl Teach-In (part 1)
Perl Teach-In (part 1)Dave Cross
 

Similar a Stuart Mitchell - Pulp Optimisation (20)

Making the most of 2.2
Making the most of 2.2Making the most of 2.2
Making the most of 2.2
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
 
Revoke-Obfuscation
Revoke-ObfuscationRevoke-Obfuscation
Revoke-Obfuscation
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
 
Pythonlearn-03-Conditional.pptx
Pythonlearn-03-Conditional.pptxPythonlearn-03-Conditional.pptx
Pythonlearn-03-Conditional.pptx
 
Cucumber
CucumberCucumber
Cucumber
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
Adventures in infrastructure as code
Adventures in infrastructure as codeAdventures in infrastructure as code
Adventures in infrastructure as code
 
What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?
 
Getting more out of Matplotlib with GR
Getting more out of Matplotlib with GRGetting more out of Matplotlib with GR
Getting more out of Matplotlib with GR
 
Bioinformatica p4-io
Bioinformatica p4-ioBioinformatica p4-io
Bioinformatica p4-io
 
Exact Real Arithmetic for Tcl
Exact Real Arithmetic for TclExact Real Arithmetic for Tcl
Exact Real Arithmetic for Tcl
 
Multiprocessing with python
Multiprocessing with pythonMultiprocessing with python
Multiprocessing with python
 
Översättning av django-program
Översättning av django-programÖversättning av django-program
Översättning av django-program
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
 
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Find it. Fix it. Real-World SQL Tuning Cases with Karen MortonFind it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
 
Procedures, the Pop-11 stack and debugging
Procedures, the Pop-11 stack and debuggingProcedures, the Pop-11 stack and debugging
Procedures, the Pop-11 stack and debugging
 
Call Execute For Everyone
Call Execute For EveryoneCall Execute For Everyone
Call Execute For Everyone
 
Perl Teach-In (part 1)
Perl Teach-In (part 1)Perl Teach-In (part 1)
Perl Teach-In (part 1)
 

Último

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 

Último (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

Stuart Mitchell - Pulp Optimisation

  • 1. The Optimisation University of Auckland Dr. Stuart Mitchell Department of Engineering Science University of Auckland New Zealand s.mitchell@auckland.ac.nz December 2008 meeting of NZPUG
  • 2. Contents of presentation What is Mathematical Programing The Whiskas Problem PuLP Further PuLP examples
  • 3. What is Mathematical programing • A simple mathematically precise way of stating optimisation problems. • Use mathematically rigorous ways to find a solution • Examples of Optimisation Problems that can be solved with MP: – Shortest Path Problem – Scheduling Problems (Set partitioning) – Knapsack problems – Blending Problems
  • 4. The Whiskas blending problem • Taken from http://130.216.209.237/engsci392/pulp/ABlendingProblem • Whiskas cat food want to produce their cat food products as cheaply as possible while ensuring they meet the stated nutritional analysis requirements shown on the cans. • Thus they want to vary the quantities of each ingredient used (the main ingredients being chicken, beef, mutton, rice, wheat and gel) while still meeting their nutritional standards.
  • 5. The Whiskas blending problem $/kg $13 $8 $10 $2 $5 $1
  • 6. Whiskas blending problem • We wish to identify decision variables • Assume we only have chicken and beef • Let – xc = the percentage of chicken meat – xb = the percentage of beef used • Then we wish to minimise $13xc + $8xb
  • 7. Whiskas Blending Problem • What about the nutritional requirements • Subject to xc+ xb= 100 0.100xc + 0.200xb >= 8.0 0.080xc + 0.100xb >= 6.0 0.001xc + 0.005xb <= 2.0 0.002xc + 0.005xb <= 0.4 • xc>=0, xb>=0
  • 8. MP Model Let : I ∈{c , b , m , w , g}the set of ingredients x i be the percentage of ingredient i in the cat food i∈ I C i be the cost of ingredient i i ∈I Pi be the protien content of ingredient i i ∈I F i be the fat content of ingredient i i ∈I Fbi be the fibre content of ingredient i i ∈ I Si be the salt content of ingredient i i ∈ I min ∑ i∈ I C i x i s.t. ∑i∈ I x i=100 ∑ i∈ I F i x i≥8 ∑i∈ I Fb i xi ≤2 ∑i∈ I Si xi≤0.4 x i≥0 ∀ i ∈ I
  • 9. Ok where is the python • On google code you can find pulp-or http://code.google.com/p/pulp-or/ • This is a python module that allows the easy statement and solution of linear programing problems. • Pulp leverages features of python and the open source optimisation libraries Coin-or
  • 10. Whiskas model in python quot;quot;quot; The Full Whiskas Model Python Formulation for the PuLP Modeller Authors: Antony Phillips, Dr Stuart Mitchell 2007 quot;quot;quot; # Import PuLP modeler functions from pulp import * # Creates a list of the Ingredients Ingredients = ['CHICKEN', 'BEEF', 'MUTTON', 'RICE', 'WHEAT', 'GEL'] # A dictionary of the costs of each of the Ingredients is created costs = {'CHICKEN': 0.013, 'BEEF': 0.008, 'MUTTON': 0.010, 'RICE': 0.002, 'WHEAT': 0.005, 'GEL': 0.001} # A dictionary of the protein percent in each of the Ingredients is created proteinPercent = {'CHICKEN': 0.100, 'BEEF': 0.200, 'MUTTON': 0.150, 'RICE': 0.000, 'WHEAT': 0.040, 'GEL': 0.000} # A dictionary of the fat percent in each of the Ingredients is created fatPercent = {'CHICKEN': 0.080, 'BEEF': 0.100, 'MUTTON': 0.110, 'RICE': 0.010, 'WHEAT': 0.010, 'GEL': 0.000} # A dictionary of the fibre percent in each of the Ingredients is created fibrePercent = {'CHICKEN': 0.001, 'BEEF': 0.005, 'MUTTON': 0.003, 'RICE': 0.100, 'WHEAT': 0.150,
  • 11. # Create the 'prob' variable to contain the problem data prob = LpProblem(quot;The Whiskas Problemquot;, LpMinimize) # A dictionary called 'Vars' is created to contain the referenced Variables vars = LpVariable.dicts(quot;Ingrquot;,Ingredients,0) # The objective function is added to 'prob' first prob += lpSum([costs[i]*vars[i] for i in Ingredients]), quot;Total Cost of Ingredients per canquot; # The five constraints are added to 'prob' prob += lpSum([vars[i] for i in Ingredients]) == 100, quot;PercentagesSumquot; prob += lpSum([proteinPercent[i] * vars[i] for i in Ingredients]) >= 8.0, quot;ProteinRequirementquot; prob += lpSum([fatPercent[i] * vars[i] for i in Ingredients]) >= 6.0, quot;FatRequirementquot; prob += lpSum([fibrePercent[i] * vars[i] for i in Ingredients]) <= 2.0, quot;FibreRequirementquot; prob += lpSum([saltPercent[i] * vars[i] for i in Ingredients]) <= 0.4, quot;SaltRequirementquot; # The problem data is written to an .lp file prob.writeLP(quot;WhiskasModel2.lpquot;) # The problem is solved using PuLP's choice of Solver prob.solve() # The status of the solution is printed to the screen print quot;Status:quot;, LpStatus[prob.status] # Each of the variables is printed with it's resolved optimum value for v in prob.variables(): print v.name, quot;=quot;, v.varValue # The optimised objective function value is printed to the screen print quot;Total Cost of Ingredients per can = quot;, value(prob.objective)
  • 12. The open source future for OR • Presently the Computational OR tools used, taught within this department, are closed source. – Excel /Storm – AMPL, GAMS – CPLEX, EXPRESS, ZIP • Students can not afford commercial licences of this software • Students cannot see how this software works.
  • 13. The open source future for OR • Outcomes for students – Ability to access free (no cost) software to implement their own solutions once they graduate – Ability to access free (open) source code to see how the algorithms are implemented. • Imagine the difference to 391?? – The ability to improve the software they use.
  • 14. PuLP • PuLP is a python module that allows the easy expression of Mathematical Programs • PuLP is built to interface with separate solvers • PuLP is similar in style to: – AMPL – GAMS – OPL – LINGO – FLOPC++ etc.
  • 15. PuLP • Why Python? – Core Python syntax leads to the concise statement of MP's – Python is a scripting language so no compilation is needed and the code is platform independent – Python interfaces easily with external solvers that do the heavy lifting – Python comes with 'batteries included' • The Python standard library is huge
  • 16. PuLP • Written initially by J. S. Roy • Now maintained by S. A. Mitchell • It is available at http://pulp-or.google-code.com • Now available for Windows and Linux
  • 17. Generating a Yield Frontier Total Volume of Pulp logs 40m3 Lines joining extreme points represent the Yield Frontier Total Volume 20m3 of Saw logs
  • 18. Generating a Yield Frontier • Using pulp we formulate the bucking problem (with a single objective) as a set packing problem by log section.
  • 19. lp = LpProblem(quot;Bucking Modelquot;, LpMaximize) #set up the logvolume variables logvol=LpVariable.dicts(quot;LogVolume(quot;%squot;)quot;,logtypes,0) #objective lp+=lpSum([l.price * logvol[l] for l in logtypes]), quot;Revenue“ #setup the arc variables x=LpVariable.dict(quot;x(%s)quot;,f_logs,0,1,LpInteger) #set up a section set partitioning problem count = 0 for s in stems: slogs = fs_logs[s] for i,sec in enumerate(s.sections): lp +=( lpSum((x[log] for log in slogs if log.startl <= sec.start if log.endl > sec.start)) <= 1 , quot;Stem_Section(quot;%squot;,%i)quot; % (str(s),i)) count += 1 #add the constraints that link the log volumes for lt in logtypes: lp +=( lpSum((log.volume*x[log] for log in fl_logs[lt])) - logvol[lt] == 0 , quot;Logtype_volume(quot;%squot;)quot; % str(lt))
  • 20. Generating a Yield Frontier Using Pulp • We then iteratively solve the problem to find all extreme supported solutions on the Yield Frontier • Equivalent to projecting the problem into the log volume space • I added a module to PuLP that implements projection using Iterative Hull Methods (Lassez, Lassez 1992) >>> pprob, ppoints = polytope.project(lp, totalvars)
  • 21. Find Yield Frontier for the Dataset (176, 649) (0, 650) (235, 611) (737,145) (823, 35) (0, 0) (838, 0)
  • 22. Find Yield Frontier for a Single Stem * Total projected * Minimize OBJ: __dummy Subject To _C1: DomSaw + 1.11154598826 ex <= 2669.27592955 _C2: DomSaw + 1.34653465347 ex <= 3118.57425743 _C3: 1.00863930886 DomSaw + ex <= 2522.60691145 Bounds __dummy = 0 End
  • 23. Travelling tournament problem with PuLP • This problem models the allocation of teams to Home and Away games in a tournament • A full problem description and datasets are found at Michael Trick's page • http://mat.tepper.cmu.edu/TOURN/
  • 24. Travelling tournament problem with PuLP • At IFORS 2008 M. Trick presented an approach to finding lower bounds to this problem using combinatorial benders cuts • That evening I implemented his algorithm using PuLP • Along the way I also added Powerset, Combination and Permutation operators to PuLP
  • 25. lp = LpProblem(quot;Travelling tournement Masterquot;, LpMinimize) #create variables triplist = [Trip(t1,p) for t1 in teams for p in allpermutations([t for t in teams if t !=t1] ,k) if p[0] <= p[-1]] tripvars = LpVariable.dict(quot;mastervar quot;,triplist,0,1,LpInteger) #objective lp += lpSum([t.cost()*tripvars[t] for t in triplist]) #construct constraints to ensure that all teams visit each other for t1 in teams: for t2 in teams: if t1 != t2: lp += lpSum([tripvars[t] for t in triplist if t.team == t1 if t2 in a.awayteams]) == 1, quot;Team_%s_Visits_%squot;%(t1,t2)
  • 26. Further examples • The 392 course has been converted from AMPL to PuLP http://130.216.209.237/engsci392/pulp/OptimisationWithPuLP • There you can see a number of different ways to construct problems • Note that new language features can be added very easily only needing approval from the BDFL