def RA2_is_possible( env, agent, target ): agent_ref = get_pos( agent ) result = u.vector_diff( target, agent_ref ) target_unit = vector_one_max( result ) cell_ref = u.vector_sum( agent_ref, target_unit ) cell = e.get_cell( env, cell_ref ) return ( not c.agent_is_present(cell) ) and get_metabolism(agent) <= get_sugar_level(agent) + c.get_sugar_level(cell)
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 add_partner_in_places(env, env_size, l, l_pos, i, j): """ Adds two different elements to two different lists. The first one is composed by the cells containing potential males and the second one contains the positions of this cells in the environment. """ cell = e.get_cell(env, (i % env_size, j % env_size)) if c.agent_is_present(cell): l.append(cell) l_pos.append((i % env_size, j % env_size))
def theres_is_an_other_sex_around(agent): """ évaluer la présence d’un potentiel (dans le sens ou il peut se reproduire avec) agent à proximité """ vision = 1 x,y = get_pos(agent) agent_sex = get_sex(agent) env = get_env(agent) already_found = False i = -vision while i <= vision and not already_found: if i !=0: cell_x = e.get_cell(env,correct_position((x,y+vision),env)) cell_y = e.get_cell(env,correct_position((x+vision,y),env)) if c.agent_is_present(cell_x): already_found = (get_sex(c.get_present_agent(cell_x)) != agent_sex) if c.agent_is_present(cell_y): already_found = (get_sex(c.get_present_agent(cell_y)) != agent_sex) i+=1 return already_found
def is_possible_to_move(agent,position): """ vérifie si un agent peut se déplcer, on vérifie si: la quantité de sucre de la cellule additionnée à la réserve de sucre de l'agent est suffisante au métabolisme, si il n'y a pas d'agent dans sa cellule """ env = get_env(agent) cell = e.get_cell(env,position) possible_to_move = not c.agent_is_present(cell) \ and (c.get_sugar_level(cell)+get_sugar_level(agent)) >= get_metabolism(agent) return possible_to_move
def is_possible_to_move(agent, position): """ vérifie si un agent peut se déplcer, on vérifie si: la quantité de sucre de la cellule additionnée à la réserve de sucre de l'agent est suffisante au métabolisme, si il n'y a pas d'agent dans sa cellule """ env = get_env(agent) cell = e.get_cell(env, position) possible_to_move = not c.agent_is_present(cell) \ and (c.get_sugar_level(cell)+get_sugar_level(agent)) >= get_metabolism(agent) return possible_to_move
def possible_cell_position(env, walker, pos): """ Returns the position where the walker will have to go if there is no agent in the cell the position belongs to. """ if not c.agent_is_present(e.get_cell(env, pos)) or c.get_present_agent( e.get_cell(env, pos)) == "alerte": pos_cell = pos else: pos_cell = get_pos(walker) return pos_cell
def random_cell_ref_without_agent(env): """ Return a random position in the environment that has no agent on it (e.g., for agent initialisation). Assumption: There is at least one free cell in the environment. """ # Note: This is not the most efficient approach! continue_search = True while continue_search: cell_ref = random_cell_ref(env) cell = get_cell(env, cell_ref) continue_search = c.agent_is_present(cell) return cell_ref
def possible_cell_position(env, agent, pos): """ Returns a tuple of two int containing the next position of an agent. """ if c.get_present_agent(e.get_cell( env, pos)) == "alerte" and agent_is_warrior(agent): pos_cell = pos elif not c.agent_is_present(e.get_cell(env, pos)): pos_cell = pos else: pos_cell = get_pos(agent) return pos_cell
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 theres_is_an_other_sex_around(agent): """ évaluer la présence d’un potentiel (dans le sens ou il peut se reproduire avec) agent à proximité """ vision = 1 x, y = get_pos(agent) agent_sex = get_sex(agent) env = get_env(agent) already_found = False i = -vision while i <= vision and not already_found: if i != 0: cell_x = e.get_cell(env, correct_position((x, y + vision), env)) cell_y = e.get_cell(env, correct_position((x + vision, y), env)) if c.agent_is_present(cell_x): already_found = (get_sex(c.get_present_agent(cell_x)) != agent_sex) if c.agent_is_present(cell_y): already_found = (get_sex(c.get_present_agent(cell_y)) != agent_sex) i += 1 return already_found
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 add_cell_in_places(agent, env, env_size, l, l_pos, i, j): """ Adds two different elements to two different lists. The first one is composed by the cells containing potential places and the second one contains the positions of this cells in the environment. """ cell = e.get_cell(env, (i % env_size, j % env_size)) if agent_is_warrior(agent) and c.get_present_agent(cell) == "alerte": l.append(cell) l_pos.append((i % env_size, j % env_size)) if not c.agent_is_present(cell): l.append(cell) l_pos.append((i % env_size, j % env_size)) return l, l_pos
def new_first_pos(env, env_size, pop_size): """ Generates a list containing tuples of 2D positions which are all in the same environment and at a different place. """ first_pos = [] for i in range(pop_size): x, y = randrange(env_size), randrange(env_size) cell = e.get_cell(env, (x, y)) while (x, y) in first_pos and c.agent_is_present(cell): x, y = randrange(env_size), randrange(env_size) cell = e.get_cell(env, (x, y)) else: first_pos.append((x, y)) return first_pos
def friend_need(env, agent, target): """ Regarde dans la vision de l'agent si d'autres agent on besoin du sucre dans la cible. """ cells_refs = a.get_cells_refs( env, agent ) # On parcourt les cellules en vision, voir si des agents sont dans le besoin for cell_ref in cells_refs: cell = get_cell(env, cell_ref) if c.agent_is_present( cell ): agent = c.get_present_agent(cell) if a.get_sugar_level(agent) < a.get_metabolism(agent) and a.is_in_vision(agent, env, target): return True return False ############################################################################
def average_around_agents_sugar_level(env, l_good_pos): """ Returns a list containing list containing the average of the sugar level of the agents around the cells where the agent can go. """ vision = 3 l_average_res = [] for pos in l_good_pos: x, y = pos tot_list = [] for i in range(x - vision, x + vision + 1): for j in range(y - vision, y + vision + 1): cell = e.get_cell(env, (i, j)) if (i, j) != (x, y) and c.agent_is_present(cell) and len( c.get_present_agent(cell)) == AGENT_MAX_IDX + 1: agent = c.get_present_agent(cell) tot_list.append(get_sugar_level(agent)) add_average_sugar_level(tot_list, l_average_res) return l_average_res
def get_cells_refs ( env, agent ): """ Renvoie une liste des mouvements possibles aux alentours de l'agent. """ agent_ref = get_pos(agent) # Position agent vision = get_vision ( agent ) # Vision agent vectors = [] # Liste de vecteurs for move in range(-vision, vision+1): if move != 0: vectors.append( [move,0] ) vectors.append( [0,move] ) cells_refs = [] for vec in vectors: cell_ref = u.vector_sum( agent_ref, vec ) # On somme la position de l'agent cell = e.get_cell( env, cell_ref ) # La cellule if not c.agent_is_present( cell ): cells_refs.append( vec ) cells_refs = list( u.vector_list_sum( cells_refs, agent_ref ) ) cells_refs.append( agent_ref ) # Car la cellule peut rester sur place return cells_refs