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 agent_plot(agent): if a.get_is_living(agent): pos = a.get_pos(agent) env = a.get_env(agent) plt.scatter([pos[0] + 0.5], [pos[1] + 0.5], color='red', s=500.0 / e.size(env))
def run_experiment(mas, config, window_size=600): """ Run an experiment on the initialised MAS with an animated graphical representation. """ # Initialise the graphical environment (tkinter) app = tk.Tk() app.geometry(str(window_size-TEXT_HEIGHT) + 'x' + str(window_size)) canvas = tk.Canvas(app, width=window_size-TEXT_HEIGHT, height=window_size) canvas.pack() cell_size = (window_size - 2*MARGIN - TEXT_HEIGHT) / e.size(m.get_env(mas)) # Define a local function that represents the "graphical loop" def tki_experiment_loop(mas): cycle = m.get_cycle(mas) canvas.delete(tk.ALL) # Clear the canvas __draw_mas(canvas, mas, cell_size) pop = m.get_pop(mas) canvas.create_text(MARGIN, MARGIN, anchor=tk.NW, text="Cycle #" + str(cycle)) if not ending_condition(mas): m.increment_cycle(mas) m.run_one_cycle(mas,config) app.update() app.after(TIME_OF_FRAME, tki_experiment_loop, mas) # Initialise the experiment (the MAS) m.set_cycle(mas, 0) ending_condition = m.get_ending_condition(mas) # Run the experiment tki_experiment_loop(mas) app.mainloop()
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 __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 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 correct_position(position, env): """ corrige la position : passer d'une cellule du bord de l'environement à la cellule opposé """ n_line = n_row = e.size(env) x, y = position return (x % n_row, y % n_line)
def run_experiment(mas, window_size=600): """ Run an experiment on the initialised MAS with an animated graphical representation. """ # Initialise the graphical environment (tkinter) app = tk.Tk() app.geometry(str(window_size-TEXT_HEIGHT) + 'x' + str(window_size)) canvas = tk.Canvas(app, width=window_size-TEXT_HEIGHT, height=window_size) canvas.pack() cell_size = (window_size - 2*MARGIN - TEXT_HEIGHT) / e.size(m.get_env(mas)) # Define a local function that represents the "graphical loop" def tki_experiment_loop(mas): cycle = m.get_cycle(mas) canvas.delete(tk.ALL) # Clear the canvas __draw_mas(canvas, mas, cell_size) pop = m.get_pop(mas) canvas.create_text(MARGIN, MARGIN, anchor=tk.NW, text="Cycle #" + str(cycle)) if not ending_condition(mas): m.increment_cycle(mas) m.run_one_cycle(mas) app.update() app.after(TIME_OF_FRAME, tki_experiment_loop, mas) # Initialise the experiment (the MAS) m.set_cycle(mas, 0) ending_condition = m.get_ending_condition(mas) # Run the experiment tki_experiment_loop(mas) app.mainloop()
def correct_position(position,env): """ corrige la position : passer d'une cellule du bord de l'environement à la cellule opposé """ n_line = n_row = e.size(env) x,y = position return (x % n_row, y % n_line)
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 __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 __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 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 __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 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 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 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 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 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 run_experiment(mas, window_size=600): """ Run an experiment on the initialised MAS with an animated graphical representation. """ # Initialise the graphical environment (tkinter) app = tk.Tk() app.geometry(str(window_size-TEXT_HEIGHT) + 'x' + str(window_size)) canvas = tk.Canvas(app, width=window_size-TEXT_HEIGHT, height=window_size) canvas.pack() cell_size = (window_size - 2*MARGIN - TEXT_HEIGHT) / e.size(m.get_env(mas)) # Define a local function that represents the "graphical loop" def tki_experiment_loop(mas): cycle = m.get_cycle(mas) canvas.delete(tk.ALL) # Clear the canvas __draw_mas(canvas, mas, cell_size) pop = m.get_pop(mas) canvas.create_text(MARGIN, MARGIN, anchor=tk.NW, text="Cycle #" + str(cycle)) male,female = p.get_agents_alive_by_sex(pop) canvas.create_text(MARGIN, MARGIN-15, anchor=tk.NW, text="Population #" + str(male+female)) canvas.create_text(MARGIN+100, MARGIN, anchor=tk.NW, text="Femme #" + str(female)) canvas.create_oval(MARGIN+180,MARGIN-4,MARGIN+188,MARGIN-12,fill='black') canvas.create_text(MARGIN+100, MARGIN-15, anchor=tk.NW, text="Homme #" + str(male)) canvas.create_oval(MARGIN+180,MARGIN+3,MARGIN+188,MARGIN+11,fill='red') canvas.create_text(MARGIN+200, MARGIN,anchor=tk.NW, text="Dead agents #" + str(p.get_dead_agents(pop))) if not eval("m."+ending_condition+"(mas)"): m.increment_cycle(mas) m.run_one_cycle(mas) app.update() app.after(TIME_OF_FRAME, tki_experiment_loop, mas) # Initialise the experiment (the MAS) m.set_cycle(mas, 0) ending_condition = m.get_ending_condition(mas) # Run the experiment tki_experiment_loop(mas) app.mainloop()
def agent_plot(agent): if a.get_is_living(agent): pos = a.get_pos(agent) env = a.get_env(agent) plt.scatter([pos[0]+0.5], [pos[1]+0.5], color='red', s=500.0/e.size(env))
def run_experiment(mas, window_size=600): """ Run an experiment on the initialised MAS with an animated graphical representation. """ # Initialise the graphical environment (tkinter) app = tk.Tk() app.geometry(str(window_size - TEXT_HEIGHT) + 'x' + str(window_size)) canvas = tk.Canvas(app, width=window_size - TEXT_HEIGHT, height=window_size) canvas.pack() cell_size = (window_size - 2 * MARGIN - TEXT_HEIGHT) / e.size( m.get_env(mas)) # Define a local function that represents the "graphical loop" def tki_experiment_loop(mas): cycle = m.get_cycle(mas) canvas.delete(tk.ALL) # Clear the canvas __draw_mas(canvas, mas, cell_size) pop = m.get_pop(mas) canvas.create_text(MARGIN, MARGIN, anchor=tk.NW, text="Cycle #" + str(cycle)) male, female = p.get_agents_alive_by_sex(pop) canvas.create_text(MARGIN, MARGIN - 15, anchor=tk.NW, text="Population #" + str(male + female)) canvas.create_text(MARGIN + 100, MARGIN, anchor=tk.NW, text="Femme #" + str(female)) canvas.create_oval(MARGIN + 180, MARGIN - 4, MARGIN + 188, MARGIN - 12, fill='black') canvas.create_text(MARGIN + 100, MARGIN - 15, anchor=tk.NW, text="Homme #" + str(male)) canvas.create_oval(MARGIN + 180, MARGIN + 3, MARGIN + 188, MARGIN + 11, fill='red') canvas.create_text(MARGIN + 200, MARGIN, anchor=tk.NW, text="Dead agents #" + str(p.get_dead_agents(pop))) if not eval("m." + ending_condition + "(mas)"): m.increment_cycle(mas) m.run_one_cycle(mas) app.update() app.after(TIME_OF_FRAME, tki_experiment_loop, mas) # Initialise the experiment (the MAS) m.set_cycle(mas, 0) ending_condition = m.get_ending_condition(mas) # Run the experiment tki_experiment_loop(mas) app.mainloop()