def test_3nodes_tree_cycle(self):
        domain = ['a', 'b', 'c']
        x1 = Variable('x1', domain)
        x2 = Variable('x2', domain)
        x3 = Variable('x3', domain)
        variables = [x1, x2, x3]

        r1 = NAryFunctionRelation(lambda x, y: x + y, [x1, x2], name='r1')
        r2 = NAryFunctionRelation(lambda x, y: x + y, [x1, x3], name='r2')
        r3 = NAryFunctionRelation(lambda x, y: x + y, [x2, x3], name='r3')
        relations = [r1, r2, r3]

        dcop = DCOP('test', 'min')
        dcop.add_constraint(r1)
        dcop.add_constraint(r2)
        dcop.add_constraint(r3)

        cg = build_computation_graph(dcop)
        self.assertEqual(len(cg.nodes), len(variables))

        self.assertEqual(len(cg.roots), 1)
        # All variables have the same number of neighbors, they could all be
        #  root
        self.assertIn(cg.roots[0].variable, [x1, x2, x3])
        self.assertEqual(cg.roots[0].parent, None)

        root = _generate_dfs_tree(variables, relations, root=x1)
        self.assertEqual(root.variable, x1)
        self.assertEqual(root.parent, None)
    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)))
Example #3
0
    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)))
Example #4
0
    def setup_method(self):
        self.x0 = Variable("x0", ["a", "b"])
        self.x1 = Variable("x1", ["a", "b"])

        self.r0_1 = NAryMatrixRelation([self.x0, self.x1],
                                       np.array([[1, 2], [4, 3]]))

        self.sender0 = DummySender()
        self.sender1 = DummySender()
        compdef = MagicMock()
        compdef.algo.algo = "dpop"
        compdef.algo.mode = "max"
        self.a0 = dpop.DpopAlgo(
            self.x0,
            parent=None,
            children=[self.x1.name],
            constraints=[],
            comp_def=compdef,
        )
        self.a1 = dpop.DpopAlgo(
            self.x1,
            parent=self.x0.name,
            children=[],
            constraints=[self.r0_1],
            comp_def=compdef,
        )

        self.a0.message_sender = self.sender0
        self.a1.message_sender = self.sender1
