pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
ga.py
1 """!
2 
3 @brief Cluster analysis algorithm: Genetic clustering algorithm (GA).
4 @details Implementation based on papers @cite article::ga::1, @cite article::ga::2.
5 
6 @authors Aleksey Kukushkin, Andrei Novikov (pyclustering@yandex.ru)
7 @date 2014-2020
8 @copyright BSD-3-Clause
9 
10 """
11 
12 
13 import numpy
14 
15 import matplotlib.pyplot as plt
16 import matplotlib.animation as animation
17 
18 from pyclustering.cluster import cluster_visualizer
19 from pyclustering.cluster.ga_maths import ga_math
20 
21 
22 
24  """!
25  @brief Genetic algorithm observer that is used to collect information about clustering process on each iteration.
26 
27  """
28 
29  def __init__(self, need_global_best=False, need_population_best=False, need_mean_ff=False):
30  """!
31  @brief Constructs genetic algorithm observer to collect specific information.
32 
33  @param[in] need_global_best (bool): If 'True' then the best chromosomes and its fitness function value (global optimum) for each iteration are stored.
34  @param[in] need_population_best (bool): If 'True' then current (on each iteration) best chromosomes and its fitness function value (local optimum) are stored.
35  @param[in] need_mean_ff (bool): If 'True' then average value of fitness function on each iteration is stored.
36 
37  """
38 
39  # Global best chromosome and fitness function for each population
40  self._global_best_result = {'chromosome': [], 'fitness_function': []}
41 
42  # Best chromosome and fitness function on a population
43  self._best_population_result = {'chromosome': [], 'fitness_function': []}
44 
45  # Mean fitness function on each population
46  self._mean_ff_result = []
47 
48  # Flags to collect
49  self._need_global_best = need_global_best
50  self._need_population_best = need_population_best
51  self._need_mean_ff = need_mean_ff
52 
53 
54  def __len__(self):
55  """!
56  @brief Returns amount of iterations that genetic algorithm was observed.
57 
58  """
59  global_length = len(self._global_best_result['chromosome'])
60  local_length = len(self._best_population_result['chromosome'])
61  average_length = len(self._mean_ff_result)
62 
63  return max(global_length, local_length, average_length)
64 
65 
66  def collect_global_best(self, best_chromosome, best_fitness_function):
67  """!
68  @brief Stores the best chromosome and its fitness function's value.
69 
70  @param[in] best_chromosome (list): The best chromosome that were observed.
71  @param[in] best_fitness_function (float): Fitness function value of the best chromosome.
72 
73  """
74 
75  if not self._need_global_best:
76  return
77 
78  self._global_best_result['chromosome'].append(best_chromosome)
79  self._global_best_result['fitness_function'].append(best_fitness_function)
80 
81 
82  def collect_population_best(self, best_chromosome, best_fitness_function):
83  """!
84  @brief Stores the best chromosome for current specific iteration and its fitness function's value.
85 
86  @param[in] best_chromosome (list): The best chromosome on specific iteration.
87  @param[in] best_fitness_function (float): Fitness function value of the chromosome.
88 
89  """
90 
91  if not self._need_population_best:
92  return
93 
94  self._best_population_result['chromosome'].append(best_chromosome)
95  self._best_population_result['fitness_function'].append(best_fitness_function)
96 
97 
98  def collect_mean(self, fitness_functions):
99  """!
100  @brief Stores average value of fitness function among chromosomes on specific iteration.
101 
102  @param[in] fitness_functions (float): Average value of fitness functions among chromosomes.
103 
104  """
105 
106  if not self._need_mean_ff:
107  return
108 
109  self._mean_ff_result.append(numpy.mean(fitness_functions))
110 
111 
112  def get_global_best(self):
113  """!
114  @return (dict) Returns dictionary with keys 'chromosome' and 'fitness_function' where evolution of the best chromosome
115  and its fitness function's value (evolution of global optimum) are stored in lists.
116 
117  """
118  return self._global_best_result
119 
120 
122  """!
123  @brief (dict) Returns dictionary with keys 'chromosome' and 'fitness_function' where evolution of the current best chromosome
124  and its fitness function's value (evolution of local optimum) are stored in lists.
125 
126  """
127  return self._best_population_result
128 
129 
131  """!
132  @brief (list) Returns fitness function's values on each iteration.
133 
134  """
135  return self._mean_ff_result
136 
137 
138 
140  """!
141  @brief Genetic algorithm visualizer is used to show clustering results that are specific for
142  this particular algorithm: clusters, evolution of global and local optimum.
143  @details The visualizer requires 'ga_observer' that collects evolution of clustering process in
144  genetic algorithm. The observer is created by user and passed to genetic algorithm. There
145  is usage example of the visualizer using the observer:
146  @code
147  from pyclustering.cluster.ga import genetic_algorithm, ga_observer, ga_visualizer
148  from pyclustering.utils import read_sample
149  from pyclustering.samples.definitions import SIMPLE_SAMPLES
150 
151 
152  # Read data for clustering
153  sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1)
154 
155  # Create instance of observer that will collect all information:
156  observer_instance = ga_observer(True, True, True)
157 
158  # Create genetic algorithm where observer will collect information:
159  ga_instance = genetic_algorithm(data=sample,
160  count_clusters=2,
161  chromosome_count=20,
162  population_count=20,
163  count_mutation_gens=1,
164  observer=observer_instance)
165 
166  # Start processing
167  ga_instance.process()
168 
169  # Obtain results
170  clusters = ga_instance.get_clusters()
171 
172  # Print cluster to console
173  print("Amount of clusters: '%d'. Clusters: '%s'" % (len(clusters), clusters))
174 
175  # Show cluster using observer:
176  ga_visualizer.show_clusters(sample, observer_instance)
177  @endcode
178 
179  @see cluster_visualizer
180 
181  """
182 
183  @staticmethod
184  def show_evolution(observer, start_iteration=0, stop_iteration=None, ax=None, display=True):
185  """!
186  @brief Displays evolution of fitness function for the best chromosome, for the current best chromosome and
187  average value among all chromosomes.
188 
189  @param[in] observer (ga_observer): Genetic algorithm observer that was used for collecting evolution in the algorithm and
190  where whole required information for visualization is stored.
191  @param[in] start_iteration (uint): Iteration from that evolution should be shown.
192  @param[in] stop_iteration (uint): Iteration after that evolution shouldn't be shown.
193  @param[in] ax (Axes): Canvas where evolution should be displayed.
194  @param[in] display (bool): If 'True' then visualization of the evolution will be shown by the function.
195  This argument should be 'False' if you want to add something else to the canvas and display it later.
196 
197  @return (Axis) Canvas where evolution was shown.
198 
199  """
200 
201  if (ax is None):
202  _, ax = plt.subplots(1)
203  ax.set_title("Evolution")
204 
205  if stop_iteration is None:
206  stop_iteration = len(observer)
207 
208  line_best, = ax.plot(observer.get_global_best()['fitness_function'][start_iteration:stop_iteration], 'r')
209  line_current, = ax.plot(observer.get_population_best()['fitness_function'][start_iteration:stop_iteration], 'k')
210  line_mean, = ax.plot(observer.get_mean_fitness_function()[start_iteration:stop_iteration], 'c')
211 
212  if start_iteration < (stop_iteration - 1):
213  ax.set_xlim([start_iteration, (stop_iteration - 1)])
214 
215  ax.set_xlabel("Iteration")
216  ax.set_ylabel("Fitness function")
217  ax.legend([line_best, line_current, line_mean], ["The best pop.", "Cur. best pop.", "Average"], prop={'size': 10})
218  ax.grid()
219 
220  if display is True:
221  plt.show()
222 
223  return ax
224 
225 
226  @staticmethod
227  def show_clusters(data, observer, marker='.', markersize=None):
228  """!
229  @brief Shows allocated clusters by the genetic algorithm.
230 
231  @param[in] data (list): Input data that was used for clustering process by the algorithm.
232  @param[in] observer (ga_observer): Observer that was used for collection information about clustering process.
233  @param[in] marker (char): Type of marker that should be used for object (point) representation.
234  @param[in] markersize (uint): Size of the marker that is used for object (point) representation.
235 
236  @note If you have clusters instead of observer then 'cluster_visualizer' can be used for visualization purposes.
237 
238  @see cluster_visualizer
239 
240  """
241 
242  figure = plt.figure()
243  ax1 = figure.add_subplot(121)
244 
245  clusters = ga_math.get_clusters_representation(observer.get_global_best()['chromosome'][-1])
246 
247  visualizer = cluster_visualizer(1, 2)
248  visualizer.append_clusters(clusters, data, 0, marker, markersize)
249  visualizer.show(figure, display=False)
250 
251  ga_visualizer.show_evolution(observer, 0, None, ax1, True)
252 
253 
254  @staticmethod
255  def animate_cluster_allocation(data, observer, animation_velocity=75, movie_fps=5, save_movie=None):
256  """!
257  @brief Animate clustering process of genetic clustering algorithm.
258  @details This method can be also used for rendering movie of clustering process and 'ffmpeg' is required for that purpuse.
259 
260  @param[in] data (list): Input data that was used for clustering process by the algorithm.
261  @param[in] observer (ga_observer): Observer that was used for collection information about clustering process.
262  Be sure that whole information was collected by the observer.
263  @param[in] animation_velocity (uint): Interval between frames in milliseconds (for run-time animation only).
264  @param[in] movie_fps (uint): Defines frames per second (for rendering movie only).
265  @param[in] save_movie (string): If it is specified then animation will be stored to file that is specified in this parameter.
266 
267  """
268 
269  figure = plt.figure()
270 
271  def init_frame():
272  return frame_generation(0)
273 
274  def frame_generation(index_iteration):
275  figure.clf()
276 
277  figure.suptitle("Clustering genetic algorithm (iteration: " + str(index_iteration) + ")", fontsize=18, fontweight='bold')
278 
279  visualizer = cluster_visualizer(4, 2, ["The best pop. on step #" + str(index_iteration), "The best population"])
280 
281  local_minimum_clusters = ga_math.get_clusters_representation(observer.get_population_best()['chromosome'][index_iteration])
282  visualizer.append_clusters(local_minimum_clusters, data, 0)
283 
284  global_minimum_clusters = ga_math.get_clusters_representation(observer.get_global_best()['chromosome'][index_iteration])
285  visualizer.append_clusters(global_minimum_clusters, data, 1)
286 
287  ax1 = plt.subplot2grid((2, 2), (1, 0), colspan=2)
288  ga_visualizer.show_evolution(observer, 0, index_iteration + 1, ax1, False)
289 
290  visualizer.show(figure, shift=0, display=False)
291  figure.subplots_adjust(top=0.85)
292 
293  return [figure.gca()]
294 
295  iterations = len(observer)
296  cluster_animation = animation.FuncAnimation(figure, frame_generation, iterations, interval=animation_velocity, init_func=init_frame, repeat_delay=5000)
297 
298  if save_movie is not None:
299  cluster_animation.save(save_movie, writer='ffmpeg', fps=movie_fps, bitrate=1500)
300  else:
301  plt.show()
302 
303 
305  """!
306  @brief Class represents Genetic clustering algorithm.
307  @details The searching capability of genetic algorithms is exploited in order to search for appropriate
308  cluster centres.
309 
310  Example of clustering using genetic algorithm:
311  @code
312  from pyclustering.cluster.ga import genetic_algorithm, ga_observer
313  from pyclustering.utils import read_sample
314  from pyclustering.samples.definitions import SIMPLE_SAMPLES
315 
316 
317  # Read input data for clustering
318  sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE4)
319 
320  # Create instance of observer that will collect all information:
321  observer_instance = ga_observer(True, True, True)
322 
323  # Create genetic algorithm for clustering
324  ga_instance = genetic_algorithm(data=sample,
325  count_clusters=4,
326  chromosome_count=100,
327  population_count=200,
328  count_mutation_gens=1)
329 
330  # Start processing
331  ga_instance.process()
332 
333  # Obtain results
334  clusters = ga_instance.get_clusters()
335 
336  # Print cluster to console
337  print("Amount of clusters: '%d'. Clusters: '%s'" % (len(clusters), clusters))
338  @endcode
339 
340  There is an example of clustering results (fitness function evolution and allocated clusters) that were
341  visualized by 'ga_visualizer':
342 
343  @image html ga_clustering_sample_simple_04.png
344 
345  @see ga_visualizer
346  @see ga_observer
347 
348  """
349 
350  def __init__(self, data, count_clusters, chromosome_count, population_count, **kwargs):
351  """!
352  @brief Initialize genetic clustering algorithm.
353 
354  @param[in] data (numpy.array|list): Input data for clustering that is represented by two dimensional array
355  where each row is a point, for example, [[0.0, 2.1], [0.1, 2.0], [-0.2, 2.4]].
356  @param[in] count_clusters (uint): The amount of clusters that should be allocated in the data.
357  @param[in] chromosome_count (uint): The amount of chromosomes in each population.
358  @param[in] population_count (uint): The amount of populations that essentially defines the amount of iterations.
359  @param[in] **kwargs: Arbitrary keyword arguments (available arguments: `count_mutation_gens`,
360  `coeff_mutation_count`, `select_coeff`, `crossover_rate`, `observer`, `random_state`).
361 
362  <b>Keyword Args:</b><br>
363  - count_mutation_gens (uint): Amount of genes in chromosome that is mutated on each step.
364  - coeff_mutation_count (float): Percent of chromosomes for mutation, distributed in range (0, 1] and
365  thus amount of chromosomes is defined as follows: `chromosome_count` * `coeff_mutation_count`
366  - select_coeff (float): Exponential coefficient for selection procedure that is used as follows:
367  `math.exp(1 + fitness(chromosome) * select_coeff)`.
368  - crossover_rate (float): Crossover rate.
369  - observer (ga_observer): Observer that is used for collecting information of about clustering process on each step.
370  - random_state (int): Seed for random state (by default is `None`, current system time is used).
371 
372  """
373 
374  # Initialize random
375  numpy.random.seed(kwargs.get('random_state', None))
376 
377  # Clustering data
378  self._data = numpy.array(data)
379 
380  # Count clusters
381  self._count_clusters = count_clusters
382 
383  # Home many chromosome in population
384  self._chromosome_count = chromosome_count
385 
386  # How many populations
387  self._population_count = population_count
388 
389  # Count mutation genes
390  self._count_mutation_gens = kwargs.get('count_mutation_gens', 2)
391 
392  # Crossover rate
393  self._crossover_rate = kwargs.get('crossover_rate', 1.0)
394 
395  # Count of chromosome for mutation (range [0, 1])
396  self._coeff_mutation_count = kwargs.get('coeff_mutation_count', 0.25)
397 
398  # Exponential coeff for selection
399  self._select_coeff = kwargs.get('select_coeff', 1.0)
400 
401  # Result of clustering : best chromosome
402  self._result_clustering = {'best_chromosome': [],
403  'best_fitness_function': 0.0}
404 
405  # Observer
406  self._observer = kwargs.get('observer', ga_observer())
407 
408  self._verify_arguments()
409 
410 
411  def process(self):
412  """!
413  @brief Perform clustering procedure in line with rule of genetic clustering algorithm.
414 
415  @see get_clusters()
416 
417  """
418 
419  # Initialize population
420  chromosomes = self._init_population(self._count_clusters, len(self._data), self._chromosome_count)
421 
422  # Initialize the Best solution
423  best_chromosome, best_ff, first_fitness_functions \
424  = self._get_best_chromosome(chromosomes, self._data, self._count_clusters)
425 
426  # Save best result into observer
427  if self._observer is not None:
428  self._observer.collect_global_best(best_chromosome, best_ff)
429  self._observer.collect_population_best(best_chromosome, best_ff)
430  self._observer.collect_mean(first_fitness_functions)
431 
432  # Next population
433  for _ in range(self._population_count):
434 
435  # Select
436  chromosomes = self._select(chromosomes, self._data, self._count_clusters, self._select_coeff)
437 
438  # Crossover
439  self._crossover(chromosomes)
440 
441  # Mutation
442  self._mutation(chromosomes, self._count_clusters, self._count_mutation_gens, self._coeff_mutation_count)
443 
444  # Update the Best Solution
445  new_best_chromosome, new_best_ff, fitness_functions \
446  = self._get_best_chromosome(chromosomes, self._data, self._count_clusters)
447 
448  # Get best chromosome
449  if new_best_ff < best_ff:
450  best_ff = new_best_ff
451  best_chromosome = new_best_chromosome
452 
453  # Save best result into observer
454  if self._observer is not None:
455  self._observer.collect_global_best(best_chromosome, best_ff)
456  self._observer.collect_population_best(new_best_chromosome, new_best_ff)
457  self._observer.collect_mean(fitness_functions)
458 
459  # Save result
460  self._result_clustering['best_chromosome'] = best_chromosome
461  self._result_clustering['best_fitness_function'] = best_ff
462 
463  return best_chromosome, best_ff
464 
465 
466  def get_observer(self):
467  """!
468  @brief Returns genetic algorithm observer.
469 
470  """
471  return self._observer
472 
473 
474  def get_clusters(self):
475  """!
476  @brief Returns list of allocated clusters, each cluster contains indexes of objects from the data.
477 
478  @return (list) List of allocated clusters.
479 
480  @see process()
481 
482  """
483 
484  return ga_math.get_clusters_representation(self._result_clustering['best_chromosome'], self._count_clusters)
485 
486 
487  @staticmethod
488  def _select(chromosomes, data, count_clusters, select_coeff):
489  """!
490  @brief Performs selection procedure where new chromosomes are calculated.
491 
492  @param[in] chromosomes (numpy.array): Chromosomes
493 
494  """
495 
496  # Calc centers
497  centres = ga_math.get_centres(chromosomes, data, count_clusters)
498 
499  # Calc fitness functions
500  fitness = genetic_algorithm._calc_fitness_function(centres, data, chromosomes)
501  fitness = numpy.exp(1.0 + fitness * select_coeff)
502 
503  # Calc probability vector
504  probabilities = ga_math.calc_probability_vector(fitness)
505 
506  # Select P chromosomes with probabilities
507  new_chromosomes = numpy.zeros(chromosomes.shape, dtype=numpy.int)
508 
509  # Selecting
510  for _idx in range(len(chromosomes)):
511  new_chromosomes[_idx] = chromosomes[ga_math.get_uniform(probabilities)]
512 
513  return new_chromosomes
514 
515 
516  @staticmethod
517  def _crossover(chromosomes):
518  """!
519  @brief Crossover procedure.
520 
521  """
522 
523  # Get pairs to Crossover
524  pairs_to_crossover = numpy.array(range(len(chromosomes)))
525 
526  # Set random pairs
527  numpy.random.shuffle(pairs_to_crossover)
528 
529  # Index offset ( pairs_to_crossover split into 2 parts : [V1, V2, .. | P1, P2, ...] crossover between V<->P)
530  offset_in_pair = int(len(pairs_to_crossover) / 2)
531 
532  # For each pair
533  for _idx in range(offset_in_pair):
534 
535  # Generate random mask for crossover
536  crossover_mask = genetic_algorithm._get_crossover_mask(len(chromosomes[_idx]))
537 
538  # Crossover a pair
539  genetic_algorithm._crossover_a_pair(chromosomes[pairs_to_crossover[_idx]],
540  chromosomes[pairs_to_crossover[_idx + offset_in_pair]],
541  crossover_mask)
542 
543 
544  @staticmethod
545  def _mutation(chromosomes, count_clusters, count_gen_for_mutation, coeff_mutation_count):
546  """!
547  @brief Mutation procedure.
548 
549  """
550 
551  # Count gens in Chromosome
552  count_gens = len(chromosomes[0])
553 
554  # Get random chromosomes for mutation
555  random_idx_chromosomes = numpy.array(range(len(chromosomes)))
556  numpy.random.shuffle(random_idx_chromosomes)
557 
558  #
559  for _idx_chromosome in range(int(len(random_idx_chromosomes) * coeff_mutation_count)):
560 
561  #
562  for _ in range(count_gen_for_mutation):
563 
564  # Get random gen
565  gen_num = numpy.random.randint(count_gens)
566 
567  # Set random cluster
568  chromosomes[random_idx_chromosomes[_idx_chromosome]][gen_num] = numpy.random.randint(count_clusters)
569 
570 
571  @staticmethod
572  def _crossover_a_pair(chromosome_1, chromosome_2, mask):
573  """!
574  @brief Crossovers a pair of chromosomes.
575 
576  @param[in] chromosome_1 (numpy.array): The first chromosome for crossover.
577  @param[in] chromosome_2 (numpy.array): The second chromosome for crossover.
578  @param[in] mask (numpy.array): Crossover mask that defines which genes should be swapped.
579 
580  """
581 
582  for _idx in range(len(chromosome_1)):
583 
584  if mask[_idx] == 1:
585  # Swap values
586  chromosome_1[_idx], chromosome_2[_idx] = chromosome_2[_idx], chromosome_1[_idx]
587 
588 
589  @staticmethod
590  def _get_crossover_mask(mask_length):
591  """!
592  @brief Crossover mask to crossover a pair of chromosomes.
593 
594  @param[in] mask_length (uint): Length of the mask.
595 
596  """
597 
598  # Initialize mask
599  mask = numpy.zeros(mask_length)
600 
601  # Set a half of array to 1
602  mask[:int(int(mask_length) / 2)] = 1
603 
604  # Random shuffle
605  numpy.random.shuffle(mask)
606 
607  return mask
608 
609 
610  @staticmethod
611  def _init_population(count_clusters, count_data, chromosome_count):
612  """!
613  @brief Returns first population as a uniform random choice.
614 
615  @param[in] count_clusters (uint): Amount of clusters that should be allocated.
616  @param[in] count_data (uint): Data size that is used for clustering process.
617  @param[in] chromosome_count (uint):Amount of chromosome that is used for clustering.
618 
619  """
620 
621  population = numpy.random.randint(count_clusters, size=(chromosome_count, count_data))
622 
623  return population
624 
625 
626  @staticmethod
627  def _get_best_chromosome(chromosomes, data, count_clusters):
628  """!
629  @brief Returns the current best chromosome.
630 
631  @param[in] chromosomes (list): Chromosomes that are used for searching.
632  @param[in] data (list): Input data that is used for clustering process.
633  @param[in] count_clusters (uint): Amount of clusters that should be allocated.
634 
635  @return (list, float, list) The best chromosome, its fitness function value and fitness function values for
636  all chromosomes.
637 
638  """
639 
640  # Calc centers
641  centres = ga_math.get_centres(chromosomes, data, count_clusters)
642 
643  # Calc Fitness functions
644  fitness_functions = genetic_algorithm._calc_fitness_function(centres, data, chromosomes)
645 
646  # Index of the best chromosome
647  best_chromosome_idx = fitness_functions.argmin()
648 
649  # Get chromosome with the best fitness function
650  return chromosomes[best_chromosome_idx], fitness_functions[best_chromosome_idx], fitness_functions
651 
652 
653  @staticmethod
654  def _calc_fitness_function(centres, data, chromosomes):
655  """!
656  @brief Calculate fitness function values for chromosomes.
657 
658  @param[in] centres (list): Cluster centers.
659  @param[in] data (list): Input data that is used for clustering process.
660  @param[in] chromosomes (list): Chromosomes whose fitness function's values are calculated.
661 
662  @return (list) Fitness function value for each chromosome correspondingly.
663 
664  """
665 
666  # Get count of chromosomes and clusters
667  count_chromosome = len(chromosomes)
668 
669  # Initialize fitness function values
670  fitness_function = numpy.zeros(count_chromosome)
671 
672  # Calc fitness function for each chromosome
673  for _idx_chromosome in range(count_chromosome):
674 
675  # Get centers for a selected chromosome
676  centres_data = numpy.zeros(data.shape)
677 
678  # Fill data centres
679  for _idx in range(len(data)):
680  centres_data[_idx] = centres[_idx_chromosome][chromosomes[_idx_chromosome][_idx]]
681 
682  # Get City Block distance for a chromosome
683  fitness_function[_idx_chromosome] += numpy.sum(abs(data - centres_data))
684 
685  return fitness_function
686 
687 
688  def _verify_arguments(self):
689  """!
690  @brief Verify input parameters for the algorithm and throw exception in case of incorrectness.
691 
692  """
693  if len(self._data) == 0:
694  raise ValueError("Input data is empty (size: '%d')." % len(self._data))
695 
696  if self._count_clusters <= 0:
697  raise ValueError("Amount of cluster (current value: '%d') for allocation should be greater than 0." %
698  self._count_clusters)
pyclustering.cluster.ga.ga_observer.collect_mean
def collect_mean(self, fitness_functions)
Stores average value of fitness function among chromosomes on specific iteration.
Definition: ga.py:98
pyclustering.cluster.ga.ga_observer.get_population_best
def get_population_best(self)
(dict) Returns dictionary with keys 'chromosome' and 'fitness_function' where evolution of the curren...
Definition: ga.py:121
pyclustering.cluster.cluster_visualizer
Common visualizer of clusters on 1D, 2D or 3D surface.
Definition: __init__.py:370
pyclustering.cluster.ga.ga_observer.collect_global_best
def collect_global_best(self, best_chromosome, best_fitness_function)
Stores the best chromosome and its fitness function's value.
Definition: ga.py:66
pyclustering.cluster.ga.genetic_algorithm._count_clusters
_count_clusters
Definition: ga.py:381
pyclustering.cluster.ga.ga_observer._mean_ff_result
_mean_ff_result
Definition: ga.py:46
pyclustering.cluster.ga.ga_observer.__init__
def __init__(self, need_global_best=False, need_population_best=False, need_mean_ff=False)
Constructs genetic algorithm observer to collect specific information.
Definition: ga.py:29
pyclustering.cluster.ga.genetic_algorithm._verify_arguments
def _verify_arguments(self)
Verify input parameters for the algorithm and throw exception in case of incorrectness.
Definition: ga.py:688
pyclustering.cluster.ga.ga_observer._need_global_best
_need_global_best
Definition: ga.py:49
pyclustering.cluster.ga.genetic_algorithm._result_clustering
_result_clustering
Definition: ga.py:402
pyclustering.cluster.ga.genetic_algorithm._select
def _select(chromosomes, data, count_clusters, select_coeff)
Performs selection procedure where new chromosomes are calculated.
Definition: ga.py:488
pyclustering.cluster.ga.ga_visualizer.show_evolution
def show_evolution(observer, start_iteration=0, stop_iteration=None, ax=None, display=True)
Displays evolution of fitness function for the best chromosome, for the current best chromosome and a...
Definition: ga.py:184
pyclustering.cluster.ga.genetic_algorithm.get_clusters
def get_clusters(self)
Returns list of allocated clusters, each cluster contains indexes of objects from the data.
Definition: ga.py:474
pyclustering.cluster.ga.ga_observer._need_population_best
_need_population_best
Definition: ga.py:50
pyclustering.cluster.ga.genetic_algorithm.get_observer
def get_observer(self)
Returns genetic algorithm observer.
Definition: ga.py:466
pyclustering.cluster.ga.ga_visualizer
Genetic algorithm visualizer is used to show clustering results that are specific for this particular...
Definition: ga.py:139
pyclustering.cluster.ga.genetic_algorithm
Class represents Genetic clustering algorithm.
Definition: ga.py:304
pyclustering.cluster
pyclustering module for cluster analysis.
Definition: __init__.py:1
pyclustering.cluster.ga.genetic_algorithm._population_count
_population_count
Definition: ga.py:387
pyclustering.cluster.ga.ga_observer.get_mean_fitness_function
def get_mean_fitness_function(self)
(list) Returns fitness function's values on each iteration.
Definition: ga.py:130
pyclustering.cluster.ga.genetic_algorithm._data
_data
Definition: ga.py:378
pyclustering.cluster.ga.ga_observer._global_best_result
_global_best_result
Definition: ga.py:40
pyclustering.cluster.ga.genetic_algorithm.process
def process(self)
Perform clustering procedure in line with rule of genetic clustering algorithm.
Definition: ga.py:411
pyclustering.cluster.ga.genetic_algorithm._get_best_chromosome
def _get_best_chromosome(chromosomes, data, count_clusters)
Returns the current best chromosome.
Definition: ga.py:627
pyclustering.cluster.ga.genetic_algorithm._init_population
def _init_population(count_clusters, count_data, chromosome_count)
Returns first population as a uniform random choice.
Definition: ga.py:611
pyclustering.cluster.ga.ga_observer.collect_population_best
def collect_population_best(self, best_chromosome, best_fitness_function)
Stores the best chromosome for current specific iteration and its fitness function's value.
Definition: ga.py:82
pyclustering.cluster.ga.genetic_algorithm._crossover_rate
_crossover_rate
Definition: ga.py:393
pyclustering.cluster.ga.genetic_algorithm._coeff_mutation_count
_coeff_mutation_count
Definition: ga.py:396
pyclustering.cluster.ga.ga_observer.__len__
def __len__(self)
Returns amount of iterations that genetic algorithm was observed.
Definition: ga.py:54
pyclustering.cluster.ga.ga_observer._need_mean_ff
_need_mean_ff
Definition: ga.py:51
pyclustering.cluster.ga.genetic_algorithm._count_mutation_gens
_count_mutation_gens
Definition: ga.py:390
pyclustering.cluster.ga.genetic_algorithm._crossover
def _crossover(chromosomes)
Crossover procedure.
Definition: ga.py:517
pyclustering.cluster.ga.genetic_algorithm.__init__
def __init__(self, data, count_clusters, chromosome_count, population_count, **kwargs)
Initialize genetic clustering algorithm.
Definition: ga.py:350
pyclustering.cluster.ga.ga_observer
Genetic algorithm observer that is used to collect information about clustering process on each itera...
Definition: ga.py:23
pyclustering.cluster.ga.ga_visualizer.animate_cluster_allocation
def animate_cluster_allocation(data, observer, animation_velocity=75, movie_fps=5, save_movie=None)
Animate clustering process of genetic clustering algorithm.
Definition: ga.py:255
pyclustering.cluster.ga.genetic_algorithm._mutation
def _mutation(chromosomes, count_clusters, count_gen_for_mutation, coeff_mutation_count)
Mutation procedure.
Definition: ga.py:545
pyclustering.cluster.ga.ga_observer.get_global_best
def get_global_best(self)
Definition: ga.py:112
pyclustering.cluster.ga.genetic_algorithm._chromosome_count
_chromosome_count
Definition: ga.py:384
pyclustering.cluster.ga.genetic_algorithm._observer
_observer
Definition: ga.py:406
pyclustering.cluster.ga.ga_observer._best_population_result
_best_population_result
Definition: ga.py:43
pyclustering.cluster.ga.ga_visualizer.show_clusters
def show_clusters(data, observer, marker='.', markersize=None)
Shows allocated clusters by the genetic algorithm.
Definition: ga.py:227
pyclustering.cluster.ga.genetic_algorithm._select_coeff
_select_coeff
Definition: ga.py:399