def breed_example_with_ga(self, population): """ application of a basic genetic algorithm for breeding """ population_cpy = copy(population) dead = [] alive = [] for individual in population_cpy: if individual.dead: dead.append(individual) else: alive.append(individual) for _ in range(len(dead)): # get the position where the child should be inserted on the field where = choice(alive)._position color = alive[0].color selected = self.select_example(population_cpy) parent1 = selected[0] parent2 = selected[1] child1, child2 = self.crossover_example(copy(parent1), copy(parent2)) child1 = self.tweak_example(child1) child2 = self.tweak_example(child2) score_child1 = self.assess_individual_fitness_example(child1) score_child2 = self.assess_individual_fitness_example(child2) if score_child1 > score_child2: new_individual = Dot(self.parent, color=color, position=where, dna=child1.get_dna()) else: new_individual = Dot(self.parent, color=color, position=where, dna=child2.get_dna()) population_cpy.append(new_individual) for dead_individual in dead: population_cpy.remove(dead_individual) return population_cpy
def init_defender(self, where, color): ''' initialize defender individual ''' new_individual = Dot(self.parent, color=color, position = where) dna = new_individual.get_dna() dna = self.create_defender_dna(dna) new_individual.dna_to_traits(dna) return new_individual
def breed_copy_dead_example(self, population): """ example breeding function simply copy dead individuals traits to a new individual """ population_cpy = copy(population) dead = [] alive = [] for individual in population_cpy: if individual.dead: dead.append(individual) else: alive.append(individual) if len(alive) == 0: print("END OF BREED") return None for _ in range(len(dead)): dead_individual = choice(dead) alive_individual = choice(alive) new_individual = Dot(self.parent, color=dead_individual.color, position=alive_individual._position, dna=dead_individual.get_dna()) population_cpy.append(new_individual) for dead_individual in dead: population_cpy.remove(dead_individual) return population_cpy
def initialize_population_example(self, num_individuals, color): """ example initializer creates individuals with random traits """ population = [] for _ in range(num_individuals): population.append(Dot(self.parent, color=color)) return population
def initialize_population_function(self, num_individuals, color): """ example initializer creates individuals with random traits """ population = [] for _ in range(num_individuals): population.append( Dot(self.parent, color=color) ) #Here I could just create random dna for num_individuals return population
def assign_value(self, a, b, c, color): x = Dot(self.parent, color=color) x.perception.predator = a[0] x.perception.poison = a[1] x.perception.health_potion = a[2] x.perception.opponent = a[3] # x.perception.food = a[4] x.perception.corpse = a[5] x.abilities.armor_ability = b[0] x.abilities.speed = b[1] x.abilities.strength = b[2] x.abilities.poison_resistance = b[3] x.abilities.toxicity = b[4] x.desires.dodge_predators = c[0] x.desires.dodge_poison = c[1] x.desires.seek_potion = c[2] x.desires.seek_opponents = c[3] x.desires.seek_food = c[4] x.desires.seek_corpse = c[5] return x
def initialize_population_min_diversity(self, num_individuals, color): """ There will be the set number of individuals. Either the individuals are initialized with a bias or with a diversity measurement. initializer that allows only individuals with a certain diversity. according to (Luke, 2013) Page 32 """ population = [] for _ in range(0,self.attacker_number): aggresive_individual = Dot(self.parent, color=color) dna = aggresive_individual.get_dna() dna = self.create_attacker_dna(dna) aggresive_individual.dna_to_traits(dna) population.append(aggresive_individual) if (self.defender_random_init): while (len(population) < num_individuals): individual = Dot(self.parent, color=color) if self.check_diversity_population(population, individual): population.append(individual) else: for _ in range(0,num_individuals-self.attacker_number): defensive_individual = Dot(self.parent, color=color) dna = defensive_individual.get_dna() dna = self.create_defender_dna(dna) defensive_individual.dna_to_traits(dna) population.append(aggresive_individual) print("Davidson's ready to conquer in", population[0].color[1], "!") return population
def breed_depending_on_profession(self, population): """ Depending on the initially set number of profession new individuals will be added to our population. The parameters can be found in the beginning of the class. attackers breed new attackers only with attackers. defenders can be breeded by all. we will take both breeded individuals not only one, so no score is evaluated. If only one can be chosen its just the first one, this is random since the parents have been chosen randomly before. """ if self.adoptive_strategy: self.update_strategy(population) population_cpy = copy(population) dead = [] alive = [] for individual in population_cpy: if individual.dead: dead.append(individual) else: alive.append(individual) all_attacker = self.create_attacker_population(population_cpy) all_defender = self.create_defender_population(population_cpy) self.count_professions_in_population(alive) if (self.intermediate_output): print("The survivers of the last round:") self.print_professions() needed_individuals = len(dead) index = 0 while index < needed_individuals: # get the position where the child should be inserted on the field where = choice(alive)._position color = alive[0].color # get the desired number of attackers and defenders if self.profession["Attacker"] < self.attacker_number: selected = self.select_individual(all_attacker) profession_flag = True else: selected = self.select_individual(population_cpy) profession_flag = False parent1 = selected[0] parent2 = selected[1] child1, child2 = self.crossover_example(copy(parent1), copy(parent2)) child1 = self.tweak_depending_on_profession(child1) child2 = self.tweak_depending_on_profession(child2) #score_child1 = self.assess_individual_fitness(child1) #score_child2 = self.assess_individual_fitness(child2) if needed_individuals - index > 2: new_individual = Dot(self.parent, color=color, position=where, dna=child1.get_dna()) population_cpy.append(new_individual) index += 1 if profession_flag: self.profession["Attacker"] += 1 else: self.profession["Defender"] +=1 new_individual = Dot(self.parent, color=color, position=where, dna=child2.get_dna()) else: new_individual = Dot(self.parent, color=color, position=where, dna=child1.get_dna()) population_cpy.append(new_individual) index += 1 if profession_flag: self.profession["Attacker"] += 1 else: self.profession["Defender"] +=1 for dead_individual in dead: population_cpy.remove(dead_individual) if (self.intermediate_output): print("Davidson's with ", len(population_cpy), " individuals, ready for the next round!") self.print_professions() return population_cpy
class Breeder: def __init__(self, parent): self.parent = parent def breed(self, population): return self.breed_function(population) def initialize_population(self, num_individuals, color): population = [] for i in range(num_individuals): greater_than = 0.5 less_than = 0.7 centric_a = round(uniform(greater_than, less_than), 2) centric_b = round(uniform(greater_than, less_than), 2) centric_c = round(uniform(greater_than, less_than), 2) a = np.random.dirichlet(np.ones(5), size=1)[0] * (1-centric_a)#perception b = np.random.dirichlet(np.ones(4), size=1)[0] * (1-centric_b)#ability c = np.random.dirichlet(np.ones(5), size=1)[0] * (1-centric_c)#desire if(i%5==0): a = np.insert(a, 4, centric_a) b = np.insert(b, 4, centric_b) c = np.insert(c, 0, centric_c) elif(i%5==1): a = np.insert(a, 5, centric_a) b = np.insert(b, 0, centric_b) c = np.insert(c, 1, centric_c) elif(i%5==2): a = np.insert(a, 0, centric_a) b = np.insert(b, 1, centric_b) c = np.insert(c, 2, centric_c) elif(i%5==3): a = np.insert(a, 1, centric_a) b = np.insert(b, 2, centric_b) c = np.insert(c, 4, centric_c) elif(i%5==4): a = np.insert(a, 2, centric_a) b = np.insert(b, 3, centric_b) c = np.insert(c, 5, centric_c) else: a = np.random.dirichlet(np.ones(5), size=1)[0] b = np.random.dirichlet(np.ones(5), size=1)[0] c = np.random.dirichlet(np.ones(5), size=1)[0] population.append(self.assign_value(a, b, c, color=(0,139,139))) return population def assign_value(self, a, b, c, color): x = Dot(self.parent, color=color) x.perception.predator = a[0] x.perception.poison = a[1] x.perception.health_potion = a[2] x.perception.opponent = a[3] # x.perception.food = a[4] x.perception.corpse = a[5] x.abilities.armor_ability = b[0] x.abilities.speed = b[1] x.abilities.strength = b[2] x.abilities.poison_resistance = b[3] x.abilities.toxicity = b[4] x.desires.dodge_predators = c[0] x.desires.dodge_poison = c[1] x.desires.seek_potion = c[2] x.desires.seek_opponents = c[3] x.desires.seek_food = c[4] x.desires.seek_corpse = c[5] return x def breed_function(self, population): """ breed function with our created crossover, tweak and mutation """ population_cpy = copy(population) dead = [] alive = [] for individual in population_cpy: if individual.dead: dead.append(individual) else: alive.append(individual) for _ in range(len(dead)): where = choice(alive)._position color = alive[0].color selected = self.select(population_cpy) parent1 = selected[0] parent2 = selected[1] child1, child2 = self.crossover(copy(parent1), copy(parent2)) child1 = self.tweak(child1) child2 = self.tweak(child2) score_child1 = self.assess_individual_fitness(child1) score_child2 = self.assess_individual_fitness(child2) if score_child1 > score_child2: new_individual = Dot(self.parent, color=color, position=where, dna=child1.get_dna()) print(score_child1) else: new_individual = Dot(self.parent, color=color, position=where, dna=child2.get_dna()) population_cpy.append(new_individual) for dead_individual in dead: population_cpy.remove(dead_individual) return population_cpy
def breed(self, population): """ this function gets called by the EGame on one population it gets a population consisting of individuals each individual has certain statistics and traits """ print("here") population_cpy = copy(population) dead = [] alive = [] for individual in population_cpy: if individual.dead: dead.append(individual) else: alive.append(individual) for _ in range(len(dead)): # get the position where the child should be inserted on the field where = choice(alive)._position color = alive[0].color selected = self.select(population_cpy) parent1 = selected[0] best = parent1 best_score = self.assess_individual_fitness(parent1) print("parent 1 ", best_score) parent2 = selected[1] score_parent2 = self.assess_individual_fitness(parent1) print("parent 1 ", score_parent2) if(score_parent2>best_score): best = parent2 best_score = score_parent2 child1, child2 = self.recombine(copy(parent1), copy(parent2)) # child1, child2 = self.crossover_example(copy(parent1), copy(parent2)) # child1 = self.tweak_example(child1) # child2 = self.tweak_example(child2) tweak_prob = random.random() if(tweak_prob < 0.1): child1 = self.tweak_random(child1) child2 = self.tweak_random(child2) elif(tweak_prob < 0.2): child1 = self.tweak_default(child1) child2 = self.tweak_default(child2) score_child1 = self.assess_individual_fitness(child1) if(score_child1>best_score): best = child1 best_score = score_child1 print("child 1 ", score_child1) score_child2 = self.assess_individual_fitness(child2) if(score_child2>best_score): best = child2 best_score = score_child2 print("child 2 ", score_child2) new_individual = Dot(self.parent, color=color, position=where, dna=best.get_dna()) population_cpy.append(new_individual) for dead_individual in dead: population_cpy.remove(dead_individual) return population_cpy