Commits

dan mackinlay committed b548b04

working single float evolution

  • Participants
  • Parent commits 0c96b34

Comments (0)

Files changed (1)

privateer_pyevolve/privateer_sim.py

 from pyevolve import GSimpleGA
 from pyevolve import Selectors
 from pyevolve import Statistics
-from pyevolve import DBAdapters
+# from pyevolve import DBAdapters
+from pyevolve import Selectors, Crossovers, Consts, Initializators, Mutators
 
 def eval_func(genome):
     """
-    
     This function is the evaluation function, we want
     to give high score to more zero'ed chromosomes
 
         :raises: AttributeError, KeyError
 
         """
-    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
+    return sum([abs(s) for s in genome])
 
 def run_main():
     # Genome instance, 1D List of 50 elements
 
     # The evaluator function (evaluation function)
     genome.evaluator.set(eval_func)
-
+    
+    # Mutator function 
+    genome.mutator.set(Mutators.G1DListMutatorRealGaussian)
+    
+    # Crossover function must be set to something that can handle 1-elem list
+    genome.crossover.set(Crossovers.G1DListCrossoverUniform)
+    
     # Genetic Algorithm Instance
-    ga = GSimpleGA.GSimpleGA(genome)
+    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)
+    ga.setMinimax(Consts.minimaxType["minimize"])
+    
+    # # 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)
+    ga.evolve(freq_stats=1)
 
     # Best individual
     print ga.bestIndividual()