Beispiel #1
0
    def test_parse_exception(self):
        data = [(['TYPE : TSP', 'EOF'], True), (['TYPE : ATSP', 'EOF'], True)]

        for lines, expected in data:
            with self.subTest(lines=lines):
                self.tsplibtour._lines = Lines(lines)

                with self.assertRaises(TypeError):
                    self.tsplibtour._parse()
Beispiel #2
0
    def test_parse_spec(self):
        data = [('NAME : gr17', 'NAME', 'gr17'), ('TYPE: TSP', 'TYPE', 'TSP'),
                ('DIMENSION    :    17', 'DIMENSION', 17)]

        for line, key, value in data:
            with self.subTest(line=line):
                self.tsplib._lines = Lines([line])
                result = self.tsplib._parse_spec()
                expected = {key: value}
                self.assertDictEqual(result, expected)
Beispiel #3
0
    def test_parse_coords(self):
        data = [
            '1 1 1', '2	2	5', '    3    4    9', '4 5.3 7', '5 9.7 10.1', 'EOF'
        ]
        expected = [(1, 1), (2, 5), (4, 9), (5.3, 7), (9.7, 10.1)]

        self.tsplib._lines = Lines(data)

        result = self.tsplib._parse_coords()
        self.assertListEqual(result, expected)
        self.assertEqual(self.tsplib._lines.current, data[-1])
Beispiel #4
0
    def test_parse_tour(self):
        data = [
            (['1', '2', '3', '4', '5', '-1'], [1, 2, 3, 4, 5]),
            (['100', '69', '123456', '666', '-1'], [100, 69, 123456, 666]),
            (['7', '6', '5', '4', '3', '2', '1'], [7, 6, 5, 4, 3, 2, 1]),
            (['1', '2', '3', '4', '5', 'EOF'], [1, 2, 3, 4, 5]),
        ]

        for lines, expected in data:
            with self.subTest(lines=lines):
                self.tsplibtour._lines = Lines(lines)
                result = self.tsplibtour._parse_tour()
                self.assertListEqual(result, expected)
Beispiel #5
0
    def test_parse_weights(self):
        data = ['0 2 3 4', ' 5    0 7 8', '9 10 0 12 13 14 15 0', 'EOF']
        expected = [[0, 2, 3, 4], [5, 0, 7, 8], [9, 10, 0, 12],
                    [13, 14, 15, 0]]

        self.tsplib.specification = {
            'DIMENSION': len(expected),
            'EDGE_WEIGHT_FORMAT': 'FULL_MATRIX'
        }
        self.tsplib._lines = Lines(data)

        result = self.tsplib._parse_weights()
        self.assertListEqual(result, expected)
        self.assertEqual(self.tsplib._lines.current, data[-1])
Beispiel #6
0
    def test_parse(self):
        data = [
            'TYPE : TOUR', 'DIMENSION : 5', 'TOUR_SECTION', '1', '4', '3', '5',
            '2', '-1', 'EOF'
        ]
        expected_spec = {'TYPE': 'TOUR', 'DIMENSION': 5}
        expected_tour = [1, 4, 3, 5, 2]

        self.tsplibtour.specification = {}
        self.tsplibtour._lines = Lines(data)
        self.tsplibtour._parse()

        self.assertDictEqual(self.tsplibtour.specification, expected_spec)
        self.assertListEqual(self.tsplibtour.tour, expected_tour)
Beispiel #7
0
    def test_parse(self):
        data = [
            'DIMENSION : 3', 'EDGE_WEIGHT_FORMAT : FULL_MATRIX',
            'EDGE_WEIGHT_SECTION', '1 2 3 4 5', '6 7 8 9',
            'NODE_COORD_SECTION', '1 1 1', '2 4 5', '3 4.20 69.9',
            'DISPLAY_DATA_SECTION', '1 2 3', '2 12.5 67', 'EOF'
        ]
        expected_spec = {'DIMENSION': 3, 'EDGE_WEIGHT_FORMAT': 'FULL_MATRIX'}
        expected_weigths = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        expected_coords = [(1, 1), (4, 5), (4.2, 69.9)]
        expected_display = [(2, 3), (12.5, 67)]

        self.tsplib.specification = {}
        self.tsplib._lines = Lines(data)
        self.tsplib._parse()

        self.assertDictEqual(self.tsplib.specification, expected_spec)
        self.assertListEqual(self.tsplib.weights, expected_weigths)
        self.assertListEqual(self.tsplib.coords, expected_coords)
        self.assertListEqual(self.tsplib.display, expected_display)