def total_gain(agent,target): """ calculer les réserves de l’agent additionnée à l’ensemble du sucre qu’il récupérera durant son voyage soustrait au métabolisme consommé à chaque étape """ pop = get_population(agent) env = get_env(agent) pos = get_pos(agent) sugar_level = get_sugar_level(agent) #x_diff,y_diff xd,yd = u.vector_diff(pos,target) if xd == 0: #the same line steps = abs(yd) step = (1 if yd < 0 else -1) for i in range(steps): cell_refs = correct_position((pos[0],pos[1]+step*i),env) cell = e.get_cell(env,cell_refs) sugar_level += c.get_sugar_level(cell) else: #the same row steps = abs(xd) step = (1 if xd < 0 else -1) for i in range(steps): cell_refs = correct_position((pos[0]+step*i,pos[1]),env) cell = e.get_cell(env,cell_refs) sugar_level += c.get_sugar_level(cell) return sugar_level - get_metabolism(agent)*steps
def total_gain(agent, target): """ calculer les réserves de l’agent additionnée à l’ensemble du sucre qu’il récupérera durant son voyage soustrait au métabolisme consommé à chaque étape """ pop = get_population(agent) env = get_env(agent) pos = get_pos(agent) sugar_level = get_sugar_level(agent) #x_diff,y_diff xd, yd = u.vector_diff(pos, target) if xd == 0: #the same line steps = abs(yd) step = (1 if yd < 0 else -1) for i in range(steps): cell_refs = correct_position((pos[0], pos[1] + step * i), env) cell = e.get_cell(env, cell_refs) sugar_level += c.get_sugar_level(cell) else: #the same row steps = abs(xd) step = (1 if xd < 0 else -1) for i in range(steps): cell_refs = correct_position((pos[0] + step * i, pos[1]), env) cell = e.get_cell(env, cell_refs) sugar_level += c.get_sugar_level(cell) return sugar_level - get_metabolism(agent) * steps
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 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 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 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_to_the_highest_sugar_level_cell(agent): """ RA1 : déplace l'agent dans la cellule à quantité de sucre la plus élevée """ cells_refs = accecible_positions(agent) if len(cells_refs) > 0: highest = cells_refs[0] env = get_env(agent) for i in range(len(cells_refs)): cell = e.get_cell(env, cells_refs[i]) highest_cell = e.get_cell(env, highest) if c.get_sugar_level(cell) > c.get_sugar_level(highest_cell): highest = cells_refs[i] move(agent, highest)
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_to_the_lowest_sugar_level_cell(agent): """ RA3 : déplace l'agent dans la cellule à quantité de sucre la moins élevée et assez pour son métabolisme """ cells_refs = accecible_positions(agent) if len(cells_refs) > 0: lowest = cells_refs[0] env = get_env(agent) for i in range(len(cells_refs)): cell = e.get_cell(env,cells_refs[i]) lowest_cell = e.get_cell(env,lowest) if c.get_sugar_level(cell) < c.get_sugar_level(lowest_cell): lowest = cells_refs[i] move (agent,lowest)
def move_to_the_lowest_sugar_level_cell(agent): """ RA3 : déplace l'agent dans la cellule à quantité de sucre la moins élevée et assez pour son métabolisme """ cells_refs = accecible_positions(agent) if len(cells_refs) > 0: lowest = cells_refs[0] env = get_env(agent) for i in range(len(cells_refs)): cell = e.get_cell(env, cells_refs[i]) lowest_cell = e.get_cell(env, lowest) if c.get_sugar_level(cell) < c.get_sugar_level(lowest_cell): lowest = cells_refs[i] move(agent, lowest)
def move_to_the_highest_sugar_level_cell(agent): """ RA1 : déplace l'agent dans la cellule à quantité de sucre la plus élevée """ cells_refs = accecible_positions(agent) if len(cells_refs) > 0: highest = cells_refs[0] env = get_env(agent) for i in range(len(cells_refs)): cell = e.get_cell(env,cells_refs[i]) highest_cell = e.get_cell(env,highest) if c.get_sugar_level(cell) > c.get_sugar_level(highest_cell): highest = cells_refs[i] move (agent,highest)
def min_sugar_level_need( env, agent, cells_refs ): """ Renvoie la cell_ref minimum que l'agent a besoin parmi la liste des cells_refs """ # On crée une liste des cellules graces à la liste des positions. cells = [] for cell_ref in cells_refs: cells.append( e.get_cell( env, cell_ref ) ) # On a deux listes: les cellules et les positions. # On ordonne la liste des cellules en fonction du taux de sucre (Et les positions s'ordonneront en fonction aussi) u.sort_on_second_list(cells_refs, cells, order_decrease_cell_by_sugar_level) # On recherche le minimum minimal_cell_ref = cells_refs[0] minimal_cell = cells[0] #On parcout tous les éléments for temp in range( len(cells) ): temp_ref = cells_refs[temp] #Position temp_cell = cells[temp] #Cellule #Si le taux suffit à la survie et que celui-ci est plus petit que le supposé "minimal". if c.get_sugar_level( temp_cell ) >= get_metabolism( agent ) and c.get_sugar_level( minimal_cell ) > c.get_sugar_level( temp_cell ): minimal_cell_ref = temp_ref # Nouvelle position minimale minimal_cell = temp_cell # Nouvelle cellule minimale return minimal_cell_ref # Renvoie la position de la cellule
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 env_plot(env, showGrid=True): """ Add a plot of the environment (possibly including a grid) to the current pylab plot. To actually show the plot, the pylab.show() method still has to be invoked. """ ax = plt.gca() ax.set_aspect('equal', 'box') ax.xaxis.set_major_locator(plt.NullLocator()) ax.yaxis.set_major_locator(plt.NullLocator()) sz = e.size(env) mat = [] row = [] col = [] sugar = [] size_factor = 3000.0/sz/e.get_max_capacity(env) # Plot sugar level of each cell for cell_ref in e.get_cell_refs(env): cell = e.get_cell(env, cell_ref) (y, x) = cell_ref row.append(y+0.5) col.append(x+0.5) sugar.append(size_factor*c.get_sugar_level(cell)) plt.scatter(row, col, sugar, color='yellow', alpha=1)#, marker='s') # Plot the grid (or only an outer border) for k in range(sz+1): if showGrid or k == 0 or k == sz: plt.plot([k, k], [0, sz], color='black', alpha=0.1, ls='-') plt.plot([0, sz], [k, k], color='black', alpha=0.1, ls='-')
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 eat_all(agent, env): cell_ref = get_pos(agent) cell = e.get_cell(env, cell_ref ) sugar = c.get_sugar_level( cell ) # On enregistre le taux de sucre de la cellule sugar += get_sugar_level ( agent ) # On ajoute la réserve de l'agent c.set_sugar_level( cell , 0 ) # On vide la cellule set_sugar_level ( agent , sugar ) # On donne tout à l'agent
def __draw_env(canvas, env, cell_size,mas): # Draw the environment on the canvas env_size = e.size(env) for cell_ref in e.get_cell_refs(env): cell = e.get_cell(env, cell_ref) cell_ref = __swap_y(env_size, cell_ref) canvas.create_rectangle(__bbox_for_cell_ref(cell_ref, cell_size), fill=__compute_cell_color(cell,mas), outline='#dddddd')
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 __draw_env(canvas, env, cell_size): # Draw the environment on the canvas env_size = e.size(env) for cell_ref in e.get_cell_refs(env): cell = e.get_cell(env, cell_ref) cell_ref = __swap_y(env_size, cell_ref) canvas.create_rectangle(__bbox_for_cell_ref(cell_ref, cell_size), fill=__compute_cell_color(cell), outline='#dddddd')
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 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 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 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 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 is_interested_to_move(agent,target): """ - vérifier si le total_gain(agent,target) est plus grand que zèro - si un autre agent peut atteindre la cellule avant lui - si il n'y a pas d'agents dans la direction choisie """ pos = get_pos(agent) xd,yd = u.vector_diff(pos,target) env = get_env(agent) agent_on_way = False if xd == 0: step = (1 if yd < 0 else -1) next_move = correct_position((pos[0],pos[1]+step),env) agent_on_way = c.get_present_agent(e.get_cell(env,next_move)) else: step = (1 if xd < 0 else -1) next_move = correct_position((pos[0]+step,pos[1]),env) agent_on_way = c.get_present_agent(e.get_cell(env,next_move)) return (not is_an_agent_can_be_there_faster(agent,target) and not agent_on_way and total_gain(agent,target) > 0)
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 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 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 is_interested_to_move(agent, target): """ - vérifier si le total_gain(agent,target) est plus grand que zèro - si un autre agent peut atteindre la cellule avant lui - si il n'y a pas d'agents dans la direction choisie """ pos = get_pos(agent) xd, yd = u.vector_diff(pos, target) env = get_env(agent) agent_on_way = False if xd == 0: step = (1 if yd < 0 else -1) next_move = correct_position((pos[0], pos[1] + step), env) agent_on_way = c.get_present_agent(e.get_cell(env, next_move)) else: step = (1 if xd < 0 else -1) next_move = correct_position((pos[0] + step, pos[1]), env) agent_on_way = c.get_present_agent(e.get_cell(env, next_move)) return (not is_an_agent_can_be_there_faster(agent, target) and not agent_on_way and total_gain(agent, target) > 0)
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 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 sort_cells_refs_decrease( env, cells_refs ): """ Trie la liste des positions. """ # On crée une liste des cellules graces à la liste des positions. cells = [] for cell_ref in cells_refs: cells.append( e.get_cell( env, cell_ref ) ) # On a deux listes: les cellules et les positions. # On ordonne la liste des cellules en fonction du taux de sucre (Et les positions s'ordonneront en fonction aussi) u.sort_on_second_list(cells_refs, cells, order_decrease_cell_by_sugar_level)
def add_cell_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 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 c.get_present_agent(cell) != None and len( c.get_present_agent(cell)) == a.AGENT_MAX_IDX + 1: l.append(cell) l_pos.append((i % env_size, j % env_size)) return l, l_pos
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 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 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 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_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 partners_list_under_conditions(env, agent, l_pos): """ Returns the partner list of a female agent. This list must consider the conditions of age and sex of the agent and her partner. """ l_partners = [] for pos in l_pos: cell = e.get_cell(env, pos) partner = c.get_present_agent(cell) if len(partner) == AGENT_MAX_IDX +1 and get_sex(agent) == 0 and get_sex(agent) != get_sex(partner)\ and get_age(agent) > 20 and get_age(partner) > 20 and get_sugar_level(agent) > 10\ and get_sugar_level(partner): l_partners.append(partner) return l_partners
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 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 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 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
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
def get_cell(agent): return e.get_cell(get_env(agent),get_pos(agent))