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 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 eat_sugar(agent, mas): matrix = e.get_cell_matrix(m.get_env(mas)) pop = m.get_pop(mas) (col, lig) = get_pos(agent) vision = get_vision(agent) sugar_agent = get_sugar_level(agent) max_sugar_agent = get_max_sugar_capacity(agent) sugar_cell = c.get_sugar_level(matrix[lig][col]) new_sugar_agent = 0.0 new_sugar_cell = 0.0 if sugar_agent == max_sugar_agent: new_sugar_agent = max_sugar_agent new_sugar_cell = sugar_cell if sugar_agent < max_sugar_agent: if sugar_agent + sugar_cell < max_sugar_agent: new_sugar_agent = sugar_agent + sugar_cell new_sugar_cell = 0.0 if sugar_agent + sugar_cell > max_sugar_agent: new_sugar_agent = max_sugar_agent new_sugar_cell = sugar_cell + (sugar_agent - max_sugar_agent) c.set_sugar_level(matrix[lig][col], new_sugar_cell) return new_sugar_agent, new_sugar_cell
def new_instance(mas, size, cfg): pop = empty_list() set_agents(pop, []) set_env(pop, m.get_env(mas)) set_max_agent_capacity(pop, cfg["MAX_AGENT_CAPACITY"]) MAX_AGENT_CAPACITY = cfg["MAX_AGENT_CAPACITY"] AGENT_PRESENT = int(cfg["INITIAL_AGENT"]) init_agents(pop, AGENT_PRESENT, cfg) return pop
def __draw_mas(canvas, mas, cell_size): # Draw the MAS on the canvas env = m.get_env(mas) pop = m.get_pop(mas) if env is not None: __draw_env(canvas, env, cell_size) # Only consider plotting the population if there is an environment if pop is not None: __draw_pop(canvas, pop, cell_size)
def set_capacity(cell, capacity, mas): """ Set the sugar capacity of the cell. """ env = m.get_env(mas) '''if capacity < 0: raise Exception("The sugar capacity of a cell cannot be negative.") if capacity > e.get_max_capacity(env): print("Capa = ",capacity," max = ",e.get_max_capacity(env)) raise Exception("The sugar capacity of a cell cannot exceed the maximum capacity for the environment.")''' __set_property(cell, SUGAR_CAPACITY_IDX, capacity)
def __compute_cell_color(cell, mas): # Compute the color of the cell, depending on its sugar level sugar_level = c.get_sugar_level(cell) env = m.get_env(mas) max_capacity = e.get_max_capacity(env) ratio = sugar_level / max_capacity color_level = int(255 * (1 - ratio)) # convert the color level to hexadecimal representation hex_color_level = hex(color_level)[2:].zfill(2) color_code = '#' + 'ff' + 'ff' + hex_color_level return color_code
def mas_plot(mas, showGrid=True): """ Plot the complete complete MAS as a matplotlib graphic. """ env = m.get_env(mas) pop = m.get_pop(mas) plt.axes([0, 0, 1, 1], axisbg=None, frameon = False) if env is not None: env_plot(env, showGrid) # Only consider plotting the population if there is an environment if pop is not None: pop_plot(pop) plt.show()
def RA3(mas, agent): (lig, col) = get_pos(agent) matrix = e.get_cell_matrix(m.get_env(mas)) vision = get_vision(agent) res = 0 size = len(matrix) - 1 idx_position = (lig, col) metabolism = get_metabolism(agent) for i in range(vision + 1): distance = vision - i if col + i > size: for j in range(distance): if metabolism <= res < matrix[lig][j][ 1]: ##cellule[1] = taux de sucres res = matrix[lig][j][1] idx_position = (lig, j) if lig + i > size: for j in range(distance): if metabolism <= res < matrix[j][col][1]: res = matrix[j][col][1] idx_position = (j, col) if col - i < 0: for j in range(distance): if metabolism <= res < matrix[lig][size - j][1]: res = matrix[lig][size - j][1] idx_position = (lig, size - j) if lig - i < 0: for j in range(distance): if metabolism <= res < matrix[size - j][col][1]: res = matrix[size - j][col][1] idx_position = (size - j, col) else: if metabolism <= res < matrix[lig][col + i][1]: res = matrix[lig][col + i][1] idx_position = (lig, col + i) if metabolism <= res < matrix[lig][col - i][1]: res = matrix[lig][col - i][1] idx_position = (lig, col - i) if metabolism <= res < matrix[lig + i][col][1]: res = matrix[lig + i][col] idx_position = (lig + i, col) elif metabolism <= res < matrix[lig - i][col]: res = matrix[lig - i][col] idx_position = (lig - i, col) return idx_position
def add_capacity(cell, capacity, mas): """ Add capacity to a cell (in addition to the capacity) the cell already has. Usually, this function is only called by other capacity initialisation functions, e.g. "add_capacity_gaussian" in the "mas_environment" module. """ # Add the current capacity of the cell to the value of the # parameter "capacity". capacity += get_capacity(cell) # Limit it to the maximum capacity of the environment. env = m.get_env(mas) max_capacity = e.get_max_capacity(env) if capacity <= max_capacity: set_capacity(cell, capacity, mas) else: set_capacity(cell, max_capacity, mas)
def new_instance(mas, sz, nb): """ Return a new population instance of size "sz", with a number "nb" of conaminated cells and "declare" to which MAS it belongs to. """ pop = __empty_instance() set_env(pop, m.get_env(mas)) set_agents(pop, []) set_walkers(pop, []) set_mas(pop, mas) for i in range(sz): agent = a.new_instance(pop) add_agent(agent, get_agents(pop)) for i in range(nb): walker = w.new_instance(pop) add_walker(walker, get_walkers(pop)) place_first_walkers(pop) # WALKERS FIRST place_first_agents(pop) return pop
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 move_agent(agent, mas, cfg, matrix): pop = m.get_pop(mas) matrix = e.get_cell_matrix(m.get_env(mas)) size = len(matrix) if cfg["RULE_AGENT"] == 'default': new_position = agent_move_to_random_position(mas, agent, size, cfg) (col, lig) = new_position set_pos(agent, new_position) set_sugar_level( agent, float(get_sugar_level(agent)) - float(get_metabolism(agent))) new_sugar_agent, new_sugar_cell = eat_sugar(agent, mas) set_sugar_level(agent, new_sugar_agent) matrix[lig][col][1] = new_sugar_cell if cfg["RULE_AGENT"] == 'RA1': new_position = RA1(mas, agent, matrix) if cfg["RULE_AGENT"] == 'RA2': new_position = RA2(mas, agent, matrix) if cfg["RULE_AGENT"] == 'RA3': new_position = RA3(mas, agent) if cfg["RULE_AGENT"] == None: raise Exception('Invalid syntax for the moving rules') return new_position
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 get_env(pop): mas = get_mas(pop) env = m.get_env(mas) return env
# Uncoment the following line to use "static" plots. # CAUTION: This only works if matplotlib is installed. #import mas_graphics as g #================================================== # TESTING #================================================== # Read the configuration file conf = u.config_read_file("config.cfg") # Create a new MAS instance from that configuration mas = m.new_instance_from_config(conf) # Set the sugar level of each cell to its capacity # (otherwise, the agent would die immediately because # there would initially be no sugar in the environment) env = m.get_env(mas) e.set_cell_sugar_level_to_capacity(env) # Run experiment (with or without visualisation) #m.run_experiment(mas) v.run_experiment(mas) #m.show(mas) # Plot the result at the end of the experiment # CAUTION: This only works if matplotlib is installed. #g.mas_plot(mas)
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()
# Uncoment the following line to use "static" plots. # CAUTION: This only works if matplotlib is installed. #import mas_graphics as g # IT USES THE MODS TO BEGIN THE MAS. # HERE IS THUS THE REASON WHY THIS FUNCTION HAS BEEN MADE. #================================================== # TESTING #================================================== # Read the configuration file conf = u.config_read_file("test.cfg") # Create a new MAS instance from that configuration mas = m_mod.new_instance_from_config(conf) # Set the sugar level of each cell to its capacity # (otherwise, the agent would die immediately because # there would initially be no sugar in the environment) env = m.get_env(mas) e.set_cell_sugar_level_to_capacity(env) # Run experiment (with or without visualisation) #m.run_experiment(mas) v_mod.run_experiment(mas) # Plot the result at the end of the experiment # CAUTION: This only works if matplotlib is installed. #g.mas_plot(mas)