def test_factor_memory_two_neighbor(self): d1 = VariableDomain('d1', '', [1, 2, 3, 4, 5]) v1 = Variable('v1', d1) d2 = VariableDomain('d1', '', [1, 2, 3]) v2 = Variable('v2', d2) f1 = relation_from_str('f1', 'v1 * 0.5 + v2', [v1, v2]) cv1 = VariableComputationNode(v1, ['f1']) cv2 = VariableComputationNode(v2, ['f1']) cf1 = FactorComputationNode(f1) self.assertEqual(computation_memory(cf1), FACTOR_UNIT_SIZE * (5 + 3))
def test_factor_memory_two_neighbor(self): d1 = VariableDomain("d1", "", [1, 2, 3, 4, 5]) v1 = Variable("v1", d1) d2 = VariableDomain("d1", "", [1, 2, 3]) v2 = Variable("v2", d2) f1 = relation_from_str("f1", "v1 * 0.5 + v2", [v1, v2]) cv1 = VariableComputationNode(v1, ["f1"]) cv2 = VariableComputationNode(v2, ["f1"]) cf1 = FactorComputationNode(f1) self.assertEqual(computation_memory(cf1), FACTOR_UNIT_SIZE * (5 + 3))
def test_hash(self): d1 = VariableDomain("d", "foo", [1, 2, 3]) v1 = Variable("v1", d1) v2 = Variable("v2", d1) self.assertNotEqual(hash(v1), hash(v2)) self.assertEqual(hash(v1), hash(Variable("v1", d1)))
def test_from_repr(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) v = ExternalVariable('v', domain, value=1) r = simple_repr(v) v1 = from_repr(r) self.assertEqual(v, v1)
def test_clone(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) v = ExternalVariable('v', domain, value=1) v_clone = v.clone() self.assertEqual(v_clone.name, 'v') self.assertEqual(v_clone.domain, domain) self.assertEqual(v_clone.value, 1)
def generate_ising(args): domain_size = args.range size = args.size d = VariableDomain('d', 'dummy', range(domain_size)) variables = {} constraints = {} for i in range(size): for j in range(size): v = Variable('v{}_{}'.format(i, j), d, floor(domain_size * random.random())) variables[(i, j)] = v for i, j in variables: c = _create_ising_constraint(i, j, i, (j + 1) % size, domain_size, variables) constraints[(i, j, i, (j + 1) % size)] = c c = _create_ising_constraint(i, j, (i + 1) % size, j, domain_size, variables) constraints[(i, j, (i + 1) % size), j] = c dcop = DCOP('radom ising', 'min') #dcop.domains = {'d': d} #dcop.variables = variables dcop._agents_def = {} for c in constraints.values(): dcop.add_constraint(c) if args.output: outputfile = args.output[0] write_in_file(outputfile, dcop_yaml(dcop)) else: print(dcop_yaml(dcop))
def test_domain_content(self): r = list(range(3)) d = VariableDomain("luminosity", "luminosity", r) self.assertEqual(d[0], r[0]) self.assertEqual(d[1], r[1]) self.assertEqual(d[2], r[2])
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_hash(self): d1 = VariableDomain('d', 'foo', [1, 2, 3]) v1 = Variable('v1', d1) v2 = Variable('v2', d1) self.assertNotEqual(hash(v1), hash(v2)) self.assertEqual(hash(v1), hash(Variable('v1', d1)))
def test_with_lambda_cost_func(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) v = VariableWithCostFunc('v', domain, cost_func=lambda x: x * 2) self.assertEqual(v.cost_for_val(1), 2) self.assertEqual(v.cost_for_val(3), 6) self.assertEqual(v.cost_for_val(4), 8)
def test_clone(self): domain = VariableDomain("d", "d", [1, 2, 3, 4]) v = ExternalVariable("v", domain, value=1) v_clone = v.clone() self.assertEqual(v_clone.name, "v") self.assertEqual(v_clone.domain, domain) self.assertEqual(v_clone.value, 1)
def test_with_expression_based_cost_func(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) cost_func = ExpressionFunction('v +1') v = VariableWithCostFunc('v', domain, cost_func=cost_func) self.assertEqual(v.cost_for_val(1), 2) self.assertEqual(v.cost_for_val(3), 4)
def test_create(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) v = ExternalVariable('v', domain, value=1) self.assertEqual(v.name, 'v') self.assertEqual(v.domain, domain) self.assertEqual(v.value, 1)
def test_simple_repr(self): d = VariableDomain("d", "foo", [1, 2, 3]) v = Variable("v1", d, 2) r = simple_repr(v) self.assertEqual(r["name"], "v1") self.assertEqual(r["initial_value"], 2) self.assertEqual(r["domain"], simple_repr(d))
def test_create(self): domain = VariableDomain("d", "d", [1, 2, 3, 4]) v = ExternalVariable("v", domain, value=1) self.assertEqual(v.name, "v") self.assertEqual(v.domain, domain) self.assertEqual(v.value, 1)
def test_simple_repr(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) v = ExternalVariable('v', domain, value=1) r = simple_repr(v) self.assertEqual(r['name'], 'v') self.assertEqual(r['value'], 1)
def test_simple_repr(self): d = VariableDomain('d', 'foo', [1, 2, 3]) v = Variable('v1', d, 2) r = simple_repr(v) self.assertEqual(r['name'], 'v1') self.assertEqual(r['initial_value'], 2) self.assertEqual(r['domain'], simple_repr(d))
def test_simple_repr(self): domain = VariableDomain("d", "d", [1, 2, 3, 4]) v = ExternalVariable("v", domain, value=1) r = simple_repr(v) self.assertEqual(r["name"], "v") self.assertEqual(r["value"], 1)
def test_cb_subscribed(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) v = ExternalVariable('v', domain, value=1) cb = MagicMock() v.subscribe(cb) v.value = 2 cb.assert_called_with(2)
def test_raise_on_expression_with_many_variable(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) cost_func = ExpressionFunction('v+ w +1') self.assertRaises(ValueError, VariableWithCostFunc, 'v', domain, cost_func=cost_func)
def test_cb_unsusbcribed(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) v = ExternalVariable('v', domain, value=1) cb = MagicMock() v.subscribe(cb) v.unsubscribe(cb) v.value = 2 self.assertFalse(cb.called)
def test_simple_repr_not_supported_with_arbitrary_cost_func(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) def cost_func(val): return val / 2 v = VariableWithCostFunc('v', domain, cost_func=cost_func) self.assertRaises(SimpleReprException, simple_repr, v)
def test_simple_repr_with_expression_function(self): domain = VariableDomain("d", "d", [1, 2, 3, 4]) cost_func = ExpressionFunction("v / 2") v = VariableNoisyCostFunc("v", domain, cost_func=cost_func) r = simple_repr(v) self.assertEqual(r["name"], "v") self.assertEqual(r["cost_func"]["expression"], "v / 2")
def test_simple_repr_with_expression_function(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) cost_func = ExpressionFunction('v / 2') v = VariableNoisyCostFunc('v', domain, cost_func=cost_func) r = simple_repr(v) self.assertEquals(r['name'], 'v') self.assertEquals(r['cost_func']['expression'], 'v / 2')
def test_factor_memory_one_neighbor(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) self.assertEqual(computation_memory(cf1), FACTOR_UNIT_SIZE * 4)
def test_factor_memory_one_neighbor(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) self.assertEqual(computation_memory(cf1), FACTOR_UNIT_SIZE * 4)
def test_variable_memory_no_neighbor(self): d1 = VariableDomain("d1", "", [1, 2, 3, 5]) v1 = Variable("v1", d1) vn1 = VariableComputationNode(v1, []) # If a variable has no neighbors, it does not need to keep any cost # and thus requires no memory self.assertEqual(computation_memory(vn1), 0)
def test_from_repr_with_expression_function(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) cost_func = ExpressionFunction('v / 2') v = VariableWithCostFunc('v', domain, cost_func=cost_func) r = simple_repr(v) v2 = from_repr(r) self.assertEqual(v2.cost_for_val(2), v.cost_for_val(2)) self.assertEqual(v2, v)
def test_set_value_not_in_domain(self): domain = VariableDomain('d', 'd', [1, 2, 3, 4]) v = ExternalVariable('v', domain, value=1) def set_value(value): v.value = value # 5 is not in the domain, this must raises and ValueError self.assertRaises(ValueError, set_value, 5) self.assertEqual(v.value, 1)
def setUp(self): global d1, v1, v2, v3, v4, v5, a1, a2 d1 = VariableDomain('d1', '', [1, 2, 3, 5]) v1 = Variable('v1', d1) v2 = Variable('v2', d1) v3 = Variable('v3', d1) v4 = Variable('v4', d1) v5 = Variable('v5', d1) a1 = AgentDef('a1', capacity=200) a2 = AgentDef('a2', capacity=200)