Example #5
0
    def test_change_function_wrong_dimensions_var(self):
        domain = list(range(10))
        x1 = Variable("x1", domain)
        x2 = Variable("x2", domain)
        x3 = Variable("x3", domain)

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            return x1_ + x2_

        @AsNAryFunctionRelation(x1, x3)
        def phi2(x1_, x3_):
            return x1_ + x3_

        comp_def = MagicMock()
        comp_def.algo.algo = "amaxsum"
        comp_def.algo.mode = "min"
        comp_def.node.factor = phi

        f = DynamicFunctionFactorComputation(comp_def=comp_def)

        # Monkey patch post_msg method with dummy mock to avoid error:
        f.post_msg = types.MethodType(lambda w, x, y, z: None, f)

        self.assertRaises(ValueError, f.change_factor_function, phi2)
    def test_current_local_cost_3_ary(self):
        x1 = Variable("x1", list(range(2)))
        x2 = Variable("x2", list(range(2)))
        x3 = Variable("x3", [1])

        @AsNAryFunctionRelation(x1, x2, x3)
        def phi(x1_, x2_, x3_):
            return x1_ + x2_ + x3_

        computation = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi]),
                AlgorithmDef.build_with_default_param("mgm2"),
            ))
        computation.__value__ = 1
        computation._neighbors_values["x2"] = 0
        computation._neighbors_values["x3"] = 1

        computation2 = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi]),
                AlgorithmDef.build_with_default_param("mgm2"),
            ))
        computation2.__value__ = 0
        computation2._neighbors_values["x2"] = 0
        computation2._neighbors_values["x3"] = 1
        self.assertEqual(computation._current_local_cost(), 2)
        self.assertEqual(computation2._current_local_cost(), 1)
    def test_compute_offers_max_mode(self):
        x1 = Variable("x1", list(range(2)))
        x2 = Variable("x2", list(range(2)))
        x3 = Variable("x3", list(range(2)))

        @AsNAryFunctionRelation(x1, x2, x3)
        def phi(x1_, x2_, x3_):
            if x1_ == x3_:
                return 2
            elif x1_ == x2_:
                return 1
            return 0

        computation = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi]),
                AlgorithmDef.build_with_default_param("mgm2", mode="max"),
            ))

        computation._neighbors_values = {"x2": 0, "x3": 0}
        computation._partner = x2
        computation.__value__ = 1
        computation.__cost__ = 0
        offers = computation._compute_offers_to_send()

        self.assertEqual(offers, {(0, 0): -2, (0, 1): -2, (1, 1): -1})
    def test_offer_has_better_unilateral_move(self):
        x1 = Variable("x1", list(range(2)))
        x2 = Variable('x2', list(range(2)))
        x3 = Variable('x3', list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        # Receives a real offer from last neighbor
        computation = Mgm2Computation(x1, [phi, psi],
                                      msg_sender=DummySender(),
                                      comp_def=MagicMock())
        computation._state = 'offer'
        computation._neighbors_values = {'x2': 1, 'x3': 1}
        computation.__value__ = 1
        computation.__cost__ = 9
        computation._potential_gain = 9  # best unilateral move
        computation._potential_value = 0  # best unilateral move
        computation.__nb_received_offers__ = 1
        computation._handle_offer_msg(
            'x2', Mgm2OfferMessage({(0, 1): 1}, is_offering=True))
        self.assertEqual(computation._offers, [('x2', {(0, 1): 1})])
        self.assertEqual(computation._state, 'gain')
        self.assertEqual(computation._potential_gain, 9)
        self.assertEqual(computation._potential_value, 0)
    def test_response_accept(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable('x2', list(range(2)))
        x3 = Variable('x3', list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(x1, [phi, psi],
                                      msg_sender=DummySender(),
                                      comp_def=MagicMock())
        computation._state = 'answer?'
        computation._is_offerer = True
        computation._neighbors_values = {'x2': 1, 'x3': 1}
        computation.__value__ = 1
        computation.__cost__ = 9
        computation._potential_gain = 9  # best unilateral move
        computation._potential_value = 2  # best unilateral move
        computation._partner = x3

        computation._handle_response_msg(
            'x3', Mgm2ResponseMessage(True, value=0, gain=10))

        self.assertEqual(computation._state, 'gain')
        self.assertEqual(computation._potential_gain, 10)
        self.assertEqual(computation._potential_value, 0)
    def test_3_ary_func(self):
        x1 = Variable("x1", list(range(2)))
        x2 = Variable('x2', list(range(2)))
        x3 = Variable('x3', [1])

        @AsNAryFunctionRelation(x1, x2, x3)
        def phi(x1_, x2_, x3_):
            return x1_ + x2_ + x3_

        computation = Mgm2Computation(x1, [phi], comp_def=MagicMock())
        self.assertEqual(
            computation._compute_cost({
                'x1': 0,
                'x2': 0,
                'x3': 1
            }), 1)
        self.assertEqual(
            computation._compute_cost({
                'x1': 0,
                'x2': 1,
                'x3': 1
            }), 2)
        self.assertEqual(
            computation._compute_cost({
                'x1': 1,
                'x2': 0,
                'x3': 1
            }), 2)
        self.assertEqual(
            computation._compute_cost({
                'x1': 1,
                'x2': 1,
                'x3': 1
            }), 3)
    def test_value_all_neighbors_received(self):
        x1 = Variable("x1", list(range(2)))
        x2 = Variable('x2', list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            return x1_ + x2_

        computation = Mgm2Computation(x1, [phi],
                                      msg_sender=DummySender(),
                                      comp_def=MagicMock())
        computation._state = 'value'
        computation.__value__ = 1
        computation._handle_value_message('x2', Mgm2ValueMessage(0))

        self.assertEqual(computation._state, 'offer')
        self.assertEqual(computation._neighbors_values['x2'], 0)
        self.assertEqual(computation._potential_gain, 1)
        self.assertEqual(computation._potential_value, 0)

        computation2 = Mgm2Computation(x1, [phi],
                                       mode='max',
                                       msg_sender=DummySender(),
                                       comp_def=MagicMock())
        computation2._state = 'value'
        computation2.__value__ = 1
        computation2._handle_value_message('x2', Mgm2ValueMessage(0))
        self.assertEqual(computation2._state, 'offer')
        self.assertEqual(computation2._neighbors_values['x2'], 0)
        self.assertEqual(computation2._potential_gain, 0)
        self.assertEqual(computation2._potential_value, 1)
    def test_2nodes_tree(self):
        domain = ['a', 'b', 'c']
        x1 = Variable('x1', domain)
        x2 = Variable('x2', domain)
        variables = [x1, x2]

        r1 = NAryFunctionRelation(lambda x, y: x + y, [x1, x2], name='r1')
        relations = [r1]

        root = _generate_dfs_tree(variables, relations, root=x1)

        self.assertEqual(root.variable, x1)
        self.assertEqual(root.parent, None)
        self.assertEqual(len(root.children), 1)
        self.assertEqual(len(root.relations), 1)
        node = root.children[0]
        self.assertEqual(node.variable, x2)
        self.assertEqual(root.pseudo_children, [])
        self.assertEqual(root.pseudo_parents, [])

        self.assertEqual(node.parent, root)
        self.assertEqual(node.children, [])
        self.assertEqual(node.pseudo_children, [])
        self.assertEqual(node.pseudo_parents, [])
        self.assertEqual(node.relations, [r1])

        check_tree(root)
    def test_4nodes(self):
        # Graph with 4 nodes, one cycle
        #
        #       x1---X3
        #        \  /
        #         x2---x4

        domain = ['a', 'b', 'c']
        x1 = Variable('x1', domain)
        x2 = Variable('x2', domain)
        x3 = Variable('x3', domain)
        x4 = Variable('x4', domain)
        variables = [x1, x2, x3, x4]

        def binary_func(x, y):
            return x + y

        r1 = NAryFunctionRelation(binary_func, [x1, x2], name='r1')
        r2 = NAryFunctionRelation(binary_func, [x1, x3], name='r2')
        r3 = NAryFunctionRelation(binary_func, [x2, x3], name='r3')
        r4 = NAryFunctionRelation(binary_func, [x2, x4], name='r4')
        relations = [r1, r2, r3, r4]

        root = _generate_dfs_tree(variables, relations, root=x1)
        _filter_relation_to_lowest_node(root)

        check_tree(root)

        self.assertEqual(root.variable, x1)
        self.assertEqual(root.parent, None)
        self.assertEqual(len(root.children), 1)
        self.assertEqual(len(root.relations), 0)
    def test_3nodes_tree_cycle_3ary_rel_bottom(self):
        # A graph with 3 variables and a single 3-ary relation.

        domain = ['a', 'b', 'c']
        x1 = Variable('x1', domain)
        x2 = Variable('x2', domain)
        x3 = Variable('x3', domain)
        variables = [x1, x2, x3]
        r1 = NAryFunctionRelation(lambda x, y, z: x + y + z, [x1, x2, x3],
                                  name='r1')
        relations = [r1]

        root = _generate_dfs_tree(variables, relations, root=x1)
        _filter_relation_to_lowest_node(root)

        self.assertEqual(root.variable, x1)
        self.assertEqual(root.parent, None)
        self.assertEqual(len(root.children), 1)
        self.assertEqual(len(root.relations), 0)
        c1 = root.children[0]
        self.assertIn(c1.variable, [x2, x3])
        self.assertEqual(len(root.pseudo_children), 1)
        self.assertEqual(root.pseudo_parents, [])

        self.assertEqual(c1.parent, root)
        self.assertEqual(len(c1.children), 1)
        c2 = c1.children[0]
        self.assertEqual(c2.children, [])
        self.assertEqual(c2.pseudo_children, [])
        self.assertEqual(c2.pseudo_parents, [root])

        self.assertEqual(len(c1.relations) + len(c2.relations), 1)

        check_tree(root)
    def test_go_reject_no_postponed_value_message(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable("x2", list(range(2)))
        x3 = Variable("x3", list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi, psi]),
                AlgorithmDef.build_with_default_param("mgm2"),
            ))
        computation.message_sender = DummySender()

        computation._neighbors_values = {"x2": 1, "x3": 1}
        computation.__value__ = 1
        computation._potential_value = 0
        computation._state = "go?"
        # from Response message or accepted offer

        computation._handle_go_message("x3", Mgm2GoMessage(False))

        self.assertEqual(computation._state, "value")
        self.assertEqual(computation.__value__, 1)
        self.test_clear_agent()
    def test_go_reject_no_postponed_value_message(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable('x2', list(range(2)))
        x3 = Variable('x3', list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(x1, [phi, psi],
                                      msg_sender=DummySender(),
                                      comp_def=MagicMock())
        computation._neighbors_values = {'x2': 1, 'x3': 1}
        computation.__value__ = 1
        computation._potential_value = 0
        computation._state = 'go?'
        # from Response message or accepted offer

        computation._handle_go_message('x3', Mgm2GoMessage(False))

        self.assertEqual(computation._state, 'value')
        self.assertEqual(computation.__value__, 1)
        self.test_clear_agent()
    def test_gain_not_all_received(self):
        x1 = Variable("x1", list(range(3)))
        x2 = Variable("x2", list(range(2)))
        x3 = Variable("x3", list(range(2)))

        @AsNAryFunctionRelation(x1, x2)
        def phi(x1_, x2_):
            if x1_ == x2_:
                return 1
            return 0

        @AsNAryFunctionRelation(x1, x3)
        def psi(x1_, x3_):
            if x1_ == x3_:
                return 8
            return 0

        computation = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi, psi]),
                AlgorithmDef.build_with_default_param("mgm2"),
            ))
        computation.message_sender = DummySender()

        computation._state = "gain"
        computation.on_gain_msg("x2", Mgm2GainMessage(5), 1)

        self.assertEqual(computation._neighbors_gains, {"x2": 5})
