예제 #1
0
 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)
예제 #2
0
 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)
예제 #3
0
    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)
예제 #4
0
    def test_clone_cb(self):
        domain = VariableDomain('d', 'd', [1, 2, 3, 4])
        v = ExternalVariable('v', domain, value=1)
        cb = MagicMock()
        cb_clone = MagicMock()

        v.subscribe(cb)
        v_clone = v.clone()
        v_clone.subscribe(cb_clone)
        v_clone.value = 3

        self.assertFalse(cb.called)
        cb_clone.assert_called_with(3)
예제 #5
0
    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)
예제 #6
0
    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)
예제 #7
0
    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)
예제 #8
0
    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)
예제 #9
0
    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)
예제 #10
0
    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)
예제 #11
0
    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)
예제 #12
0
파일: yamldcop.py 프로젝트: qslim/pyDcop
def _build_external_variables(loaded, dcop) -> Dict[str, ExternalVariable]:
    ext_vars = {}
    if "external_variables" in loaded:
        for v_name in loaded["external_variables"]:
            v = loaded["external_variables"][v_name]
            domain = dcop.domain(v["domain"])
            initial_value = v["initial_value"] if "initial_value" in v else None
            if initial_value and initial_value not in domain.values:
                raise ValueError("initial value {} is not in the domain {} "
                                 "of the variable {}".format(
                                     initial_value, domain.name, v_name))
            ext_vars[v_name] = ExternalVariable(v_name, domain, initial_value)
    return ext_vars
예제 #13
0
    def test_cb_subscribed_no_change(self):
        domain = VariableDomain('d', 'd', [1, 2, 3, 4])
        v = ExternalVariable('v', domain, value=1)
        cb = MagicMock()
        v.value = 2
        v.subscribe(cb)

        # The callback must be called only if the value has really changed
        v.value = 2
        self.assertFalse(cb.called)
        v.value = 3
        cb.assert_called_with(3)
        cb.reset_mock()

        v.value = 3
        self.assertFalse(cb.called)
예제 #14
0
def _build_external_variables(loaded, dcop) -> Dict[str, ExternalVariable]:
    ext_vars = {}
    if 'external_variables' in loaded:
        for v_name in loaded['external_variables']:
            v = loaded['external_variables'][v_name]
            domain = dcop.domain(v['domain'])
            initial_value = v['initial_value'] if 'initial_value' in v \
                else None
            if initial_value and initial_value not in domain.values:
                raise ValueError('initial value {} is not in the domain {} '
                                 'of the variable {}'.format(
                                     initial_value, domain.name, v_name))
            ext_vars[v_name] = ExternalVariable(v_name, domain, initial_value)
    return ext_vars
예제 #15
0
def dmaxsum_external_variable():

    domain = VariableDomain('colors', 'color', ['R', 'G', 'B'])
    # RW Variables
    v1 = VariableNoisyCostFunc('v1', domain, prefer_color('R'))
    v2 = VariableNoisyCostFunc('v2', domain, prefer_color('B'))
    v3 = VariableNoisyCostFunc('v3', domain, prefer_color('B'))
    v4 = VariableNoisyCostFunc('v4', domain, prefer_color('R'))

    # External Variable
    domain_e = VariableDomain('boolean', 'abstract', [False, True])
    e1 = ExternalVariable('e1', domain_e, False)

    def r1(v1_, v2_, v3_):
        if v1_ != v2_ and v2_ != v3_ and v1_ != v3_:
            return 0
        return 100

    condition = NAryFunctionRelation(lambda x: x, [e1], name='r1_cond')
    relation_if_true = NAryFunctionRelation(r1, [v1, v2, v3], name='r1')
    r1 = ConditionalRelation(condition, relation_if_true)

    def r2(v2_, v4_):
        if v2_ != v4_:
            return 0
        return 100

    r2 = NAryFunctionRelation(r2, [v2, v4], name='r2')

    def r3(v3_, v4_):
        if v3_ != v4_:
            return 0
        return 100

    r3 = NAryFunctionRelation(r3, [v3, v4], name='r3')

    r1_computation = DynamicFactorComputation(r1, name='r1')
    r2_computation = DynamicFactorComputation(r2)
    r3_computation = DynamicFactorComputation(r3)

    e1_computation = ExternalVariableComputation(e1)

    # MUST only consider current relation when building computation objects !!
    # When a relation uses external variable, these must be sliced out.
    current_r1 = r1.slice({e1.name: e1.value})
    relations = [current_r1, r2, r3]
    v1_computation = \
        DynamicFactorVariableComputation(
            v1,
            [r.name for r in find_dependent_relations(v1, relations)])
    v2_computation = \
        DynamicFactorVariableComputation(
            v2,
            [r.name for r in find_dependent_relations(v2, relations)])
    v3_computation = \
        DynamicFactorVariableComputation(
            v3,
            [r.name for r in find_dependent_relations(v3, relations)])
    v4_computation = \
        DynamicFactorVariableComputation(
            v4,
            [r.name for r in find_dependent_relations(v4, relations)])

    # Prepare the agents
    comm = InProcessCommunicationLayer()
    a1 = Agent('a1', comm)
    a1.add_computation(v1_computation)
    a1.add_computation(r1_computation)

    a2 = Agent('a2', comm)
    a2.add_computation(v2_computation)
    a1.add_computation(r2_computation)

    a3 = Agent('a3', comm)
    a3.add_computation(v3_computation)
    a3.add_computation(v4_computation)
    a3.add_computation(r3_computation)

    a4 = Agent('a4', comm)
    a4.add_computation(e1_computation)

    agents = [a1, a2, a3, a4]
    runner = AgentsRunner(agents)
    runner.run_agents()

    # Now change a factor function every two seconds
    fail = False
    for i in range(5):
        time.sleep(2)
        current_value = e1_computation.current_value
        print('###  Iteration {} - function {}'.format(i, current_value))
        print(runner.status_string())
        results = runner.variable_values()
        if current_value:
            c = r1(filter_assignment_dict(results, r1.dimensions)) + \
                r2(filter_assignment_dict(results, r2.dimensions)) + \
                r3(filter_assignment_dict(results, r3.dimensions))
            if c != 0:
                print('Error on results for {} : \nGot {}  !'.format(
                    current_value, results))
                fail = True
                break
        else:
            c = r2(filter_assignment_dict(results, r2.dimensions)) + \
                r3(filter_assignment_dict(results, r3.dimensions))
        if c != 0:
            print('Error on results for {} : \nGot {} !'.format(
                current_value, results))
            fail = True
            break

        new_val = not current_value
        print('## Changing e1 value to {}'.format(new_val))
        e1_computation.change_value(new_val)

    print('Finished, stopping agents')
    runner.request_stop_agents(wait_for_stop=True)

    if fail:
        print('Failed !')
        return 1
    else:
        print('Success !')
        return 0
예제 #16
0
    def test_set_value_in_domain(self):
        domain = VariableDomain('d', 'd', [1, 2, 3, 4])
        v = ExternalVariable('v', domain, value=1)

        v.value = 2
        self.assertEqual(v.value, 2)