def test_ant_moves_home_if_it_has_food(self): ant = Ant(nest=Nest(0, 0), chemicals=Chemicals()) ant.food = 1 ant.last_direction = NW ant.location = Location(10, 10) ant.chemicals.search[9, 9] = 1.0 ant.tick() self.assertEqual( Location(9, 9), ant.location, )
def test_if_returning_and_all_search_directions_are_the_same_move_away_from_found( self): chemicals = Chemicals() nest = Nest(0, 0) ant = Ant(nest=nest, chemicals=chemicals) ant.food = 1 ant.last_direction = NW ant.location = Location(10, 10) chemicals.found[:, :] = 1 chemicals.found[9, 9] = 0 ant.tick() self.assertEqual(Location(9, 9), ant.location)
def test_when_an_ant_encouters_food_pick_up_food(self): chemicals = Chemicals() ant = Ant(nest=Nest(0, 0), chemicals=chemicals) ant.location = Location(1, 1) chemicals.food[1, 1] = 1 ant.tick() self.assertTrue(ant.has_food)
def test_ant_leaves_behind_found_chemical(self): ant = Ant(nest=Nest(0, 0), chemicals=Chemicals()) ant.location = Location(10, 10) ant.food = 1 ant.tick() self.assertEqual(ant.chemicals.search[10, 10], 0.0) self.assertAlmostEquals(0.975, ant.chemicals.found[10, 10], 3)
def test_ants_can_all_get_home(self): chemicals = Chemicals(size=(10, 10)) nest = Nest(0, 0) chemicals.search[0, 0] = 3 chemicals.search[1, 1] = 2 chemicals.search[2, 2] = 1 ant_1 = Ant(nest=nest, chemicals=chemicals) ant_1.food = 1 ant_1.last_direction = NW ant_1.location = Location(3, 3) for _ in range(3): ant_1.tick() self.assertIs(return_state, ant_1.strategy) self.assertEqual(Location(0, 0), ant_1.location) ant_2 = Ant(nest=nest, chemicals=chemicals) ant_2.food = 1 ant_2.last_direction = NW ant_2.location = Location(3, 3) for _ in range(3): ant_1.tick() ant_2.tick() self.assertIs(return_state, ant_2.strategy) self.assertIs(gather_state, ant_1.strategy) self.assertEqual(Location(0, 0), ant_2.location) ant_3 = Ant(nest=nest, chemicals=chemicals) ant_3.food = 1 ant_3.last_direction = NW ant_3.location = Location(3, 3) for _ in range(3): ant_1.tick() ant_2.tick() ant_3.tick() self.assertEqual(Location(0, 0), ant_3.location)
search_state_with_avoidance, search_with_avoidance_and_jumps, Nest, Location import matplotlib.pyplot as plt import numpy as np from experiments.food import plot_chemicals from experiments.image_search_strategies import build_ants, run, plot_search search_strategies = [ search_state_no_advoidance, search_state_with_avoidance, search_with_avoidance_and_jumps ] WIDTH = 20 HALF_WIDTH = int(WIDTH / 2) SIZE = (WIDTH, WIDTH) n_ants = 1 n_ticks = 20 if __name__ == '__main__': fig, axes = plt.subplots(2, 2) ants = build_ants(n_ants, size=SIZE) first_ant = ants[0] first_ant.location = Location(15, 15) first_ant.food = 1 run(n_ticks, ants) plot_chemicals(ants, axes) plt.show()
def test_new_ant(self): ant = Ant(nest=Nest(0, 0), chemicals=Chemicals()) self.assertEqual(ant.nest, Location(0, 0)) self.assertEqual(ant.location, Location(0, 0))