SlideShare una empresa de Scribd logo
1 de 48
Descargar para leer sin conexión
Python, numerical optimization,
genetic algorithms
Davide Rizzo
PyCon Italia Qu4ttro

daviderizzo.net
Python, numerical optimization, genetic algorithms   daviderizzo.net




Optimization
• Actually operations research
 ▫ Mathematical optimization is the tool
• Applied maths
 ▫ Major academic and industrial research topic
• Computationally oriented
 ▫ Many commercial solutions
 ▫ Yes, you can use Python
• Not code optimization!
Python, numerical optimization, genetic algorithms   daviderizzo.net




Operations research applications
• Airlines
  ▫ scheduling planes and crews, pricing tickets, taking
    reservations, and planning fleet size
• Logistics
  ▫ routing and planning
• Financial services
  ▫ credit scoring, marketing, and internal operations
• Deployment of emergency services
• Policy studies and regulation
  ▫ environmental pollution, air traffic safety, AIDS, and
    criminal justice policy
Python, numerical optimization, genetic algorithms   daviderizzo.net




More operations research applications
•   Project planning
•   Network optimization
•   Resources allocation
•   Supply chain management
•   Automation
•   Scheduling
•   Pricing
Python, numerical optimization, genetic algorithms   daviderizzo.net




Optimization problem
• Many decision variables
 ▫ How should I price my new products?
 ▫ How many server resources should I assign to
   each client?
• An objective
 ▫ Maximize net profit
 ▫ Minimize client waiting time
Python, numerical optimization, genetic algorithms   daviderizzo.net




Objective function
• Objective: find the best choice for the decision
  variables
  ▫ the values that give the maximum yield (or the
    minimum cost)
• Objective function: the yield (or the cost) I get
  for a given choice
Python, numerical optimization, genetic algorithms   daviderizzo.net




Decisions and objectives
• I buy 100kg flour at 50€
 ▫ Want to decide how to use it
• 70kg -> bread, 30kg -> cookies
 ▫ Decision variables
    X = 70, 30
• Sell 105€ of bread and 75€ of cookies
 ▫ Objective function value:
    f(X) = 105 + 75 – 50 = 130€
Python, numerical optimization, genetic algorithms   daviderizzo.net




Visualizing the problem
Python, numerical optimization, genetic algorithms   daviderizzo.net




Constraints
• Not every solution is good
• 100kg flour → can’t use 150kg for
  bread
  ▫ flour_bread + flour_cookies ≤ 100
Python, numerical optimization, genetic algorithms   daviderizzo.net




Constraints
• Inequalities often describe
  constraints
• Many constraints
  ▫ There are problems with
    thousands inequalities
• Limited sugar
  ▫ 1 part sugar, 3 parts flour
  ▫ 20 kg sugar available
  ▫ flour_cookies < 60
Python, numerical optimization, genetic algorithms   daviderizzo.net




Search vs. optimization
Search algorithms                  Optimization algorithms
• Well-known among                 • Broader class of problems
  programmers                        ▫ Includes search problems
• Include tree-search and graph-   • Continuous search space
  search algorithms                  ▫ Discrete as a special case
• Work on a discrete search        • Search algorithms used to
  space                              solve many optimization
                                     problems
Python, numerical optimization, genetic algorithms   daviderizzo.net




Finding the optimum in linear and non-linear problems with
exact methods
Python, numerical optimization, genetic algorithms   daviderizzo.net




Objective function classification
• General theory for every problem
 ▫ Very good results for simple problems
• Objective function structure
 ▫ Smoothness and regularity determine complexity
 ▫ Good guarantees for linear functions
 ▫ No exact results if irregular
• Factors other than objective functions
 ▫ Constraints
 ▫ Integrality on variables
Python, numerical optimization, genetic algorithms   daviderizzo.net


                                   Line (one variable)


  Linear                          Plane (two variables)




A greater number of variables does not complicate matters!
Python, numerical optimization, genetic algorithms   daviderizzo.net




Linear
• Linear objective function
 ▫ c0 + sum(c[i]*x[i] for i in range(len(vars)))




 ▫ Bakery example
   vars = ['flour_bread', 'flour_cookies']
   c = [1.5, 2.5]; c0 = -50
   >>> obj_fun([70, 30])
    130.0
Python, numerical optimization, genetic algorithms   daviderizzo.net




Non-linear “smooth”
Python, numerical optimization, genetic algorithms   daviderizzo.net




Non-linear “smooth”
• Non-linearity raises some issues
• Function must be “smooth” enough
 ▫ You can stand on any point of the curved surface
   and assume you are on a plane
• No guarantee to find global optimum
Python, numerical optimization, genetic algorithms   daviderizzo.net




Non regular function
Python, numerical optimization, genetic algorithms   daviderizzo.net




Non smooth function
Python, numerical optimization, genetic algorithms   daviderizzo.net




Gradient descent
                   • Pick a point
                   • Pretend you are on a line
                     ▫ Can choose the optimal
                        direction at each point
                   • Take steps until you fall in an
                     optimum
                     ▫ Local optimum!
Python, numerical optimization, genetic algorithms   daviderizzo.net




No smoothness?
• Any other formulation for the problem?
 ▫ Decrease model complexity
 ▫ Put complexity in constraints
• Settle for an approximate solution
• Global search heuristics
 ▫ Genetic algorithms are an example
Python, numerical optimization, genetic algorithms   daviderizzo.net




SciPy optimization package
• Non-linear numerical function optimization
 ▫ http://docs.scipy.org/doc/scipy/reference/optimize.html
• optimize.fmin(func, x0)
 ▫ Unconstrained optimization
 ▫ Finds the minimum of func(x) starting x with x0
 ▫ x can be a vector, func must return a float
• Better algorithm for many variables: fmin_bfgs
• Algorithms for constrained optimization
Python, numerical optimization, genetic algorithms   daviderizzo.net




