searchspace = simple_searchspace(10, -3., 1.) ps = 40 # population size G = 80 # number of generations to go through parents = Population(Individual, ps, f11, searchspace) offspring = Population(Individual, ps, f11, searchspace) #------------------------------------------------------------------------------- #--- part 2: initialisation ---------------------------------------------------- #------------------------------------------------------------------------------- seed( 1 ) # seeding numpy's random number generator so we get reproducibly the same random numbers parents.new_random_genes() parents.eval_all() parents.sort() parents.update_no() sa_T = parents[-1].score - parents[0].score # starting temperature saga_ratio = 0.5 # fraction of offspring created by SA and not GA sa_mstep = 0.02 # mutation step size parameter sa_mprob = 0.6 # mutation probability ga_selp = 3. # selection pressure AE = 0.04 # annealing exponent --> exp(-AE) is multiplier for reducing temperature elite_size = 0 g_rec = [] # for recording x-data for plot, i.e. generation bs_rec = [] # for recording y-data for plot, here best score ws_rec = [] # for recording y-data for plot, here worst score T_rec = [] # for recording y-data for plot, here temperature ms_rec = [] # for recording y-data for plot, here mutation step size
mu=4 # size of parent population of (mu,lambda)-ES la=12 # size of offspring population of (mu,lambda)-ES dim=len(searchspace) parents=Population(Individual,mu,parabolic,searchspace) 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 ------------------------------------------- #-------------------------------------------------------------------------------
from peabox_individual import Individual from peabox_population import Population def parabolic(x): 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)
from peabox_population import Population def parabolic(x): 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?'
self.fmwave([DNA[0], DNA[2], DNA[4]], [DNA[1], DNA[3], DNA[5]]) return np.sum((self.trial - self.target)**2) # search space boundaries: searchspace = (('amp 1', -6.4, +6.35), ('omega 1', -6.4, +6.35), ('amp 2', -6.4, +6.35), ('omega 2', -6.4, +6.35), ('amp 3', -6.4, +6.35), ('omega 3', -6.4, +6.35)) ps = 4 # population size dim = len(searchspace) # search space dimension # now let's create a population to test version A of the objective function implementation pA = Population(Individual, ps, FMsynthA, searchspace) pA.new_random_genes() pA.eval_all() print 'DNAs of pA:' print pA.get_DNAs() pA.print_stuff(slim=True) # and here the objective function version B and another population to test it problem_instance = FMsynthB() objfuncB = problem_instance.call pB = Population(Individual, ps, objfuncB, searchspace) pB.copy_otherpop(pA) pB.eval_all() print 'DNAs of pB:' print pB.get_DNAs() pB.print_stuff(slim=True) # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# search space boundaries: searchspace=(('amp 1', -6.4, +6.35), ('omega 1', -6.4, +6.35), ('amp 2', -6.4, +6.35), ('omega 2', -6.4, +6.35), ('amp 3', -6.4, +6.35), ('omega 3', -6.4, +6.35)) ps=4 # population size dim=len(searchspace) # search space dimension pC=Population(FMsynthC,ps,dummyfunc,searchspace) pC.set_ncase(1) pC.new_random_genes() pC.eval_all() print 'DNAs of pC:' print pC.get_DNAs() pC.print_stuff(slim=True) for dude in pC: dude.plot_FMsynth_solution() # and the modified version pD=Population(FMsynthC,ps,dummyfunc,searchspace) pD.set_ncase(2) for dude in pD: dude.w[1]=4.7 dude.initialize_target() pD.copy_otherpop(pC) pD.eval_all()
# search space boundaries: searchspace=(('amp 1', -6.4, +6.35), ('omega 1', -6.4, +6.35), ('amp 2', -6.4, +6.35), ('omega 2', -6.4, +6.35), ('amp 3', -6.4, +6.35), ('omega 3', -6.4, +6.35)) ps=4 # population size dim=len(searchspace) # search space dimension # now let's create a population to test version A of the objective function implementation pA=Population(Individual,ps,FMsynthA,searchspace) pA.new_random_genes() pA.eval_all() print 'DNAs of pA:' print pA.get_DNAs() pA.print_stuff(slim=True) # and here the objective function version B and another population to test it problem_instance=FMsynthB() objfuncB=problem_instance.call pB=Population(Individual,ps,objfuncB,searchspace) pB.copy_otherpop(pA) pB.eval_all() print 'DNAs of pB:' print pB.get_DNAs() pB.print_stuff(slim=True)