offspring=Population(Individual,la,parabolic,searchspace) #------------------------------------------------------------------------------- #--- part 2: random starting point for search ---------------------------------- #--- and more initialisation --------------------------------------------------- #------------------------------------------------------------------------------- npr.seed(1) # seeding numpy's random number generator so we get reproducibly the same random numbers startpoint=2*npr.rand(dim)-1 # point in search space where initial population is placed parents.set_every_DNA(startpoint) parents.eval_all() print 'fitness of initial population: ' print [dude.score for dude in parents] print 'or also via get_scores():' print parents.get_scores() mstep=0.002 # mutation step size parameter print 'initial mstep: ',mstep, 2*'\n' g_rec=[] # for recording x-data for plot, i.e. generation s_rec=[] # for recording y-data for plot, i.e. score or fitness ms_rec=[] # for recording mutation step size history #------------------------------------------------------------------------------- #--- part 3: the loop for (mu,la)-ES ------------------------------------------- #------------------------------------------------------------------------------- """ The big advantage of peabox can be seen in the inner loop below: it is super easily readable (for dude in offspring: copy .. mutate .. evaluate). Commenting is not required, because the code is so readable, it looks
return np.sum(x * x) searchspace = (('length', 12., 14.), ('wall_thickness', 0.1, 1.4), ('radius', 20., 40.)) N = 5 p = Population(Individual, N, parabolic, searchspace) npr.seed(3) p.new_random_genes() p.eval_all() for dude in p: dude.score = np.round(dude.score, 2) sc = p.get_scores() print "the population's scores: ", sc # should give the list [ 1.22 1.32 0.53 0.17 1.81] dude0, dude1, dude2, dude3, dude4 = p print 'dude0<dude1 yields ', dude0 < dude1, ' and dude0.isbetter(dude1) yields ', dude0.isbetter( dude1) print 'dude0<dude2 yields ', dude0 < dude2, ' and dude0.isbetter(dude2) yields ', dude0.isbetter( dude2) print 'p.whatisfit and dude0.whatisfit are: ', p.whatisfit, dude0.whatisfit p.determine_whatisfit('max') print "now how does it look like after the spell 'p.determine_whatisfit('max')'?" print 'p.whatisfit and dude0.whatisfit are: ', p.whatisfit, dude0.whatisfit print 'and the comparisons from above?' print 'dude0<dude1 yields ', dude0 < dude1, ' and dude0.isbetter(dude1) yields ', dude0.isbetter( dude1) print 'dude0<dude2 yields ', dude0 < dude2, ' and dude0.isbetter(dude2) yields ', dude0.isbetter( dude2)
return np.sum(x*x) searchspace=(('length',12.,14.), ('wall_thickness',0.1,1.4), ('radius',20.,40.)) N=5 p=Population(Individual,N,parabolic,searchspace) npr.seed(3) p.new_random_genes() p.eval_all() for dude in p: dude.score=np.round(dude.score,2) sc=p.get_scores() print "the population's scores: ",sc # should give the list [ 1.22 1.32 0.53 0.17 1.81] dude0,dude1,dude2,dude3,dude4=p print 'dude0<dude1 yields ',dude0<dude1,' and dude0.isbetter(dude1) yields ',dude0.isbetter(dude1) print 'dude0<dude2 yields ',dude0<dude2,' and dude0.isbetter(dude2) yields ',dude0.isbetter(dude2) print 'p.whatisfit and dude0.whatisfit are: ',p.whatisfit,dude0.whatisfit p.determine_whatisfit('max') print "now how does it look like after the spell 'p.determine_whatisfit('max')'?" print 'p.whatisfit and dude0.whatisfit are: ',p.whatisfit,dude0.whatisfit print 'and the comparisons from above?' print 'dude0<dude1 yields ',dude0<dude1,' and dude0.isbetter(dude1) yields ',dude0.isbetter(dude1) print 'dude0<dude2 yields ',dude0<dude2,' and dude0.isbetter(dude2) yields ',dude0.isbetter(dude2) print 2*'\n' print "the population's scores: ",sc # should give the list [ 1.22 1.32 0.53 0.17 1.81] print 'the population itself gets printed out like this:'
#------------------------------------------------------------------------------- #--- part 2: random starting point for search ---------------------------------- #--- and more initialisation --------------------------------------------------- #------------------------------------------------------------------------------- npr.seed( 11 ) # seeding numpy's random number generator so we get reproducibly the same random numbers startpoint = 360 * npr.rand( dim) # point in search space where initial population is placed parents.set_every_DNA(startpoint) parents.eval_all() print 'fitness of initial population: ' print[dude.score for dude in parents] print 'or also via get_scores():' print parents.get_scores() mstep = 0.002 # mutation step size parameter print 'initial mstep: ', mstep, 2 * '\n' g_rec = [] # for recording x-data for plot, i.e. generation s_rec = [] # for recording y-data for plot, i.e. score or fitness ms_rec = [] # for recording mutation step size history #------------------------------------------------------------------------------- #--- part 3: the loop for (mu,la)-ES ------------------------------------------- #------------------------------------------------------------------------------- """ The big advantage of peabox can be seen in the inner loop below: it is super easily readable (for dude in offspring: copy .. mutate .. evaluate). Commenting is not required, because the code is so readable, it looks like pseudocode. This is the main purpose why I wrote this EA library.