Example #18
0
    def test_4variables(self):
        l1 = Variable("l1", list(range(10)))
        l2 = Variable("l2", list(range(10)))
        l3 = Variable("l3", list(range(10)))
        y1 = Variable("y1", list(range(10)))

        @AsNAryFunctionRelation(l1, l2, l3, y1)
        def scene_rel(l1_, l2_, l3_, y1_):
            if y1_ == round((l1_ + l2_ + l3_) / 3):
                return 0
            return 10000

        @AsNAryFunctionRelation(l3)
        def cost_l3(l3_):
            return l3_

        assert scene_rel(9, 6, 0, 5) == 0

        assert scene_rel(3, 6, 0, 5) == 10000

        joined = pydcop.dcop.relations.join(scene_rel, cost_l3)

        assert joined(9, 6, 0, 5) == 0

        assert joined(3, 6, 0, 5) == 10000

        util = pydcop.dcop.relations.projection(joined, l3, "min")
    def test_enter_offer_state(self):
        x1 = Variable("x1", list(range(2)))
        x2 = Variable("x2", list(range(2)))
        x3 = Variable("x3", list(range(2)))

        @AsNAryFunctionRelation(x1, x2, x3)
        def phi(x1_, x2_, x3_):
            return x1_ + x2_ + x3_

        computation = Mgm2Computation(
            ComputationDef(
                VariableComputationNode(x1, [phi]),
                AlgorithmDef.build_with_default_param("mgm2", mode="max"),
            ))
        computation.message_sender = DummySender()

        computation._postponed_msg["offer"] = [
            ("x2", Mgm2OfferMessage({(1, 1): 5}, is_offering=True), 1)
        ]

        computation._enter_state("offer")

        self.assertEqual(computation._state, "offer")
        self.assertEqual(computation._postponed_msg["offer"], [])
        self.assertEqual(computation._offers,
                         [("x2", Mgm2OfferMessage({(1, 1): 5}, True))])
