Example #1
0
 def test_position_functions(self):
     client = SimulationClient('sqlite:///:memory:')
     # init DB
     sid = client.init_simulation(**sim_config)
     # add a bunch of animals
     for t, c in animal_list:
         client.init_animal(sim_id=sid,
                            current_turn=0,
                            animal_type=t,
                            coordinate=c)
     # check animal in square
     coord = SquareGridCoordinate(x=1, y=3)
     animals_in_square = client.get_animal_in_position(sim_id=sid,
                                                       coordinate=coord)
     assert len(animals_in_square) == 1
     # kill that animal
     client.kill_animal(sim_id=sid, animal_ids=[animals_in_square[0].oid])
     animals_in_square = client.get_animal_in_position(sim_id=sid,
                                                       coordinate=coord)
     assert len(animals_in_square) == 0
     # adding a live one in coord
     client.init_animal(sim_id=sid,
                        current_turn=0,
                        animal_type=Animal.Fish,
                        coordinate=coord)
     animals_in_square = client.get_animal_in_position(sim_id=sid,
                                                       coordinate=coord,
                                                       live_only=False)
     assert len(animals_in_square) == 2
Example #2
0
 def test_topology_agnostic_eating(self):
     '''
     Check the topology-agnostic behavior, i.e. that the shark from one corner
     will eat the fish from the opposite corner
     '''
     # set fish breeding probability to 0
     sim_config_empty_local = copy.deepcopy(sim_config_empty)
     sim_config_empty_local['fish_breed_probability'] = 0
     client = SimulationClient('sqlite:///:memory:')
     grid = SimulationGrid(persistence=client,
                           simulation_parameters=sim_config_empty_local)
     a_list = [(Animal.Fish, SquareGridCoordinate(x=0, y=0)),
               (Animal.Shark, SquareGridCoordinate(x=9, y=9))]
     # adding some food and a shark
     for t, c in a_list:
         client.init_animal(sim_id=grid._sid,
                            current_turn=0,
                            animal_type=t,
                            coordinate=c)
     # updating grid turn
     grid._sim_turn = 6
     # AM: the latest two rows to allow optimized 'occupied coordinates' check
     grid.animals = grid.get_simulation_grid_data()
     grid.occupied_coord = set(
         zip(grid.animals.coord_x, grid.animals.coord_y))
     shark_update = grid._eat()
     # shark has eaten
     shark = grid._persistence.get_animals_by_type(
         sim_id=grid._sid, animal_type=Animal.Shark).iloc[0]
     # the shark is in update list
     assert len(
         shark_update) == 1, 'There should be one shark in update list'
     assert shark.last_fed == 6, 'Shark last fed value should have updated'
     assert shark_update[shark.oid] == SquareGridCoordinate(
         x=9, y=9), 'Shark previous coordinate in shark update'
     # Fish is dead
     animal_in_square = client.get_animal_in_position(
         sim_id=grid._sid,
         coordinate=SquareGridCoordinate(int(shark.coord_x),
                                         int(shark.coord_y)),
         live_only=False)
     assert len(
         animal_in_square) == 2, 'there shoud be two animals in that square'
     # check that there is no fish
     fish = grid._persistence.get_animals_by_type(sim_id=grid._sid,
                                                  animal_type=Animal.Fish)
     assert len(
         fish
     ) == 0, 'there should be no fish alive'  # there is no fish (since we put 0 breeding probability)
Example #3
0
 def test_eating(self):
     client = SimulationClient('sqlite:///:memory:')
     grid = SimulationGrid(persistence=client,
                           simulation_parameters=sim_config_empty)
     a_list = [(Animal.Fish, SquareGridCoordinate(x=1, y=1)),
               (Animal.Fish, SquareGridCoordinate(x=2, y=1)),
               (Animal.Fish, SquareGridCoordinate(x=3, y=1)),
               (Animal.Fish, SquareGridCoordinate(x=1, y=3)),
               (Animal.Fish, SquareGridCoordinate(x=3, y=2)),
               (Animal.Shark, SquareGridCoordinate(x=2, y=2))]
     # adding some food and a shark
     for t, c in a_list:
         client.init_animal(sim_id=grid._sid,
                            current_turn=0,
                            animal_type=t,
                            coordinate=c)
     # updating grid turn
     grid._sim_turn = 4
     # AM: the next two rows are to allow optimized 'occupied coordinates' check
     grid.animals = grid.get_simulation_grid_data()
     grid.occupied_coord = set(
         zip(grid.animals.coord_x, grid.animals.coord_y))
     shark_update = grid._eat()
     # shark has eaten
     shark = grid._persistence.get_animals_by_type(
         sim_id=grid._sid, animal_type=Animal.Shark).iloc[0]
     # the shark is in update list
     assert len(
         shark_update) == 1, 'There should be one shark in update list'
     assert shark.last_fed == 4, 'Shark last fed value should have updated'
     assert shark_update[shark.oid] == SquareGridCoordinate(
         x=2, y=2), 'Shark previous coordinate in shark update'
     # Fish is dead
     animal_in_square = client.get_animal_in_position(
         sim_id=grid._sid,
         coordinate=SquareGridCoordinate(int(shark.coord_x),
                                         int(shark.coord_y)),
         live_only=False)
     assert len(
         animal_in_square) == 2, 'there shoud be two animals in that square'
Example #4
0
from fish_bowl.dataio.persistence import SimulationClient, get_database_string
from fish_bowl.process.topology import SquareGridCoordinate
from fish_bowl.process.simple_display import display_simple_grid

client = SimulationClient(get_database_string())
print(display_simple_grid(client.get_animals_df(sim_id=1), 10))
coord_1 = SquareGridCoordinate(6, 8)
coord_2 = SquareGridCoordinate(7, 9)
print(
    client.get_animal_in_position(sim_id=1,
                                  coordinate=coord_1,
                                  live_only=False))
print(
    client.get_animal_in_position(sim_id=1,
                                  coordinate=coord_2,
                                  live_only=False))