File: ga_examples.py

package info (click to toggle)
python-pyclustering 0.10.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 38,888; python: 24,311; sh: 384; makefile: 105
file content (187 lines) | stat: -rwxr-xr-x 6,211 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""!

@brief Examples of usage and demonstration of abilities of genetic algorithm for cluster analysis.

@authors Aleksey Kukushkin (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

"""


from pyclustering.samples.definitions import SIMPLE_SAMPLES

from pyclustering.cluster.ga import genetic_algorithm, ga_observer, ga_visualizer

from pyclustering.utils import read_sample

import time


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")
#         ga_visualizer.animate_cluster_allocation(sample, observer);


def cluster_sample1():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE1,
                        count_clusters=2,
                        chromosome_count=20,
                        population_count=20,
                        count_mutation_gens=2)


def cluster_sample2():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE2,
                        count_clusters=3,
                        chromosome_count=40,
                        population_count=120,
                        count_mutation_gens=2)


def cluster_sample3():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE3,
                        count_clusters=4,
                        chromosome_count=100,
                        population_count=200,
                        count_mutation_gens=2,
                        coeff_mutation_count=0.8,
                        select_coeff=0.3)


def cluster_sample4():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE4,
                        count_clusters=5,
                        chromosome_count=100,
                        population_count=200,
                        count_mutation_gens=1)


def cluster_sample5():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE5,
                        count_clusters=4,
                        chromosome_count=40,
                        population_count=140,
                        count_mutation_gens=1)


def cluster_sample6():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE6,
                        count_clusters=2,
                        chromosome_count=20,
                        population_count=100,
                        count_mutation_gens=1)


def cluster_sample7():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE7,
                        count_clusters=2,
                        chromosome_count=20,
                        population_count=30,
                        count_mutation_gens=1)


def cluster_sample11():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE11,
                        count_clusters=2,
                        chromosome_count=20,
                        population_count=30,
                        count_mutation_gens=2)


def cluster_sample8():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE8,
                        count_clusters=4,
                        chromosome_count=50,
                        population_count=200,
                        count_mutation_gens=2,
                        coeff_mutation_count=0.15,
                        select_coeff=1.0)


def animation_cluster_sample1():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE1,
                        count_clusters=2,
                        chromosome_count=10,
                        population_count=50,
                        count_mutation_gens=2,
                        select_coeff=0.02,
                        fps=5,
                        animation=True)

def animation_cluster_sample2():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE2,
                        count_clusters=3,
                        chromosome_count=30,
                        population_count=150,
                        count_mutation_gens=2,
                        select_coeff=0.02,
                        fps=8,
                        animation=True)

def animation_cluster_sample3():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE3,
                        count_clusters=4,
                        chromosome_count=100,
                        population_count=150,
                        count_mutation_gens=2,
                        coeff_mutation_count=0.8,
                        select_coeff=0.3,
                        fps=5,
                        animation=True)

def animation_cluster_sample4():
    template_clustering(SIMPLE_SAMPLES.SAMPLE_SIMPLE4,
                        count_clusters=5,
                        chromosome_count=50,
                        population_count=500,
                        count_mutation_gens=2,
                        select_coeff=0.1,
                        fps=15,
                        animation=True)


cluster_sample1()
cluster_sample2()
cluster_sample3()
cluster_sample4()
cluster_sample5()
cluster_sample6()
cluster_sample7()
cluster_sample11()

animation_cluster_sample1()
animation_cluster_sample2()
animation_cluster_sample3()
animation_cluster_sample4()