def test_comp_creation_with_factory_method():
    d = Domain("d", "", ["R", "G"])
    v1 = Variable("v1", d)
    v2 = Variable("v2", d)
    c1 = constraint_from_str("c1", "10 if v1 == v2 else 0", [v1, v2])
    graph = build_computation_graph(None, constraints=[c1], variables=[v1, v2])

    comp_node = graph.computation("c1")
    algo_def = AlgorithmDef.build_with_default_param("maxsum")
    comp_def = ComputationDef(comp_node, algo_def)

    comp = build_computation(comp_def)
    assert comp is not None
    assert comp.name == "c1"
    assert comp.factor == c1

    comp_node = graph.computation("v1")
    algo_def = AlgorithmDef.build_with_default_param("maxsum")
    comp_def = ComputationDef(comp_node, algo_def)

    comp = build_computation(comp_def)
    assert comp is not None
    assert comp.name == "v1"
    assert comp.variable.name == "v1"
    assert comp.factors == ["c1"]
Example #21
0
def test_cost_for_2var():
    domain = list(range(10))
    x1 = Variable("x1", domain)
    domain = list(range(5))
    x2 = Variable("x2", domain)

    @AsNAryFunctionRelation(x1, x2)
    def cost(x1_, x2_):
        return abs((x1_ - x2_) / 2)

    comp_def = MagicMock()
    comp_def.algo.algo = "amaxsum"
    comp_def.algo.mode = "min"
    comp_def.node.factor = cost
    f = MaxSumFactorComputation(comp_def=comp_def)

    costs = factor_costs_for_var(cost, x1, f._costs, f.mode)

    # in this test, the factor did not receive any costs messages from
    # other variables, this means it  only uses the factor function when
    # calculating costs.

    # x1 = 5, best val for x2 is 4, with cost = 0.5
    assert costs[5] == (5 - 4) / 2
    assert costs[9] == (9 - 4) / 2
    assert costs[2] == 0