Give me the code!
• from scipy import optimize

 def f(x):
     return x[0]**2 + (x[1]-2)**2

 print optimize.fmin(f, [0,0])

• Optimization terminated successfully.
           Current function value: 0.000000
           Iterations: 63
           Function evaluations: 120
  [ -4.04997873e-06   2.00004847e+00]
Python, numerical optimization, genetic algorithms   daviderizzo.net




Many variables
• from scipy import optimize
  from numpy import array
  from random import uniform
 n = 50
 center = array([uniform(0, 10)
                 for i in range(n)])
 def f(x):
     return sum((x-center)**2)
 optimum = optimize.fmin_bfgs(f, [0]*n)
 print optimum - center
Python, numerical optimization, genetic algorithms   daviderizzo.net




Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 3
         Function evaluations: 312
         Gradient evaluations: 6
[ 1.09850102e-08    1.00864010e-08 -3.44023343e-09            1.45686556e-08
   1.95572314e-09   1.63218594e-09   3.37632766e-09           1.44865293e-08
   1.52201984e-08   1.29321513e-08 -1.59485092e-09            1.23136292e-08
   1.62830638e-08 -3.52261820e-09 -5.56838653e-09             1.24844490e-08
   8.54035953e-09   1.55625752e-08   8.50658477e-09           1.08354117e-08
   1.62710236e-08 -3.44068374e-09 -5.63388847e-09             1.13670406e-09
   1.14938468e-08 -1.92982030e-09 -1.66206160e-09            -1.50014312e-09
   1.12226592e-08 -5.60273883e-09    3.07106607e-09           6.95412705e-09
  -4.58609706e-09   1.24199513e-08 -5.76112080e-09           -7.69927677e-10
  -6.56263044e-10 -6.08281736e-09    1.10073151e-08           3.35045769e-09
   1.25477273e-09   5.98582162e-09 -5.60075986e-10            1.00968194e-08
  -1.23273258e-09 -2.11921281e-09 -6.40983949e-09             8.99856101e-09
  -1.08943476e-09   1.55362354e-08]
Python, numerical optimization, genetic algorithms   daviderizzo.net




Microeconomics model example
• Pricing problem for two products
 ▫ Choose the prices that give the best profit
• Assume to know demand curves
 ▫ From buyer’s utility maximization
• Could be generalized
 ▫   Many products
 ▫   Complex demand curves
 ▫   Complex costs and economies of scale
 ▫   Beware of non smooth functions!
Python, numerical optimization, genetic algorithms   daviderizzo.net




Microeconomics model example
def profit(p1, p2):              from scipy import optimize
    x1 = demand1(p1, p2)
    x2 = demand2(p1, p2)         def objective(x):
    income = x1*p1 + x2*p2           return -profit(*x)
    cost = costf(x1, x2)
    return income - cost         price = optimize.fmin(objective,
                                                       [1, 1])
def demand1(p1, p2):
    return 20*(20 - p1)          print 'prices:', price
                                 print 'amounts:‘
def demand2(p1, p2):             print demand1(*price), demand2(*price)
    return 40*(40 - p2)

def costf(x1, x2):
    c1 = 10*(x1**0.5)
    c2 = 20*(x2**0.5) + 2*x2
    cbase = 1000 + 5*(x1 + x2)
    return c1 + c2 + cbase
Python, numerical optimization, genetic algorithms   daviderizzo.net




Microeconomics model results
Optimization terminated successfully.
         Current function value: -10381.085398
         Iterations: 63
         Function evaluations: 122
prices: [ 12.7070282   23.69579991]
amounts: 145.859435961 652.168003412
Python, numerical optimization, genetic algorithms   daviderizzo.net




Linear programming problem
• Linear objective function
  ▫ Weighted sum of variables
• Constraints are linear inequalities
  ▫ Weighted sum of some variables ≤ 0
• Variables can also be integer or binary
  ▫ Mixed integer programming
  ▫ NP-complete
Python, numerical optimization, genetic algorithms   daviderizzo.net




Back to the bakery
• max c1x1 + c2x2
• subject to
  ▫ x1 + x2 ≤ 100
  ▫ x2 ≤ 60
Python, numerical optimization, genetic algorithms   daviderizzo.net




GNU Linear Programming Kit
• C library for linear programming
 ▫ Bindings for many languages
• Solver for large-scale problems
 ▫ Linear programming
 ▫ Mixed integer programming
• GNU MathProg modeling language
 ▫ Subset of the AMPL proprietary language
Python, numerical optimization, genetic algorithms   daviderizzo.net




PyMathProg
• Pure Python modeling language
 ▫ http://pymprog.sourceforge.net/
 ▫ Model described with natural Python operators
• Easy integration in the process workflow
• Interface to GLPK solver
Python, numerical optimization, genetic algorithms   daviderizzo.net




PyMathProg bakery
import pymprog

# inits a model
m = pymprog.model('bakery')

# defines two named variables
x = m.var(range(2), 'x')

# objective function
m.max(1.5*x[0]+2.5*x[1])

# constraints
m.st(x[0]+x[1]<=100)
m.st(x[1]<=60)

m.solve()
print x
{0: x[0]=40.000000,
1: x[1]=60.000000}
Python, numerical optimization, genetic algorithms   daviderizzo.net




Binary variables: Sudoku
p = pymprog.model("sudoku")
I, J, K = range(9), range(9), range(1, 10)
T = pymprog.iprod(I,J,K) #create Indice tuples
x = p.var(T, 'x', bool)
#x[i,j,k] = 1 means cell [i,j] is assigned number k
#assign pre-defined numbers using the "givens"
p.st( [ +x[i,j,k] == (1 if g[i][j] == k else 0)
       for (i,j,k) in T if g[i][j] > 0 ], 'given')
