def OA1(agent): """ This rule activates the agents in an random order at every cycle. """ pop = get_pop(agent) if agent == p.get_agents(pop)[0]: #IF IT IS THE FIRST AGENT shuffle(p.get_agents(pop))
def OA2(agent): """ This rule activates the agents in an order which makes the agent who has the lowest sugar level move first. This order changes at every cycle. """ pop = get_pop(agent) if agent == p.get_agents(pop)[0]: #IF IT IS THE FIRST AGENT l_res = [] for elem in p.get_agents(pop): l_res.append(get_sugar_level(elem)) u.sort_on_second_list(p.get_agents(pop), l_res, u_mod.order_scalar_asc)
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_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 add_baby_agent_if_possible(env, pop, env_size, agent, l_pos): """ Makes a female agent give birth to a new agent if there is a free position around her. If there is no free position, the baby won't born. This strategy limmits the overpopulation. """ if len(l_pos) != 0: baby = new_instance(pop) mother_dead_age = get_dead_age(agent) mother_metabolism = get_metabolism(agent) father_dead_age = get_partner_spec(agent)[0] father_metabolism = get_partner_spec(agent)[1] set_dead_age(baby, int((mother_dead_age + father_dead_age) / 2)) set_metabolism(baby, (mother_metabolism + father_metabolism) / 2) p.get_agents(pop).append(baby) set_pos(baby, l_pos[0]) c.set_present_agent(e.get_cell(env, l_pos[0]), baby)
def pop_plot(pop): """ Add a plot of the each agent to the current pylab plot. To actually show the plot, the pylab.show() method still has to be invoked. """ agents_list = p.get_agents(pop) for agent in agents_list: agent_plot(agent)
def kill(agent): """ Fonction qui tue un agent """ pop = get_population(agent) agents = p.get_agents(pop) cell = get_cell(agent) p.increment_dead_agents(pop) if agent in agents: del agents[agents.index(agent)] c.set_present_agent(cell, None)
def kill(agent): """ Fonction qui tue un agent """ pop = get_population(agent) agents = p.get_agents(pop) cell = get_cell(agent) p.increment_dead_agents(pop) if agent in agents: del agents[agents.index(agent)] c.set_present_agent(cell,None)
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 is_an_agent_can_be_there_faster(agent,target): """ si un autre agent peut atteindre avant la cellule visée """ pop = get_population(agent) agents = p.get_agents(pop) i=0 already_found = False while i < len(agents) and not already_found: if agents[i] != agent and target in accecible_positions(agents[i]): already_found = (u.eucl_dist(get_pos(agents[i]),target) < u.eucl_dist(get_pos(agent),target)) i+=1 return already_found
def is_an_agent_can_be_there_faster(agent, target): """ si un autre agent peut atteindre avant la cellule visée """ pop = get_population(agent) agents = p.get_agents(pop) i = 0 already_found = False while i < len(agents) and not already_found: if agents[i] != agent and target in accecible_positions(agents[i]): already_found = (u.eucl_dist(get_pos(agents[i]), target) < u.eucl_dist(get_pos(agent), target)) i += 1 return already_found
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 make_a_child(agent): """ règle permettant la reproduction asexuée selon une certaine probablité les agents doivent être dans un intervalle d'âge précis """ pop = get_population(agent) env = get_env(agent) min_age = p.get_pop_property(pop, "MIN_AGE_TO_MAKE_CHILDS") max_age = p.get_pop_property(pop, "MAX_AGE_TO_MAKE_CHILDS") max_pop = p.get_pop_property(pop, "MAX_POP") min_prob, max_prob = p.get_pop_property(pop, "PROB_TO_HAVE_SEX") if (min_age <= get_age(agent) <= max_age and theres_is_an_other_sex_around(agent) \ and randint(min_prob,max_prob) == 1 and p.size(pop) < max_pop ): new_child = new_instance(pop) set_age(new_child, 0) set_sugar_level(agent, 0) #le nouveau-née hérite du métabolisme et de la vision de capacité de son géniteur set_metabolism(new_child, get_metabolism(agent)) set_vision_capacity(new_child, get_vision_capacity(agent)) agents_list = p.get_agents(pop) agents_list.append(new_child)
def make_a_child(agent): """ règle permettant la reproduction asexuée selon une certaine probablité les agents doivent être dans un intervalle d'âge précis """ pop = get_population(agent) env = get_env(agent) min_age = p.get_pop_property(pop,"MIN_AGE_TO_MAKE_CHILDS") max_age = p.get_pop_property(pop,"MAX_AGE_TO_MAKE_CHILDS") max_pop = p.get_pop_property(pop,"MAX_POP") min_prob,max_prob = p.get_pop_property(pop,"PROB_TO_HAVE_SEX") if (min_age <= get_age(agent) <= max_age and theres_is_an_other_sex_around(agent) \ and randint(min_prob,max_prob) == 1 and p.size(pop) < max_pop ): new_child = new_instance(pop) set_age(new_child,0) set_sugar_level(agent,0) #le nouveau-née hérite du métabolisme et de la vision de capacité de son géniteur set_metabolism(new_child,get_metabolism(agent)) set_vision_capacity(new_child,get_vision_capacity(agent)) agents_list = p.get_agents(pop) agents_list.append(new_child)