Пример #1
0
    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)
Пример #2
0
    def test_(self):
        chemicals = Chemicals(size=(10, 10))
        nest = Nest(0, 0)

        chemicals.found[1, 1] = 1

        ant = Ant(nest=nest, chemicals=chemicals)

        self.assertTrue(ant.any_found_signal)
Пример #3
0
    def test_when_an_ant_encouters_food_pick_up_at_most_1_food(self):
        chemicals = Chemicals()
        ant = Ant(nest=Nest(0, 0), chemicals=chemicals)
        ant.x = 1
        ant.y = 1
        chemicals.food[1, 1] = 0.5

        ant.tick()

        self.assertEqual(ant.food, 0.5)
Пример #4
0
    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)
Пример #5
0
def build_ants(
    n_ants,
    size=SIZE,
    search_strategy=None,
    jump_distance=None,
    chemicals=None,
    nest=None,
):
    random.seed = 1
    ants = []
    if nest is None:
        nest = Nest(int(size[0]/2), int(size[1]/2))

    if chemicals is None:
        chemicals = Chemicals(size=size)

    for _ in range(n_ants):
        ant = Ant(
            nest=nest,
            chemicals=chemicals
        )
        if search_strategy:
            ant.search_strategy = search_strategy
        if jump_distance:
            ant.max_jump_distance=jump_distance
        ants.append(ant)

    return ants
Пример #6
0
    def test_ant_is_onre_step_away(self):

        ant = Ant(nest=Nest(0, 0), chemicals=Chemicals())

        ant.tick()

        self.assertIn(ant.distance_to_nest, (1, 2))
Пример #7
0
    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)
Пример #8
0
    def test_ants_should_avoid_existing_search_traces(self):
        ant = Ant(nest=Nest(0, 0), chemicals=Chemicals())
        ant.chemicals.search += 1.0
        ant.chemicals.search[N.x, N.y] = 0.0
        ant.last_direction = N
        ant.search_strategy = SearchWithAvoidance()

        ant.tick()

        self.assertEqual(N, ant.location)
Пример #9
0
    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)
Пример #10
0
    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,
        )
Пример #11
0
    def test_when_an_ant_has_food_has_arrives_home_he_drops_off_the_food_in_the_nest(
            self):
        chemicals = Chemicals()
        nest = Nest(0, 0)

        ant = Ant(nest=nest, chemicals=chemicals)
        ant.food = 1

        ant.tick()

        self.assertEqual(ant.food, 0.0)
        self.assertEqual(nest.food, 1.0)
        self.assertFalse(ant.has_food)
Пример #12
0
import random

import matplotlib.pyplot as plt

from core.ant import Chemicals, Nest
from experiments.image_search_strategies import build_ants, run, plot_chemicals

if __name__ == '__main__':
    random.seed = 1
    n_ants = 1000
    n_ticks = 1000
    size = (40, 40)

    chemicals = Chemicals(size=size)
    nest = Nest(3, 3)
    # chemicals.search[nest.x, nest.y] = 1
    # chemicals.food[6:16, 6:16] = 1
    chemicals.food[16, 16] = 1
    ants = build_ants(n_ants=n_ants, size=size, chemicals=chemicals, nest=nest)
    run(n_ticks, ants)

    fig, axes = plt.subplots(2, 2)

    plot_chemicals(ants, axes)

    axes[1, 1].plot(nest.food_history)
    plt.show()
    print('/n'.join(str(a.strategy) for a in ants))
Пример #13
0
    def test_ant_leaves_behind_search_chemical(self):

        ant = Ant(nest=Nest(0, 0), chemicals=Chemicals())
        ant.tick()

        self.assertAlmostEquals(0.975, ant.chemicals.search[0, 0], 3)
Пример #14
0
    def test_setting_a_chemical(self):
        chemicals = Chemicals()
        chemicals.search[9, 9] = 1.0

        self.assertEqual(chemicals.search[9, 9], 1.0)
Пример #15
0
    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))