def apply_population_rules(mas, pop, cfg, matrix): liste = get_agents(pop) if cfg["RULE_POPULATION"] == 'default': for agent in liste: Default_move(agent, mas, cfg, matrix) if cfg["RULE_POPULATION"] == 'OA1': ls = copy.deepcopy(get_agents(m.get_pop(mas))) while not ls == []: x = random.randint(0, len(ls) - 1) agent = ls[x] del ls[x] Default_move(agent, mas, cfg, matrix) if cfg["RULE_POPULATION"] == 'OA2': ls = copy.deepcopy(get_agents(m.get_pop(mas))) while not ls == []: res = 0 agent1 = 0 for i in range(len(ls)): if res < ls[i][2]: res = ls[i][2] agent1 = ls[i] position = Default_move(agent1, mas, cfg, matrix) a.set_pos(agent1, position) del ls[i]
def apply_walker_rules(mas): """ Apply all walker rules to each walker of the MAS's population. """ pop = m.get_pop(mas) for walker_rule in get_walker_rules(mas): p.apply_rule_walkers(pop, walker_rule)
def RA1(mas, agent, matrix): pop = m.get_pop(mas) position = get_pos(agent) vision = get_vision(agent) idx_position = position res = 0 while vision > 0: a, z = horizontal1(matrix, vision, res, idx_position, position) b, y = horizontal2(matrix, vision, res, idx_position, position) c, x = vertical1(matrix, vision, res, idx_position, position) d, w = vertical2(matrix, vision, res, idx_position, position) res = max(a, b, c, d) if res == a: idx_position = z elif res == b: idx_position = y elif res == c: idx_position = x elif res == d: idx_position = w else: p.kill_agent(pop, agent) vision -= 1 (col, lig) = idx_position set_pos(agent, idx_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 return idx_position
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 __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 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)
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)
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 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)
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)
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 agent_move_to_random_position(mas, agent, size, cfg): pop = m.get_pop(mas) return (random.randint(1, int(size) - 1), random.randint(1, int(size) - 1))