pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
sync.py
1 """!
2 
3 @brief Graph coloring algorithm based on Sync Oscillatory Network
4 @details Implementation based on paper @cite article::gcolor::sync::1.
5 
6 @authors Andrei Novikov (pyclustering@yandex.ru)
7 @date 2014-2020
8 @copyright BSD-3-Clause
9 
10 """
11 
12 from pyclustering.nnet import *
13 from pyclustering.nnet.sync import sync_network
14 from pyclustering.nnet.sync import sync_dynamic
15 
16 
18  """!
19  @brief Analyser of output dynamic of the oscillatory network syncgcolor.
20 
21  """
22 
23  def __init__(self, phase, time, pointer_sync_analyser):
24  """!
25  @brief Constructor of the analyser.
26 
27  @param[in] phase (list): Output dynamic of the oscillatory network, where one iteration consists of all phases of oscillators.
28  @param[in] time (list): Simulation time.
29  @param[in] pointer_sync_analyser (POINTER): Pointer to CCORE analyser, if specified then other arguments can be omitted.
30 
31  """
32 
33  super().__init__(phase, time, pointer_sync_analyser);
34 
35 
36  def allocate_color_clusters(self, tolerance = 0.1):
37  """!
38  @brief Allocates clusters, when one cluster defines only one color.
39 
40  @param[in] tolerance (double): Defines maximum deviation between phases.
41 
42  @return (list) Clusters [vertices with color 1], [vertices with color 2], ..., [vertices with color n].
43 
44  """
45 
46  return self.allocate_sync_ensembles(tolerance);
47 
48 
49  def allocate_map_coloring(self, tolerance = 0.1):
50  """!
51  @brief Allocates coloring map for graph that has been processed.
52 
53  @param[in] tolerance (double): Defines maximum deviation between phases.
54 
55  @return (list) Colors for each node (index of node in graph), for example [color1, color2, color2, ...].
56 
57  """
58 
59  clusters = self.allocate_color_clusters(tolerance);
60  number_oscillators = len(self._dynamic[0]);
61 
62  coloring_map = [0] * number_oscillators;
63 
64  for color_index in range(len(clusters)):
65  for node_index in clusters[color_index]:
66  coloring_map[node_index] = color_index;
67 
68  return coloring_map;
69 
70 
72  """!
73  @brief Oscillatory network based on Kuramoto model with negative and positive connections for graph coloring problem.
74 
75  """
76 
77  def __init__(self, graph_matrix, positive_weight, negative_weight, reduction = None):
78  """!
79  @brief Constructor of the oscillatory network syncgcolor for graph coloring problem.
80 
81  @param[in] graph_matrix (list): Graph represented by matrix.
82  @param[in] positive_weight (double): Value of weight of positive connections.
83  @param[in] negative_weight (double): Value of weight of negative connections.
84  @param[in] reduction (bool): Inverse degree of the processed graph.
85 
86  """
87  number_oscillators = len(graph_matrix);
88  super().__init__(number_oscillators, type_conn = conn_type.DYNAMIC, ccore = False);
89 
90  if (reduction == None):
91  self._reduction = self._num_osc;
92  else:
93  self._reduction = reduction;
94 
95  self._positive_weight = positive_weight;
96  self._negative_weight = negative_weight;
97 
98  self._create_connections(graph_matrix);
99 
100 
101  def _create_connections(self, graph_matrix):
102  """!
103  @brief Creates connection in the network in line with graph.
104 
105  @param[in] graph_matrix (list): Matrix representation of the graph.
106 
107  """
108 
109  for row in range(0, len(graph_matrix)):
110  for column in range (0, len(graph_matrix[row])):
111  if (graph_matrix[row][column] > 0):
112  self.set_connection(row, column);
113 
114 
115  def _phase_kuramoto(self, teta, t, argv):
116  """!
117  @brief Returns result of phase calculation for oscillator in the network.
118 
119  @param[in] teta (double): Value of phase of the oscillator with index argv in the network.
120  @param[in] t (double): Unused, can be ignored.
121  @param[in] argv (uint): Index of the oscillator in the network.
122 
123  @return (double) New value of phase for oscillator with index argv.
124 
125  """
126 
127  index = argv;
128  phase = 0;
129 
130  for k in range(0, self._num_osc):
131  if (self.has_connection(index, k) == True):
132  phase += self._negative_weight * math.sin(self._phases[k] - teta);
133  else:
134  phase += self._positive_weight * math.sin(self._phases[k] - teta);
135 
136  return ( phase / self._reduction );
137 
138 
139  def process(self, order = 0.998, solution = solve_type.FAST, collect_dynamic = False):
140  """!
141  @brief Performs simulation of the network (performs solving of graph coloring problem).
142 
143  @param[in] order (double): Defines when process of synchronization in the network is over, range from 0 to 1.
144  @param[in] solution (solve_type): defines type (method) of solving diff. equation.
145  @param[in] collect_dynamic (bool): If True - return full dynamic of the network, otherwise - last state of phases.
146 
147  @return (syncnet_analyser) Returns analyser of results of coloring.
148 
149  """
150 
151  analyser = self.simulate_dynamic(order, solution, collect_dynamic);
152  return syncgcolor_analyser(analyser.output, analyser.time, None);
153 
154 
pyclustering.nnet.sync.sync_dynamic
Represents output dynamic of Sync.
Definition: sync.py:93
pyclustering.nnet.sync
Neural Network: Oscillatory Neural Network based on Kuramoto model.
Definition: sync.py:1
pyclustering.gcolor.sync.syncgcolor._negative_weight
_negative_weight
Definition: sync.py:96
pyclustering.nnet.sync.sync_network.simulate_dynamic
def simulate_dynamic(self, order=0.998, solution=solve_type.FAST, collect_dynamic=False, step=0.1, int_step=0.01, threshold_changes=0.0000001)
Performs dynamic simulation of the network until stop condition is not reached.
Definition: sync.py:851
pyclustering.nnet.sync.sync_dynamic._dynamic
_dynamic
Definition: sync.py:106
pyclustering.gcolor.sync.syncgcolor_analyser
Analyser of output dynamic of the oscillatory network syncgcolor.
Definition: sync.py:17
pyclustering.nnet.sync.sync_network
Model of oscillatory network that is based on the Kuramoto model of synchronization.
Definition: sync.py:702
pyclustering.gcolor.sync.syncgcolor._reduction
_reduction
Definition: sync.py:91
pyclustering.nnet.network._num_osc
int _num_osc
Definition: __init__.py:88
pyclustering.gcolor.sync.syncgcolor.__init__
def __init__(self, graph_matrix, positive_weight, negative_weight, reduction=None)
Constructor of the oscillatory network syncgcolor for graph coloring problem.
Definition: sync.py:77
pyclustering.gcolor.sync.syncgcolor.process
def process(self, order=0.998, solution=solve_type.FAST, collect_dynamic=False)
Performs simulation of the network (performs solving of graph coloring problem).
Definition: sync.py:139
pyclustering.gcolor.sync.syncgcolor_analyser.__init__
def __init__(self, phase, time, pointer_sync_analyser)
Constructor of the analyser.
Definition: sync.py:23
pyclustering.nnet.network.has_connection
def has_connection(self, i, j)
Returns True if there is connection between i and j oscillators and False - if connection doesn't exi...
Definition: __init__.py:351
pyclustering.gcolor.sync.syncgcolor
Oscillatory network based on Kuramoto model with negative and positive connections for graph coloring...
Definition: sync.py:71
pyclustering.nnet.network.set_connection
def set_connection(self, i, j)
Couples two specified oscillators in the network with dynamic connections.
Definition: __init__.py:372
pyclustering.gcolor.sync.syncgcolor._positive_weight
_positive_weight
Definition: sync.py:95
pyclustering.gcolor.sync.syncgcolor._create_connections
def _create_connections(self, graph_matrix)
Creates connection in the network in line with graph.
Definition: sync.py:101
pyclustering.nnet
Neural and oscillatory network module. Consists of models of bio-inspired networks.
Definition: __init__.py:1
pyclustering.nnet.sync.sync_dynamic.allocate_sync_ensembles
def allocate_sync_ensembles(self, tolerance=0.01, indexes=None, iteration=None)
Allocate clusters in line with ensembles of synchronous oscillators where each synchronous ensemble c...
Definition: sync.py:174
pyclustering.nnet.sync.sync_network._phases
_phases
Definition: sync.py:736
pyclustering.gcolor.sync.syncgcolor_analyser.allocate_map_coloring
def allocate_map_coloring(self, tolerance=0.1)
Allocates coloring map for graph that has been processed.
Definition: sync.py:49
pyclustering.gcolor.sync.syncgcolor_analyser.allocate_color_clusters
def allocate_color_clusters(self, tolerance=0.1)
Allocates clusters, when one cluster defines only one color.
Definition: sync.py:36