| 1 |  |  | """!
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | @brief Unit-tests for algorithm based on modified Sync.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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.nnet import solve_type;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | from pyclustering.gcolor.sync import syncgcolor;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | from pyclustering.utils.graph import read_graph;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | from pyclustering.samples.definitions import GRAPH_SIMPLE_SAMPLES;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  | class Test(unittest.TestCase):
 | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     def templateTestColoringNegativeConnections(self, filename, solver_type = solve_type.FAST):
 | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         result_testing = False;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         
 | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         # If phases crosses each other because of random part of the network then we should try again.
 | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         for attempt in range(0, 3, 1):        
 | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             graph = read_graph(filename);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             syncgcolor_network = syncgcolor(graph.data, 0, -1);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             
 | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             analyser = syncgcolor_network.process(solution = solver_type);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             
 | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             map_coloring = analyser.allocate_map_coloring(0.05);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             
 | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             # Check number of colors
 | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             assigned_colors = set(map_coloring);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             
 | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             # Check validity of color numbers
 | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             for color_number in range(0, len(assigned_colors), 1):
 | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 if (color_number not in assigned_colors):
 | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                     continue;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 
 | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             # Check validity of colors
 | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             for index_node in range(len(graph.data)):
 | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 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];
 | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 #print(index_node, map_coloring[index_node], color_neighbors, assigned_colors, map_coloring, "\n\n");
 | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 if (map_coloring[index_node] in color_neighbors):
 | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     continue;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             
 | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             result_testing = True;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 
 | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         assert result_testing;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     def testColoringFull1(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL1);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |          
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     def testColoringFull2(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL2);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |          
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     def testColoringBrokenCircle1(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE1);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |          
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     def testColoringBrokenCircle2(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE2);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |          
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     def testColoringCircle1(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE1);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     def testColoringCircle2(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE2);     
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |          
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     def testColoringFivePointedStar(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_STAR); 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |          
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     def testColoringFivePointedFrameStar(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_FRAME_STAR);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |          
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     def testColoringVerification(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE1);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_BROKEN_CIRCLE2);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_FRAME_STAR);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FIVE_POINTED_STAR);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL1);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL2);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE1);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE2);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CIRCLE3);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_CROSSROAD);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_ONE_LINE);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_SIMPLE1);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_TWO_CROSSROADS);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     def testOdeIntSolutionGraphFull1(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL1, solve_type.RK4);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     def testOdeIntSolutionGraphFull2(self):
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         self.templateTestColoringNegativeConnections(GRAPH_SIMPLE_SAMPLES.GRAPH_FULL2, solve_type.RK4);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  | if __name__ == "__main__":
 | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |     unittest.main(); |