3 @brief Graph coloring algorithm: Algorithm based on Hysteresis Oscillatory Network
4 @details Implementation based on paper @cite article::gcolor::hysteresis::1.
6 @authors Andrei Novikov (pyclustering@yandex.ru)
8 @copyright BSD-3-Clause
18 @brief Performs analysis of output dynamic of the hysteresis oscillatory network to extract information about clusters or color allocation.
24 @brief Constructor of the analyser.
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.
35 @brief Returns list of clusters in line with state of ocillators (phases).
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
42 @remark Results can be obtained only after network simulation (graph processing by the network).
44 @return (list) List of clusters, for example [ [cluster1], [cluster2], ... ].
46 @see allocate_map_coloring()
54 @brief Returns list of color indexes that are assigned to each object from input data space accordingly.
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
61 @remark Results can be obtained only after network simulation (graph processing by the network).
63 @return (list) Color indexes that are assigned to each object from input data space accordingly.
65 @see allocate_clusters()
70 coloring_map = [0] * len(self.
_dynamic[0])
72 for color_index
in range(len(clusters)):
73 for node_index
in clusters[color_index]:
74 coloring_map[node_index] = color_index
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
88 # import required modules
89 from pyclustering.nnet.hysteresis import hysteresis_visualizer;
91 from pyclustering.gcolor.hysteresis import hysteresisgcolor;
93 from pyclustering.utils.graph import read_graph, draw_graph;
95 # load graph from a file
96 graph = read_graph(filename);
98 # create oscillatory network for solving graph coloring problem
99 network = hysteresisgcolor(graph.data, alpha, eps);
101 # perform simulation of the network
102 output_dynamic = network.simulate(2000, 20);
104 # show dynamic of the network
105 hysteresis_visualizer.show_output_dynamic(output_dynamic);
107 # obtain results of graph coloring and display results
108 coloring_map = hysteresis_visualizer.allocate_map_coloring();
109 draw_graph(graph, coloring_map);
116 @brief Constructor of hysteresis oscillatory network for graph coloring.
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).
123 number_oscillators = len(graph_matrix)
125 super().
__init__(number_oscillators)
141 self.
_weight[row][col] = -alpha * (graph_matrix[row][col]) / sum(graph_matrix[row])
143 self.
_weight[row][col] = -alpha - eps
146 def process(self, steps, time, collect_dynamic=True):
148 @brief Peforms graph coloring analysis using simulation of the oscillatory network.
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.
154 @return (hysteresis_analyser) Returns analyser of results of clustering.
158 output_dynamic = super().
simulate(steps, time, collect_dynamic=collect_dynamic)