Exemplo n.º 1
0
 def test_animal_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)
     # retrieve animals
     animals_df = client.get_animals_df(sim_id=1)
     assert isinstance(animals_df, pd.DataFrame), 'results is a DataFrame'
     assert len(animals_df) == 8, 'should be only 9 animals'
     # update animals
     # create update dict
     update_dict = {
         oid: {
             'breed_count': 1,
             'last_breed': 5,
             'last_fed': 4
         }
         for oid in animals_df.oid.values
     }
     client.update_animals(sim_id=sid, update_dict=update_dict)
     animals_df = client.get_animals_df(sim_id=1)
     assert animals_df.breed_count.unique(
     )[0] == 1, 'Breed count should be one now'
     client.update_animals(sim_id=sid, update_dict={1: {'alive': False}})
     # this update should not work
     animal = client.get_animal(sim_id=sid, animal_id=1)
     assert animal.alive, 'Animal should still be alive'
     # but this should work
     client.kill_animal(sim_id=sid, animal_ids=[1, 2, 3])
     animal = client.get_animal(sim_id=sid, animal_id=1)
     assert not animal.alive, 'This time alive was updates'
     # a dead animal does not occupy a square
     assert not client.coordinate_is_occupied(
         sim_id=sid, coordinate=SquareGridCoordinate(1, 3))
     # test get animal by types
     shark_df = client.get_animals_by_type(sim_id=sid,
                                           animal_type=Animal.Shark)
     assert shark_df.animal_type.unique()[0] == Animal.Shark
Exemplo n.º 2
0
sim_config_empty = {
    'grid_size': 10,
    'init_nb_fish': 0,
    'fish_breed_maturity': 3,
    'fish_breed_probability': 80,
    'fish_speed': 2,
    'init_nb_shark': 0,
    'shark_breed_maturity': 5,
    'shark_breed_probability': 100,
    'shark_speed': 4,
    'shark_starving': 4}

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d:%(message)s")
    # save_simulation_config(sim_config, 'simulation_config_1', overwrite=True)
    # sim_config = read_simulation_config('simulation_config_2')
    client = SimulationClient(get_database_string())
    # sid = client.init_simulation(**sim_config)
    grid = SimulationGrid(persistence=client, simulation_parameters=sim_config)
    # for t, c in a_list:
    #     client.init_animal(sim_id=grid._sid, current_turn=0, animal_type=t, coordinate=c)

    print(display_simple_grid(client.get_animals_df(grid._sid), grid_size=sim_config['grid_size']))
    for turn in range(100):
        _logger.info('Turn: {}'.format(grid._sim_turn))
        grid.play_turn()
        print(display_simple_grid(grid.get_simulation_grid_data(), grid.get_simulation_parameters().grid_size))
        # _ = input('press key')

Exemplo n.º 3
0
                         help="""
                         Configuration file path. If specified, configuration file will be loaded from this path
                         """)
 args = cmd_parser.parse_args()
 if args.config_path is not None:
     raise NotImplementedError(
         'Code for directing to an alternative configuration'
         ' repository has not been implemented yet')
 # Load simulation configuration
 sim_config = read_simulation_config(args.config_name)
 # Instantiate client
 client = SimulationClient(get_database_string())
 # display initial grid
 grid = SimulationGrid(persistence=client, simulation_parameters=sim_config)
 print(
     display_simple_grid(client.get_animals_df(grid._sid),
                         grid_size=sim_config['grid_size']))
 for turn in range(args.max_turn):
     start_time = current_milli_time()
     grid.play_turn()
     print(''.join(['*'] * sim_config['grid_size'] * 2))
     print('Turn: {turn: ^{size}}'.format(turn=grid._sim_turn,
                                          size=sim_config['grid_size']))
     print()
     print(
         display_simple_grid(grid.get_simulation_grid_data(),
                             sim_config['grid_size']))
     print()
     end_time = current_milli_time()
     print('Turn duration: {} ms'.format(end_time - start_time))
     print()
Exemplo n.º 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))