def test___iter__(self): agent1 = Agent(agent_id='1') agent2 = Agent(agent_id='2') agent3 = Agent(agent_id='3') self.hierarchy.agents = [agent1, agent2, agent3] agents = [a for a in self.hierarchy] self.assertEqual(agents, [agent1, agent2, agent3])
def test_add_2_agents(self): agent1 = Agent(agent_id='1', name='Agent 1') agent2 = Agent(agent_id='2', name='Agent 2') self.hierarchy.agents = [agent1, agent2] self.assertEqual(self.hierarchy._agent_name_map, { agent1: 'Agent 1', agent2: 'Agent 2' })
def test_name_agent_map(self): agent_1_1 = Agent(agent_id='1', name='Agent') agent_1_2 = Agent(agent_id='2', name='Bgent') self.hierarchy.agents = [agent_1_1, agent_1_2] self.assertEqual(self.hierarchy.name_agent_map, { 'Agent': agent_1_1, 'Bgent': agent_1_2 })
def test_agent_exists_error(self): """Creating additional agent with the same ID raises an Exception.""" agent1 = Agent(agent_id='1') agent2 = Agent(agent_id='1') with self.assertRaisesRegex( AgentExistsError, r'HIERARCHY_1 already has an agent with ID 1\.', ): self.hierarchy.agents = [agent1, agent2]
def test_set_agent_name(self): """Setting the agent name updates the hierarchy.""" agent = Agent(agent_id='1', name='Agent') self.assertEqual(agent.name, 'Agent') agent.name = 'Bgent' self.assertEqual(agent.name, 'Bgent') self.hierarchy.agents = [agent] self.assertEqual(self.hierarchy._agent_name_map, {agent: 'Bgent'}) self.assertEqual(self.hierarchy.name_agent_map, {'Bgent': agent})
def test_preference_position(self): agent_2_1 = Agent(agent_id='1') agent_2_2 = Agent(agent_id='2') agent_2_3 = Agent(agent_id='3') self.agent.preferences = [agent_2_1, agent_2_2, agent_2_3] self.assertEqual(self.agent.preference_position(agent_2_1), 1) self.assertEqual(self.agent.preference_position(agent_2_2), 2) self.assertEqual(self.agent.preference_position(agent_2_3), 3)
class TestAgent(unittest.TestCase): def setUp(self): self.agent = Agent(agent_id='1') self.hierarchy = Hierarchy(level=1) def test___str__(self): self.assertEqual(str(self.agent), 'AGENT_1') def test_upper_capacity(self): self.agent.capacities = [0, 1] self.assertEqual(self.agent.upper_capacity, 1) def test_lower_capacity(self): self.agent.capacities = [0, 1] self.assertEqual(self.agent.lower_capacity, 0) def test_capacity_difference(self): self.agent.capacities = [0, 1] self.assertEqual(self.agent.capacity_difference, 1) def test_preference_position(self): agent_2_1 = Agent(agent_id='1') agent_2_2 = Agent(agent_id='2') agent_2_3 = Agent(agent_id='3') self.agent.preferences = [agent_2_1, agent_2_2, agent_2_3] self.assertEqual(self.agent.preference_position(agent_2_1), 1) self.assertEqual(self.agent.preference_position(agent_2_2), 2) self.assertEqual(self.agent.preference_position(agent_2_3), 3) def test_preference_position_tie(self): agent_2_1 = Agent(agent_id='1') agent_2_2 = Agent(agent_id='2') agent_2_3 = Agent(agent_id='3') agent_2_4 = Agent(agent_id='4') self.agent.preferences = [agent_2_1, [agent_2_2, agent_2_3], agent_2_4] self.assertEqual(self.agent.preference_position(agent_2_1), 1) self.assertEqual(self.agent.preference_position(agent_2_2), 2) self.assertEqual(self.agent.preference_position(agent_2_3), 2) self.assertEqual(self.agent.preference_position(agent_2_4), 3) def test_preference_position_no_preference(self): other = Agent(agent_id='1') self.assertEqual(self.agent.preferences, []) self.assertEqual(self.agent.preference_position(other), 0)
def create_agents(self) -> None: """Create agents and add to hierarchies in reverse order, setting the preferences as we go. """ number_of_hierarchies = len(self.file_data_objects) upper_hierarchy = Hierarchy(level=number_of_hierarchies + 1) for file_data, lower_hierarchy in zip(reversed(self.file_data_objects), reversed(self.hierarchies)): for line in file_data.file_content: preferences = [ upper_hierarchy.name_agent_map.get(preference) for preference in line.raw_preferences ] agent = Agent(capacities=line.capacities, preferences=preferences, name=line.raw_name) lower_hierarchy.agents.append(agent) upper_hierarchy = lower_hierarchy
def test_number_of_agents(self): self.assertEqual(self.hierarchy.number_of_agents, 0) agent = Agent(agent_id='1') self.hierarchy.agents = [agent] self.assertEqual(self.hierarchy.number_of_agents, 1)
def test_add_1_agent(self): agent = Agent(agent_id='1', name='Agent') self.hierarchy.agents = [agent] self.assertEqual(self.hierarchy._agent_name_map, {agent: 'Agent'})
def setUp(self): self.agent = Agent(agent_id='1') self.hierarchy = Hierarchy(level=1)
def setUp(self): self.cost = spa_cost self.students = Hierarchy(level=1) self.projects = Hierarchy(level=2) self.supervisors = Hierarchy(level=3) self.supervisor1 = Agent(agent_id='1', capacities=(1, 2), name='Supervisor 1') self.supervisor2 = Agent(agent_id='2', capacities=(0, 2), name='Supervisor 2') self.supervisor3 = Agent(agent_id='3', capacities=(0, 2), name='Supervisor 3') self.supervisor4 = Agent(agent_id='4', capacities=(0, 2), name='Supervisor 4') self.supervisors.agents = [ self.supervisor1, self.supervisor2, self.supervisor3, self.supervisor4 ] self.project1 = Agent( agent_id='5', capacities=(0, 2), preferences=[[self.supervisor1, self.supervisor2]], name='Project 1') self.project2 = Agent( agent_id='6', capacities=(0, 2), preferences=[[self.supervisor2, self.supervisor3], [self.supervisor1, self.supervisor4]], name='Project 2') self.projects.agents = [self.project1, self.project2] self.student1 = Agent(agent_id='7', capacities=(0, 1), preferences=[self.project1, self.project2], name='Student 1') self.student2 = Agent(agent_id='8', capacities=(0, 1), preferences=[self.project2], name='Student 2') self.student3 = Agent(agent_id='9', capacities=(0, 1), preferences=[self.project1, self.project2], name='Student 3') self.students.agents = [self.student1, self.student2, self.student3] self.graph = AllocationGraph.with_edges( hierarchies=[self.students, self.projects, self.supervisors], cost=self.cost) self.source = self.graph.source self.sink = self.graph.sink self.example_flow = { self.source: { AgentNode(self.student1, POSITIVE): 1, AgentNode(self.student2, POSITIVE): 1, AgentNode(self.student3, POSITIVE): 1, }, AgentNode(self.student1, POSITIVE): { AgentNode(self.student1, NEGATIVE): 1 }, AgentNode(self.student2, POSITIVE): { AgentNode(self.student2, NEGATIVE): 1 }, AgentNode(self.student3, POSITIVE): { AgentNode(self.student3, NEGATIVE): 1 }, AgentNode(self.student1, NEGATIVE): { AgentNode(self.project1, POSITIVE): 1, AgentNode(self.project2, POSITIVE): 0 }, AgentNode(self.student2, NEGATIVE): { AgentNode(self.project2, POSITIVE): 1 }, AgentNode(self.student3, NEGATIVE): { AgentNode(self.project1, POSITIVE): 1, AgentNode(self.project2, POSITIVE): 0 }, AgentNode(self.project1, POSITIVE): { AgentNode(self.project1, NEGATIVE): 2 }, AgentNode(self.project2, POSITIVE): { AgentNode(self.project2, NEGATIVE): 1 }, AgentNode(self.project1, NEGATIVE): { AgentNode(self.supervisor1, POSITIVE): 1, AgentNode(self.supervisor2, POSITIVE): 1 }, AgentNode(self.project2, NEGATIVE): { AgentNode(self.supervisor1, POSITIVE): 0, AgentNode(self.supervisor2, POSITIVE): 0, AgentNode(self.supervisor3, POSITIVE): 1, AgentNode(self.supervisor4, POSITIVE): 0 }, AgentNode(self.supervisor1, POSITIVE): { AgentNode(self.supervisor1, NEGATIVE): 0 }, AgentNode(self.supervisor2, POSITIVE): { AgentNode(self.supervisor2, NEGATIVE): 1 }, AgentNode(self.supervisor3, POSITIVE): { AgentNode(self.supervisor3, NEGATIVE): 1 }, AgentNode(self.supervisor4, POSITIVE): { AgentNode(self.supervisor4, NEGATIVE): 0 }, AgentNode(self.supervisor1, NEGATIVE): { self.sink: 1 }, AgentNode(self.supervisor2, NEGATIVE): { self.sink: 1 }, AgentNode(self.supervisor3, NEGATIVE): { self.sink: 1 }, AgentNode(self.supervisor4, NEGATIVE): { self.sink: 0 }, }
def test_upper_capacity_sum(self): agent1 = Agent(agent_id='1', capacities=[0, 1]) agent2 = Agent(agent_id='2', capacities=[0, 10]) agent3 = Agent(agent_id='3', capacities=[0, 100]) self.hierarchy.agents = [agent1, agent2, agent3] self.assertEqual(self.hierarchy.upper_capacity_sum, 111)
def test_has_agent_with_id(self): agent = Agent(agent_id='1') self.hierarchy.agents = [agent] self.assertEqual(self.hierarchy._has_agent_with_id('1'), True) self.assertEqual(self.hierarchy._has_agent_with_id('2'), False)
def test_max_preference_length(self): # Level 1 Agents. agent_1_1 = Agent(agent_id='1') agent_1_2 = Agent(agent_id='2') agent_1_3 = Agent(agent_id='3') self.hierarchy.agents = [agent_1_1, agent_1_2, agent_1_3] # Level 2 Agents. hierarchy2 = Hierarchy(level=2) agent_2_1 = Agent(agent_id='1') agent_2_2 = Agent(agent_id='2') agent_2_3 = Agent(agent_id='3') agent_2_4 = Agent(agent_id='4') agent_2_5 = Agent(agent_id='5') hierarchy2.agents = [ agent_2_1, agent_2_2, agent_2_3, agent_2_4, agent_2_5 ] agent_1_1.preferences = [agent_2_1, agent_2_2] agent_1_2.preferences = [agent_2_3, agent_2_4] agent_1_3.preferences = [agent_2_1, agent_2_3, agent_2_5] self.assertEqual(self.hierarchy.max_preferences_length, 3)
def test_preferred(self): # Level 1 agents. agent_1_1 = Agent(agent_id='1') agent_1_2 = Agent(agent_id='2') agent_1_3 = Agent(agent_id='3') self.hierarchy.agents = [agent_1_1, agent_1_2, agent_1_3] # Level 2 agents. agent_2_1 = Agent(agent_id='1') agent_2_2 = Agent(agent_id='2') agent_2_3 = Agent(agent_id='3') agent_2_4 = Agent(agent_id='4') agent_2_5 = Agent(agent_id='5') agent_1_1.preferences = [agent_2_1, agent_2_2, agent_2_3] agent_1_2.preferences = [agent_2_2, agent_2_3, agent_2_4] agent_1_3.preferences = [agent_2_5] test_cases = [ # Size 1 subsets. ({agent_1_1}, [agent_2_1, agent_2_2, agent_2_3]), ({agent_1_2}, [agent_2_2, agent_2_3, agent_2_4]), ({agent_1_3}, [agent_2_5]), # Size 2 subsets. ({agent_1_1, agent_1_2}, [agent_2_1, agent_2_2, agent_2_3, agent_2_4]), ({agent_1_1, agent_1_3}, [agent_2_1, agent_2_2, agent_2_3, agent_2_5]), ({agent_1_2, agent_1_3}, [agent_2_2, agent_2_3, agent_2_4, agent_2_5]), # Size 3 subset. ({agent_1_1, agent_1_2, agent_1_3}, [agent_2_1, agent_2_2, agent_2_3, agent_2_4, agent_2_5]), ] for agent_subset, expected in test_cases: self.assertEqual(self.hierarchy.preferred(agent_subset), expected)
def test_preference_position_no_preference(self): other = Agent(agent_id='1') self.assertEqual(self.agent.preferences, []) self.assertEqual(self.agent.preference_position(other), 0)
def setUp(self): self.hierarchy = Hierarchy(level=1) self.agent = Agent(agent_id='1') self.positive_node = AgentNode(agent=self.agent, polarity=POSITIVE) self.negative_node = AgentNode(agent=self.agent, polarity=NEGATIVE)