Ejemplo n.º 1
0
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");
Ejemplo n.º 2
0
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")
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
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))