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)
Beispiel #2
0
    def test_1_unary_constraint(self):

        variable = Variable('a', [0, 1, 2, 3, 4])
        c1 = UnaryFunctionRelation('c1', variable, lambda x: abs(x - 2))

        computation = DsaComputation(variable, [c1], comp_def=MagicMock())
        val, sum_costs = computation._compute_best_value()

        self.assertEqual(val, [2])
        self.assertEqual(sum_costs, 0)
Beispiel #3
0
    def test_2_unary_constraints(self):
        variable = Variable('a', [0, 1, 2, 3, 4])
        c1 = UnaryFunctionRelation('c1', variable, lambda x: abs(x - 3))
        c2 = UnaryFunctionRelation('c1', variable, lambda x: abs(x - 1) * 2)

        computation = DsaComputation(variable, [c1, c2], comp_def=MagicMock())
        val, sum_costs = computation._compute_best_value()
        print(val, sum_costs)

        self.assertEqual(val, [1])
        self.assertEqual(sum_costs, 2)
Beispiel #4
0
    def test_1_binary_constraint(self):

        v1 = Variable('v1', [0, 1, 2, 3, 4])
        v2 = Variable('v2', [0, 1, 2, 3, 4])

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

        computation = DsaComputation(v1, [c1], comp_def=MagicMock())
        computation._neighbors_values = {'v1': 1}
        val, sum_costs = computation._compute_best_value()

        self.assertEqual(val, [1])
        self.assertEqual(sum_costs, 0)
def test_footprint_on_computation_object():
    v1 = Variable('v1', [0, 1, 2, 3, 4])
    v2 = Variable('v2', [0, 1, 2, 3, 4])
    c1 = relation_from_str('c1', '0 if v1 == v2 else  1', [v1, v2])
    n1 = VariableComputationNode(v1, [c1])
    n2 = VariableComputationNode(v2, [c1])
    comp_def = ComputationDef(
        n1, AlgorithmDef.build_with_default_param('dsa', mode='min'))
    c = DsaComputation(comp_def)

    # Must fix unit size otherwise the tests fails when we change the default
    # value
    dsa.UNIT_SIZE = 1

    footprint = c.footprint()
    assert footprint == 1
def test_build_computation_max_mode():
    v1 = Variable('v1', [0, 1, 2, 3, 4])
    n1 = VariableComputationNode(v1, [])
    comp_def = ComputationDef(
        n1, AlgorithmDef.build_with_default_param('dsa', mode='max'))
    c = DsaComputation(comp_def)
    assert c.mode == 'max'
Beispiel #7
0
    def test_2_unary_constraint_means_no_neighbors(self):
        variable = Variable('a', [0, 1, 2, 3, 4])
        c1 = UnaryFunctionRelation('c1', variable, lambda x: abs(x - 3))
        c2 = UnaryFunctionRelation('c1', variable, lambda x: abs(x - 1) * 2)

        computation = DsaComputation(variable, [c1, c2], comp_def=MagicMock())
        self.assertEqual(len(computation._neighbors), 0)
def test_build_computation_default_params():
    v1 = Variable('v1', [0, 1, 2, 3, 4])
    n1 = VariableComputationNode(v1, [])
    comp_def = ComputationDef(n1, AlgorithmDef.build_with_default_param('dsa'))
    c = DsaComputation(comp_def)
    assert c.mode == 'min'
    assert c.variant == 'B'
    assert c.stop_cycle == 0
    assert c.probability == 0.7
def test_repr_dsa_class():
    variable = Variable('a', [0, 1, 2, 3, 4])
    c1 = UnaryFunctionRelation('c1', variable, lambda x: abs(x - 2))

    computation = DsaComputation(
        ComputationDef(VariableComputationNode(variable, [c1]),
                       AlgorithmDef.build_with_default_param('dsa')))

    assert repr(computation) == "dsa.DsaComputation(a)"
Beispiel #10
0
    def test_mode_is_min(self):
        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)
        def c1(v1_, v2_):
            return abs(v1_ - v2_)

        @AsNAryFunctionRelation(v1, v3)
        def c2(v1_, v3_):
            return abs(v1_ - v3_)

        computation = DsaComputation(v1, [c1, c2], comp_def=MagicMock())
        c, b = computation._compute_boundary([c1, c2])

        self.assertEqual(c, [c1, c2])
        self.assertEqual(b, 0)
def test_1_unary_constraint_means_no_neighbors():
    variable = Variable('a', [0, 1, 2, 3, 4])
    c1 = UnaryFunctionRelation('c1', variable, lambda x: abs(x - 2))

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

    computation = DsaComputation(comp_def=comp_def)
    assert len(computation.neighbors) == 0
Beispiel #12
0
    def test_3ary_constraint_2_neighbors(self):
        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_)

        computation = DsaComputation(v1, [c1], comp_def=MagicMock())
        self.assertEqual(len(computation._neighbors), 2)
Beispiel #13
0
    def test_2_binary_constraint_one_neighbors(self):
        v1 = Variable('v1', [0, 1, 2, 3, 4])
        v2 = Variable('v2', [0, 1, 2, 3, 4])

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

        @AsNAryFunctionRelation(v1, v2)
        def c2(v1_, v2_):
            return abs(v1_ + v2_)

        computation = DsaComputation(v1, [c1, c2], comp_def=MagicMock())
        self.assertEqual(len(computation._neighbors), 1)
def test_3ary_constraint_2_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)
    assert len(computation.neighbors) == 2
def test_build_computation_with_params():
    v1 = Variable('v1', [0, 1, 2, 3, 4])
    n1 = VariableComputationNode(v1, [])
    comp_def = ComputationDef(
        n1,
        AlgorithmDef.build_with_default_param('dsa',
                                              mode='max',
                                              params={
                                                  'variant': 'C',
                                                  'stop_cycle': 10,
                                                  'probability': 0.5
                                              }))
    c = DsaComputation(comp_def)
    assert c.mode == 'max'
    assert c.variant == 'C'
    assert c.stop_cycle == 10
    assert c.probability == 0.5