def frame_generation(index_iteration): figure.clf() figure.suptitle("Clustering genetic algorithm (iteration: " + str(index_iteration) + ")", fontsize=18, fontweight='bold') visualizer = cluster_visualizer(4, 2, [ "The best pop. on step #" + str(index_iteration), "The best population" ]) local_minimum_clusters = ga_math.get_clusters_representation( observer.get_population_best()['chromosome'][index_iteration]) visualizer.append_clusters(local_minimum_clusters, data, 0) global_minimum_clusters = ga_math.get_clusters_representation( observer.get_global_best()['chromosome'][index_iteration]) visualizer.append_clusters(global_minimum_clusters, data, 1) ax1 = plt.subplot2grid((2, 2), (1, 0), colspan=2) ga_visualizer.show_evolution(observer, 0, index_iteration + 1, ax1, False) visualizer.show(figure, shift=0, display=False) figure.subplots_adjust(top=0.85) return [figure.gca()]
def show_clusters(data, observer, marker='.', markersize=None): """! @brief Shows allocated clusters by the genetic algorithm. @param[in] data (list): Input data that was used for clustering process by the algorithm. @param[in] observer (ga_observer): Observer that was used for collection information about clustering process. @param[in] marker (char): Type of marker that should be used for object (point) representation. @param[in] markersize (uint): Size of the marker that is used for object (point) representation. @note If you have clusters instead of observer then 'cluster_visualizer' can be used for visualization purposes. @see cluster_visualizer """ figure = plt.figure() ax1 = figure.add_subplot(121) clusters = ga_math.get_clusters_representation( observer.get_global_best()['chromosome'][-1]) visualizer = cluster_visualizer(1, 2) visualizer.append_clusters(clusters, data, 0, marker, markersize) visualizer.show(figure, display=False) ga_visualizer.show_evolution(observer, 0, None, ax1, True)
def templateTestObserverCollecting(self, amount_clusters, iterations, global_optimum, local_optimum, average, **kwargs): observer_instance = ga_observer(global_optimum, local_optimum, average) self.assertEqual(0, len(observer_instance)) sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1) ga_instance = genetic_algorithm(sample, amount_clusters, 20, iterations, count_mutation_gens=2, coeff_mutation_count=0.25, observer=observer_instance, **kwargs) ga_instance.process() self.assertEqual(observer_instance, ga_instance.get_observer()) expected_length = 0 if global_optimum is True: expected_length = iterations + 1 self.assertEqual(expected_length, len(observer_instance)) self.assertEqual( expected_length, len(observer_instance.get_global_best()['chromosome'])) self.assertEqual( expected_length, len(observer_instance.get_global_best()['fitness_function'])) expected_length = 0 if local_optimum is True: expected_length = iterations + 1 self.assertEqual(expected_length, len(observer_instance)) self.assertEqual( expected_length, len(observer_instance.get_population_best()['chromosome'])) self.assertEqual( expected_length, len(observer_instance.get_population_best()['fitness_function'])) expected_length = 0 if average is True: expected_length = iterations + 1 self.assertEqual(expected_length, len(observer_instance)) self.assertEqual(expected_length, len(observer_instance.get_mean_fitness_function())) if global_optimum is True: clusters = ga_math.get_clusters_representation( observer_instance.get_global_best()['chromosome'][-1]) self.assertEqual(amount_clusters, len(clusters)) return sample, observer_instance
def get_clusters(self): """! @brief Returns list of allocated clusters, each cluster contains indexes of objects from the data. @return (list) List of allocated clusters. @see process() """ return ga_math.get_clusters_representation(self._result_clustering['best_chromosome'], self._count_clusters)
def templateTestObserverCollecting(self, amount_clusters, iterations, global_optimum, local_optimum, average): testing_result = False observer_instance = None sample = None for _ in range(3): observer_instance = ga_observer(global_optimum, local_optimum, average) assert len(observer_instance) == 0 sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1) ga_instance = genetic_algorithm(sample, amount_clusters, 20, iterations, count_mutation_gens=2, coeff_mutation_count=0.25, observer=observer_instance) ga_instance.process() assert observer_instance == ga_instance.get_observer() expected_length = 0 if (global_optimum is True): expected_length = iterations + 1 assert expected_length == len(observer_instance) assert expected_length == len(observer_instance.get_global_best()['chromosome']); assert expected_length == len(observer_instance.get_global_best()['fitness_function']); expected_length = 0 if (local_optimum is True): expected_length = iterations + 1 assert expected_length == len(observer_instance); assert expected_length == len(observer_instance.get_population_best()['chromosome']); assert expected_length == len(observer_instance.get_population_best()['fitness_function']); expected_length = 0 if (average is True): expected_length = iterations + 1 assert expected_length == len(observer_instance); assert expected_length == len(observer_instance.get_mean_fitness_function()); if (global_optimum is True): clusters = ga_math.get_clusters_representation(observer_instance.get_global_best()['chromosome'][-1]) if amount_clusters != len(clusters): continue testing_result = True break assert testing_result == True return sample, observer_instance