def test_breed(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() assert len(shark_update) == 1, 'Shark should have fed' breed_moved = grid._breed_and_move(fed_sharks=shark_update) assert len( breed_moved ) == 5, '4 fishes and one shark should have moved due to breeding' assert len(breed_moved) == (len(a_list) - 1) grid_df = grid.get_simulation_grid_data() assert len(grid_df[grid_df['animal_type'] == Animal.Shark]) == 2, 'Should be 2 Sharks' assert len(grid_df[grid_df['animal_type'] == Animal.Fish]) == 8, 'Should be 8 fishes'
def test_check_if_occupied(self): ''' Test the modified function ''' client = SimulationClient('sqlite:///:memory:') grid = SimulationGrid(persistence=client, simulation_parameters=sim_config_empty) a_list = [(Animal.Fish, SquareGridCoordinate(x=1, y=1)), (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) grid.animals = grid.get_simulation_grid_data() grid.occupied_coord = set( zip(grid.animals.coord_x, grid.animals.coord_y)) assert grid.check_if_occupied(SquareGridCoordinate( x=1, y=1)) == True, 'should be occupied' assert grid.check_if_occupied(SquareGridCoordinate( x=2, y=2)) == True, 'should be occupied' assert grid.check_if_occupied(SquareGridCoordinate( x=1, y=2)) == False, 'should be spare' # repeat the same for 1x1 case with initialization of 1 Fish del client, grid, a_list sim_config_empty_local = copy.deepcopy(sim_config_empty) sim_config_empty_local['init_nb_fish'] = 1 sim_config_empty_local['grid_size'] = 1 client = SimulationClient('sqlite:///:memory:') grid = SimulationGrid(persistence=client, simulation_parameters=sim_config_empty_local) assert grid.check_if_occupied(SquareGridCoordinate( x=0, y=0)) == True, 'should be occupied'
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)
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'