def templateDataClustering(self, sample_path, amount_clusters, chromosome_count, population_count, count_mutation_gens, coeff_mutation_count, expected_clusters_sizes): testing_result = False; for _ in range(3): sample = read_sample(sample_path); ga_instance = genetic_algorithm(sample, amount_clusters, chromosome_count, population_count, count_mutation_gens, coeff_mutation_count); ga_instance.process(); clusters = ga_instance.get_clusters(); obtained_cluster_sizes = [len(cluster) for cluster in clusters]; if (len(sample) != sum(obtained_cluster_sizes)): continue; if (expected_clusters_sizes != None): obtained_cluster_sizes.sort(); expected_clusters_sizes.sort(); if (obtained_cluster_sizes != expected_clusters_sizes): continue; testing_result = True; break; assert testing_result == True;
def templateDataClustering(self, sample_path, amount_clusters, chromosome_count, population_count, count_mutation_gens, coeff_mutation_count, expected_clusters_sizes): testing_result = False for _ in range(3): sample = read_sample(sample_path) ga_instance = genetic_algorithm(sample, amount_clusters, chromosome_count, population_count, count_mutations_gen=count_mutation_gens, coeff_mutation_count=coeff_mutation_count) ga_instance.process() clusters = ga_instance.get_clusters() obtained_cluster_sizes = [len(cluster) for cluster in clusters] if len(sample) != sum(obtained_cluster_sizes): continue if expected_clusters_sizes is not None: obtained_cluster_sizes.sort() expected_clusters_sizes.sort() if obtained_cluster_sizes != expected_clusters_sizes: continue testing_result = True break assert testing_result is True
def templateDataClustering(self, sample_path, amount_clusters, chromosome_count, population_count, count_mutation_gens, coeff_mutation_count, expected_clusters_sizes, **kwargs): scale_points = kwargs.get('scale_points', None) sample = numpy.array(read_sample(sample_path)) if scale_points is not None: sample = sample * scale_points ga_instance = genetic_algorithm( sample, amount_clusters, chromosome_count, population_count, count_mutations_gen=count_mutation_gens, coeff_mutation_count=coeff_mutation_count, **kwargs) ga_instance.process() clusters = ga_instance.get_clusters() obtained_cluster_sizes = [len(cluster) for cluster in clusters] self.assertEqual(len(sample), sum(obtained_cluster_sizes)) if expected_clusters_sizes is not None: obtained_cluster_sizes.sort() expected_clusters_sizes.sort() self.assertEqual(obtained_cluster_sizes, expected_clusters_sizes)
def runGeneticAlgorithm(self, test_case_name, data, count_chromosomes, count_clusters, count_populations, count_mutations_gen, result_should_be): # Result best_ff = float('inf') # Several attempts for randomize algorithm for _attempt in range(GeneticAlgorithmClusteringUnitTest.attempts): _, best_ff, = genetic_algorithm( data=data, count_clusters=count_clusters, chromosome_count=count_chromosomes, population_count=count_populations, count_mutation_gens=count_mutations_gen).process() # Check result for attempt if best_ff == result_should_be: if _attempt > 0: print('Test case :', test_case_name, ' success with attempts : ', _attempt + 1) break # Check result self.assertEqual(best_ff, result_should_be)
def template_clustering(path, count_clusters, chromosome_count, population_count, count_mutation_gens, coeff_mutation_count=0.25, select_coeff=1.0, fps=15, animation=False): sample = read_sample(path); algo_instance = genetic_algorithm(data=sample, count_clusters=count_clusters, chromosome_count=chromosome_count, population_count=population_count, count_mutation_gens=count_mutation_gens, coeff_mutation_count=coeff_mutation_count, select_coeff=select_coeff, observer=ga_observer(True, True, True)) start_time = time.time(); algo_instance.process(); print("Sample: ", path, "\t\tExecution time: ", time.time() - start_time, "\n"); observer = algo_instance.get_observer(); ga_visualizer.show_clusters(sample, observer); if (animation is True): ga_visualizer.animate_cluster_allocation(sample, observer, movie_fps=fps, save_movie="clustering_animation.mp4");
def template_clustering(path, count_clusters, chromosome_count, population_count, count_mutation_gens, coeff_mutation_count=0.25, select_coeff=1.0, fps=15, animation=False): sample = read_sample(path) algo_instance = genetic_algorithm(data=sample, count_clusters=count_clusters, chromosome_count=chromosome_count, population_count=population_count, count_mutation_gens=count_mutation_gens, coeff_mutation_count=coeff_mutation_count, select_coeff=select_coeff, observer=ga_observer(True, True, True)) start_time = time.time() algo_instance.process() print("Sample: ", path, "\t\tExecution time: ", time.time() - start_time, "\n") observer = algo_instance.get_observer() ga_visualizer.show_clusters(sample, observer) if (animation is True): ga_visualizer.animate_cluster_allocation(sample, observer, movie_fps=fps, save_movie="clustering_animation.mp4")
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 testNoneObserver(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1) ga_instance = genetic_algorithm(sample, 2, 20, 20, 2, 0.25, observer=None) ga_instance.process() assert None == ga_instance.get_observer()
def testNoneObserver(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1) ga_instance = genetic_algorithm(sample, 2, 20, 20, count_mutation_gens=2, coeff_mutation_count=0.25, observer=None) ga_instance.process() assert None is ga_instance.get_observer()
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
def runGeneticAlgorithm(self, test_case_name, data, count_chromosomes, count_clusters, count_populations, count_mutations_gen, result_should_be): _, best_ff, = genetic_algorithm( data=data, count_clusters=count_clusters, chromosome_count=count_chromosomes, population_count=count_populations, count_mutation_gens=count_mutations_gen, random_state=1000).process() # Check result self.assertEqual(best_ff, result_should_be)
def runGeneticAlgorithm(self, test_case_name, data, count_chromosomes, count_clusters, count_populations, count_mutations_gen, result_should_be): # Result best_ff = float('inf') # Several attempts for randomize algorithm for _attempt in range(GeneticAlgorithmClusteringUnitTest.attempts): _, best_ff, = genetic_algorithm(data=data, count_clusters=count_clusters, chromosome_count=count_chromosomes, population_count=count_populations, count_mutation_gens=count_mutations_gen).process() # Check result for attempt if best_ff == result_should_be: if _attempt > 0: print('Test case :', test_case_name, ' success with attempts : ', _attempt + 1) break # Check result self.assertEqual(best_ff, result_should_be)
from pyclustering.cluster.ga import genetic_algorithm, ga_observer from pyclustering.utils import read_sample from pyclustering.samples.definitions import SIMPLE_SAMPLES # Read input data for clustering sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE4) print("SIMPLE_SAMPLES.SAMPLE_SIMPLE4)", SIMPLE_SAMPLES.SAMPLE_SIMPLE4) print("sample", sample) # Create instance of observer that will collect all information: observer_instance = ga_observer(True, True, True) # Create genetic algorithm for clustering ga_instance = genetic_algorithm(data=sample, count_clusters=4, chromosome_count=100, population_count=200, count_mutation_gens=1) # Start processing ga_instance.process() # Obtain results clusters = ga_instance.get_clusters() # Print cluster to console print("Amount of clusters: '%d'. Clusters: '%s'" % (len(clusters), clusters))