Beispiel #1
0
    def test_create_several_variables_from_list(self):
        d = Domain('color', '', ['R', 'G', 'B'])
        vars = create_variables('x_', ['a1', 'a2', 'a3'], d)

        self.assertIn('x_a1', vars)
        self.assertTrue(isinstance(vars['x_a2'], Variable))
        self.assertEqual(vars['x_a3'].name, 'x_a3')
    def test_create_several_variables_from_range(self):
        d = Domain("color", "", ["R", "G", "B"])
        variables = create_variables("x_", range(10), d)

        self.assertIn("x_1", variables)
        self.assertTrue(isinstance(variables["x_2"], Variable))
        self.assertEqual(variables["x_3"].name, "x_3")
    def test_create_several_variables_from_list(self):
        d = Domain("color", "", ["R", "G", "B"])
        variables = create_variables("x_", ["a1", "a2", "a3"], d)

        self.assertIn("x_a1", variables)
        self.assertTrue(isinstance(variables["x_a2"], Variable))
        self.assertEqual(variables["x_a3"].name, "x_a3")
Beispiel #4
0
def test_api_dcop_graph_coloring():

    # Graph coloring with 3 variables and color preferences

    dcop = DCOP('test')

    # Domain and variables
    d = Domain('color', '', ['R', 'G'])
    variables = create_variables('v', [1, 2, 3], d)

    # Creating constraints using the convenience += notation
    # We could also use the more verbose dcop.add_constraint method.
    # Variable(s) and domain(s) involved in the constraint are automatically
    # added to the dcop.
    # unary constraints for preferences
    dcop += 'cost_1', '-0.1 if v1 == "R" else 0.1 ', variables
    dcop += 'cost_2', '-0.1 if v2 == "G" else 0.1 ', variables
    dcop += 'cost_3', '-0.1 if v3 == "G" else 0.1 ', variables
    # coloring constraints : v1 != v2 != v3
    dcop += 'c1', '1 if v1 == v2 else 0', variables
    dcop += 'c2', '1 if v3 == v2 else 0', variables

    # let's check the dcop really has all required domain, variables and
    # constraints
    assert len(dcop.variables) == 3
    assert 'v1' in dcop.variables

    assert len(dcop.domains) == 1
    assert 'color' in dcop.domains

    assert len(dcop.constraints) == 5
    assert 'cost_3' in dcop.constraints
    assert 'c2' in dcop.constraints
Beispiel #5
0
    def test_create_several_variables_from_several_lists(self):
        d = Domain('color', '', ['R', 'G', 'B'])
        vars = create_variables('m_', (['x1', 'x2'], ['a1', 'a2', 'a3']), d)

        self.assertEqual(len(vars), 6)
        self.assertIn(('x1', 'a2'), vars)
        self.assertTrue(isinstance(vars[('x2', 'a3')], Variable))
        self.assertEqual(vars[('x2', 'a3')].name, 'm_x2_a3')
    def test_create_several_variables_from_several_lists(self):
        d = Domain("color", "", ["R", "G", "B"])
        variables = create_variables("m_", (["x1", "x2"], ["a1", "a2", "a3"]), d)

        self.assertEqual(len(variables), 6)
        self.assertIn(("x1", "a2"), variables)
        self.assertTrue(isinstance(variables[("x2", "a3")], Variable))
        self.assertEqual(variables[("x2", "a3")].name, "m_x2_a3")
def create_dcop():
    dcop = DCOP('test')
    # Domain and vraibales
    d = Domain('color', '', ['R', 'G'])
    variables = create_variables('v', [1, 2, 3], d)
    # unary constraints for preferences
    dcop += 'cost_1', '-0.1 if v1 == "R" else 0.1 ', variables
    dcop += 'cost_2', '-0.1 if v2 == "G" else 0.1 ', variables
    dcop += 'cost_3', '-0.1 if v3 == "G" else 0.1 ', variables
    # coloring constraints : v1 != v2 != v3
    dcop += 'c1', '1 if v1 == v2 else 0', variables
    dcop += 'c2', '1 if v3 == v2 else 0', variables

    return dcop
    def setUp(self):

        variables = list(
            create_variables('v', ['1', '2', '3'], Domain('d', '',
                                                          [1, 2])).values())
        all_diff = constraint_from_str('all_diff', 'v1 + v2 + v3 ', variables)
        v1, v2, v3 = variables
        c1 = VariableComputationNode(v1, [all_diff])
        c2 = VariableComputationNode(v2, [all_diff])
        c3 = VariableComputationNode(v3, [all_diff])
        nodes = [c1, c2, c3]
        # links = [ConstraintLink('all_diff', ['c1', 'c2', 'c3'])]

        self.cg = ComputationConstraintsHyperGraph(nodes)
        self.agents = [AgentDef('a1'), AgentDef('a2'), AgentDef('a3')]
def dcop_graphcoloring_3():
    """
    Build a simple 3-variable graph coloring problem.

    v1--v2--v3

    Each variable has a cost function which makes it prefer one color:
    v1 prefers R
    v2 prefers G
    v3 prefers G

    Of course, the preferences of v2 and v3 conflict.
    The best affectation is
    v1 - R
    v2 - G
    v3 - R

    Returns
    -------

    """
    dcop = DCOP('graphcoloring_3')
    # Domain and variables
    d = Domain('color', '', ['R', 'G'])
    variables = create_variables('v', ['1', '2', '3'], d)
    # unary constraints for preferences
    dcop += 'cost_1', '-0.1 if v1 == "R" else 0.1 ', variables
    dcop += 'cost_2', '-0.1 if v2 == "G" else 0.1 ', variables
    dcop += 'cost_3', '-0.1 if v3 == "G" else 0.1 ', variables
    # coloring constraints : v1 != v2 != v3
    dcop += 'c1', '1 if v1 == v2 else 0', variables
    dcop += 'c2', '1 if v3 == v2 else 0', variables

    # Note that we do not define the agents for this dcop here,
    # as the number of agents depends on the test case we will use the dcop for.

    return dcop