Example #22
0
    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_3nodes_tree_cycle(self):
        domain = ['a', 'b', 'c']
        x1 = Variable('x1', domain)
        x2 = Variable('x2', domain)
        x3 = Variable('x3', domain)
        variables = [x1, x2, x3]

        r1 = NAryFunctionRelation(lambda x, y: x + y, [x1, x2], name='r1')
        r2 = NAryFunctionRelation(lambda x, y: x + y, [x1, x3], name='r2')
        r3 = NAryFunctionRelation(lambda x, y: x + y, [x2, x3], name='r3')
        relations = [r1, r2, r3]

        root = _generate_dfs_tree(variables, relations, root=x1)

        self.assertEqual(root.variable, x1)
        self.assertEqual(root.parent, None)
        self.assertEqual(len(root.children), 1)
        c1 = root.children[0]
        self.assertIn(c1.variable, [x2, x3])
        self.assertEqual(len(root.pseudo_children), 1)
        self.assertEqual(root.pseudo_parents, [])

        self.assertEqual(c1.parent, root)
        self.assertEqual(len(c1.children), 1)
        c2 = c1.children[0]
        self.assertEqual(c2.children, [])
        self.assertEqual(c2.pseudo_children, [])
        self.assertEqual(c2.pseudo_parents, [root])

        check_tree(root)
def test_select_and_send_random_value_when_starting():
    # When starting, a DSA computation select a random value and send it to
    # all neighbors
    v1 = Variable('v1', [0, 1, 2, 3, 4])
    v2 = Variable('v2', [0, 1, 2, 3, 4])
    v3 = Variable('v3', [0, 1, 2, 3, 4])

    @AsNAryFunctionRelation(v1, v2, v3)
    def c1(v1_, v2_, v3_):
        return abs(v1_ - v2_ + v3_)

    node = VariableComputationNode(v1, [c1])
    comp_def = ComputationDef(node,
                              AlgorithmDef.build_with_default_param('dsa'))

    computation = DsaComputation(comp_def=comp_def)
    message_sender = MagicMock()
    computation.message_sender = message_sender

    computation.start()

    assert computation.current_value in v1.domain
    expected_message = DsaMessage(computation.current_value)
    print("message_sender.mock_calls", message_sender.mock_calls)
    message_sender.assert_has_calls([
        call('v1', 'v2', expected_message, None, None),
        call('v1', 'v3', expected_message, None, None)
    ],
                                    any_order=True)
Example #25
0
    def test_4variables(self):
        l1 = Variable('l1', list(range(10)))
        l2 = Variable('l2', list(range(10)))
        l3 = Variable('l3', list(range(10)))
        y1 = Variable('y1', list(range(10)))

        @AsNAryFunctionRelation(l1, l2, l3, y1)
        def scene_rel(l1_, l2_, l3_, y1_):
            if y1_ == round((l1_ + l2_ + l3_) / 3):
                return 0
            return 10000

        @AsNAryFunctionRelation(l3)
        def cost_l3(l3_):
            return l3_

        self.assertEqual(scene_rel(9, 6, 0, 5), 0)

        self.assertEqual(scene_rel(3, 6, 0, 5), 10000)

        joined = dpop.join_utils(scene_rel, cost_l3)

        self.assertEqual(joined(9, 6, 0, 5), 0)

        self.assertEqual(joined(3, 6, 0, 5), 10000)

        util = dpop.projection(joined, l3, 'min')
