def calc_nutrient(group, agent): nutrient_strength = 0 for nutrient in group: if distance(group[nutrient], agent) != 0: nutrient_strength += 1 / (distance(group[nutrient], agent)**2) else: nutrient_strength += sys.maxsize return nutrient_strength
def calc_toxin(group, agent): """ Calculate the strength of a toxin / nutrient field for an agent. We will use an inverse square law. """ toxin_strength = 0 for toxin in group: if distance(group[toxin], agent) != 0: toxin_strength += 1 / (distance(group[toxin], agent)**2) else: toxin_strength += sys.maxsize toxin_strength *= -1 return toxin_strength
def bird_action(this_bird, **kwargs): """ Finds the closest agent to the current agent and calculates the distance between the two, inverting the direction if the distance is too close. """ if DEBUG: print(str(this_bird), " is at ", this_bird.get_pos()) nearest_bird = get_env().get_closest_agent(this_bird) if nearest_bird is not None: curr_distance = distance(this_bird, nearest_bird) if DEBUG: print("Distance between ", nearest_bird, " and ", this_bird, " is ", curr_distance) if abs(curr_distance - DEF_DESIRED_DISTANCE) < (DEF_DESIRED_DISTANCE * ACCEPTABLE_DEV): return True this_bird["angle"] = calc_angle(this_bird, nearest_bird) if DEBUG: print(this_bird.name, "'s angle rel. to ", nearest_bird.name, "is", this_bird["angle"]) if curr_distance < DEF_DESIRED_DISTANCE: this_bird["angle"] = invert_direction(this_bird["angle"]) return MOVE
def test_get_closest_agent(self): closest = self.space.get_closest_agent(self.newton) self.assertTrue( distance(self.newton, closest) <= self.space.get_max_distance())
def person_action(agent, **kwargs): """ This is what people do each turn in the epidemic. """ execution_key = get_exec_key(kwargs=kwargs) infec_dist = get_prop('infection_distance', DEF_INFEC_DIST, execution_key=execution_key) old_state = agent[STATE] if is_healthy(agent): distance_mod = 1 if agent["is_wearing_mask"]: distance_mod = 0.5 curr_reg = CircularRegion(center=agent.get_pos(), radius=(2 * infec_dist * distance_mod), execution_key=execution_key) sub_reg = curr_reg.create_sub_reg(center=agent.get_pos(), radius=(infec_dist * distance_mod), execution_key=execution_key) agent_list = sub_reg.get_agents(exclude_self=True) if (agent_list is not None and (len(agent_list) > 0)): if DEBUG2: user_log_notif("Exposing nearby people!") agent[STATE] = EX else: for curr_agent in curr_reg.get_agents(exclude_self=True): curr_distance = (distance(curr_agent, agent) - DEF_INFEC_DIST) if (curr_distance > infec_dist and curr_distance <= (infec_dist * 2)): inverse_square_val = ((1 / (curr_distance ** 2)) * distance_mod) if inverse_square_val > 0: r = random() if inverse_square_val / 100 > r: agent[STATE] = EX # if we didn't catch disease above, do probabilistic transition: if old_state == agent[STATE]: # we gotta do these str/int shenanigans with state cause # JSON only allows strings as dict keys agent[STATE] = str(prob_state_trans(int(old_state), STATE_TRANS)) if agent[STATE] == EX: user_log_notif("Person spontaneously catching virus.") if old_state != agent[STATE]: # if we entered a new state, then... group_map = get_env(execution_key=execution_key).get_attr(GROUP_MAP) if group_map is None: user_log_err("group_map is None!") return DONT_MOVE agent.has_acted = True get_env(execution_key=execution_key).add_switch(agent, group_map[old_state], group_map[ agent[STATE]]) if is_dead(agent): return DONT_MOVE if not is_isolated(agent, **kwargs): social_distancing(agent, **kwargs) return MOVE