def test_rule_with_model(self): hints = DistributionHints(must_host={ 'a1': ['v1'], 'a3': ['v2'] }, host_with={'m1': ['mf1']}) agents = [ AgentDef('a{}'.format(i), capacity=100) for i in range(1, 11) ] agent_mapping = distribute(self.cg, agents, hints, computation_memory=lambda x: 10) # rule should be hosted either with model m1 or variable v2 # print(agent_mapping.agent_for('m1'), agent_mapping.agent_for('mf1'), # agent_mapping.agent_for('v1'), agent_mapping.agent_for('v2'), # agent_mapping.agent_for('r1')) self.assertTrue( agent_mapping.agent_for('r1') == agent_mapping.agent_for('v2') or agent_mapping.agent_for('m1') == agent_mapping.agent_for('r1')) self.assertTrue(is_all_hosted(self.cg, agent_mapping))
def test_host_on_highest_dependent_agent(self): d1 = VariableDomain('d1', '', [1, 2, 3, 5]) v1 = Variable('v1', d1) v2 = Variable('v2', d1) v3 = Variable('v3', d1) f1 = relation_from_str('f1', 'v1 + v2', [v1, v2]) f2 = relation_from_str('f2', 'v1 - v2 + v3', [v1, v2, v3]) cv1 = VariableComputationNode(v1, ['f1', 'f2']) cv2 = VariableComputationNode(v2, ['f1', 'f2']) cv3 = VariableComputationNode(v3, ['f2']) cf1 = FactorComputationNode(f1) cf2 = FactorComputationNode(f2) cg = ComputationsFactorGraph([cv1, cv2, cv3], [cf1, cf2]) hints = DistributionHints(must_host={'a1': ['v1'], 'a2': ['v2', 'v3']}) # we must set the capacity to make sure that a2 cannot take f1 agents = [AgentDef('a{}'.format(i), capacity=41) for i in range(1, 11)] agent_mapping = distribute(cg, agents, hints, computation_memory=lambda x: 10) print(agent_mapping) self.assertEqual(agent_mapping.agent_for('f1'), 'a1') self.assertEqual(agent_mapping.agent_for('f2'), 'a2') self.assertTrue(is_all_hosted(cg, agent_mapping))
def test_no_hints(self): d1 = VariableDomain('d1', '', [1, 2, 3, 5]) v1 = Variable('v1', d1) f1 = relation_from_str('f1', 'v1 * 0.5', [v1]) cv1 = VariableComputationNode(v1, ['f1']) cf1 = FactorComputationNode(f1) cg = ComputationsFactorGraph([cv1], [cf1]) agents = [AgentDef('a1', capacity=100), AgentDef('a2', capacity=100)] agent_mapping = distribute(cg, agents, hints=None, computation_memory=lambda x: 10) self.assertTrue(agent_mapping.is_hosted(['v1', 'f1']))
def test_rule_with_light(self): hints = DistributionHints(must_host={'a1': ['v1'], 'a3': ['v2']}, host_with={'m1': ['mf1']}) agents = [AgentDef('a{}'.format(i), capacity=100) for i in range(1, 11)] agent_mapping = distribute(self.cg, agents, hints, computation_memory=lambda x: 10) # rule r2 only depends on v3, it must be hosted on the same agent self.assertEqual(agent_mapping.agent_for('v3'), agent_mapping.agent_for('r2')) self.assertTrue(is_all_hosted(self.cg, agent_mapping))
def test_model_on_dependent_light(self): hints = DistributionHints(must_host={'a1': ['v1'], 'a2': ['v2']}, host_with={'m1': ['mf1']}) agents = [AgentDef('a{}'.format(i), capacity=100) for i in range(1, 11)] agent_mapping = distribute(self.cg, agents, hints, computation_memory=lambda x: 10) # Check that the variable and relation of the model are on the same # agent self.assertIn(agent_mapping.agent_for('m1'), ['a1', 'a2']) self.assertIn(agent_mapping.agent_for('mf1'), ['a1', 'a2']) self.assertTrue(is_all_hosted(self.cg, agent_mapping))
def test_api_distribute_dsa_adhoc(): from pydcop.computations_graph import factor_graph from pydcop.distribution import adhoc from pydcop.algorithms import dsa dcop = dcop_graphcoloring_3() agents = create_agents('a', [1, 2, 3], capacity=100) dcop._agents_def = agents cg = factor_graph.build_computation_graph(dcop) dist = adhoc.distribute(cg, dcop.agents.values(), computation_memory=dsa.computation_memory, communication_load=dsa.communication_load) assert dist.is_hosted(['v1', 'v2', 'v3'])
def test_must_host_one(self): d1 = VariableDomain('d1', '', [1, 2, 3, 5]) v1 = Variable('v1', d1) f1 = relation_from_str('f1', 'v1 * 0.5', [v1]) cv1 = VariableComputationNode(v1, ['f1']) cf1 = FactorComputationNode(f1) cg = ComputationsFactorGraph([cv1], [cf1]) hints = DistributionHints({'a1': ['v1']}, None) agents = [AgentDef('a1', capacity=100), AgentDef('a2', capacity=100)] agent_mapping = distribute(cg, agents, hints, computation_memory=lambda x: 10) self.assertIn('v1', agent_mapping.computations_hosted('a1')) self.assertTrue(is_all_hosted(cg, agent_mapping))
def test_host_with(self): d1 = VariableDomain('d1', '', [1, 2, 3, 5]) v1 = Variable('v1', d1) v2 = Variable('v2', d1) f1 = relation_from_str('f1', 'v1 * 0.5', [v1]) cv1 = VariableComputationNode(v1, ['f1']) cv2 = VariableComputationNode(v2, []) cf1 = FactorComputationNode(f1) cg = ComputationsFactorGraph([cv1, cv2], [cf1]) hints = DistributionHints(None, {'v1': ['f1']}) agents = [AgentDef('a{}'.format(i), capacity=100) for i in range(1, 11)] agent_mapping = distribute(cg, agents, hints, computation_memory=lambda x: 10) self.assertEqual(agent_mapping.agent_for('v1'), agent_mapping.agent_for('f1')) self.assertTrue(is_all_hosted(cg, agent_mapping))