def screen_observer(population, num_generations, num_evaluations, args): import numpy population = list(population) population.sort(reverse=True) worst_fit = population[-1].fitness best_fit = population[0].fitness med_fit = numpy.median([p.fitness for p in population]) avg_fit = numpy.mean([p.fitness for p in population]) std_fit = numpy.std([p.fitness for p in population], ddof=1) if(num_generations % 50 != 0): return print('Generation Evaluation Worst Best Median Average Std Dev ') print('---------- ---------- ---------- ---------- ---------- ---------- ----------') print('{0:10} {1:10} {2:10} {3:10} {4:10} {5:10} {6:10}\n'.format(num_generations, num_evaluations, worst_fit, best_fit, med_fit, avg_fit, std_fit)) print('Current Population: (id,fitness,todo,2 bit solution)') print('----------------------------------------------------------------------------') for i, c in enumerate(population): #f = CGPPhenotype(enc, c.candidate) #exp = f([Symbol('x'), RealNumber(1.0)]) pass for i, c in enumerate(population): #f = CGPPhenotype(enc, c.candidate) #exp = f([Symbol('x'), RealNumber(1.0)]) phe = SMCGPPhenotype(enc, c.candidate) #print i, c.fitness, phe.todo_list, phe print i, c.fitness, [p.__name__ for p in phe.todo_list], phe.get_modified()
def test_overwrite(self): gen1 = [input_gene(), input_gene(), mod_gene(mod.overwrite,[2,1],[1,1,2]), func_gene(3,[1,1]), func_gene(4,[2,1]), output_gene(1)] func = SMCGPPhenotype(self.enc, list(gen1)) modified = func.get_modified() assert len(modified.candidate) == len(func.candidate) assert func.candidate[3] == modified.candidate[3] assert func.candidate[4] == modified.candidate[4]
def fitness(candidates, args): sel_cases, sel_targets = select_test_cases(all_cases, all_targets, 100) fitness = [] for cand in candidates: fit = 0 func = SMCGPPhenotype(enc, cand) iii=0 for cases, targets in zip(sel_cases, sel_targets): iii += 1 func = func.get_modified() if not func.is_valid(): break case_fit = 0 for case, target in zip(cases, targets): solution = func(case)[0] if solution == target: #print solution, target, case case_fit += 1 fit += case_fit if case_fit != len(cases): break #print "cases", iii fitness.append(fit) return fitness
def test_dup(self): gen1 = [input_gene(), input_gene(), mod_gene(mod.duplicate,[2,1],[1,2,1]), func_gene(3,[1,1]), func_gene(4,[2,1]), output_gene(1)] func = SMCGPPhenotype(self.enc, list(gen1)) modified = func.get_modified() assert len(modified.candidate) == len(func.candidate) + 2 assert func.candidate[3:4] == modified.candidate[3:4] assert func.candidate[3:4] == modified.candidate[5:6]
def test_scale_connections(self): gen1 = [input_gene(), input_gene(), mod_gene(mod.scale_connections,[2,1],[1,2,3]), func_gene(3,[1,1]), func_gene(4,[2,1]), output_gene(1)] func = SMCGPPhenotype(self.enc, list(gen1)) modified = func.get_modified() assert len(modified.candidate) == len(func.candidate) assert func.candidate[3].inputs[0] * 3 == modified.candidate[3].inputs[0] assert func.candidate[3].inputs[1] * 3 == modified.candidate[3].inputs[1]
def test_change_functions(self): gen1 = [input_gene(), input_gene(),func_gene(3,[1,1]),func_gene(4,[1,1]), mod_gene(mod.change_connection,[2,1],[2,0,2]), mod_gene(mod.change_function,[2,1],[2,3,0]), mod_gene(mod.change_parameter,[2,1],[2,0,3]), output_gene(1)] func = SMCGPPhenotype(self.enc, list(gen1)) modified = func.get_modified() assert modified.candidate[2].function == modified.candidate[3].function assert modified.candidate[2].inputs[0] == 3 assert modified.candidate[2].vars[0] == 3 assert len(modified.candidate) == len(func.candidate)
def test_del_add(self): gen1 = [input_gene(), input_gene(), mod_gene(mod.add,[2,1],[3,3,0]), mod_gene(mod.delete,[2,1],[1,1,0]), func_gene(3,[1,2]), func_gene(4,[2,1]), output_gene(1)] func = SMCGPPhenotype(self.enc, list(gen1)) modified = func.get_modified() assert len(func.candidate) == 7 #print len(modified.candidate) assert len(modified.candidate) == 9 deleted = gen1[4] #print deleted #print modified.candidate assert deleted not in modified.candidate assert func.candidate[:3] == modified.candidate[:3] assert func.candidate[5:] == modified.candidate[7:]
def test_del(self): gen1 = [input_gene(), input_gene(), mod_gene(mod.delete,[2,1],[1,2,0]), func_gene(3,[1,1]), func_gene(4,[2,1]),func_gene(4,[2,1]), output_gene(1)] func = SMCGPPhenotype(self.enc, list(gen1)) modified = func.get_modified() assert len(func.candidate) == 7 assert len(modified.candidate) == 5 assert func.candidate[:3] == modified.candidate[:3] assert func.candidate[5:] == modified.candidate[3:] mod2 = modified.get_modified() print func.candidate print modified.candidate print mod2.candidate assert len(mod2.candidate) == 3; #no more to delete assert modified.candidate[:3] == mod2.candidate[:3]
def test_add(self): gen1 = [input_gene(), input_gene(), mod_gene(mod.add,[2,1],[1,2,0]), func_gene(3,[1,1]), func_gene(4,[2,1]), output_gene(1)] func = SMCGPPhenotype(self.enc, list(gen1)) modified = func.get_modified() assert len(func.candidate) == 6 assert len(modified.candidate) == 8 assert func.candidate[:3] == modified.candidate[:3] assert func.candidate[3:] == modified.candidate[5:] #print "mod", modified.candidate mod2 = modified.get_modified() #print "mod2", mod2.candidate #print "mod2",len(mod2.candidate) assert len(mod2.candidate) == 10 assert modified.candidate[:3] == mod2.candidate[:3] assert modified.candidate[3:] == mod2.candidate[5:]
def test_dup_scale(self): gen1 = [input_gene(), input_gene(), mod_gene(mod.duplicate_and_scale,[2,1],[1,2,3]), func_gene(3,[1,1]), func_gene(4,[2,1]), output_gene(1)] func = SMCGPPhenotype(self.enc, list(gen1)) modified = func.get_modified() assert len(modified.candidate) == len(func.candidate) + 2 assert func.candidate[3:4] == modified.candidate[3:4] assert func.candidate[3].function == modified.candidate[5].function assert func.candidate[3].vars[0] == modified.candidate[5].vars[0] assert func.candidate[3].vars[1] == modified.candidate[5].vars[1] assert func.candidate[3].inputs[0] * 3 == modified.candidate[5].inputs[0] assert func.candidate[3].inputs[1] * 3 == modified.candidate[5].inputs[1] assert func.candidate[4].function == modified.candidate[6].function assert func.candidate[4].vars[0] == modified.candidate[6].vars[0] assert func.candidate[4].vars[1] == modified.candidate[6].vars[1] assert func.candidate[4].inputs[0] * 3 == modified.candidate[6].inputs[0] assert func.candidate[4].inputs[1] * 3 == modified.candidate[6].inputs[1]