#import matplotlib.pyplot as plt 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)
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
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
def call(self, DNA): 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)
ps=40 # population size G=41 # number of generations to go through dim=len(searchspace) parents=Population(Individual,ps,rastrigin,searchspace) offspring=Population(Individual,ps,rastrigin,searchspace) rw=RouletteWheel(dim) #------------------------------------------------------------------------------- #--- part 2: random starting point for search ---------------------------------- #--- and more initialisation --------------------------------------------------- #------------------------------------------------------------------------------- npr.seed(3) # seed differently or comment out if you want to see an algorithm's average performance over several runs parents.new_random_genes() parents.eval_all() parents.sort() print 'initial population:\nbest score = ',parents[0].score # change the plot interval to a low value, e.g. 1, if you want to see # the different characters of change patterns of the current best solution #ginter=G/5 # interval to take a snapshot of best solution (for plot variant 2) ginter=1 # interval to take a snapshot of best solution (for plot variants 1 and 3) glist=[]; bestDNAs=[]; bestscores=[] #------------------------------------------------------------------------------- #--- part 3: the GA generational loop ------------------------------------------ #--- step 1: generate offspring via CO-operator -------------------------------- #--- step 2: mutate offspring with rather small probability --------------------
""" # 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)
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)