Ejemplo n.º 1
0
class TestGACVertexColoring(unittest.TestCase):
    """ Test class for GAC and vertex coloring """

    def setUp(self):

        self.variables = ["v1", "v2", "v3", "v4"]

        self.constraints = [
            VertexConstraint("v1 != v2"),
            VertexConstraint("v2 != v3"),
            VertexConstraint("v3 != v4"),
            VertexConstraint("v4 != v1"),
        ]

        self.domains = {"v1": [0], "v2": [0, 1], "v3": [0, 1], "v4": [0, 1]}

        self.solution = {"v1": [0], "v2": [1], "v3": [0], "v4": [1]}

        self.gac = GAC()

        self.gac.initialize(self.domains, self.constraints)

    def test_solve(self):
        """ Test that domain filtering solves an example with only one step """
        results = self.gac.domain_filtering()
        self.assertTrue(results)
        self.assertEqual(self.domains, self.solution)
Ejemplo n.º 2
0
class TestGAC(unittest.TestCase):
    """ Simple GAC test case """
    def setUp(self):
        variables = ['a', 'b', 'c']
        self.constraints = [
            SimpleConstraint('a != b', variables),
            SimpleConstraint('a != c', variables),
            SimpleConstraint('b != c', variables),
            SimpleConstraint('a == 2', variables),
            SimpleConstraint('b == 1', variables),
        ]

        self.domains = {
            'a': [0, 1, 2],
            'b': [0, 1, 2],
            'c': [0, 1, 2]
        }

        self.solution = {
            'a': [2],
            'c': [0],
            'b': [1],
        }

        self.gac = GAC()
        self.gac.initialize(self.domains, self.constraints)

    def test_solve(self):
        """ Test domain filtering on example which yields solution first run """
        results = self.gac.domain_filtering()
        self.assertTrue(results)
        self.assertEqual(self.domains, self.solution)
Ejemplo n.º 3
0
    def setUp(self):

        self.variables = ['a', 'b', 'c', 'd']

        self.constraints = [
            NonogramConstraint('a [0] == c [0]'),
            NonogramConstraint('a [1] == d [0]'),
            NonogramConstraint('b [0] == c [1]'),
            NonogramConstraint('b [1] == d [1]')
        ]

        self.domains = {
            'a': ['01', '10', '00', '11'],
            'b': ['10'],
            'c': ['01'],
            'd': ['10']
        }

        self.solution = {
            'a': ['01'],
            'b': ['10'],
            'c': ['01'],
            'd': ['10']
        }

        self.gac = GAC()

        self.gac.initialize(self.domains, self.constraints)
Ejemplo n.º 4
0
class NonogramBfs(BestFirstSearch):
    """ Nonogram version of A* """

    def __init__(self, start, gui=None):
        BestFirstSearch.__init__(self, start, gui)

        self.gac = GAC()

    def create_root_node(self):
        root = NonogramState(self.start, self.gac)

        self.gac.initialize(root.domains, root.state.constraints)
        self.gac.domain_filtering()

        return root

    def arc_cost(self, a, b):
        return 0.5
Ejemplo n.º 5
0
class VertexColoringBfs(BestFirstSearch):
    """ Map navigation version of A* """

    def __init__(self, start, gui=None, num_colors=None):
        BestFirstSearch.__init__(self, start, gui)

        self.gac = GAC()
        self.num_colors = num_colors or gui.num_colors

    def create_root_node(self):
        root = VertexColoringState(self.start, self.gac, self.num_colors)

        self.gac.initialize(root.domains, root.state.constraints)
        self.gac.domain_filtering()

        return root

    def arc_cost(self, a, b):
        return 0
Ejemplo n.º 6
0
class TestGACOnNonogram(unittest.TestCase):
    """ GAC test with nonogram example """
    def setUp(self):

        self.variables = ['a', 'b', 'c', 'd']

        self.constraints = [
            NonogramConstraint('a [0] == c [0]'),
            NonogramConstraint('a [1] == d [0]'),
            NonogramConstraint('b [0] == c [1]'),
            NonogramConstraint('b [1] == d [1]')
        ]

        self.domains = {
            'a': ['01', '10', '00', '11'],
            'b': ['10'],
            'c': ['01'],
            'd': ['10']
        }

        self.solution = {
            'a': ['01'],
            'b': ['10'],
            'c': ['01'],
            'd': ['10']
        }

        self.gac = GAC()

        self.gac.initialize(self.domains, self.constraints)

    def test_solve(self):
        """ Checks that domain filtering reduces the domain """
        results = self.gac.domain_filtering()
        self.assertTrue(results)
        self.assertEqual(self.domains, self.solution)
Ejemplo n.º 7
0
    def setUp(self):

        self.variables = ["v1", "v2", "v3", "v4"]

        self.constraints = [
            VertexConstraint("v1 != v2"),
            VertexConstraint("v2 != v3"),
            VertexConstraint("v3 != v4"),
            VertexConstraint("v4 != v1"),
        ]

        self.domains = {"v1": [0], "v2": [0, 1], "v3": [0, 1], "v4": [0, 1]}

        self.solution = {"v1": [0], "v2": [1], "v3": [0], "v4": [1]}

        self.gac = GAC()

        self.gac.initialize(self.domains, self.constraints)
Ejemplo n.º 8
0
    def setUp(self):
        variables = ['a', 'b', 'c']
        self.constraints = [
            SimpleConstraint('a != b', variables),
            SimpleConstraint('a != c', variables),
            SimpleConstraint('b != c', variables),
            SimpleConstraint('a == 2', variables),
            SimpleConstraint('b == 1', variables),
        ]

        self.domains = {
            'a': [0, 1, 2],
            'b': [0, 1, 2],
            'c': [0, 1, 2]
        }

        self.solution = {
            'a': [2],
            'c': [0],
            'b': [1],
        }

        self.gac = GAC()
        self.gac.initialize(self.domains, self.constraints)
Ejemplo n.º 9
0
    def __init__(self, start, gui=None, num_colors=None):
        BestFirstSearch.__init__(self, start, gui)

        self.gac = GAC()
        self.num_colors = num_colors or gui.num_colors
Ejemplo n.º 10
0
    def __init__(self, start, gui=None):
        BestFirstSearch.__init__(self, start, gui)

        self.gac = GAC()