def create_training_data(): FULL_SET.clear() with open('train.txt', 'r') as f: for line in f.readlines(): temp_list_attr = line.split('\t') FULL_SET.append( Human(temp_list_attr[0], int(temp_list_attr[2]), int(temp_list_attr[3]), temp_list_attr[1].strip()).get_fuzzy_info()) TRUE_RESULT[temp_list_attr[0]] = int(temp_list_attr[4]) f.close()
def _flush_pending_families(self): self.done = True for schema, family in self.pending: flag = False for human in family: if isinstance(human, Adult) or isinstance(human, Elder): flag = True break if not flag: family.append(Human.factory(self.covid_model, 30)) self.families.append(family) self.human_count += len(family)
def check_spread(sample_location, spreading_states=None, check_step=True, in_social_event=False): if spreading_states is None: spreading_states = list(SimulationState) # Creates an infected and a susceptible human sample_human_1 = Human.factory(covid_model=model, forced_age=20) sample_human_1.infection_status = InfectionStatus.INFECTED sample_human_1.infection_days_count = 1 sample_human_1.infection_latency = 0 assert sample_human_1.is_infected() and sample_human_1.is_contagious() sample_human_2 = Adult(covid_model=model, age=50, msp=0.05, hsp=0.1, mfd=False) sample_human_2.strid = 'human_2' sample_human_2.infection_status = InfectionStatus.SUSCEPTIBLE assert not sample_human_2.is_infected() # If in social event if in_social_event: sample_human_1.social_event = sample_location sample_human_2.social_event = sample_location # Adding humans to location sample_location.humans += [sample_human_1, sample_human_2] # Changing location contagion probability to 1 sample_location.set_custom_parameters([('contagion_probability', 1.0)], {'contagion_probability': 1.0}) for state in SimulationState: sample_location.covid_model.current_state = state if sample_location.covid_model.current_state in spreading_states: print(state) if check_step: sample_location.step() else: sample_location.check_spreading(sample_human_1, sample_human_2) assert sample_human_2.is_infected() sample_human_2.infection_status = InfectionStatus.SUSCEPTIBLE else: print(state) if check_step: sample_location.step() assert not sample_human_2.is_infected() print(spreading_states) return True
def __init__(self, unique_id, covid_model, size): super().__init__(unique_id, covid_model) self.size = size self.covid_model = covid_model self.custom_parameters = {} count = 0 for i in range(size): human = Human.factory(covid_model, self) self.covid_model.global_count.non_infected_people.append(human) self.covid_model.global_count.non_infected_count += 1 if human.immune: self.covid_model.global_count.immune_count += 1 else: self.covid_model.global_count.susceptible_count += 1 if not flip_coin(self.get_parameter('initial_infection_rate')): count += 1 else: self.covid_model.global_count.non_infected_people[ count].infect(count)
def test_location(): sample_hb_1 = location.Location(model, '', '') sample_hb_2 = location.Location(model, '', '') assert sample_hb_1.strid == 'Location' # Tests get_parameter assert sample_hb_1.get_parameter('contagion_probability') == 0.0 # Tests set_custom_parameter sample_hb_1.set_custom_parameters([('contagion_probability', 1.0)], {'contagion_probability': 1.0}) assert sample_hb_1.get_parameter('contagion_probability') == 1.0 # Test move_to sample_human_1 = Human.factory(covid_model=model, forced_age=20) sample_hb_1.humans.append(sample_human_1) assert sample_human_1 in sample_hb_1.humans assert not sample_hb_2.humans sample_hb_1.move_to(sample_human_1, sample_hb_2) assert sample_human_1 not in sample_hb_1.humans assert sample_human_1 in sample_hb_2.humans # Test check_spreading assert check_spread(sample_hb_1, [SimulationState.POST_WORK_ACTIVITY], check_step=False)
def factory(self, population_size): for i in range(population_size): self._push(Human.factory(self.covid_model, None)) self._flush_pending_families()
def test_district(): sample_district_1 = location.District(name="district", covid_model=model, strid_prefix='dummy', strid_suffix='') assert isinstance(sample_district_1, location.District) # Test get_buildings sample_human_1 = Human.factory(covid_model=model, forced_age=20) sample_hb_1 = location.HomogeneousBuilding(building_capacity=capacity, covid_model=model, strid_prefix='dummy', strid_suffix='1') sample_hb_1.humans.append(sample_human_1) sample_district_1.locations.append(sample_hb_1) sample_hb_1.allocation[sample_human_1] = sample_hb_1 sample_district_1.allocation[sample_human_1] = [sample_hb_1] assert sample_district_1.get_buildings(sample_human_1) == [sample_hb_1] # Test get_available_restaurant sample_restaurant = location.Restaurant( capacity=capacity, restaurant_type=RestaurantType.FAST_FOOD, is_outdoor=False, covid_model=model, strid_prefix='dummy', strid_suffix='') sample_restaurant.available = 100 sample_district_1.locations.append(sample_restaurant) assert sample_district_1.get_available_restaurant( people_count=100, outdoor=False, restaurant_type=RestaurantType.FAST_FOOD) == sample_restaurant assert not sample_district_1.get_available_restaurant( people_count=101, outdoor=False, restaurant_type=RestaurantType.FAST_FOOD) assert not sample_district_1.get_available_restaurant( people_count=100, outdoor=True, restaurant_type=RestaurantType.FAST_FOOD) assert not sample_district_1.get_available_restaurant( people_count=100, outdoor=False, restaurant_type=RestaurantType.FANCY) # Test move_to sample_district_2 = location.District(name="district", covid_model=model, strid_prefix='dummy', strid_suffix='') sample_human_2 = Human.factory(covid_model=model, forced_age=20) sample_hb_2 = location.HomogeneousBuilding(building_capacity=capacity, covid_model=model, strid_prefix='dummy', strid_suffix='2') sample_hb_2.humans.append(sample_human_2) sample_district_2.locations.append(sample_hb_2) sample_district_2.allocation[sample_human_2] = sample_hb_2 sample_district_1.move_to(sample_human_1, sample_hb_2) assert sample_human_1 in sample_hb_2.humans assert sample_human_2 in sample_hb_2.humans # Test allocate sample_bu_1 = location.BuildingUnit(capacity=capacity, covid_model=model, strid_prefix='dummy', strid_suffix='') sample_district_1.locations.append(sample_bu_1) sample_hb_1.locations.append(sample_bu_1) sample_district_1.allocate([sample_human_1, sample_human_2], True, True, True) assert sample_human_1 in sample_district_1.allocation.keys() assert sample_human_2 in sample_district_1.allocation.keys()
state_winner_triples = get_state_hash_and_winner(env) Vx = initialV_x(env, state_winner_triples) p1.setV(Vx) Vo = initialV_o(env, state_winner_triples) p2.setV(Vo) # give each player their symbol p1.set_symbol(env.x) p2.set_symbol(env.o) T = 1000000 for t in range(T): if t % 1000 == 0: print(t) play_game(p1, p2, Environment()) # play human vs. agent # do you think the agent learned to play the game well? human = Human() human.set_symbol(env.o) while True: p1.set_verbose(True) play_game(p1, human, Environment(), draw=2) # I made the agent player 1 because I wanted to see if it would # select the center as its starting move. If you want the agent # to go second you can switch the human and AI. answer = input("Play again? [Y/n]: ") if answer and answer.lower()[0] == 'n': break