#each cell must be assigned exactly one number
p.st([sum(x[i,j,k] for k in K)==1 for i in I for j in J], 'cell')
#cells in the same row must be assigned distinct numbers
p.st([sum(x[i,j,k] for j in J)==1 for i in I for k in K], 'row')
#cells in the same column must be assigned distinct numbers
p.st([sum(x[i,j,k] for i in I)==1 for j in J for k in K], 'col')
#cells in the same region must be assigned distinct numbers
p.st([sum(x[i,j,k] for i in range(r,r+3) for j in range(c, c+3))==1
    for r in range(0,9,3) for c in range(0,9,3) for k in K],'reg')
Python, numerical optimization, genetic algorithms   daviderizzo.net




CLV model example
• Marketing problem
• Potential and acquired customers
 ▫ Lifetime value (expected cash flow)
 ▫ Sensibility to promotions
• Limited budget for promotions
• Choose promotions as to maximize the total cash
  flow
Python, numerical optimization, genetic algorithms   daviderizzo.net




CLV model example code
BUDGET = 5000

# model
m = pymprog.model('clv')

y = m.var(customers, 'y', bool)
x = m.var(c_p, 'x', bool)

m.max(sum(CLV[c]*y[c] for c in customers) -
       sum(C[p]*sum(x[c,p] for c in customers) for p in promos))

m.st(sum(x[c,p]*C[p] for c, p in c_p) <= BUDGET)
for c in customers:
    m.st(y[c] <= sum(x[c,p]*S[c][p] for p in promos))

m.solve()
Python, numerical optimization, genetic algorithms   daviderizzo.net




CLV with activation costs
y = m.var(customers, 'y', bool)
x = m.var(c_p, 'x', bool)
z = m.var(promos, 'z', bool)

m.max(sum(CLV[c]*y[c] for c in customers) -
       sum(C[p]*sum(x[c,p] for c in customers) for p in promos) -
       sum(CP[p]*z[c] for p in promos))