Example #26
0
    def setUp(self):
        self.x0 = Variable('x0', ['a', 'b'])
        self.x1 = Variable('x1', ['a', 'b'])
        self.x2 = Variable('x2', ['a', 'b'])

        self.r0_1 = NAryMatrixRelation([self.x0, self.x1],
                                       np.array([[1, 2], [2, 3]]))
        self.r0_2 = NAryMatrixRelation([self.x0, self.x2],
                                       np.array([[5, 2], [3, 1]]))

        self.sender0 = DummySender()
        self.sender1 = DummySender()
        self.sender2 = DummySender()
        self.a0 = dpop.DpopAlgo(self.x0,
                                parent=None,
                                children=[self.x1.name, self.x2.name],
                                constraints=[],
                                msg_sender=self.sender0,
                                comp_def=MagicMock())
        self.a1 = dpop.DpopAlgo(self.x1,
                                parent=self.x0.name,
                                children=[],
                                constraints=[self.r0_1],
                                msg_sender=self.sender1,
                                comp_def=MagicMock())
        self.a2 = dpop.DpopAlgo(self.x2,
                                parent=self.x0.name,
                                children=[],
                                constraints=[self.r0_2],
                                msg_sender=self.sender2,
                                comp_def=MagicMock())
Example #27
0
    def test_eff_cost_M_n_ary(self):
        domain = list(range(3))
        x1 = Variable('x1', domain)
        x2 = Variable('x2', domain)
        x3 = Variable('x3', domain)

        @AsNAryFunctionRelation(x1, x2, x3)
        def phi(x1_, x2_, x3_):
            if x1_ == x2_:
                return 2
            if x1_ == x3_:
                return 1
            return 0

        g = GdbaComputation(x1, [phi], modifier='M', comp_def=MagicMock())
        g._neighbors_values['x2'] = 1
        g._neighbors_values['x3'] = 2
        c, _, _ = g.__constraints__[0]
        asgt = frozenset({'x1': 0, 'x2': 1, 'x3': 2}.items())
        asgt2 = frozenset({'x1': 1, 'x2': 1, 'x3': 2}.items())
        g.__constraints_modifiers__[c][asgt] = 5
        g.__constraints_modifiers__[c][asgt2] = 5

        self.assertEqual(g._eff_cost(c, 0), 0)
        self.assertEqual(g._eff_cost(c, 1), 10)
        self.assertEqual(g._eff_cost(c, 2), 1)
Example #28
0
def test_build_graph_from_variables_constraints():
    d = Domain('d', 'test', [1, 2, 3])
    v1 = Variable('v1', d)
    v2 = Variable('v2', d)
    v3 = Variable('v3', d)
    c1 = constraint_from_str('c1', 'v1 * 0.5 + v2 - v3', [v1, v2, v3])

    graph = build_computation_graph(variables=[v1, v2, v3], constraints=[c1])
def test_computation_memory_one_constraint():
    v1 = Variable('v1', list(range(10)))
    v2 = Variable('v2', list(range(10)))
    v3 = Variable('v3', list(range(10)))
    c1 = constraint_from_str('c1', ' v1 + v2 == v3', [v1, v2, v3])
    v1_node = VariableComputationNode(v1, [c1])

    # here, we have an hyper-edges with 3 vertices
    assert dsa.computation_memory(v1_node) == dsa.UNIT_SIZE * 2
def test_computation_memory_one_constraint():
    v1 = Variable("v1", list(range(10)))
    v2 = Variable("v2", list(range(10)))
    v3 = Variable("v3", list(range(10)))
    c1 = constraint_from_str("c1", " v1 + v2 == v3", [v1, v2, v3])
    v1_node = VariableComputationNode(v1, [c1])

    # here, we have an hyper-edges with 3 vertices
    assert mgm2.computation_memory(v1_node) == mgm2.UNIT_SIZE * 2 * 2