def ga(maxPop, mutation_rate, chromosome_length, maxGen): pop = Population(maxPop, mutation_rate, chromosome_length=chromosome_length) # populating with maxPop elements pop.populate() i = 0 for i in range(maxGen): # calculating fitness pop.calc_fitness() # natural selection parents = pop.selection() # crossover + mutation pop.crossover(parents) pop.mutate() # printing print('Gen ' + str(i) + ": ", pop.getFittest()[0], pop.getAvgFitness()) pop.population[pop.getFittest()[1]].plot() pop.population[pop.getFittest()[1]].plot()
initial_fitess_values = map(population.fitness_function, population) #print initial_fitess_values number_generations = 100 population.sus_sampler(initial_fitess_values) max_list = [] mean_list = [] for i in xrange(number_generations): #print "initial population" #for i in population: # print i.rep #print "before crossover" population.crossover() #print "after crossover" #for i in population: # print i.rep #print "before mutation" population.mutation() #for i in population: # print i.rep #print "after mutation" fitness_vals = map(population.fitness_function, population) population.sus_sampler(fitness_vals) mean_val = np.mean(fitness_vals) max_val = max(fitness_vals) print str(mean_val) + "," + str(max_val) mean_list.append(mean_val) max_list.append(max_val)
# print i.transform_individual(signal_ranges) initial_fitess_values = map(population.fitness_function, population) #print initial_fitess_values number_generations = 100 population.sus_sampler(initial_fitess_values) max_list = [] mean_list = [] for i in xrange(number_generations): #print "initial population" #for i in population: # print i.rep #print "before crossover" population.crossover() #print "after crossover" #for i in population: # print i.rep #print "before mutation" population.mutation() #for i in population: # print i.rep #print "after mutation" fitness_vals = map(population.fitness_function, population) population.sus_sampler(fitness_vals) mean_val = np.mean(fitness_vals) max_val = max(fitness_vals) print str(mean_val) + "," + str(max_val) mean_list.append(mean_val) max_list.append(max_val)