The Rastrigin function

This notebook is adapted from a Pyevolve example.

From Wikipedia, the free encyclopedia: in mathematical optimization, the Rastrigin function is a non-convex function used as a performance test problem for optimization algorithms. It is a typical example of non-linear multimodal function. It was first proposed by Rastrigin as a 2-dimensional function and has been generalized by Mühlenbein et al. Finding the minimum of this function is a fairly difficult problem due to its large search space and its large number of local minima.

Below is the code to minimize the deceptive Rastrigin function in a 20-dimensional space, i.e. with 20 parameters.

You should add the necessary statements for storing the results in a database named 'rastrigin.db' with identifier 'ex1', which will be used later for checking the evolution.


In [ ]:
from pyevolve import GSimpleGA
from pyevolve import G1DList
from pyevolve import Mutators, Initializators
from pyevolve import Selectors
from pyevolve import Consts
import math

In [ ]:
from pyevolve import DBAdapters

In [ ]:
def rastrigin(genome):
    n = len(genome)
    total = 0
    for i in xrange(n):
        total += genome[i]**2 - 10*math.cos(2*math.pi*genome[i])
    return (10*n) + total

In [ ]:
genome = G1DList.G1DList(20)
genome.setParams(rangemin=-5.2, rangemax=5.3, bestrawscore=0.0, rounddecimal=1)
genome.initializator.set(Initializators.G1DListInitializatorReal)
genome.mutator.set(Mutators.G1DListMutatorRealGaussian)
genome.evaluator.set(rastrigin)

In [ ]:
sqlite_adapter = DBAdapters.DBSQLite(dbname='rastrigin.db', identify="ex1", resetDB=True)

In [ ]:
ga = GSimpleGA.GSimpleGA(genome)
ga.terminationCriteria.set(GSimpleGA.RawScoreCriteria)
ga.setMinimax(Consts.minimaxType["minimize"])
ga.setGenerations(500)
ga.setCrossoverRate(0.8)
ga.setPopulationSize(100)
ga.setMutationRate(0.06)

In [ ]:
ga.setDBAdapter(sqlite_adapter)

In [ ]:
ga.evolve(freq_stats=50)
best = ga.bestIndividual()
print best

You can check now the results by plotting some graphs of the evolution process in this notebook.