Find the global minima of function $f(x) = x + 10sin(5x) + 7cos(4x)$

Create individual (use binary encoding)


In [1]:
from gaft.components import BinaryIndividual
indv = BinaryIndividual(ranges=[(0, 10)], eps=0.001)

Create a population with 50 individuals


In [2]:
from gaft.components import Population
population = Population(indv_template=indv, size=50).init()

Create genetic operators: selection, crossover, mutation

1. Tournament selection


In [3]:
from gaft.operators import TournamentSelection
selection = TournamentSelection()

2. Uniform crossover

  • pc is the probabililty of crossover operation
  • pe is the exchange probabiltiy for each possible gene bit in chromsome

In [4]:
from gaft.operators import UniformCrossover
crossover = UniformCrossover(pc=0.8, pe=0.5)

3. Flip bit mutation

  • pm is the probability of mutation

In [5]:
from gaft.operators import FlipBitMutation
mutation = FlipBitMutation(pm=0.1)

Import an on-the-fly analysis plugin to output info to console


In [6]:
from gaft.analysis import ConsoleOutput

Create an engine to run


In [7]:
from gaft import GAEngine

engine = GAEngine(population=population, selection=selection,
                  crossover=crossover, mutation=mutation,
                  analysis=[ConsoleOutput])

Define target function to optimize

here we try to find the global minima of $f(x) = x + 10sin(5x) + 7cos(4x)$

GAFT find the maxima of the fitness function, here we use the engine.minimize decorator to tell GAFT to find the minima.


In [8]:
from math import sin, cos

@engine.fitness_register
@engine.minimize
def fitness(indv):
    x, = indv.solution
    return x + 10*sin(5*x) + 7*cos(4*x)

Run the engine


In [9]:
engine.run(ng=50)


gaft.ConsoleOutput   INFO     Generation number: 50 Population number: 50
gaft.ConsoleOutput   INFO     Generation: 1, best fitness: 15.077, scaled fitness: 15.077
gaft.ConsoleOutput   INFO     Generation: 2, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 3, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 4, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 5, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 6, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 7, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 8, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 9, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 10, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 11, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 12, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 13, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 14, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 15, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 16, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 17, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 18, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 19, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 20, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 21, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 22, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 23, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 24, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 25, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 26, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 27, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 28, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 29, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 30, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 31, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 32, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 33, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 34, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 35, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 36, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 37, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 38, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 39, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 40, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 41, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 42, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 43, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 44, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 45, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 46, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 47, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 48, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 49, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Generation: 50, best fitness: 15.164, scaled fitness: 15.164
gaft.ConsoleOutput   INFO     Optimal solution: ([0.892333984375], 15.164338102247847)

After engine running, we can do something more...

Get the best individual


In [10]:
best_indv = engine.population.best_indv(engine.fitness)

Get the solution


In [11]:
best_indv.solution


Out[11]:
[0.892333984375]

And the fitness value


In [12]:
engine.fitness(best_indv)


Out[12]:
15.164338102247847