def test_createInfectedPersonsBestEffort2(): """ Test that when the population size is less then the amount of people that had been chosen to be infected, the simulation doesn't crash """ config_path = os.path.join(os.path.dirname(__file__),"..","src","config.json") with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__),"..","src", paramsDataPath), override=True) ageList = [random.randint(19,40) for i in range(10)] PersonList = list(map(Person, ageList)) env_arr = [] my_world = World( all_people = PersonList, all_environments=env_arr, generating_city_name = "test", generating_scale = 1) my_simulation = Simulation(world = my_world, initial_date= INITIAL_DATE) my_simulation.infect_random_set(num_infected = 0, infection_doc = "", per_to_immune = 2,city_name = None,min_age=18,people_per_day =0) my_simulation.simulate_day() cnt_immune = 0 cnt_sick = 0 for person in my_world.all_people(): if person.get_disease_state() == DiseaseState.IMMUNE: cnt_immune += 1 else: cnt_sick += 1 assert cnt_immune == 10 assert cnt_sick == 0
def test_createInfectedPersons3(): config_path = os.path.join(os.path.dirname(__file__),"..","src","config.json") Expected = -1 with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__),"..","src", paramsDataPath), override=True) kids = [random.randint(0,17) for i in range(5)] adults = [random.randint(19,40) for i in range(5)] ageList = kids + adults PersonList = list(map(Person, ageList)) env_arr = [] my_world = World( all_people = PersonList, all_environments=env_arr, generating_city_name = "test", generating_scale = 1) my_simulation = Simulation(world = my_world, initial_date= INITIAL_DATE) my_simulation.infect_random_set(num_infected = 0, infection_doc = "", per_to_immune = 0.5,Immune_compliance = 0,city_name = None,min_age=18,people_per_day =5) my_simulation.simulate_day() #assert events dictionary is not empty cnt_immune = 0 for person in my_world.all_people(): if person.get_disease_state() == DiseaseState.IMMUNE: cnt_immune = cnt_immune + 1 assert cnt_immune == 0
def test_createInfectedPersonsOredredASCENDING(): config_path = os.path.join(os.path.dirname(__file__),"..","src","config.json") Expected = -1 with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__),"..","src", paramsDataPath), override=True) kids = [0,4,8,12,16] adults = [25,29,33] ageList = kids + adults youngest = Person(21) Oldest = Person(37) PersonList = list(map(Person, ageList)) PersonList = PersonList + [youngest , Oldest] env_arr = [] my_world = World( all_people = PersonList, all_environments=env_arr, generating_city_name = "test", generating_scale = 1) my_simulation = Simulation(world = my_world, initial_date= INITIAL_DATE) my_simulation.infect_random_set(num_infected = 0, infection_doc = "", per_to_immune = 0.5,order= ORDER.ASCENDING,city_name = None,min_age=18,people_per_day =1) my_simulation.simulate_day() assert youngest.get_disease_state() == DiseaseState.IMMUNE #Can't check day by day lots of noise with seir times for _ in range(4): my_simulation.simulate_day() cnt_immune =0 for person in my_world.all_people(): if person.get_disease_state() == DiseaseState.IMMUNE: cnt_immune = cnt_immune + 1 assert cnt_immune <= 5
def test_createInfectedPersonsByHouseHoldBestEffort2(): """ Test that when the population size is less then the amount of people that had been chosen to be infected by households, the simulation doesn't crash """ config_path = os.path.join(os.path.dirname(__file__),"..","src","config.json") with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__),"..","src", paramsDataPath), override=True) #create diff enviroments KidsHouse = Household(city = None,contact_prob_between_each_two_people=1) AdultsHouse = Household(city = None,contact_prob_between_each_two_people=1) MixedHouse = Household(city = None,contact_prob_between_each_two_people=1) kidsAges = [random.randint(0,17) for i in range(4)] adultsAges = [random.randint(19,40) for i in range(3)] KidsLst = list(map(Person, kidsAges)) adultsLst = list(map(Person, adultsAges)) persons_arr = KidsLst + adultsLst #register people to diff env KidsHouse.sign_up_for_today(KidsLst[0],1) KidsHouse.sign_up_for_today(KidsLst[1],1) AdultsHouse.sign_up_for_today(adultsLst[0],1) AdultsHouse.sign_up_for_today(adultsLst[1],1) MixedHouse.sign_up_for_today(adultsLst[2],1) MixedHouse.sign_up_for_today(KidsLst[2],1) MixedHouse.sign_up_for_today(KidsLst[3],1) assert len(KidsHouse.get_people()) == 2 assert len(AdultsHouse.get_people()) == 2 assert len(MixedHouse.get_people()) == 3 env_arr = [KidsHouse,AdultsHouse,MixedHouse] my_world = World( all_people = persons_arr, all_environments=env_arr, generating_city_name = "test", generating_scale = 1,) my_simulation = Simulation(world = my_world, initial_date= INITIAL_DATE) my_simulation.immune_households_infect_others(num_infected = 0, infection_doc = "", per_to_immune = 2 ,city_name = None,min_age=18,people_per_day =5) my_simulation.simulate_day() cnt_immune = 0 cnt_sick = 0 for person in my_world.all_people(): if person.get_disease_state() == DiseaseState.IMMUNE: cnt_immune += 1 elif person.get_disease_state() != DiseaseState.SUSCEPTIBLE: cnt_sick += 1 assert cnt_immune == 3 assert cnt_sick == 0
class PlayState(BaseState): def start(self, renderbuffer): self.world = World(WORLDWIDTH, WORLDHEIGHT) self.renderbuffer = renderbuffer def update(self): for event in pygame.event.get(): if event.type == pygame.QUIT: return True def draw(self): self.world.draw(self.renderbuffer)
def test_put(self): world = World() location = Location(2, 3) world.put_cell(location, CellType.EMPTY) self.assertEqual(CellType.EMPTY, world.get_cell(location)) world.put_cell(location, CellType.FOOD) self.assertEqual(CellType.FOOD, world.get_cell(location)) result = world.put_cell(location, CellType.EMPTY) self.assertFalse(result)
def test_createImmunehouseholds4(): config_path = os.path.join(os.path.dirname(__file__),"..","src","config.json") with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__),"..","src", paramsDataPath), override=True) #create diff enviroments KidsHouse = Household(city = None,contact_prob_between_each_two_people=1) AdultsHouse = Household(city = None,contact_prob_between_each_two_people=1) MixedHouse = Household(city = None,contact_prob_between_each_two_people=1) kidsAges = [random.randint(0,17) for i in range(4)] adultsAges = [random.randint(19,40) for i in range(3)] KidsLst = list(map(Person, kidsAges)) adultsLst = list(map(Person, adultsAges)) persons_arr = KidsLst + adultsLst #register people to diff env KidsHouse.sign_up_for_today(KidsLst[0],1) KidsHouse.sign_up_for_today(KidsLst[1],1) AdultsHouse.sign_up_for_today(adultsLst[0],1) AdultsHouse.sign_up_for_today(adultsLst[1],1) MixedHouse.sign_up_for_today(adultsLst[2],1) MixedHouse.sign_up_for_today(KidsLst[2],1) MixedHouse.sign_up_for_today(KidsLst[3],1) assert len(KidsHouse.get_people()) == 2 assert len(AdultsHouse.get_people()) == 2 assert len(MixedHouse.get_people()) == 3 env_arr = [KidsHouse,AdultsHouse,MixedHouse] my_world = World( all_people = persons_arr, all_environments=env_arr, generating_city_name = "test", generating_scale = 1,) my_simulation = Simulation(world = my_world, initial_date= INITIAL_DATE) my_simulation.immune_households_infect_others(num_infected = 0, infection_doc = "", per_to_immune = 1,Immune_compliance= 0 ,city_name = None,min_age=18,people_per_day= 3 ) my_simulation.simulate_day() #assert events dictionary is not empty cnt_immune = 0 for person in my_world.all_people(): if person.get_disease_state() == DiseaseState.IMMUNE: cnt_immune = cnt_immune + 1 assert cnt_immune == 0
def __init__(self, game): self.game = game self.world = World() self.world_viewport = WorldViewport(self.world.world_map) self.player_command = ""
def main(): """Runs the program.""" # State objects boat = Boat() world = World() # Threads # airmar_thread = airmar.AirmarInputThread() logger = Logger() rc_thread = rc.RCInputThread() Sail = sail.SailListener(boat, world) Rudder = rudder.RudderListener() captain_thread = captain.Captain(boat, world) # airmar_thread.start() rc_thread.start() captain_thread.start() while True: print( "Waiting for input:\nd: drop mark\ns: start navigation\ne: end navigation\nc: clear course\n^C: exit program" ) cmd = input() if cmd == 'd': captain_thread.drop_mark() elif cmd == 's': captain_thread.enable() elif cmd == 'e': captain_thread.disable() elif cmd == 'c': captain_thread.clear_course()
def __init__(self, *args, **kwargs): pyglet.window.Window.__init__(self, *args, **kwargs) self.set_size(MAX_X, MAX_Y) self.world = World() self.bot_pool = BootPool(self.world) self.bots_count_label = None self.generation_label = None
def test_createImmunehouseholds2(): config_path = os.path.join(os.path.dirname(__file__),"..","src","config.json") with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__),"..","src", paramsDataPath), override=True) #create diff enviroments house1 = Household(city = None,contact_prob_between_each_two_people=1) house2 = Household(city = None,contact_prob_between_each_two_people=1) house1Ages = [98,93,5] house2Ages = [94,6] house1Lst = list(map(Person, house1Ages)) house2Lst = list(map(Person, house2Ages)) persons_arr = house1Lst + house2Lst #register people to diff env house1.sign_up_for_today(house1Lst[0],1) house1.sign_up_for_today(house1Lst[1],1) house1.sign_up_for_today(house1Lst[2],1) house2.sign_up_for_today(house2Lst[0],1) house2.sign_up_for_today(house2Lst[1],1) assert len(house1.get_people()) == 3 assert len(house2.get_people()) == 2 env_arr = [house1,house2] my_world = World( all_people = persons_arr, all_environments=env_arr, generating_city_name = "test", generating_scale = 1,) my_simulation = Simulation(world = my_world, initial_date= INITIAL_DATE) my_simulation.immune_households_infect_others(num_infected = 0, infection_doc = "", per_to_immune = 0.6,Sort_order=ORDER.DESCENDING ,city_name = None,min_age=18,people_per_day= 3 ) my_simulation.simulate_day() #assert events dictionary is not empty cnt_immune = 0 for person in my_world.all_people(): if (person.get_age() in [94,93,98]) and (person.get_disease_state() == DiseaseState.IMMUNE): cnt_immune = cnt_immune + 1 assert cnt_immune == 3
class GameScene: def __init__(self, game): self.game = game self.world = World() self.world_viewport = WorldViewport(self.world.world_map) self.player_command = "" def update(self): self.world.update() def render(self, window): window.fill((0, 0, 0)) self.world_viewport.render(window) self.game.console.render_string(window, self.game.console.instream, 10, 460, cursor=True) self.world.render(window) def handle_events(self, events): self.game.console.input.get_key(events) for e in events: if e.type == pygame.QUIT: self.game.running = False if e.type == pygame.KEYDOWN and e.key == pygame.K_RETURN: self.player_command = self.game.console.input.get_stream() self.player_command = ''.join(self.player_command) self.execute_command(self.player_command) self.game.console.input.reset_stream() def execute_command(self, command): callbacks = { "quit": self.game.quit, "clear": self.game.console.clear, "north": self.world.player.move_north, "south": self.world.player.move_south, "east": self.world.player.move_east, "west": self.world.player.move_west, } callbacks[command]()
def test_move_to_empty(self): world = World() old_location = Location(2, 2) bot = Bot(old_location, world) world.update_cell(old_location, bot) expected_future_location = Location(2, 1) world.update_cell(expected_future_location, CellType.EMPTY) bot.direction = Direction.N bot.move(0) self.assertTrue( isinstance(world.get_cell(expected_future_location), Bot)) self.assertEqual(CellType.EMPTY, world.get_cell(old_location)) self.assertEqual(Direction.N, bot.direction)
def test_move_to_poison(self): world = World() old_location = Location(3, 1) expected_future_location = Location(4, 2) world.update_cell(expected_future_location, CellType.POISON) bot = Bot(old_location, world) world.update_cell(old_location, bot) bot.direction = Direction.E bot.move(1) self.assertEqual(CellType.POISON, world.get_cell(expected_future_location)) self.assertEqual(CellType.EMPTY, world.get_cell(old_location)) self.assertEqual(Direction.E, bot.direction)
def generate_entire_country(city_list, is_smart_household_generation=True, scaling=1.0, verbosity=False): """ Generates the World of a model of the entire country, in the same way as was detailed in the specification document. :param city_list: The list of cities which comprise this model (workplace_city_distribution should already be initialized) :param is_smart_household_generation: Are we using smart_population_generation or naive_population_generation. This should only be False for testing/debugging purposes. :param scaling: The scale that should multiply all of the cities involved. :param verbosity: Whether or not we should print debug information. :return: A world object corresponding to a model of all of these cities. """ all_people = [] all_environments = [] city_to_workers = {city: [] for city in city_list} workplace_node = None city_env_params = Params.loader()['city_environments'] for env_params in city_env_params: if env_params["env_name"] == 'workplace': workplace_node = load_cross_environment_data_from_json(env_params) assert workplace_node is not None, "Could not find workplace in params.json!" for city in city_list: city_people, city_environments = generate_city( city, is_smart_household_generation=is_smart_household_generation, internal_workplaces=False, scaling=scaling, verbosity=verbosity, to_world=False) for person in city_people: all_people.append(person) if workplace_node.age_segment[0] <= person.get_age( ) <= workplace_node.age_segment[1]: workplace_city = city.workplace_city_distribution.sample() city_to_workers[workplace_city].append(person) for environment in city_environments: all_environments.append(environment) for city, workers in city_to_workers.items(): workplaces_in_city, people_segments = divide_people_to_environments( workers, workplace_node.size, Workplace, city, workplace_node.average_daily_contacts, age_segment=workplace_node.age_segment, name=workplace_node.env_name) for workplace in workplaces_in_city: all_environments.append(workplace) return World(all_people, all_environments, 'all', scaling)
def test_peek_poison(self): world = World() bot_location = Location(2, 2) bot = Bot(bot_location, world) world.update_cell(bot_location, bot) bot.energy = 77 bot.direction = Direction.E poison_location = Location(3, 3) world.update_cell(poison_location, CellType.POISON) bot.lookup(1) self.assertEqual(77, bot.energy) self.assertEqual(Direction.E, bot.direction) self.assertEqual(CellType.POISON, world.get_cell(poison_location)) self.assertTrue(isinstance(world.get_cell(bot_location), Bot)) self.assertEqual(24, bot.genes.get_next())
def test_move_to_food(self): world = World() old_location = Location(2, 2) expected_future_location = Location(3, 1) world.update_cell(expected_future_location, CellType.FOOD) bot = Bot(old_location, world) world.update_cell(old_location, bot) bot.direction = Direction.NE bot.energy = 11 bot.move(0) self.assertTrue( isinstance(world.get_cell(expected_future_location), Bot)) self.assertEqual(CellType.EMPTY, world.get_cell(old_location)) self.assertEqual(Direction.NE, bot.direction) self.assertEqual(22, bot.energy)
def test_move_to_bot(self): world = World() other_bot_location = Location(2, 4) current_bot_location = Location(1, 3) other_bot = Bot(other_bot_location, world) current_bot = Bot(current_bot_location, world) world.update_cell(other_bot_location, other_bot) world.update_cell(current_bot_location, current_bot) current_bot.direction = Direction.S current_bot.energy = 10 current_bot.move(7) self.assertEqual(current_bot, world.get_cell(current_bot_location)) self.assertEqual(other_bot, world.get_cell(other_bot_location)) self.assertEqual(Direction.S, current_bot.direction) self.assertEqual(12, current_bot.energy) self.assertEqual(ROTATE_45_DEGREE_GENE, current_bot.genes.get_next())
def test_lookup_empty(self): world = World() location = Location(2, 2) bot = Bot(location, world) world.update_cell(location, bot) world.update_cell(Location(3, 1), CellType.EMPTY) bot.energy = 77 bot.direction = Direction.NE bot.lookup(0) self.assertEqual(77, bot.energy) self.assertEqual(Direction.NE, bot.direction) self.assertEqual(ROTATE_45_DEGREE_GENE, bot.genes.get_next()) self.assertTrue(isinstance(world.get_cell(location), Bot))
def test_peek_food(self): world = World() bot_location = Location(2, 2) bot = Bot(bot_location, world) world.update_cell(bot_location, bot) bot.energy = 77 bot.direction = Direction.E food_location = Location(3, 2) world.update_cell(food_location, CellType.FOOD) bot.lookup(0) self.assertEqual(77, bot.energy) self.assertEqual(Direction.E, bot.direction) self.assertTrue(isinstance(world.get_cell(bot_location), Bot)) self.assertEqual(8, bot.genes.get_next())
def test_peek_bot(self): world = World() current_bot_location = Location(3, 4) current_bot = Bot(current_bot_location, world) world.update_cell(current_bot_location, current_bot) current_bot.energy = 77 current_bot.direction = Direction.NW other_bot_location = Location(2, 3) other_bot = Bot(other_bot_location, world) world.update_cell(other_bot_location, other_bot) current_bot.lookup(7) self.assertEqual(77, current_bot.energy) self.assertEqual(Direction.NW, current_bot.direction) self.assertEqual(other_bot, world.get_cell(other_bot_location)) self.assertEqual(current_bot, world.get_cell(current_bot_location)) self.assertEqual(ROTATE_45_DEGREE_GENE, current_bot.genes.get_next())
def test_move_to_wall(self): world = World() old_location = Location(1, 1) bot = Bot(old_location, world) world.update_cell(old_location, bot) bot.direction = Direction.NW bot.move(0) self.assertEqual(CellType.WALL, world.get_cell(Location(0, 0))) self.assertTrue(isinstance(world.get_cell(old_location), Bot)) self.assertEqual(Direction.NW, bot.direction) self.assertEqual(ROTATE_45_DEGREE_GENE, bot.genes.get_next())
def test_CreateDeltaFile(helpers): helpers.clean_outputs() config_path = os.path.join(os.path.dirname(__file__), "..", "src", "config.json") with open(config_path) as json_data_file: ConfigData = json.load(json_data_file) paramsDataPath = ConfigData['ParamsFilePath'] Params.load_from(os.path.join(os.path.dirname(__file__), "..", "src", paramsDataPath), override=True) ageList = [random.randint(0, 40) for i in range(10)] PersonList = list(map(Person, ageList)) events_acc = [] for person in PersonList: states_table = ((DiseaseState.LATENT, daysdelta(3)), (DiseaseState.ASYMPTOMATICINFECTIOUS, daysdelta(3)), (DiseaseState.IMMUNE, daysdelta(3)), (DiseaseState.IMMUNE, None)) events = person.gen_and_register_events_from_seir_times( date=INITIAL_DATE, states_and_times=states_table) events_acc += events # person.set_disease_state(DiseaseState.LATENT) env_arr = [] my_world = World(all_people=PersonList, all_environments=env_arr, generating_city_name="test", generating_scale=1) my_simulation = Simulation(world=my_world, initial_date=INITIAL_DATE) my_simulation.register_events(events_acc) my_simulation.run_simulation(num_days=10, name="test") #assert events dictionary is not empty txt = my_simulation.stats.get_state_stratified_summary_table( table_format=TableFormat.CSV) test_data = StringIO(txt) tbl = pd.read_csv(test_data) assert len(tbl) == 7 print(tbl) assert tbl.iloc[0, DiseaseState.SUSCEPTIBLE.value] == 10 assert tbl.iloc[3, DiseaseState.ASYMPTOMATICINFECTIOUS.value] == 10 assert tbl.iloc[6, DiseaseState.IMMUNE.value] == 10
def test_peek_wall(self): world = World() bot_location = Location(1, 1) bot = Bot(bot_location, world) world.update_cell(bot_location, bot) bot.energy = 77 bot.direction = Direction.E bot.lookup(7) self.assertEqual(77, bot.energy) self.assertEqual(Direction.E, bot.direction) self.assertEqual(CellType.WALL, world.get_cell(Location(0, 0))) self.assertTrue(isinstance(world.get_cell(bot_location), Bot)) self.assertEqual(ROTATE_45_DEGREE_GENE, bot.genes.get_next())
def main(): """Runs the program.""" print( "Virginia Tech SailBOT\n\n \"I wish to have no connection with " "any ship that does not sail fast\"\n - John Paul Jones\n\n\n" ) # State objects boat = Boat() world = World() # Threads airmar_thread = airmar.AirmarInputThread() logger = Logger() rc_thread = rc.RCInputThread() Sail = sail.SailListener(boat, world) Rudder = rudder.RudderListener() arduino_thread = Arduino() captain_thread = captain.Captain(boat, world) airmar_thread.start() rc_thread.start() captain_thread.start() arduino_thread.start() while True: print( "Waiting for input:\nd: drop mark\ns: start navigation\ne: end navigation\nc: clear course\n^C: exit program" ) cmd = input() if cmd == 'd': captain_thread.drop_mark() elif cmd == 's': captain_thread.enable() elif cmd == 'e': captain_thread.disable() elif cmd == 'c': captain_thread.clear_course()
def test_update_when_is_not_free(self): world = World() world.put_cell(Location(1, 1), CellType.FOOD) old_value = world.update_cell(Location(1, 1), CellType.POISON) self.assertEqual(old_value, CellType.FOOD)
def test_put_when_not_empty(self): world = World() world.put_cell(Location(1, 1), CellType.FOOD) result = world.put_cell(Location(1, 1), CellType.FOOD) self.assertFalse(result)
def test_put_when_out_of_range(self): world = World() with self.assertRaises(Exception): world.put_cell(Location(999999, 988888), CellType.POISON)
def test_update_when_is_free(self): world = World() old_value = world.update_cell(Location(1, 1), CellType.POISON) self.assertIsNone(old_value)
def test_get_when_not_exist(self): world = World() self.assertEqual(world.get_cell(Location(1, 1)), CellType.EMPTY)
def test_update_empty_when_empty(self): world = World() location = Location(1, 1) world.update_cell(location, CellType.EMPTY) world.update_cell(location, CellType.EMPTY)
def test_update(self): world = World() location = Location(2, 3) world.update_cell(location, CellType.EMPTY) self.assertEqual(CellType.EMPTY, world.get_cell(location)) world.update_cell(location, CellType.FOOD) self.assertEqual(CellType.FOOD, world.get_cell(location)) world.update_cell(location, CellType.EMPTY) self.assertEqual(CellType.EMPTY, world.get_cell(location))