def deplace(walker): """ Makes the walker move if it is possible and kills it if there is a warrior next to it. """ pop = get_pop(walker) mas = p.get_mas(pop) env = p.get_env(pop) env_size = e.size(env) l, l_pos = potential_places(pop, walker, env, env_size, get_vision(walker), l=[], l_pos=[]) pos_cell = get_pos(walker) unplace_walker(env, pop, walker, pos_cell) l_cells_warriors, l_pos_warriors = potential_places(pop, walker, env, env_size, 1, l=[], l_pos=[]) if warrior_around(l_cells_warriors): p.end_walker(walker, p.get_walkers(pop)) else: move_walker(mas, env, pop, walker, l_pos, env_size)
def average_living(pop, cell_refs): """ cherche le niveau de vie moyen des agents situé dans un bloc de coté 6 (zone*2) centré dans la cellule cible """ x, y = cell_refs zone = 3 cells_average_list = [] sugar_level_sum = 0 agents_in_zone_of_cell = 0 env = p.get_env(pop) pos_start_calc = correct_position((x - zone, y + zone), env) xi, yi = pos_start_calc i = 0 j = -2 * zone for j in range(-2 * zone, 0, 1): for i in range(0, 2 * zone): if i != j: cell = e.get_cell(env, correct_position((xi + i, yi + j), env)) if c.agent_is_present(cell): agents_in_zone_of_cell += 1 sugar_level_sum += get_sugar_level( c.get_present_agent(cell)) if agents_in_zone_of_cell != 0: average = sugar_level_sum / agents_in_zone_of_cell else: average = 0 return average
def average_living(pop,cell_refs): """ cherche le niveau de vie moyen des agents situé dans un bloc de coté 6 (zone*2) centré dans la cellule cible """ x,y = cell_refs zone = 3 cells_average_list = [] sugar_level_sum = 0 agents_in_zone_of_cell = 0 env = p.get_env(pop) pos_start_calc = correct_position((x-zone,y+zone),env) xi,yi = pos_start_calc i = 0 j = -2*zone for j in range(-2*zone,0,1): for i in range(0,2*zone): if i!=j: cell = e.get_cell(env,correct_position((xi+i,yi+j),env)) if c.agent_is_present(cell): agents_in_zone_of_cell +=1 sugar_level_sum += get_sugar_level(c.get_present_agent(cell)) if agents_in_zone_of_cell != 0: average = sugar_level_sum/agents_in_zone_of_cell else: average = 0 return average
def RA4(agent): """ This fourth rule makes the agents move to the cell which has enough sugar for his metabolism, or to the cell which has the closest sugar level to his metabolism if there is no cell which has a bigger sugar level than the meatabolism of the agent. After this, this rule checks the average of the sugar levels of the agents who are near every cell the agent can move to. This agent will choose a cell which has this average result above the sugar level of the agent. If it is not possible, the agent will go on the cell with the nearest average result. """ pop = get_pop(agent) env = p.get_env(pop) env_size = e.size(env) l, l_pos = potential_places(pop, agent, env, env_size, get_vision(agent), l=[], l_pos=[]) pos_cell = get_pos(agent) if len(l_pos) > 0: l_good_pos = better_ressources_for_average(l, l_pos, agent) l_average_res = average_around_agents_sugar_level(env, l_good_pos) pos_cell = better_average_ressource_cell(l, l_good_pos, l_average_res, agent) cell = e.get_cell(env, pos_cell) eat_and_consumpt_sugar(agent, cell) deplace_or_remove(env, pop, agent, cell, pos_cell)
def remove_metabolism_on_sugar_level(agent, pop): sugar = get_sugar_level( agent ) # Enregistre le taux de sucre de l'agent sugar -= get_metabolism( agent ) # On dimine par le metabolism set_sugar_level( agent, sugar ) # On donne le nouveau niveau de sucre de l'agent if get_sugar_level(agent) < 0: env = p.get_env(pop) kill(pop, env, agent)
def kill_agent(pop, agent): """ Kills the agent. """ env = p.get_env(pop) cell = e.get_cell(env, get_pos(agent)) p.end_agent(agent, p.get_agents(pop)) c.set_present_agent(cell, None)
def __draw_pop(canvas, pop, cell_size): # Draw the population on the canvas env = p.get_env(pop) env_size = e.size(env) for agent in p.get_agents(pop): if a.get_is_living(agent): cell_ref = __swap_y(env_size, a.get_pos(agent)) canvas.create_oval(__bbox_for_cell_ref(cell_ref, cell_size, 0.6), fill='red', width=0)
def kill_walker(pop, walker): """ Kills the walker. """ env = p.get_env(pop) cell = e.get_cell(env, get_pos(walker)) p.end_walker(agent, p.get_walkers(pop)) c.set_present_agent(cell, None) take_aura(pop, agent)
def __draw_walkers(canvas, pop, cell_size): # Draw the walker population on the canvas env = p.get_env(pop) env_size = e.size(env) for walker in p.get_walkers(pop): cell_ref = v.__swap_y(env_size, w.get_pos(walker)) canvas.create_oval(v.__bbox_for_cell_ref(cell_ref, cell_size, 0.6), fill=rgb_to_hex((0, 0, 0)), width=0)
def add_age(agent): """ Increments the age of the agent by one. """ pop = get_pop(agent) env = p.get_env(pop) cell = e.get_cell(env, get_pos(agent)) age = get_age(agent) set_age(agent, age + 1) if age > get_dead_age(agent): kill_agent(pop, agent)
def __draw_pop(canvas, pop, cell_size): # Draw the population on the canvas # WE ADD THE color_agent FUNCTION IN PLACE OF "red" # AND THE THICKNESS EVOLVES THANKS THE thk FUNCTION env = p.get_env(pop) env_size = e.size(env) for agent in p.get_agents(pop): if a.get_is_living(agent): cell_ref = v.__swap_y(env_size, a.get_pos(agent)) canvas.create_oval(v.__bbox_for_cell_ref(cell_ref, cell_size, thk(agent)), fill=color_agent(agent), width=0)
def put_aura(pop, walker): """ Puts the aura of the walker on the environnement. """ vision = get_vision(walker) - 2 # aura radius = vision radius -2 env = p.get_env(pop) env_size = e.size(env) x, y = get_pos(walker) for i in range(x - vision, x + vision + 1): for j in range(y - vision, y + vision + 1): if (i, j) != (x, y): cell = e.get_cell(env, (i, j)) if c.get_present_agent(cell) == None: c.set_present_agent(cell, "alerte")
def take_aura(pop, walker): """ Makes the aura of the walker disappear from the environnement. """ vision = get_vision(walker) - 1 env = p.get_env(pop) env_size = e.size(env) x, y = get_pos(walker) for i in range(x - vision, x + vision + 1): for j in range(y - vision, y + vision + 1): if (i, j) != (x, y): cell = e.get_cell(env, (i, j)) if c.get_present_agent(cell) == "alerte": c.set_present_agent(cell, None)
def walker_around_check(pop, agent): """ Returns the list of the contamined cells around the agent. """ walker_around = False l_around = [] env = p.get_env(pop) x, y = get_pos(agent) all_x = range(x - 1, x + 2) all_y = range(y - 1, y + 2) for i in all_x: for j in all_y: cell = e.get_cell(env, (i, j)) l_around.append(c.get_present_agent(cell)) for elem in l_around: if elem != None and len(elem) == w.WALKER_MAX_IDX + 1: walker_around = True return walker_around
def reproduction(agent): """ Choose the best male around the female agent in order the make her enter in a gestation period which will allow to give birth to a new agent 9 cycles after the reproduction. """ pop = get_pop(agent) env = p.get_env(pop) env_size = e.size(env) first_pop_size = len(p.get_agents(pop)) l, l_pos = potential_partners(pop, agent, env, env_size, 1, l=[], l_pos=[]) l_partners = partners_list_under_conditions(env, agent, l_pos) if len(l_partners) != 0: best_age = get_dead_age(best_partner_from_list(l_partners)) metabolism = get_metabolism(best_partner_from_list(l_partners)) if get_gestation(agent) == -1: set_gestation(agent, 9) set_partner_spec(agent, (best_age, metabolism))
def new_instance(pop): agent = __empty_instance() #Recupère la liste des propriétés prop = p.get_properties(pop) set_population(agent,pop) #choisi une valeur aléatoire entre les minimum et maximum venant du fichier de configuration set_vision_capacity(agent,randint(prop["MIN_VISION_CAPACITY"],prop["MAX_VISION_CAPACITY"])) set_metabolism(agent,uniform(prop["MIN_METABOLISM"],prop["MAX_METABOLISM"])) #les agents ont au départ un âge reflettant le réalisme set_age(agent,randint(prop["MIN_AGENT_AGE"],prop["MAX_AGENT_AGE"])) #La réserve en sucre au départ est suffisante à sa survie (métabolism) set_sugar_level(agent,get_metabolism(agent)) env = p.get_env(pop) random_position = e.random_cell_ref_without_agent(env) set_pos(agent,random_position) cell = e.get_cell(env,random_position) set_sex(agent,randint(1,2)) c.set_present_agent(cell,agent) return agent
def RA1(agent): """ This first rule makes the agents move to the cell in their vision which has the biggest sugar level. """ pop = get_pop(agent) env = p.get_env(pop) env_size = e.size(env) l, l_pos = potential_places(pop, agent, env, env_size, get_vision(agent), l=[], l_pos=[]) pos_cell = get_pos(agent) if len(l_pos) > 0: pos_cell = max_ressource_cell_position(l, l_pos) cell = e.get_cell(env, pos_cell) eat_and_consumpt_sugar(agent, cell) deplace_or_remove(env, pop, agent, cell, pos_cell)
def not_immune(agent): """ This agent_rule makes the agents sensible to an eventually contamination by the contamined cells. It doesn't affect the warriors agents. """ pop = get_pop(agent) env = p.get_env(pop) walker_around = walker_around_check(pop, agent) if walker_around and not agent_is_warrior(agent): x, y = get_pos(agent) cell = e.get_cell(env, (x, y)) if not agent_is_warrior( agent) and get_age(agent) <= 20: # TOO YOUNG AND NOT A WARRIOR kill_agent(pop, agent) elif not agent_is_warrior( agent) and get_age(agent) > 20: # MATURE AND NOT A WARRIOR kill_agent(pop, agent) walker = w.new_instance(pop) w.set_pos(walker, (x, y)) c.set_present_agent(cell, walker) p.add_walker(walker, p.get_walkers(pop))
def new_instance(pop): agent = __empty_instance() #Recupère la liste des propriétés prop = p.get_properties(pop) set_population(agent, pop) #choisi une valeur aléatoire entre les minimum et maximum venant du fichier de configuration set_vision_capacity( agent, randint(prop["MIN_VISION_CAPACITY"], prop["MAX_VISION_CAPACITY"])) set_metabolism(agent, uniform(prop["MIN_METABOLISM"], prop["MAX_METABOLISM"])) #les agents ont au départ un âge reflettant le réalisme set_age(agent, randint(prop["MIN_AGENT_AGE"], prop["MAX_AGENT_AGE"])) #La réserve en sucre au départ est suffisante à sa survie (métabolism) set_sugar_level(agent, get_metabolism(agent)) env = p.get_env(pop) random_position = e.random_cell_ref_without_agent(env) set_pos(agent, random_position) cell = e.get_cell(env, random_position) set_sex(agent, randint(1, 2)) c.set_present_agent(cell, agent) return agent
def give_birth(agent): """ Makes the pregnant female agent give birth if it is her correct cycle or decrement her cycle by one otherwise. """ pop = get_pop(agent) env = p.get_env(pop) env_size = e.size(env) if get_gestation(agent) == 0: l, l_pos = potential_places(pop, agent, env, env_size, 1, l=[], l_pos=[]) add_baby_agent_if_possible(env, pop, env_size, agent, l_pos) if get_gestation(agent) > -1: set_gestation(agent, get_gestation(agent) - 1) if get_sugar_level(agent) > 10 / 9: set_sugar_level(agent, get_sugar_level(agent) - 10 / 9) else: set_gestation(agent, -1)
def RA2(agent): """ This second rule makes the agents see the cell in their vision which has the biggest sugar level. But they can only move one step by one step. It means that this cell can change at every cycle. """ pop = get_pop(agent) env = p.get_env(pop) env_size = e.size(env) l, l_pos = potential_places(pop, agent, env, env_size, get_vision(agent), l=[], l_pos=[]) pos_cell = get_pos(agent) if len(l_pos) > 0: pos_cell = max_ressource_cell_position(l, l_pos) pos_cell = unital_move(env, env_size, agent, pos_cell) cell = e.get_cell(env, pos_cell) eat_and_consumpt_sugar(agent, cell) deplace_or_remove(env, pop, agent, cell, pos_cell)
def RA3(agent): """ This third rule makes the agents move to the cell in their vision which has the best sugar level. "Best" means as closest as possible to the metabolism of the agent. And if the sugar level of the cell is close enough and bigger the the metabolism of the agent, then he will move to this cell. """ pop = get_pop(agent) env = p.get_env(pop) env_size = e.size(env) l, l_pos = potential_places(pop, agent, env, env_size, get_vision(agent), l=[], l_pos=[]) pos_cell = get_pos(agent) if len(l_pos) > 0: pos_cell = better_ressource_cell_position(l, l_pos, agent) cell = e.get_cell(env, pos_cell) eat_and_consumpt_sugar(agent, cell) deplace_or_remove(env, pop, agent, cell, pos_cell)
def get_env(agent): return p.get_env(get_population(agent))