File: pyevolve_ex1_simple.py

package info (click to toggle)
pyevolve 0.6~rc1%2Bsvn398%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,060 kB
  • ctags: 832
  • sloc: python: 5,119; xml: 183; sh: 71; makefile: 38
file content (54 lines) | stat: -rw-r--r-- 1,588 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from pyevolve import G1DList
from pyevolve import GSimpleGA
from pyevolve import Selectors
from pyevolve import Statistics
from pyevolve import DBAdapters

# This function is the evaluation function, we want
# to give high score to more zero'ed chromosomes
def eval_func(genome):
   score = 0.0

   # iterate over the chromosome
   # The same as "score = len(filter(lambda x: x==0, genome))"
   for value in genome:
      if value==0:
         score += 1
   
   return score

def run_main():
   # Genome instance, 1D List of 50 elements
   genome = G1DList.G1DList(50)

   # Sets the range max and min of the 1D List
   genome.setParams(rangemin=0, rangemax=10)

   # The evaluator function (evaluation function)
   genome.evaluator.set(eval_func)

   # Genetic Algorithm Instance
   ga = GSimpleGA.GSimpleGA(genome)

   # Set the Roulette Wheel selector method, the number of generations and
   # the termination criteria
   ga.selector.set(Selectors.GRouletteWheel)
   ga.setGenerations(500)
   ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)

   # Sets the DB Adapter, the resetDB flag will make the Adapter recreate
   # the database and erase all data every run, you should use this flag
   # just in the first time, after the pyevolve.db was created, you can
   # omit it.
   sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True)
   ga.setDBAdapter(sqlite_adapter)

   # Do the evolution, with stats dump
   # frequency of 20 generations
   ga.evolve(freq_stats=20)

   # Best individual
   print ga.bestIndividual()

if __name__ == "__main__":
   run_main()