def test_remove_agent(self): model = Model(5) xsize = 20 ysize = 25 grid_name = "sampleGridA" grid_name_b = "sampleGridB" env = ObjectGrid2D(grid_name, xsize, ysize, model) env_b = ObjectGrid2D(grid_name_b, xsize, ysize, model) pos = (5, 5) pos_b = (5, 6, 7) a = AgentX() a.add_agent_to_grid(grid_name, pos, model) a.add_agent_to_grid(grid_name_b, pos_b, model) model.schedule.agents.add(a) self.assertEqual(1, env.grid[pos].__len__()) self.assertEqual(1, env_b.grid[pos_b].__len__()) self.assertEqual(pos, a.environment_positions[grid_name]) self.assertEqual(pos_b, a.environment_positions[grid_name_b]) a.remove_agent(model) self.assertIsNone(a.environment_positions[grid_name]) self.assertIsNone(a.environment_positions[grid_name_b]) self.assertEqual(0, env.grid[pos].__len__()) self.assertEqual(0, env_b.grid[pos_b].__len__()) self.assertEqual(a, model.schedule.agents_to_remove.pop())
def test_get_least_populated_moore_neigh_edgecase(self): model = Model(5) grid_name = "sampleGridJ" env = ObjectGrid2D(grid_name, 50, 50, model) self.assertEqual(env.get_least_populated_moore_neigh((100, 100)), None)
def test_get_least_populated_moore_neigh_(self): model = Model(5) grid_name = "sampleGridI" env = ObjectGrid2D(grid_name, 50, 50, model) a = AgentX() b = AgentX() c = AgentX() d = AgentX() e = AgentX() f = AgentX() g = AgentX() a.add_agent_to_grid(grid_name, (19, 21), model) b.add_agent_to_grid(grid_name, (20, 21), model) c.add_agent_to_grid(grid_name, (21, 21), model) d.add_agent_to_grid(grid_name, (19, 20), model) e.add_agent_to_grid(grid_name, (21, 20), model) f.add_agent_to_grid(grid_name, (19, 19), model) g.add_agent_to_grid(grid_name, (20, 19), model) self.assertEqual(env.get_least_populated_moore_neigh((20, 20)), (21, 19))
def test_move_agent_valid(self): model = Model(5) grid_name = "sampleGridB" ObjectGrid2D(grid_name, 20, 20, model) agent = SimpleAgent() position = (5, 6) agent.add_agent_to_grid(grid_name, position, model) position_b = (6, 7) agent.move_agent(grid_name, position_b, model) self.assertEqual(agent.environment_positions[grid_name], position_b) self.assertEqual(len(model.environments[grid_name].grid[position]), 0) self.assertEqual(len(model.environments[grid_name].grid[position_b]), 1) position_c = (9, 9) agent.move_agent(grid_name, position_c, model) self.assertEqual(agent.environment_positions[grid_name], position_c) self.assertEqual(len(model.environments[grid_name].grid[position_b]), 0) self.assertEqual(model.environments[grid_name].grid[position_c].pop(), agent)
def test_add_agent_invalid(self): model = Model(5) grid_name = "sampleGridB" ObjectGrid2D(grid_name, 20, 20, model) agent = SimpleAgent() position = (50, 50) agent.add_agent_to_grid(grid_name, position, model) self.assertEqual(len(agent.environment_positions), 0)
def test_add_agent_to_grid(self): model = Model(5) grid_name = "sampleGridB" ObjectGrid2D(grid_name, 10, 10, model) agent = SimpleAgent() position = (5, 6) agent.add_agent_to_grid(grid_name, position, model) self.assertEqual(agent.environment_positions[grid_name], position) self.assertEqual(model.environments[grid_name].grid[position].pop(), agent)
def test_remove_agent_grid_2d(self): model = Model(5) xsize = 20 ysize = 25 grid_name = "sampleGridA" env = ObjectGrid2D(grid_name, xsize, ysize, model) pos = (5, 5) a = AgentX() a.add_agent_to_grid(grid_name, pos, model) self.assertEqual(1, env.grid[pos].__len__()) env.remove_agent(a, pos) self.assertEqual(0, env.grid[pos].__len__())
def test_object_grid_2d_sample(self): model = Model(5) xsize = 20 ysize = 25 grid_name = "sampleGridA" env = ObjectGrid2D(grid_name, xsize, ysize, model) self.assertEqual(env.xsize, xsize) self.assertEqual(env.ysize, ysize) self.assertEqual(env.name, grid_name) self.assertEqual(len(env.grid[(0, 0)]), 0) self.assertEqual(env, model.environments[grid_name])
def test_get_most_populated_moore_neigh(self): model = Model(5) grid_name = "sampleGridG" env = ObjectGrid2D(grid_name, 50, 50, model) a = AgentX() b = AgentX() c = AgentX() max_pos = (20, 20) a.add_agent_to_grid(grid_name, max_pos, model) b.add_agent_to_grid(grid_name, max_pos, model) c.add_agent_to_grid(grid_name, (19, 20), model) self.assertEqual(env.get_most_populated_moore_neigh((19, 19)), max_pos)
def test_remove_agent_from_grid(self): model = Model(5) xsize = 20 ysize = 25 grid_name = "sampleGridA" env = ObjectGrid2D(grid_name, xsize, ysize, model) pos = (5, 5) a = AgentX() a.add_agent_to_grid(grid_name, pos, model) self.assertEqual(1, env.grid[pos].__len__()) self.assertEqual(pos, a.environment_positions[grid_name]) a.remove_agent_from_grid(grid_name, model) self.assertEqual(0, env.grid[pos].__len__()) self.assertIsNone(a.environment_positions[grid_name])
def setup_model(num_agents, num_infectious, spatial_mode=True, max_num_epochs=1000): model = Model(max_num_epochs) xsize = ysize = 30 ObjectGrid2D("virus_env", xsize, ysize, model) peak_infections = list() model.properties['spatial_transmission'] = spatial_mode for i in range(num_agents): xlimit,ylimit = env_limit(model.environments["virus_env"]) agent_position = (random.randint(0,xlimit),random.randint(0,ylimit)) agent = SimpleAgent(i,agent_position) model.schedule.agents.add(agent) agent.add_agent_to_grid("virus_env", agent_position, model) for p in range(1,num_infectious+1): agent_position2 = (random.randint(0,xlimit),random.randint(0,ylimit)) ill_agent = SimpleAgent(p+i,agent_position2,initial_state=State.infectious) model.schedule.agents.add(ill_agent) ill_agent.add_agent_to_grid("virus_env",agent_position2, model) return model
return if current_position[0] == xlimit: new_position = (0, current_position[1] + 1) else: new_position = (current_position[0] + 1, current_position[1]) self.move_agent("agent_env", new_position, model) xsize = ysize = 20 model = Model(xsize * ysize + 5) # Creating the grid automatically binds it to the model ObjectGrid2D("agent_env", xsize, ysize, model) numerical_env = NumericalGrid2D("value_env", xsize, ysize, model) # Adding the agent to the schedule and to the environment agent = SimpleAgent() model.schedule.agents.add(agent) agent.add_agent_to_grid("agent_env", (0, 0), model) val = 0 for y in range(ysize): for x in range(xsize): numerical_env.grid[(x, y)] = val val += 1 model.run()