m.st(sum(x[c,p]*C[p] for c, p in c_p)<=BUDGET)
m.st([([y[c] <= sum(x[c,p]*S[c][p] for p in promos))
       for c in customers])
m.st([(z[c] <= sum(x[c,p] for c in customers) for p in promos])
Python, numerical optimization, genetic algorithms   daviderizzo.net




«Take a bunch of random solutions, mix them randomly, repeat
an undefined number of times, get the optimum»
Python, numerical optimization, genetic algorithms   daviderizzo.net




Genetic algorithms
• Biological metaphor for an optimization
  technique
 ▫ Individual = proposed solution
 ▫ Gene = decision variable
 ▫ Fitness = objective function
• Natural selection-like
 ▫ The most fit individuals breed
 ▫ The children are similar to the parents
 ▫ Every generation better than the previous one
Python, numerical optimization, genetic algorithms   daviderizzo.net




Individuals and genomes
• Genetic representation of the solution
 ▫ How do the genes map to the solution?
 ▫ Historically as a string of bits
 ▫ Many alternatives: numbers, lists, graphs
• Genetic operators
 ▫ How are children different from parents?
 ▫ Crossover and mutation
• Fitness function
 ▫ Just like the objective function
Python, numerical optimization, genetic algorithms   daviderizzo.net




The simple genetic algorithm
• Randomly generate a population of individuals
• Repeat until I get a good enough solution
 ▫ Select the individuals with highest fitness
 ▫ Discard the least-fit ones
 ▫ Generate children from crossover and mutation
• Termination condition not always well defined
Python, numerical optimization, genetic algorithms   daviderizzo.net




Some genome encodings
• Binary string encoding
 ▫   Fixed length, each bit maps to a binary feature
 ▫   Crossover splits the string
 ▫   Mutation inverts a random bit
 ▫   Example: knapsack problem
• Sequence encoding
 ▫   Fixed elements, the order maps to the solution
 ▫   Crossover splits the sequence
 ▫   Mutation exchanges two elements
 ▫   Example: travelling salesman problem
Python, numerical optimization, genetic algorithms   daviderizzo.net




  Single point crossover
                  Parent 1                    Parent 2

         010110100 1110               101101111 0100

Crossover point




                             010110100 0100
      Offspring

                             101101111 1110
Python, numerical optimization, genetic algorithms   daviderizzo.net




Example: knapsack problem
Python, numerical optimization, genetic algorithms   daviderizzo.net




Knapsack problem
• Binary string encoding
 ▫ One bit for each item
• Fitness = value of the knapsack
 ▫ Zero if not fitting (is there a better way?)
• Single point crossover
• Simple mutation
Python, numerical optimization, genetic algorithms   daviderizzo.net




GAs pros and cons
•   Very easy to implement
•   Quite easy to get right
•   Good at problems where exact methods fail
•   Relatively very slow
•   Don’t scale well
•   Not well defined end condition
Python, numerical optimization, genetic algorithms   daviderizzo.net




Summing up
• Non-linear programming
 ▫ scipy.optimization
• Linear and integer programming
 ▫ GLPK and PyMathProg
• Global search heuristics
 ▫ Genetic algorithms
• Get updates!
 ▫ http://daviderizzo.net/blog/
Python, numerical optimization, genetic algorithms   daviderizzo.net

Más contenido relacionado

Similar a Python: ottimizzazione numerica algoritmi genetici

Agile Experiments in Machine Learning
Agile Experiments in Machine LearningAgile Experiments in Machine Learning
Agile Experiments in Machine Learningmathias-brandewinder
 
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016MLconf
 
MLConf 2016 SigOpt Talk by Scott Clark
MLConf 2016 SigOpt Talk by Scott ClarkMLConf 2016 SigOpt Talk by Scott Clark
MLConf 2016 SigOpt Talk by Scott ClarkSigOpt
 
01-Introduction_to_Optimization-v2021.2-Sept23-2021.pptx
01-Introduction_to_Optimization-v2021.2-Sept23-2021.pptx01-Introduction_to_Optimization-v2021.2-Sept23-2021.pptx
01-Introduction_to_Optimization-v2021.2-Sept23-2021.pptxTran273185
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to pythonActiveState
 
Machine learning workshop @DYP Pune
Machine learning workshop @DYP PuneMachine learning workshop @DYP Pune
Machine learning workshop @DYP PuneGanesh Raskar
 
Sea Amsterdam 2014 November 19
Sea Amsterdam 2014 November 19Sea Amsterdam 2014 November 19
Sea Amsterdam 2014 November 19GoDataDriven
 
Using Bayesian Optimization to Tune Machine Learning Models
Using Bayesian Optimization to Tune Machine Learning ModelsUsing Bayesian Optimization to Tune Machine Learning Models
Using Bayesian Optimization to Tune Machine Learning ModelsScott Clark
 
Using Bayesian Optimization to Tune Machine Learning Models
Using Bayesian Optimization to Tune Machine Learning ModelsUsing Bayesian Optimization to Tune Machine Learning Models
Using Bayesian Optimization to Tune Machine Learning ModelsSigOpt
 
Deploying Data Science for Distribution of The New York Times - Anne Bauer
Deploying Data Science for Distribution of The New York Times - Anne BauerDeploying Data Science for Distribution of The New York Times - Anne Bauer
Deploying Data Science for Distribution of The New York Times - Anne BauerPyData
 
High-Performance Python
High-Performance PythonHigh-Performance Python
High-Performance PythonWork-Bench
 
Begin with Machine Learning
Begin with Machine LearningBegin with Machine Learning
Begin with Machine LearningNarong Intiruk
 
Feature Engineering in H2O Driverless AI - Dmitry Larko - H2O AI World London...
Feature Engineering in H2O Driverless AI - Dmitry Larko - H2O AI World London...Feature Engineering in H2O Driverless AI - Dmitry Larko - H2O AI World London...
Feature Engineering in H2O Driverless AI - Dmitry Larko - H2O AI World London...Sri Ambati
 
Causal reasoning and Learning Systems
Causal reasoning and Learning SystemsCausal reasoning and Learning Systems
Causal reasoning and Learning SystemsTrieu Nguyen
 
DataScienceLab2017_Оптимизация гиперпараметров машинного обучения при помощи ...
DataScienceLab2017_Оптимизация гиперпараметров машинного обучения при помощи ...DataScienceLab2017_Оптимизация гиперпараметров машинного обучения при помощи ...
DataScienceLab2017_Оптимизация гиперпараметров машинного обучения при помощи ...GeeksLab Odessa
 
Deep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudDataDeep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudDataWeCloudData
 
How to easily find the optimal solution without exhaustive search using Genet...
How to easily find the optimal solution without exhaustive search using Genet...How to easily find the optimal solution without exhaustive search using Genet...
How to easily find the optimal solution without exhaustive search using Genet...Viach Kakovskyi
 
Machine Learning with TensorFlow 2
Machine Learning with TensorFlow 2Machine Learning with TensorFlow 2
Machine Learning with TensorFlow 2Sarah Stemmler
 

Similar a Python: ottimizzazione numerica algoritmi genetici (20)

Agile Experiments in Machine Learning
Agile Experiments in Machine LearningAgile Experiments in Machine Learning
Agile Experiments in Machine Learning
 
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016
 
MLConf 2016 SigOpt Talk by Scott Clark
MLConf 2016 SigOpt Talk by Scott ClarkMLConf 2016 SigOpt Talk by Scott Clark
MLConf 2016 SigOpt Talk by Scott Clark
 
01-Introduction_to_Optimization-v2021.2-Sept23-2021.pptx
01-Introduction_to_Optimization-v2021.2-Sept23-2021.pptx01-Introduction_to_Optimization-v2021.2-Sept23-2021.pptx
01-Introduction_to_Optimization-v2021.2-Sept23-2021.pptx
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to python
 
Machine learning workshop @DYP Pune
Machine learning workshop @DYP PuneMachine learning workshop @DYP Pune
Machine learning workshop @DYP Pune
 
Neural Network Part-2
Neural Network Part-2Neural Network Part-2
Neural Network Part-2
 
Sea Amsterdam 2014 November 19
Sea Amsterdam 2014 November 19Sea Amsterdam 2014 November 19
Sea Amsterdam 2014 November 19
 
Mlcc #4
Mlcc #4Mlcc #4
Mlcc #4
 
Using Bayesian Optimization to Tune Machine Learning Models
Using Bayesian Optimization to Tune Machine Learning ModelsUsing Bayesian Optimization to Tune Machine Learning Models
Using Bayesian Optimization to Tune Machine Learning Models
 
Using Bayesian Optimization to Tune Machine Learning Models
Using Bayesian Optimization to Tune Machine Learning ModelsUsing Bayesian Optimization to Tune Machine Learning Models
Using Bayesian Optimization to Tune Machine Learning Models
 
Deploying Data Science for Distribution of The New York Times - Anne Bauer
Deploying Data Science for Distribution of The New York Times - Anne BauerDeploying Data Science for Distribution of The New York Times - Anne Bauer
Deploying Data Science for Distribution of The New York Times - Anne Bauer
 
High-Performance Python
High-Performance PythonHigh-Performance Python
High-Performance Python
 
Begin with Machine Learning
Begin with Machine LearningBegin with Machine Learning
Begin with Machine Learning
 
Feature Engineering in H2O Driverless AI - Dmitry Larko - H2O AI World London...
Feature Engineering in H2O Driverless AI - Dmitry Larko - H2O AI World London...Feature Engineering in H2O Driverless AI - Dmitry Larko - H2O AI World London...
Feature Engineering in H2O Driverless AI - Dmitry Larko - H2O AI World London...
 
Causal reasoning and Learning Systems
Causal reasoning and Learning SystemsCausal reasoning and Learning Systems
Causal reasoning and Learning Systems
 
DataScienceLab2017_Оптимизация гиперпараметров машинного обучения при помощи ...
DataScienceLab2017_Оптимизация гиперпараметров машинного обучения при помощи ...DataScienceLab2017_Оптимизация гиперпараметров машинного обучения при помощи ...
DataScienceLab2017_Оптимизация гиперпараметров машинного обучения при помощи ...
 
Deep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudDataDeep Learning Introduction - WeCloudData
Deep Learning Introduction - WeCloudData
 
How to easily find the optimal solution without exhaustive search using Genet...
How to easily find the optimal solution without exhaustive search using Genet...How to easily find the optimal solution without exhaustive search using Genet...
How to easily find the optimal solution without exhaustive search using Genet...
 
Machine Learning with TensorFlow 2
Machine Learning with TensorFlow 2Machine Learning with TensorFlow 2
Machine Learning with TensorFlow 2
 

Más de PyCon Italia

Feed back report 2010
Feed back report 2010Feed back report 2010
Feed back report 2010PyCon Italia
 
Spyppolare o non spyppolare
Spyppolare o non spyppolareSpyppolare o non spyppolare
Spyppolare o non spyppolarePyCon Italia
 
zc.buildout: "Un modo estremamente civile per sviluppare un'applicazione"
zc.buildout: "Un modo estremamente civile per sviluppare un'applicazione"zc.buildout: "Un modo estremamente civile per sviluppare un'applicazione"
zc.buildout: "Un modo estremamente civile per sviluppare un'applicazione"PyCon Italia
 
Undici anni di lavoro con Python
Undici anni di lavoro con PythonUndici anni di lavoro con Python
Undici anni di lavoro con PythonPyCon Italia
 
socket e SocketServer: il framework per i server Internet in Python
socket e SocketServer: il framework per i server Internet in Pythonsocket e SocketServer: il framework per i server Internet in Python
socket e SocketServer: il framework per i server Internet in PythonPyCon Italia
 
Qt mobile PySide bindings
Qt mobile PySide bindingsQt mobile PySide bindings
Qt mobile PySide bindingsPyCon Italia
 
Python in the browser
Python in the browserPython in the browser
Python in the browserPyCon Italia
 
PyPy 1.2: snakes never crawled so fast
PyPy 1.2: snakes never crawled so fastPyPy 1.2: snakes never crawled so fast
PyPy 1.2: snakes never crawled so fastPyCon Italia
 
PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python
PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni pythonPyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python
PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni pythonPyCon Italia
 
OpenERP e l'arte della gestione aziendale con Python
OpenERP e l'arte della gestione aziendale con PythonOpenERP e l'arte della gestione aziendale con Python
OpenERP e l'arte della gestione aziendale con PythonPyCon Italia
 
New and improved: Coming changes to the unittest module
 	 New and improved: Coming changes to the unittest module 	 New and improved: Coming changes to the unittest module
New and improved: Coming changes to the unittest modulePyCon Italia
 
Monitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntopMonitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntopPyCon Italia
 
Jython for embedded software validation
Jython for embedded software validationJython for embedded software validation
Jython for embedded software validationPyCon Italia
 
Foxgame introduzione all'apprendimento automatico
Foxgame introduzione all'apprendimento automaticoFoxgame introduzione all'apprendimento automatico
Foxgame introduzione all'apprendimento automaticoPyCon Italia
 
Django è pronto per l'Enterprise
Django è pronto per l'EnterpriseDjango è pronto per l'Enterprise
Django è pronto per l'EnterprisePyCon Italia
 
Crogioli, alambicchi e beute: dove mettere i vostri dati.
Crogioli, alambicchi e beute: dove mettere i vostri dati.Crogioli, alambicchi e beute: dove mettere i vostri dati.
Crogioli, alambicchi e beute: dove mettere i vostri dati.PyCon Italia
 
Comet web applications with Python, Django & Orbited
Comet web applications with Python, Django & OrbitedComet web applications with Python, Django & Orbited
Comet web applications with Python, Django & OrbitedPyCon Italia
 
Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1PyCon Italia
 

Más de PyCon Italia (20)

Feed back report 2010
Feed back report 2010Feed back report 2010
Feed back report 2010
 
Spyppolare o non spyppolare
Spyppolare o non spyppolareSpyppolare o non spyppolare
Spyppolare o non spyppolare
 
zc.buildout: "Un modo estremamente civile per sviluppare un'applicazione"
zc.buildout: "Un modo estremamente civile per sviluppare un'applicazione"zc.buildout: "Un modo estremamente civile per sviluppare un'applicazione"
zc.buildout: "Un modo estremamente civile per sviluppare un'applicazione"
 
Undici anni di lavoro con Python
Undici anni di lavoro con PythonUndici anni di lavoro con Python
Undici anni di lavoro con Python
 
socket e SocketServer: il framework per i server Internet in Python
socket e SocketServer: il framework per i server Internet in Pythonsocket e SocketServer: il framework per i server Internet in Python
socket e SocketServer: il framework per i server Internet in Python
 
Qt mobile PySide bindings
Qt mobile PySide bindingsQt mobile PySide bindings
Qt mobile PySide bindings
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Python in the browser
Python in the browserPython in the browser
Python in the browser
 
PyPy 1.2: snakes never crawled so fast
PyPy 1.2: snakes never crawled so fastPyPy 1.2: snakes never crawled so fast
PyPy 1.2: snakes never crawled so fast
 
PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python
PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni pythonPyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python
PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python
 
OpenERP e l'arte della gestione aziendale con Python
OpenERP e l'arte della gestione aziendale con PythonOpenERP e l'arte della gestione aziendale con Python
OpenERP e l'arte della gestione aziendale con Python
 
New and improved: Coming changes to the unittest module
 	 New and improved: Coming changes to the unittest module 	 New and improved: Coming changes to the unittest module
New and improved: Coming changes to the unittest module
 
Monitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntopMonitoraggio del Traffico di Rete Usando Python ed ntop
Monitoraggio del Traffico di Rete Usando Python ed ntop
 
Jython for embedded software validation
Jython for embedded software validationJython for embedded software validation
Jython for embedded software validation
 
Foxgame introduzione all'apprendimento automatico
Foxgame introduzione all'apprendimento automaticoFoxgame introduzione all'apprendimento automatico
Foxgame introduzione all'apprendimento automatico
 
Effective EC2
Effective EC2Effective EC2
Effective EC2
 
Django è pronto per l'Enterprise
Django è pronto per l'EnterpriseDjango è pronto per l'Enterprise
Django è pronto per l'Enterprise
 
Crogioli, alambicchi e beute: dove mettere i vostri dati.
Crogioli, alambicchi e beute: dove mettere i vostri dati.Crogioli, alambicchi e beute: dove mettere i vostri dati.
Crogioli, alambicchi e beute: dove mettere i vostri dati.
 
Comet web applications with Python, Django & Orbited
Comet web applications with Python, Django & OrbitedComet web applications with Python, Django & Orbited
Comet web applications with Python, Django & Orbited
 
Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1Cleanup and new optimizations in WPython 1.1
Cleanup and new optimizations in WPython 1.1
 

Último

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 

Último (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 

Python: ottimizzazione numerica algoritmi genetici

  • 1. Python, numerical optimization, genetic algorithms Davide Rizzo PyCon Italia Qu4ttro daviderizzo.net
  • 2. Python, numerical optimization, genetic algorithms daviderizzo.net Optimization • Actually operations research ▫ Mathematical optimization is the tool • Applied maths ▫ Major academic and industrial research topic • Computationally oriented ▫ Many commercial solutions ▫ Yes, you can use Python • Not code optimization!
  • 3. Python, numerical optimization, genetic algorithms daviderizzo.net Operations research applications • Airlines ▫ scheduling planes and crews, pricing tickets, taking reservations, and planning fleet size • Logistics ▫ routing and planning • Financial services ▫ credit scoring, marketing, and internal operations • Deployment of emergency services • Policy studies and regulation ▫ environmental pollution, air traffic safety, AIDS, and criminal justice policy
  • 4. Python, numerical optimization, genetic algorithms daviderizzo.net More operations research applications • Project planning • Network optimization • Resources allocation • Supply chain management • Automation • Scheduling • Pricing
  • 5. Python, numerical optimization, genetic algorithms daviderizzo.net Optimization problem • Many decision variables ▫ How should I price my new products? ▫ How many server resources should I assign to each client? • An objective ▫ Maximize net profit ▫ Minimize client waiting time
  • 6. Python, numerical optimization, genetic algorithms daviderizzo.net Objective function • Objective: find the best choice for the decision variables ▫ the values that give the maximum yield (or the minimum cost) • Objective function: the yield (or the cost) I get for a given choice
  • 7. Python, numerical optimization, genetic algorithms daviderizzo.net Decisions and objectives • I buy 100kg flour at 50€ ▫ Want to decide how to use it • 70kg -> bread, 30kg -> cookies ▫ Decision variables  X = 70, 30 • Sell 105€ of bread and 75€ of cookies ▫ Objective function value:  f(X) = 105 + 75 – 50 = 130€
  • 8. Python, numerical optimization, genetic algorithms daviderizzo.net Visualizing the problem
  • 9. Python, numerical optimization, genetic algorithms daviderizzo.net Constraints • Not every solution is good • 100kg flour → can’t use 150kg for bread ▫ flour_bread + flour_cookies ≤ 100
  • 10. Python, numerical optimization, genetic algorithms daviderizzo.net Constraints • Inequalities often describe constraints • Many constraints ▫ There are problems with thousands inequalities • Limited sugar ▫ 1 part sugar, 3 parts flour ▫ 20 kg sugar available ▫ flour_cookies < 60
  • 11. Python, numerical optimization, genetic algorithms daviderizzo.net Search vs. optimization Search algorithms Optimization algorithms • Well-known among • Broader class of problems programmers ▫ Includes search problems • Include tree-search and graph- • Continuous search space search algorithms ▫ Discrete as a special case • Work on a discrete search • Search algorithms used to space solve many optimization problems
  • 12. Python, numerical optimization, genetic algorithms daviderizzo.net Finding the optimum in linear and non-linear problems with exact methods
  • 13. Python, numerical optimization, genetic algorithms daviderizzo.net Objective function classification • General theory for every problem ▫ Very good results for simple problems • Objective function structure ▫ Smoothness and regularity determine complexity ▫ Good guarantees for linear functions ▫ No exact results if irregular • Factors other than objective functions ▫ Constraints ▫ Integrality on variables
  • 14. Python, numerical optimization, genetic algorithms daviderizzo.net Line (one variable) Linear Plane (two variables) A greater number of variables does not complicate matters!
  • 15. Python, numerical optimization, genetic algorithms daviderizzo.net Linear • Linear objective function ▫ c0 + sum(c[i]*x[i] for i in range(len(vars))) ▫ Bakery example vars = ['flour_bread', 'flour_cookies'] c = [1.5, 2.5]; c0 = -50 >>> obj_fun([70, 30]) 130.0
  • 16. Python, numerical optimization, genetic algorithms daviderizzo.net Non-linear “smooth”
  • 17. Python, numerical optimization, genetic algorithms daviderizzo.net Non-linear “smooth” • Non-linearity raises some issues • Function must be “smooth” enough ▫ You can stand on any point of the curved surface and assume you are on a plane • No guarantee to find global optimum
  • 18. Python, numerical optimization, genetic algorithms daviderizzo.net Non regular function
  • 19. Python, numerical optimization, genetic algorithms daviderizzo.net Non smooth function
  • 20. Python, numerical optimization, genetic algorithms daviderizzo.net Gradient descent • Pick a point • Pretend you are on a line ▫ Can choose the optimal direction at each point • Take steps until you fall in an optimum ▫ Local optimum!
  • 21. Python, numerical optimization, genetic algorithms daviderizzo.net No smoothness? • Any other formulation for the problem? ▫ Decrease model complexity ▫ Put complexity in constraints • Settle for an approximate solution • Global search heuristics ▫ Genetic algorithms are an example
  • 22. Python, numerical optimization, genetic algorithms daviderizzo.net SciPy optimization package • Non-linear numerical function optimization ▫ http://docs.scipy.org/doc/scipy/reference/optimize.html • optimize.fmin(func, x0) ▫ Unconstrained optimization ▫ Finds the minimum of func(x) starting x with x0 ▫ x can be a vector, func must return a float • Better algorithm for many variables: fmin_bfgs • Algorithms for constrained optimization
  • 23. Python, numerical optimization, genetic algorithms daviderizzo.net Give me the code! • from scipy import optimize def f(x): return x[0]**2 + (x[1]-2)**2 print optimize.fmin(f, [0,0]) • Optimization terminated successfully. Current function value: 0.000000 Iterations: 63 Function evaluations: 120 [ -4.04997873e-06 2.00004847e+00]
  • 24. Python, numerical optimization, genetic algorithms daviderizzo.net Many variables • from scipy import optimize from numpy import array from random import uniform n = 50 center = array([uniform(0, 10) for i in range(n)]) def f(x): return sum((x-center)**2) optimum = optimize.fmin_bfgs(f, [0]*n) print optimum - center
  • 25. Python, numerical optimization, genetic algorithms daviderizzo.net Optimization terminated successfully. Current function value: 0.000000 Iterations: 3 Function evaluations: 312 Gradient evaluations: 6 [ 1.09850102e-08 1.00864010e-08 -3.44023343e-09 1.45686556e-08 1.95572314e-09 1.63218594e-09 3.37632766e-09 1.44865293e-08 1.52201984e-08 1.29321513e-08 -1.59485092e-09 1.23136292e-08 1.62830638e-08 -3.52261820e-09 -5.56838653e-09 1.24844490e-08 8.54035953e-09 1.55625752e-08 8.50658477e-09 1.08354117e-08 1.62710236e-08 -3.44068374e-09 -5.63388847e-09 1.13670406e-09 1.14938468e-08 -1.92982030e-09 -1.66206160e-09 -1.50014312e-09 1.12226592e-08 -5.60273883e-09 3.07106607e-09 6.95412705e-09 -4.58609706e-09 1.24199513e-08 -5.76112080e-09 -7.69927677e-10 -6.56263044e-10 -6.08281736e-09 1.10073151e-08 3.35045769e-09 1.25477273e-09 5.98582162e-09 -5.60075986e-10 1.00968194e-08 -1.23273258e-09 -2.11921281e-09 -6.40983949e-09 8.99856101e-09 -1.08943476e-09 1.55362354e-08]
  • 26. Python, numerical optimization, genetic algorithms daviderizzo.net Microeconomics model example • Pricing problem for two products ▫ Choose the prices that give the best profit • Assume to know demand curves ▫ From buyer’s utility maximization • Could be generalized ▫ Many products ▫ Complex demand curves ▫ Complex costs and economies of scale ▫ Beware of non smooth functions!
  • 27. Python, numerical optimization, genetic algorithms daviderizzo.net Microeconomics model example def profit(p1, p2): from scipy import optimize x1 = demand1(p1, p2) x2 = demand2(p1, p2) def objective(x): income = x1*p1 + x2*p2 return -profit(*x) cost = costf(x1, x2) return income - cost price = optimize.fmin(objective, [1, 1]) def demand1(p1, p2): return 20*(20 - p1) print 'prices:', price print 'amounts:‘ def demand2(p1, p2): print demand1(*price), demand2(*price) return 40*(40 - p2) def costf(x1, x2): c1 = 10*(x1**0.5) c2 = 20*(x2**0.5) + 2*x2 cbase = 1000 + 5*(x1 + x2) return c1 + c2 + cbase
  • 28. Python, numerical optimization, genetic algorithms daviderizzo.net Microeconomics model results Optimization terminated successfully. Current function value: -10381.085398 Iterations: 63 Function evaluations: 122 prices: [ 12.7070282 23.69579991] amounts: 145.859435961 652.168003412
  • 29. Python, numerical optimization, genetic algorithms daviderizzo.net Linear programming problem • Linear objective function ▫ Weighted sum of variables • Constraints are linear inequalities ▫ Weighted sum of some variables ≤ 0 • Variables can also be integer or binary ▫ Mixed integer programming ▫ NP-complete
  • 30. Python, numerical optimization, genetic algorithms daviderizzo.net Back to the bakery • max c1x1 + c2x2 • subject to ▫ x1 + x2 ≤ 100 ▫ x2 ≤ 60
  • 31. Python, numerical optimization, genetic algorithms daviderizzo.net GNU Linear Programming Kit • C library for linear programming ▫ Bindings for many languages • Solver for large-scale problems ▫ Linear programming ▫ Mixed integer programming • GNU MathProg modeling language ▫ Subset of the AMPL proprietary language
  • 32. Python, numerical optimization, genetic algorithms daviderizzo.net PyMathProg • Pure Python modeling language ▫ http://pymprog.sourceforge.net/ ▫ Model described with natural Python operators • Easy integration in the process workflow • Interface to GLPK solver
  • 33. Python, numerical optimization, genetic algorithms daviderizzo.net PyMathProg bakery import pymprog # inits a model m = pymprog.model('bakery') # defines two named variables x = m.var(range(2), 'x') # objective function m.max(1.5*x[0]+2.5*x[1]) # constraints m.st(x[0]+x[1]<=100) m.st(x[1]<=60) m.solve() print x {0: x[0]=40.000000, 1: x[1]=60.000000}
  • 34. Python, numerical optimization, genetic algorithms daviderizzo.net Binary variables: Sudoku p = pymprog.model("sudoku") I, J, K = range(9), range(9), range(1, 10) T = pymprog.iprod(I,J,K) #create Indice tuples x = p.var(T, 'x', bool) #x[i,j,k] = 1 means cell [i,j] is assigned number k #assign pre-defined numbers using the "givens" p.st( [ +x[i,j,k] == (1 if g[i][j] == k else 0) for (i,j,k) in T if g[i][j] > 0 ], 'given') #each cell must be assigned exactly one number p.st([sum(x[i,j,k] for k in K)==1 for i in I for j in J], 'cell') #cells in the same row must be assigned distinct numbers p.st([sum(x[i,j,k] for j in J)==1 for i in I for k in K], 'row') #cells in the same column must be assigned distinct numbers p.st([sum(x[i,j,k] for i in I)==1 for j in J for k in K], 'col') #cells in the same region must be assigned distinct numbers p.st([sum(x[i,j,k] for i in range(r,r+3) for j in range(c, c+3))==1 for r in range(0,9,3) for c in range(0,9,3) for k in K],'reg')
  • 35. Python, numerical optimization, genetic algorithms daviderizzo.net CLV model example • Marketing problem • Potential and acquired customers ▫ Lifetime value (expected cash flow) ▫ Sensibility to promotions • Limited budget for promotions • Choose promotions as to maximize the total cash flow
  • 36. Python, numerical optimization, genetic algorithms daviderizzo.net CLV model example code BUDGET = 5000 # model m = pymprog.model('clv') y = m.var(customers, 'y', bool) x = m.var(c_p, 'x', bool) m.max(sum(CLV[c]*y[c] for c in customers) - sum(C[p]*sum(x[c,p] for c in customers) for p in promos)) m.st(sum(x[c,p]*C[p] for c, p in c_p) <= BUDGET) for c in customers: m.st(y[c] <= sum(x[c,p]*S[c][p] for p in promos)) m.solve()
  • 37. Python, numerical optimization, genetic algorithms daviderizzo.net CLV with activation costs y = m.var(customers, 'y', bool) x = m.var(c_p, 'x', bool) z = m.var(promos, 'z', bool) m.max(sum(CLV[c]*y[c] for c in customers) - sum(C[p]*sum(x[c,p] for c in customers) for p in promos) - sum(CP[p]*z[c] for p in promos)) m.st(sum(x[c,p]*C[p] for c, p in c_p)<=BUDGET) m.st([([y[c] <= sum(x[c,p]*S[c][p] for p in promos)) for c in customers]) m.st([(z[c] <= sum(x[c,p] for c in customers) for p in promos])
  • 38. Python, numerical optimization, genetic algorithms daviderizzo.net «Take a bunch of random solutions, mix them randomly, repeat an undefined number of times, get the optimum»
  • 39. Python, numerical optimization, genetic algorithms daviderizzo.net Genetic algorithms • Biological metaphor for an optimization technique ▫ Individual = proposed solution ▫ Gene = decision variable ▫ Fitness = objective function • Natural selection-like ▫ The most fit individuals breed ▫ The children are similar to the parents ▫ Every generation better than the previous one
  • 40. Python, numerical optimization, genetic algorithms daviderizzo.net Individuals and genomes • Genetic representation of the solution ▫ How do the genes map to the solution? ▫ Historically as a string of bits ▫ Many alternatives: numbers, lists, graphs • Genetic operators ▫ How are children different from parents? ▫ Crossover and mutation • Fitness function ▫ Just like the objective function
  • 41. Python, numerical optimization, genetic algorithms daviderizzo.net The simple genetic algorithm • Randomly generate a population of individuals • Repeat until I get a good enough solution ▫ Select the individuals with highest fitness ▫ Discard the least-fit ones ▫ Generate children from crossover and mutation • Termination condition not always well defined
  • 42. Python, numerical optimization, genetic algorithms daviderizzo.net Some genome encodings • Binary string encoding ▫ Fixed length, each bit maps to a binary feature ▫ Crossover splits the string ▫ Mutation inverts a random bit ▫ Example: knapsack problem • Sequence encoding ▫ Fixed elements, the order maps to the solution ▫ Crossover splits the sequence ▫ Mutation exchanges two elements ▫ Example: travelling salesman problem
  • 43. Python, numerical optimization, genetic algorithms daviderizzo.net Single point crossover Parent 1 Parent 2 010110100 1110 101101111 0100 Crossover point 010110100 0100 Offspring 101101111 1110
  • 44. Python, numerical optimization, genetic algorithms daviderizzo.net Example: knapsack problem
  • 45. Python, numerical optimization, genetic algorithms daviderizzo.net Knapsack problem • Binary string encoding ▫ One bit for each item • Fitness = value of the knapsack ▫ Zero if not fitting (is there a better way?) • Single point crossover • Simple mutation
  • 46. Python, numerical optimization, genetic algorithms daviderizzo.net GAs pros and cons • Very easy to implement • Quite easy to get right • Good at problems where exact methods fail • Relatively very slow • Don’t scale well • Not well defined end condition
  • 47. Python, numerical optimization, genetic algorithms daviderizzo.net Summing up • Non-linear programming ▫ scipy.optimization • Linear and integer programming ▫ GLPK and PyMathProg • Global search heuristics ▫ Genetic algorithms • Get updates! ▫ http://daviderizzo.net/blog/
  • 48. Python, numerical optimization, genetic algorithms daviderizzo.net