def get_best_fitness_mean_std(data, cfg, is_plot=False): best_fitness = [] similarity_rep = [] best_rep = -1 best_fit = 9999999 for i in xrange(len(data)): d = data[i] gen_data_keys = sorted([int(x) for x in d.keys()]) gen_data_keys = [str(x) for x in gen_data_keys] last_k = gen_data_keys[-1] best_ind = min(d[last_k]['data'], key=lambda x: x['fitness']) c = chromosome.Chromosome(cfg).from_list(best_ind['chromosome']) fitness = evaluate(c, cfg) best_fitness.append(fitness) if fitness < best_fit: best_rep = i sim_gen = [] for j in gen_data_keys: gen_d = d[j]['data'] #print 'GEN_D', gen_d uniq = set() for x in gen_d: #print x strx = ','.join(map(str, x['chromosome'])) #print strx uniq.add(strx) size_total = len(gen_d) size_non_repeated = len(uniq) percent_repeated = ((size_total - size_non_repeated) * 100) / float(size_total) sim_gen.append(percent_repeated) similarity_rep.append(sim_gen) #print 'similarity_rep', similarity_rep #print 'best_fitness', best_fitness best_fitness = remove_outliers_with_mean(best_fitness) print 'best_fitness no outliers', best_fitness mean = np.mean(best_fitness) std = np.std(best_fitness) if is_plot: get_fitness_plot(data[best_rep]) get_similarity_plot(similarity_rep) total_best_ind = min(data[best_rep][last_k]['data'], key=lambda x: x['fitness']) best_c = chromosome.Chromosome(cfg).from_list(total_best_ind['chromosome']) evaluate(best_c, cfg, is_plot=True) return {'mean': mean, 'std': std, 'best':min(best_fitness)}
def test_invalid_init(self): chromo_values = [ .8, 3, .15, .15, 3, 7, #<< this is the invalid value .8, .95, .50, .25, .15, .1, .95, .3, 0, .3, .90, .8, .7, .3, .4, .5, 8 ] with self.assertRaises(ValueError): c = chromo.Chromosome(chromo_values) chromo_values = [ .8, 3, .15, .15, 3, 3, .8, .95, .50, .25, .15, .1, .95, .3, 0, .3, .90, .8, .7, .3, .4, .51, #<< now this is the invalid value 8 ] with self.assertRaises(ValueError): c = chromo.Chromosome(chromo_values)
def test_case_check_if_recombination_takes_place(self): """ Test 7 Test to verify if 1-point crossover takes place and genome is modified :return: """ r = [(2, 10), (2, 10), (2, 10), (5, 15), (3, 20)] c1 = C.Chromosome(N=5, type='int', range_val=r) c2 = C.Chromosome(N=5, type='int', range_val=r) c1_mod,c2_mod = recombination_1_point(c1,c2)
def test_case_bin_chromosome_unique_id_check(self): """ Test 4 - See if chromosome generates unique ID :return: None """ N=40 ch1 = C.Chromosome(N) ch2 = C.Chromosome(N) self.assertNotEquals(ch1.ID,ch2.ID)
def rand_init(inputdim, outputdim): global innov_ctr, z newchromo = chromosome.Chromosome(0) newchromo.node_ctr = inputdim + outputdim + 1 innov_ctr = 1 # Warning!! these two lines change(reset) global variables, here might be some error lisI = [ gene.Node(num_setter, 'I') for num_setter in range(1, newchromo.node_ctr - outputdim) ] lisO = [ gene.Node(num_setter, 'O') for num_setter in range(inputdim + 1, newchromo.node_ctr) ] newchromo.node_arr = lisI + lisO for inputt in lisI: for outputt in lisO: newchromo.conn_arr.append( gene.Conn(innov_ctr, (inputt, outputt), z, status=True)) z = z + 1 innov_ctr += 1 newchromo.bias_arr = [ gene.BiasConn(outputt, np.random.random()) for outputt in lisO ] newchromo.dob = 0 return newchromo
def dummy_popultation(number): #return list of chromosomes chromolis = [] for i in range(number): newchromo = chromosome.Chromosome(0) newchromo.rand_init() chromolis.append(newchromo) return chromolis
def best_gene(self): best_score = -1 best_gene = chromosome.Chromosome(7) for c in self.colony: if c.fitness_score >= best_score: best_gene = c return best_gene
def __init__(self,population,crossoverRate,mutationRate, numberOfGenes,geneInit = chromosome.geneFunction): self.population = population self.crossoverRate = crossoverRate self.mutationRate = mutationRate self.fitnesses = [] self.chromosomes = [chromosome.Chromosome(numberOfGenes,i,geneInit) for i in xrange(population)]
def test_to_array(self): chromo_values = [ .8, 3, .15, .15, 3, 3, .8, .95, .50, .25, .15, .1, .95, .3, 0, .3, .90, .8, .7, .3, .4, .5, 8 ] c = chromo.Chromosome(chromo_values) self.assertEqual(chromo_values, c.to_array())
def test_case_int_chromosome_range_wrong_tuple_format(self): """ Test 6 For an int chromosome - see if all the tuples in the range are in correct format :return: None """ range2 = [(2, 10), (2, 10), (10, 2), (5, 15), (3, 20)] c = C.Chromosome(N=5, type='int', name='int_pop', range_val=range2) self.assertEquals(c.genome, None)
def test_case_bin_chromosome_length_not_num(self): """ Test 3 - See if chromosome length matches input size :return: None """ N = "Nval" ch = C.Chromosome(N) N_assert = 0 self.assertEquals(ch.N, N_assert)
def test_case_bin_chromosome_length_not_valid(self): """ Test 2 - See chromosome state for invalid chromosome length :return: None """ N = -6 ch = C.Chromosome(N) N_assert = 0 self.assertEquals(ch.N, N_assert)
def one_point_recombination(pre_chromosome: c.Chromosome, post_chromosome: c.Chromosome) -> c.Chromosome: pre_genotype = list(''.join(pre_chromosome.genotype)) post_genotype = list(''.join(post_chromosome.genotype)) point_index = rd.randint(0, len(pre_genotype) - 1) pre_genotype[point_index:], post_genotype[point_index:] = \ post_genotype[point_index:], pre_genotype[point_index:] return c.Chromosome(pre_genotype)
def crossover(self, p1, p2): i1, i2 = random.sample(range(len(p1.order) - 1), 2) child_order = [-1] * len(p1.order) if i1 < i2: middle = p1.order[i1:i2] child_order[i1:i2] = middle non_middle = p1.order[:i1] + p1.order[i2:] p2_candidates = [] for j in range(len(p2.order)): if p2.order[j] not in middle: p2_candidates.append(p2.order[j]) k = 0 for j in range(0, i1): child_order[j] = p2_candidates[k] k += 1 for j in range(i2, len(p2.order)): child_order[j] = p2_candidates[k] k += 1 # k = 0 # for j in range(len(p2.order)): # if child_order[j] == -1: # child_order[j] = non_middle[k] # k+=1 # else: # print(k) # child_order[j] = non_middle[k] # k += 1 else: middle = p1.order[i2:i1] child_order[i2:i1] = middle non_middle = p1.order[:i2] + p1.order[i1:] p2_candidates = [] for j in range(len(p2.order)): if p2.order[j] not in middle: p2_candidates.append(p2.order[j]) k = 0 for j in range(0, i2): child_order[j] = p2_candidates[k] k += 1 for j in range(i1, len(p2.order)): child_order[j] = p2_candidates[k] k += 1 # print("parent1", p1.order) # print("parent2", p2.order) # print("i1, i2", i1, i2) # print("Crossover child", child_order) # crossover_point = random.randint(1, len(p1.order) - 1) # rand_int = random.randint(0, 1) # if rand_int == 1: # child_order = (p1.order[:crossover_point] + #here could be problem with synchronization, as node should be passed by reference and not by instance # p2.order[crossover_point:]) # else: # child_order = (p2.order[crossover_point:] + # p1.order[:crossover_point]) return ch.Chromosome(child_order, self.tests_info)
def __init__(self, population_size, chromosome_size): self.size = population_size self.total_fitness = 0 max_conflicts = chromosome_size * (chromosome_size - 1) / 2 bit_size = chromosome_size * (chromosome_size.bit_length() - 1) self.chromosomes = [ c.Chromosome(None, chromosome_size, bit_size, max_conflicts) for i in range(population_size) ]
def __init__(self,inputdim,outputdim, max_hidden_units, size=50, limittup=(-1, 1)): self.size = size self.max_hidden_units = max_hidden_units self.list_chromo = [chromosome.Chromosome(inputdim,outputdim) for i in range(self.size)] self.objective_arr = None
def generate_chromosome(self): first = [] second = [] for test in self.tests: if (self.tests_info[test][0]): second.append(test) else: first.append(test) random.shuffle(first) random.shuffle(second) ret = first + second return ch.Chromosome(ret, self.tests_info)
def _generate_chromosome(self, fname): ''' It parses the current filename, and then append the genes that found in a chromosome and return it. ''' chromo = chromosome.Chromosome(serializer=self.serializer, deserializer=self.deserializer) if fname != None: print '[!] Parsing: %s (%s)' % (fname, chromo.uid) self.campaign.log('Parsing: %s (%s)' % (fname, chromo.uid)) chromo.deserialize(fname) return chromo
def test_case_bin_chromosome_length_positive(self): """ Test 1 - See if chromosome length matches input size :return: None """ N = 50 ch = C.Chromosome(N) N_assert = 50 len_assert = 50 self.assertEquals(ch.N,N_assert) self.assertEquals(len(ch.genome),len_assert)
def crossover(self, champions, target): children = list() combs = list(itertools.combinations(champions, 2)) for parents in random.sample(combs, target): cross_point = random.choice(range(len(parents[0].chrom.genome))) recombined = chromosome.Chromosome( attributes=parents[0].chrom.attributes, genome=parents[0].chrom.genome[:cross_point] + parents[1].chrom.genome[cross_point:]) children.append( champion.DynamicChampion(recombined, make_folder=self.make_folder)) return children
def test_valid_init(self): chromo_values = [ .8, 3, .15, .15, 3, 3, .8, .95, .50, .25, .15, .1, .95, .3, 0, .3, .90, .8, .7, .3, .4, .5, 8 ] c = chromo.Chromosome(chromo_values) self.assertEqual(23, c.size) #self.assertEqual(len(chromo_values), len(c._genes)) for i in range(0, len(chromo.Chromosome.GENE_NAMES)): self.assertEqual(chromo_values[i], c._genes[chromo.Chromosome.GENE_NAMES[i]].value, chromo.Chromosome.GENE_NAMES[i])
def test_to_file_string(self): chromo_values = [ .8, 3, .15, .15, 3, 3, .8, .95, .50, .25, .15, .1, .95, .3, 0, .3, .90, .8, .7, .3, .4, .5, 8 ] expected_string = '0 0.8\n1 3\n2 0.15\n3 0.15\n4 3\n5 3\n6 0.8\n7 0.95\n8 0.5\n9 0.25\n' \ '10 0.15\n11 0.1\n12 0.95\n13 0.3\n14 0\n15 0.3\n16 0.9\n17 0.8\n18 0.7\n19 0.3\n20 0.4' \ '\n21 0.5\n22 8\n' c = chromo.Chromosome(chromo_values) self.assertEqual(23, c.size) self.assertEqual(expected_string, c.to_file_string())
def test_case_int_chromosome_range_restrict(self): """ Test 5 For an int chromosome - see if all the integer values fall into the range specified :return: None """ range2 = [(2, 10), (2, 10), (2, 10), (5, 15), (3, 20)] c = C.Chromosome(N=5, type='int', name='int_pop', range_val=range2) result = True for i in range(0,c.N): if c.genome[i]>=c.range[i][0] and c.genome[i]<=c.range[i][1]: result = True else: result=False break self.assertEquals(result, True)
def test_from_array(self): ''' Assumes that test_to_array passes :return: ''' c = chromo.Chromosome() #creates random chromosome chromo_values = [ .8, 3, .15, .15, 3, 3, .8, .95, .50, .25, .15, .1, .95, .3, 0, .3, .90, .8, .7, .3, .4, .5, 8 ] self.assertNotEqual(chromo_values, c.to_array()) #fills chromosome with known values c = chromo.Chromosome.from_array(chromo_values) self.assertEqual(chromo_values, c.to_array())
def two_point_recombination(pre_chromosome: c.Chromosome, post_chromosome: c.Chromosome) -> c.Chromosome: """ :param pre_chromosome: :param post_chromosome: :return: """ pre_genotype = list(''.join(pre_chromosome.genotype)) post_genotype = list(''.join(post_chromosome.genotype)) pre_point_index = rd.randint(0, len(pre_genotype) - 2) post_point_index = rd.randint(pre_point_index + 1, len(pre_genotype) - 1) pre_genotype[pre_point_index: post_point_index], post_genotype[pre_point_index: post_point_index] = \ post_genotype[pre_point_index:post_point_index], pre_genotype[pre_point_index: post_point_index] return c.Chromosome(pre_genotype)
def gene_recombination(pre_chromosome: c.Chromosome, post_chromosome: c.Chromosome) -> c.Chromosome: """ :param pre_chromosome: :param post_chromosome: :return: """ pre_genotype = list(''.join(pre_chromosome.genotype)) post_genotype = list(''.join(post_chromosome.genotype)) gene_index = rd.randint(0, Parameter.num_of_genes - 1) point_index = gene_index * Parameter.num_of_genes pre_genotype[point_index:], post_genotype[point_index:] = \ post_genotype[point_index:], pre_genotype[point_index:] return c.Chromosome(pre_genotype)
def convert_to_chromosome(self,inputdim,outputdim,dob): #newchromo.reset_chromo_to_zero() # very important function, without it duplicate connections will be created dicW=self.WMatrix dicC=self.CMatrix #print([tup for tup in self.couple_map.items()][:3]) for key in dicW.keys(): key_tup = split_key(key) m,n = dicW[key].shape for row in range(m): for col in range(n): if dicW[key][row][col]: conn_here = None node1 = self.node_map[key_tup[0]][row] node2 = self.node_map[key_tup[1]][col] couple = (node1, node2) if couple in self.couple_to_conn_map: conn_here = self.couple_to_conn_map[couple] else: print(row, col, key, "key_tup", key_tup) print("key error h yaar") #print ("hihihihi") weight = dicW[key][row][col] conn_here.weight = weight newchromo = chromosome.Chromosome(inputdim,outputdim) newchromo.reset_chromo_to_zero() newchromo.__setattr__('conn_arr', self.conn_lis) newchromo.__setattr__('bias_conn_arr', self.Bias_conn_arr) newchromo.__setattr__('node_arr', self.node_lis) newchromo.__setattr__('dob', dob) newchromo.set_node_ctr() return newchromo
def __init__(self): self.probability = list() self.max_fitness = 0 self.max_fitness_chromosome = None self.population = list() self.max_fitness_index = None self.sum_of_fitness = 0 self.repeat_times = 0 while True: self.population.clear() for i in range(Parameter.population_size): c = chromosome.Chromosome() c.fitness = self.compute_chromosome_fitness( self.compute_chromosome_value(c), i) self.population.append(c) self.compute_max_fitness() if self.max_fitness != 0: break # print('Num of generation: 1') self.compute_probability()
def test_mutation(self): #TODO: update this test chromo1 = chromo.Chromosome([ .9, 2, .20, .60, 4, 4, .9, .7, .4, .2, .1, .3, .7, .4, .3, .4, .8, .7, .6, .4, .5, .6, 20 ]) p1 = {'chromosome': chromo1} #patches methods from random random.random = self.rand_for_mutation #problem: with rand_for_mutation we expect 2nd gene to be mutated, but mutation uses a different gene ordering random.choice = self.choice_for_mutation geneticoperators.mutation(p1, 0.5) p1_values = p1['chromosome'].to_array() self.assertEqual(p1_values, [ .9, 2, .20, .60, 4, 4, .9, .7, .4, .2, .1, .3, .7, .4, .3, .999, .8, .7, .6, .4, .5, .6, 20 ])
def test_random_init(self): c = chromo.Chromosome() self.assertEqual(23, c.size) values = [] for i in range(1, 100): #tests the domain of the genes for g in c._genes.values(): if g.name == 's_build_barracks_denominator': values = [1, 2, 3, 4, 5] elif g.name == 's_train_scv_denominator': values = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0] elif g.name == 's_train_medic_ratio': values = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0] elif g.name == 'm_pack_size': values = range(6, 24, 2) else: values = domain.STANDARD_INTERVAL self.assertTrue(g.value in values)