pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
hysteresis.py
1 """!
2 
3 @brief Graph coloring algorithm: Algorithm based on Hysteresis Oscillatory Network
4 @details Implementation based on paper @cite article::gcolor::hysteresis::1.
5 
6 @authors Andrei Novikov (pyclustering@yandex.ru)
7 @date 2014-2020
8 @copyright BSD-3-Clause
9 
10 """
11 
12 
13 from pyclustering.nnet.hysteresis import hysteresis_network, hysteresis_dynamic
14 
15 
17  """!
18  @brief Performs analysis of output dynamic of the hysteresis oscillatory network to extract information about clusters or color allocation.
19 
20  """
21 
22  def __init__(self, amplitudes, time):
23  """!
24  @brief Constructor of the analyser.
25 
26  @param[in] amplitudes (list): Output dynamic of the hysteresis oscillatory network, where one iteration consists of all amplitudes of oscillators.
27  @param[in] time (list): Simulation time (timestamps of simulation steps) when amplitudes are stored.
28 
29  """
30  super().__init__(amplitudes, time)
31 
32 
33  def allocate_clusters(self, tolerance=0.1, threshold_steps=10):
34  """!
35  @brief Returns list of clusters in line with state of ocillators (phases).
36 
37  @param[in] tolerance (double): Maximum error for allocation of synchronous ensemble oscillators.
38  @param[in] threshold_steps (uint): Number of steps from the end of simulation that should be analysed for ensemble allocation.
39  If amount of simulation steps has been less than threshold steps than amount of steps will be reduced to amount
40  of simulation steps.
41 
42  @remark Results can be obtained only after network simulation (graph processing by the network).
43 
44  @return (list) List of clusters, for example [ [cluster1], [cluster2], ... ].
45 
46  @see allocate_map_coloring()
47 
48  """
49  return self.allocate_sync_ensembles(tolerance, threshold_steps=threshold_steps)
50 
51 
52  def allocate_map_coloring(self, tolerance, threshold_steps = 10):
53  """!
54  @brief Returns list of color indexes that are assigned to each object from input data space accordingly.
55 
56  @param[in] tolerance (double): Tolerance level that define maximal difference between outputs of oscillators in one synchronous ensemble.
57  @param[in] threshold_steps (uint): Number of steps from the end of simulation that should be analysed for ensemble allocation.
58  If amount of simulation steps has been less than threshold steps than amount of steps will be reduced to amount
59  of simulation steps.
60 
61  @remark Results can be obtained only after network simulation (graph processing by the network).
62 
63  @return (list) Color indexes that are assigned to each object from input data space accordingly.
64 
65  @see allocate_clusters()
66 
67  """
68  clusters = self.allocate_clusters(tolerance, threshold_steps)
69 
70  coloring_map = [0] * len(self._dynamic[0])
71 
72  for color_index in range(len(clusters)):
73  for node_index in clusters[color_index]:
74  coloring_map[node_index] = color_index
75 
76  return coloring_map
77 
78 
80  """!
81  @brief Class represents graph coloring algorithm based on hysteresis oscillatory network.
82  This is bio-inspired algorithm where the network uses relaxation oscillators that is
83  regarded as a multi-vibrator. Each ensemble of synchronous oscillators corresponds to
84  only one color.
85 
86  Example
87  @code
88  # import required modules
89  from pyclustering.nnet.hysteresis import hysteresis_visualizer;
90 
91  from pyclustering.gcolor.hysteresis import hysteresisgcolor;
92 
93  from pyclustering.utils.graph import read_graph, draw_graph;
94 
95  # load graph from a file
96  graph = read_graph(filename);
97 
98  # create oscillatory network for solving graph coloring problem
99  network = hysteresisgcolor(graph.data, alpha, eps);
100 
101  # perform simulation of the network
102  output_dynamic = network.simulate(2000, 20);
103 
104  # show dynamic of the network
105  hysteresis_visualizer.show_output_dynamic(output_dynamic);
106 
107  # obtain results of graph coloring and display results
108  coloring_map = hysteresis_visualizer.allocate_map_coloring();
109  draw_graph(graph, coloring_map);
110  @endcode
111 
112  """
113 
114  def __init__(self, graph_matrix, alpha, eps):
115  """!
116  @brief Constructor of hysteresis oscillatory network for graph coloring.
117 
118  @param[in] graph_matrix (list): Matrix representation of a graph.
119  @param[in] alpha (double): Positive constant (affect weight between two oscillators w[i][j]).
120  @param[in] eps (double): Positive constant (affect feedback to itself (i = j) of each oscillator w[i][j] = -alpha - eps).
121 
122  """
123  number_oscillators = len(graph_matrix)
124 
125  super().__init__(number_oscillators)
126 
127  self._states = [0] * self._num_osc
128  for i in range(0, self._num_osc):
129  self._states[i] = 1 - (2 / self._num_osc) * i
130 
131  self._outputs = [-1] * self._num_osc
132  self._outputs_buffer = [-1] * self._num_osc
133  self._time_contant = 1
134 
135  # Create connections
136  self._weight = []
137  for row in range(0, self._num_osc):
138  self._weight.append([0] * self._num_osc)
139  for col in range(0, self._num_osc):
140  if (row != col):
141  self._weight[row][col] = -alpha * (graph_matrix[row][col]) / sum(graph_matrix[row])
142  else:
143  self._weight[row][col] = -alpha - eps
144 
145 
146  def process(self, steps, time, collect_dynamic=True):
147  """!
148  @brief Peforms graph coloring analysis using simulation of the oscillatory network.
149 
150  @param[in] steps (uint): Number steps of simulations during simulation.
151  @param[in] time (double): Time of simulation.
152  @param[in] collect_dynamic (bool): Specified requirement to collect whole dynamic of the network.
153 
154  @return (hysteresis_analyser) Returns analyser of results of clustering.
155 
156  """
157 
158  output_dynamic = super().simulate(steps, time, collect_dynamic=collect_dynamic)
159  return hysteresis_analyser(output_dynamic.output, output_dynamic.time)
pyclustering.gcolor.hysteresis.hysteresisgcolor.process
def process(self, steps, time, collect_dynamic=True)
Peforms graph coloring analysis using simulation of the oscillatory network.
Definition: hysteresis.py:146
pyclustering.nnet.hysteresis.hysteresis_dynamic
Represents output dynamic of hysteresis oscillatory network.
Definition: hysteresis.py:21
pyclustering.gcolor.hysteresis.hysteresisgcolor._states
_states
Definition: hysteresis.py:127
pyclustering.nnet.network._num_osc
int _num_osc
Definition: __init__.py:88
pyclustering.gcolor.hysteresis.hysteresis_analyser.allocate_map_coloring
def allocate_map_coloring(self, tolerance, threshold_steps=10)
Returns list of color indexes that are assigned to each object from input data space accordingly.
Definition: hysteresis.py:52
pyclustering.gcolor.hysteresis.hysteresis_analyser
Performs analysis of output dynamic of the hysteresis oscillatory network to extract information abou...
Definition: hysteresis.py:16
pyclustering.gcolor.hysteresis.hysteresis_analyser.allocate_clusters
def allocate_clusters(self, tolerance=0.1, threshold_steps=10)
Returns list of clusters in line with state of ocillators (phases).
Definition: hysteresis.py:33
pyclustering.gcolor.hysteresis.hysteresisgcolor.__init__
def __init__(self, graph_matrix, alpha, eps)
Constructor of hysteresis oscillatory network for graph coloring.
Definition: hysteresis.py:114
pyclustering.nnet.hysteresis.hysteresis_network.simulate
def simulate(self, steps, time, solution=solve_type.RK4, collect_dynamic=True)
Performs static simulation of hysteresis oscillatory network.
Definition: hysteresis.py:267
pyclustering.nnet.hysteresis.hysteresis_network
Hysteresis oscillatory network that uses relaxation oscillators that are represented by objective hys...
Definition: hysteresis.py:137
pyclustering.gcolor.hysteresis.hysteresisgcolor._outputs
_outputs
Definition: hysteresis.py:131
pyclustering.nnet.hysteresis.hysteresis_dynamic._dynamic
_dynamic
Definition: hysteresis.py:58
pyclustering.gcolor.hysteresis.hysteresisgcolor._time_contant
_time_contant
Definition: hysteresis.py:133
pyclustering.nnet.hysteresis
Neural Network: Hysteresis Oscillatory Network.
Definition: hysteresis.py:1
pyclustering.gcolor.hysteresis.hysteresisgcolor
Class represents graph coloring algorithm based on hysteresis oscillatory network.
Definition: hysteresis.py:79
pyclustering.gcolor.hysteresis.hysteresis_analyser.__init__
def __init__(self, amplitudes, time)
Constructor of the analyser.
Definition: hysteresis.py:22
pyclustering.nnet.hysteresis.hysteresis_dynamic.allocate_sync_ensembles
def allocate_sync_ensembles(self, tolerance=0.1, threshold_steps=1)
Allocate clusters in line with ensembles of synchronous oscillators where each synchronous ensemble c...
Definition: hysteresis.py:72
pyclustering.gcolor.hysteresis.hysteresisgcolor._weight
_weight
Definition: hysteresis.py:136
pyclustering.gcolor.hysteresis.hysteresisgcolor._outputs_buffer
_outputs_buffer
Definition: hysteresis.py:132