def unplace_walker(env, pop, walker, pos_cell): """ Makes the walker and its aura disappear from the environment. """ cell = e.get_cell(env, get_pos(walker)) c.set_present_agent(cell, None) take_aura(pop, walker)
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 replace_walker(env, pop, walker, pos_cell): """ Makes the walker and its aura appear on the environment. """ cell = e.get_cell(env, pos_cell) set_pos(walker, pos_cell) c.set_present_agent(cell, walker) put_aura(pop, walker)
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 deplace_or_remove(env, pop, agent, cell, pos_cell): """ If the agent has enough sugar (float > 0), he can move to his next position, he dies and completely disappears otherwise. """ c.set_present_agent(e.get_cell(env, get_pos(agent)), None) if get_is_living(agent): set_pos(agent, pos_cell) c.set_present_agent(cell, agent) else: kill_agent(pop, 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 place_first_agents(pop): """ Places the agents when the mas begins at the places generated by the function new_first_pos """ env = get_env(pop) env_size = e.size(env) pop_size = len(get_agents(pop)) first_pos = new_first_pos(env, env_size, pop_size) for i in range(len(get_agents(pop))): a.set_pos(get_agents(pop)[i], first_pos[i]) c.set_present_agent(e.get_cell(env, first_pos[i]), get_agents(pop)[i])
def move_to(env, agent, target_cell_ref): cell_ref = get_pos(agent) cell_old = e.get_cell(env, cell_ref) # Ancienne cellule où l'agent se trouvait cell_new = e.get_cell(env, target_cell_ref) # Nouvelle cellule où l'agent se trouve sz = e.size(env) (x, y) = target_cell_ref target_cell_ref = ( x%sz, y%sz ) # Permet de passer de gauche à droite et de haut en bas dans la matrice environnement set_pos(agent, target_cell_ref ) # Indique à l'agent la nouvelle position. c.set_present_agent(cell_old, None) # Indique à l'ancienne cellule qu'il n'y a plus d'agent présent. c.set_present_agent(cell_new, agent) # Indique à la nouvelle cellule quel agent est présent.
def place_first_walkers(pop): """ Places the walkers when the mas begins at the places generated by the function new_first_pos """ env = get_env(pop) env_size = e.size(env) walkers_size = len(get_walkers(pop)) first_pos = new_first_pos(env, env_size, walkers_size) for i in range(len(get_walkers(pop))): w.set_pos(get_walkers(pop)[i], first_pos[i]) c.set_present_agent(e.get_cell(env, first_pos[i]), get_walkers(pop)[i]) w.put_aura(pop, get_walkers(pop)[i])
def move(agent,position): """ Change la position de l'agent """ env = get_env(agent) current_position = get_pos(agent) cell = e.get_cell(env,current_position) target_cell = e.get_cell(env,position) if c.agent_is_present(target_cell): raise Exception("cannot move an agent to a cell while theres is an other agent") set_pos(agent,position) c.set_present_agent(cell,None) c.set_present_agent(target_cell,agent)
def move(agent, position): """ Change la position de l'agent """ env = get_env(agent) current_position = get_pos(agent) cell = e.get_cell(env, current_position) target_cell = e.get_cell(env, position) if c.agent_is_present(target_cell): raise Exception( "cannot move an agent to a cell while theres is an other agent") set_pos(agent, position) c.set_present_agent(cell, None) c.set_present_agent(target_cell, agent)
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 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 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 new_instance(mas, sz): """ Return a new population instance of size "sz" and "declare" to which MAS it belongs to. """ env = m.get_env( mas ) agents = [] # Les 'sz' agents for x in range ( sz ): cell_ref = e.random_cell_ref_without_agent(env) # On récupère la position d'une cellule sans agent agent = a.new_random() # On crée un agent avec des valeurs aléatoires a.set_pos( agent, cell_ref ) # On modifie la position de l'agent cell = e.get_cell( env, cell_ref ) # On récupère la cellule (liste) via la position c.set_present_agent( cell, agent ) # On indique à la cellule qu'un agent est présent agents.append ( agent ) # On ajoute l'agent dans la liste des 'sz' agents population = __empty_instance() set_agents( population, agents ) set_env( population, env ) return population
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 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 kill(pop, env, agent): cell_ref = get_pos(agent) cell = e.get_cell(env, cell_ref) c.set_present_agent(cell, None) # Dis qu'il n'y a plus d'agent présent dans la cellule p.remove_agent(pop, agent) # On retire l'agent de la liste de la population