Skip to content
This repository
Newer
Older
100644 40 lines (33 sloc) 1.146 kb
5e26f858 »
2010-04-21 Dos2Unix conversion.
1 from pyevolve import GSimpleGA
2 from pyevolve import G1DList
3 from pyevolve import Mutators, Initializators
4 from pyevolve import Selectors
5 from pyevolve import Consts
6 import math
7
8 # This is the Rastrigin Function, a deception function
9 def rastrigin(genome):
438918c1 » Melevir
2013-12-19 PEP8 fixes
10 n = len(genome)
11 total = 0
12 for i in xrange(n):
13 total += genome[i]**2 - 10*math.cos(2*math.pi*genome[i])
14 return (10*n) + total
5e26f858 »
2010-04-21 Dos2Unix conversion.
15
16 def run_main():
438918c1 » Melevir
2013-12-19 PEP8 fixes
17 # Genome instance
18 genome = G1DList.G1DList(20)
19 genome.setParams(rangemin=-5.2, rangemax=5.30, bestrawscore=0.00, rounddecimal=2)
20 genome.initializator.set(Initializators.G1DListInitializatorReal)
21 genome.mutator.set(Mutators.G1DListMutatorRealGaussian)
5e26f858 »
2010-04-21 Dos2Unix conversion.
22
438918c1 » Melevir
2013-12-19 PEP8 fixes
23 genome.evaluator.set(rastrigin)
5e26f858 »
2010-04-21 Dos2Unix conversion.
24
438918c1 » Melevir
2013-12-19 PEP8 fixes
25 # Genetic Algorithm Instance
26 ga = GSimpleGA.GSimpleGA(genome)
27 ga.terminationCriteria.set(GSimpleGA.RawScoreCriteria)
28 ga.setMinimax(Consts.minimaxType["minimize"])
29 ga.setGenerations(3000)
30 ga.setCrossoverRate(0.8)
31 ga.setPopulationSize(100)
32 ga.setMutationRate(0.06)
5e26f858 »
2010-04-21 Dos2Unix conversion.
33
438918c1 » Melevir
2013-12-19 PEP8 fixes
34 ga.evolve(freq_stats=50)
5e26f858 »
2010-04-21 Dos2Unix conversion.
35
438918c1 » Melevir
2013-12-19 PEP8 fixes
36 best = ga.bestIndividual()
37 print best
5e26f858 »
2010-04-21 Dos2Unix conversion.
38
39 if __name__ == "__main__":
438918c1 » Melevir
2013-12-19 PEP8 fixes
40 run_main()
Something went wrong with that request. Please try again.