def test_make_reproduction_moves_false3_one_couple(self): yana_tiger = Animal("tiger", 5, "Yana", "female", 30) self.zoopark.animals.append(yana_tiger) gosho_tiger = Animal("panda", 4, "Gosho", "male", 30) self.zoopark.animals.append(gosho_tiger) self.zoopark.make_reproduction_moves() self.assertFalse(yana_tiger.is_pregnant)
def test_add_animal_same_name(self): gosho_tiger = Animal("tiger", 4, "Gosho", "male", 30) self.zoopark.add_animal(gosho_tiger) ivan_tiger = Animal("tiger", 4, "Gosho", "male", 30) self.zoopark.add_animal(ivan_tiger) self.assertEqual(len(self.zoopark.animals), 1) self.assertEqual(self.zoopark.animals[0], gosho_tiger)
def test_zoo_budget_update(self): ivan_tiger = Animal("tiger", 4, "Gosho", "male", 30) self.zoopark.animals.append(ivan_tiger) gosho_tiger = Animal("tiger", 4, "Gosho", "male", 30) self.zoopark.animals.append(gosho_tiger) self.zoopark._update_zoo_budget(2) self.assertEqual(self.zoopark.budget, 2981)
def breed_constant(self): """Iterates the entire Population to a new generation, calculating the number of offspring of each Animal with CONSTANT population size""" calc_payoff = np.vectorize(lambda x: x.lifetime_payoff(self._positions)) lifetime_payoff = calc_payoff(self._animals) mean_payoff = np.mean(lifetime_payoff) if (mean_payoff == 0): raise RuntimeError("Mean payoff of population decreased to 0. Check your parameters!") else: payoff_factor = lifetime_payoff/mean_payoff offspring = np.random.poisson(lam=payoff_factor) born_animals = np.repeat(self._animals,offspring) mutate_pop = np.vectorize(lambda x: Animal(x.mutate(),x.position)) new_animals = mutate_pop(born_animals) N = len(new_animals) if self._constants["verbose"]: print("\n\nAnimals per environment: {0}".format(self._positions)) print("Population size: {0}\tMean payoff: {1:.2f}".format(N,mean_payoff)) if (N > self._constants["population_size"]): new_animals = np.random.choice(new_animals,self._constants["population_size"]\ ,replace=False) elif (N < self._constants["population_size"]): clone_candidates = np.random.choice(new_animals,\ self._constants["population_size"] - N) clones = [Animal(x.genes,x.position) for x in clone_candidates] new_animals = np.append(new_animals,clones) self._animals = new_animals self._positions = self.positions()
def test_1(self): a1 = Animal(0, 0, 0, 5, [1, 2, 3, 4, 5, 6, 7, 8]) a2 = Animal(0, 0, 0, 5, [1, 2, 3, 4, 5, 6, 7, 8]) animal_list = [a1, a2] newWorld = World(10, 10, 2, 2, 1, 1) wmanager = WorldManager(newWorld, animal_list, 2, 0, 10) wmanager.Start()
def test_make_reproduction_moves_true_one_couple(self): yana_tiger = Animal("tiger", 8, "Yana", "female", 30) self.zoopark.animals.append(yana_tiger) gosho_tiger = Animal("tiger", 4, "Gosho", "male", 30) self.zoopark.animals.append(gosho_tiger) self.zoopark.make_reproduction_moves() self.assertTrue(yana_tiger.is_pregnant) self.assertEqual(yana_tiger.gestination_period, 0)
def test_get_age(self): animal_instance = Animal('Dog', 12) self.assertEquals(animal_instance.get_age(), 12) animal_instance = Animal('Dog', 12) self.assertEquals(animal_instance.get_age(), 12) animal_instance = Animal('Dog', 12) self.assertEquals(animal_instance.get_age(), 12)
def test_add_animal_full_zoo(self): self.zoopark.add_animal(self.puh_panda) gosho_tiger = Animal("tiger", 4, "Gosho", "male", 30) self.zoopark.add_animal(gosho_tiger) ivan_tiger = Animal("tiger", 4, "Ivan", "male", 30) self.zoopark.add_animal(ivan_tiger) self.assertEqual(len(self.zoopark.animals), 2) self.assertEqual(self.zoopark.animals[0], self.puh_panda) self.assertEqual(self.zoopark.animals[1], gosho_tiger)
def test_mange(): medor = Animal('Medor', 600) kiki = Animal('Kiki', 20) medor(kiki) # Médor mange Kiki assert medor.estVivant() assert not kiki.estVivant() assert kiki.masse == 0 assert medor.masse == 620 kiki = Animal("Kiki Jr.", 20) kiki(medor) # Kiki Jr. mange Médor assert not medor.estVivant() assert kiki.estVivant() assert kiki.masse == 22 assert medor.masse == 618 # Médor a perdu du poids en se faisant manger!
def test_grow(self): self.puh_panda.grow(5) self.assertEqual(self.puh_panda.age, 25) self.assertEqual(self.puh_panda.weight, 101.5) self.assertEqual(self.puh_panda.relax_period, 25) self.assertEqual(self.puh_panda.gestination_period, 0) yana_tiger = Animal("tiger", 5, "Yana", "female", 30) yana_tiger.is_pregnant = True yana_tiger.grow(5) self.assertEqual(yana_tiger.relax_period, 0) ivo_tiger = Animal("tiger", 5, "Ivo", "female", 101) ivo_tiger.grow(5) #cannot get bigger anymore, it is fat enough self.assertEqual(ivo_tiger.weight, 101)
def test_make_reproduction_moves_true_some_animals(self): yana_tiger = Animal("tiger", 8, "Yana", "female", 30) self.zoopark.animals.append(yana_tiger) gosho_tiger = Animal("tiger", 4, "Gosho", "male", 30) self.zoopark.animals.append(gosho_tiger) self.zoopark.animals.append(self.puh_panda) puha_panda_f = Animal("panda", 8, "Vanya", "female", 100) self.zoopark.animals.append(puha_panda_f) dif_animal = Animal("bear", 8, "Vasilka", "female", 100) self.zoopark.animals.append(dif_animal) self.zoopark.make_reproduction_moves() self.assertTrue(yana_tiger.is_pregnant) self.assertEqual(yana_tiger.gestination_period, 0) self.assertTrue(puha_panda_f.is_pregnant) self.assertEqual(puha_panda_f.gestination_period, 0)
def test_growing(self): self.zoopark.animals.append(self.puh_panda) yana_tiger = Animal("tiger", 8, "Yana", "female", 30) self.zoopark.animals.append(yana_tiger) self.zoopark.grow_animals(5) self.assertEqual(self.puh_panda.age, 8) self.assertEqual(yana_tiger.age, 13)
def __add_animal(self): print('\nInput animal\'s data:') self.__datasource.add_animal( Animal(kind=input('kind: '), token=input('token: '), sound=input('sound: '), owner_phone=input('owner_phone: ')))
def add_animal(self): identifier = random.randint(0, 1e9) while identifier in self.animal_dict: identifier = random.randint(0, 1e9) coordinates = self.map.get_random_coordinates() animal = Animal(coordinates, self) self.animal_dict[identifier] = animal
def breed_variable(self): """Iterates the entire Population to a new generation, calculating the number of offspring of each Animal with VARIABLE population size""" nE = len(self._constants["environments"]) calc_payoff = np.vectorize(lambda x: x.lifetime_payoff(self._positions)) lifetime_payoff = calc_payoff(self._animals) max_payoff = 1/self._constants["q"] #(1-1/nE)/self._constants["q"] payoff_factor = lifetime_payoff/max_payoff offspring = np.random.poisson(lam=payoff_factor) born_animals = np.repeat(self._animals,offspring) try: # check if all animals are dead yet born_animals[0] except IndexError: self._size = 0 return mutate_pop = np.vectorize(lambda x: Animal(x.mutate(),x.position)) new_animals = mutate_pop(born_animals) N = len(new_animals) if self._constants["verbose"]: print("\n\nAnimals per environment: {0}".format(self._positions)) print("Population size: {0}".format(N)) if (N > self._constants["population_size"]): new_animals = np.random.choice(new_animals,self._constants["population_size"]\ ,replace=False) self._animals = new_animals self._size = len(new_animals) self._positions = self.positions()
def set_up_turtle(level): name = [0 for i in range(4)] animals = [0 for i in range(4)] start_position = set_up_start_position(level) name[0], name[1], name[2], name[3] = get_name() choose = bet() color = random_color() animals[0] = Animal(name[0], color[0], start_position[4], start_position[0], 0, 2, 'turtle') animals[1] = Animal(name[1], color[1], start_position[4], start_position[1], 0, 2, 'turtle') animals[2] = Animal(name[2], color[2], start_position[4], start_position[2], 0, 2, 'turtle') animals[3] = Animal(name[3], color[3], start_position[4], start_position[3], 0, 2, 'turtle') return name, choose, animals
def breed(self): if self.empty_slots <= 0: return self.animals else: self.animals.append(Animal()) self.empty_slots -= 1 return self.empty_slots
def test_animals(self): animal = Animal(50,50) self.assertEqual(animal.eat(),49) self.assertEqual(animal.drink(),49) self.assertEqual(animal.play(),(50,50)) self.assertEqual(animal.get_hunger(),50) self.assertEqual(animal.get_thirst(),50)
def addAnimal(): try: print("Please type details of Animal: ") gender = input("Gender: ") birth = input("Birth: ") color = input("Color: ") print("\nPlease type Environment Conditions: \n") humidity = input("Relative Humidity: ") size = input("Enclosure Size (m2): ") temperature = input("Temperature: ") hours_of_light = input("Hours of light per day: ") flag = 0 while (flag == 0): animalNo = str(randint(1000, 9999)) animalList = appliaction.getAllAnimal() if len(animalList) != 0: for animal in animalList: if animalNo not in animal.animalNo: flag = 1 else: flag = 1 newAnimal = Animal() newAnimal.set(animalNo, gender, birth, color, humidity, size, temperature, hours_of_light) appliaction.addAnimalToList(newAnimal) print("\nA new animal added successfully\n") except: print("\nSome Error occured, try again.\n") main()
def __init__(self, space_width, space_height, animal_count, pred_count, genomes): self.pool = Pool() self.manager = Manager() self.width = space_width self.height = space_height self.genomes = genomes self.predators = [ Predator(random.randrange(0, self.width, 1), random.randrange(0, self.height, 1)) for _ in range(pred_count) ] self.animals = [ Animal(random.choice(genomes), PHEROMONES) for _ in range(animal_count) ] self.objects = [ Food(random.randrange(0, self.width, 1), random.randrange(0, self.height, 1), 10) for _ in range(animal_count) ] for a in self.animals: a.teleport(random.randrange(0, self.width, 1), random.randrange(0, self.height, 1), random.uniform(0, 6.28)) self.next_food = random.expovariate(1.0 / FOOD_PERIOD) self.next_poison = random.expovariate(1.0 / POISON_PERIOD) self.pheromones = self.manager.list([]) self.next_breed = random.expovariate(1.0 / BREEDING_PERIOD) self.new_genomes = []
def open_animal(self, path, tracknames=None): a = Animal(path) # init it just to parse its name exec_lines = ("try: %s; \n" "except NameError: %s = Animal(%r)\n" "%s.load(%r)" % (a.name, a.name, path, a.name, tracknames)) self.ipw.execute(exec_lines)
def _read_data(self): zoo_path = path.relpath("datasets/zoo.data") zoo_file = open(zoo_path) animals = [] for line in zoo_file: animals.append(Animal(line.strip())) return animals
def test_animal_walking(self): animal = Animal() animal.set_legs(12) animal.walk() expected = 1.2 result = animal.speed self.assertEqual(result, expected)
def test_set_legs(self): animal = Animal() animal.set_legs(12) animal.walk() speed = animal.speed # self.assertEqual(round(speed, 1), 1.2) self.assertEqual(speed, 1.2)
def restart(self): self.animals = [Animal(self) for _ in range(35)] self.animals_to_add = [] self.dead_animals = [] self.empty_food = [] self.food = [self._make_random_food() for _ in range(80)] self.time = 0
def test_remove_animal_no_such_animal(capsys): world = create_test_world() animal = Animal(world, 0, 0, 1) animal_not_present = Bunch() world.remove_animal(animal_not_present) assert world.animals == [animal] assert capsys.readouterr()[0] == 'Error: Cannot remove animal from World(size=10).\n'
def test_animal_can_walk(self): """ tests that a newly created animal object can walk and errors will be raised if needed """ test_animal = Animal() with self.assertRaises(ValueError): test_animal.walk() test_animal.set_legs(4) test_animal.walk() self.assertEqual(test_animal.speed, .4)
def test_actions_with_pregnant_one(self): yana_tiger = Animal("tiger", 8, "Yana", "female", 30) yana_tiger.is_pregnant = True self.zoopark.animals.append(yana_tiger) self.zoopark.actions_with_pregnant_one(5) self.assertEqual(yana_tiger.gestination_period, 5) self.assertEqual(len(self.zoopark.animals), 1) self.assertEqual(yana_tiger.relax_period, 8)
def __init__(self, resolution): print("starting up") pygame.init() self.screen = pygame.display.set_mode(resolution) self.title = "catdogdogcat" self.running = True self.paused = False self.highscore_screen = False self.game_loosed = False self.FPS = 30 self.playtime = 0.0 # Things for game difficulty self.gravity = 1 self.gravity_tick = 0.5 self.difficulty = 1 self.difficulty_frequency_sec = 10 self.difficulty_tick_time = 10 self.clock = pygame.time.Clock() # time in seconds when a new obstacle needs to be added to the game self.obstacle_tick_time = 0 self.obstacles = pygame.sprite.Group() # things that pop up by your head when you eat food self.score_bubbles = pygame.sprite.Group() # player score self.points = 0 self.MIN_SCORE = -9 self.move_ticker = 0 logo = pygame.image.load(path.join("assets", "logo32x32.png")) pygame.display.set_icon(logo) pygame.display.set_caption(self.title) self.background_image = \ pygame.image.load(path.join("assets", "background.png")).convert() self.animal = Animal(self.screen) self.fonts = { 'score': pygame.font.Font('assets/GochiHand-Regular.ttf', 50), 'title': pygame.font.Font('assets/GochiHand-Regular.ttf', 100), 'menu': pygame.font.Font('assets/GochiHand-Regular.ttf', 30) } # menu self.text_color = (0, 0, 0) self.menu_color = (0, 0, 255) self.menu_item_texts = ["Start", "Highscores", "Quit"] self.menu_items = [] self.selected_menu_item = 0
def insert_animal(self, genome): a = Animal(genome, PHEROMONES) a.teleport(random.randrange(0, self.width, 1), random.randrange(0, self.height, 1), random.uniform(0, 6.28)) self.animals.append(a) self.objects.append( Food(random.randrange(0, self.width, 1), random.randrange(0, self.height, 1), 10))