Completed
Branch master (5cc02c)
by Andrei
01:30
created

pyclustering.gcolor.tests.Test   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 54
Duplicated Lines 0 %
Metric Value
dl 0
loc 54
rs 10
wmc 15
1
"""!
2
3
@brief Unit-tests for DSATUR algorithm.
4
5
@authors Andrei Novikov ([email protected])
6
@date 2014-2016
7
@copyright GNU Public License
8
9
@cond GNU_PUBLIC_LICENSE
10
    PyClustering is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
    
15
    PyClustering is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
    
20
    You should have received a copy of the GNU General Public License
21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
@endcond
23
24
"""
25
26
import unittest;
27
28
from pyclustering.gcolor.dsatur import dsatur;
29
30
from pyclustering.utils.graph import read_graph;
31
32
from pyclustering.samples.definitions import GRAPH_SIMPLE_SAMPLES, GRAPH_DSJC_SAMPLES;
33
34
class Test(unittest.TestCase):
35
    def templateTestColoring(self, filename):
36
        graph = read_graph(filename);
37
        
38
        dsatur_intance = dsatur(graph.data);
39
        dsatur_intance.process();
40
        map_coloring = dsatur_intance.get_colors();
41
        
42
        # Check number of colors
43
        assigned_colors = set(map_coloring);
44
        
45
        # Check validity of color numbers
46
        for color_number in range(1, len(assigned_colors) + 1, 1):
47
            assert color_number in assigned_colors;
48
            
49
        # Check validity of colors
50
        for index_node in range(len(graph.data)):
51
            color_neighbors = [ map_coloring[index] for index in range(len(graph.data[index_node])) if graph.data[index_node][index] != 0 and index_node != index];
52
            #print(index_node, map_coloring[index_node], color_neighbors, assigned_colors, map_coloring, "\n\n");
53
            assert map_coloring[index_node] not in color_neighbors;
54
    
55
    def testColoringFull1(self):
56
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL1);
57
        
58
    def testColoringBrokenCircle1(self):
59
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE1);
60
         
61
    def testColoringOneLine(self):
62
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_LINE);
63
         
64
    def testColoringOneCircle1(self):
65
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE1);
66
         
67
    def testColoringFivePointedStar(self):
68
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_STAR);
69
        
70
    def testColoringDSJC250d5(self):
71
        self.templateTestColoring(GRAPH_DSJC_SAMPLES.DSJC_250_5);
72
        
73
    def testColoringVerification(self):
74
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE1);
75
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE2);
76
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_FRAME_STAR);
77
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_STAR);
78
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL1);
79
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL2);
80
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE1);
81
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE2);
82
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE3);
83
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CROSSROAD);
84
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_LINE);
85
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_SIMPLE1);
86
        self.templateTestColoring(GRAPH_SIMPLE_SAMPLES.GRAPH_TWO_CROSSROADS);
87
        self.templateTestColoring(GRAPH_DSJC_SAMPLES.DSJC_250_5);
88
        
89
         
90
91
if __name__ == "__main__":
92
    unittest.main();