1
|
|
|
"""! |
2
|
|
|
|
3
|
|
|
@brief Cluster analysis algorithm: Ant-Mean |
4
|
|
|
@details Implementation based on article: |
5
|
|
|
- W.A.Tao, Y.Ma, J.H.Tian, M.Y.Li, W.S.Duan, Y.Y.Liang. An improved ant colony clustering algorithm. 2012. |
6
|
|
|
|
7
|
|
|
@authors Andrei Novikov, Aleksey Kukushkin ([email protected]) |
8
|
|
|
@date 2014-2017 |
9
|
|
|
@copyright GNU Public License |
10
|
|
|
|
11
|
|
|
@cond GNU_PUBLIC_LICENSE |
12
|
|
|
PyClustering is free software: you can redistribute it and/or modify |
13
|
|
|
it under the terms of the GNU General Public License as published by |
14
|
|
|
the Free Software Foundation, either version 3 of the License, or |
15
|
|
|
(at your option) any later version. |
16
|
|
|
|
17
|
|
|
PyClustering is distributed in the hope that it will be useful, |
18
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
GNU General Public License for more details. |
21
|
|
|
|
22
|
|
|
You should have received a copy of the GNU General Public License |
23
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
@endcond |
25
|
|
|
|
26
|
|
|
""" |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
import pyclustering.core.antmean_wrapper as wrapper; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class antmean_clustering_params: |
33
|
|
|
"""! |
34
|
|
|
@brief Ant-Mean algorithm parameters. |
35
|
|
|
|
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
def __init__(self): |
39
|
|
|
"""! |
40
|
|
|
@brief Constructs Ant-Mean algorithm parameters. |
41
|
|
|
|
42
|
|
|
""" |
43
|
|
|
|
44
|
|
|
## Used for pheramone evaporation |
45
|
|
|
self.ro = 0.9; |
46
|
|
|
|
47
|
|
|
## Initial value for pheramones |
48
|
|
|
self.pheramone_init = 0.1; |
49
|
|
|
|
50
|
|
|
## Amount of iterations that is used for solving |
51
|
|
|
self.iterations = 50; |
52
|
|
|
|
53
|
|
|
## Amount of ants that is used on each iteration |
54
|
|
|
self.count_ants = 20; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class antmean: |
58
|
|
|
"""! |
59
|
|
|
@brief The ant colony clustering algorithm is a swarm-intelligent method used for clustering problems that is inspired |
60
|
|
|
by the behavior of ant colonies that cluster their corpses and sort their larvae. |
61
|
|
|
|
62
|
|
|
@details |
63
|
|
|
|
64
|
|
|
Code example: |
65
|
|
|
@code |
66
|
|
|
# Define ant-colony parameters |
67
|
|
|
params = antmean_clustering_params(); |
68
|
|
|
params.iterations = 300; |
69
|
|
|
params.count_ants = 200; |
70
|
|
|
params.pheramone_init = 0.1; |
71
|
|
|
params.ro = 0.9; |
72
|
|
|
|
73
|
|
|
# Read data from text file |
74
|
|
|
sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE2); |
75
|
|
|
|
76
|
|
|
# Create instance of the algorithm |
77
|
|
|
algo = antmean(sample, 3, params); |
78
|
|
|
|
79
|
|
|
# Start clustering process |
80
|
|
|
algo.process(); |
81
|
|
|
|
82
|
|
|
# Obtain allocated clusters by the colony |
83
|
|
|
res = algo.get_clusters(); |
84
|
|
|
|
85
|
|
|
# Display output result (allocated clusters) |
86
|
|
|
print(res); |
87
|
|
|
@endcode |
88
|
|
|
|
89
|
|
|
""" |
90
|
|
|
|
91
|
|
|
def __init__(self, sample, count_clusters, parameters): |
92
|
|
|
"""! |
93
|
|
|
@brief Construct ant mean clustering algorithm using colony parameters. |
94
|
|
|
@details This algorithm is implemented on core side only (C/C++ part of the library). |
95
|
|
|
|
96
|
|
|
@warning Ant-Mean is working using core of the library - CCORE. |
97
|
|
|
|
98
|
|
|
@param[in] sample (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple. |
99
|
|
|
@param[in] count_clusters (uint): Amount of clusters that should be allocated. |
100
|
|
|
@param[in] parameters (antmean_clustering_params): Ant colony parameters. |
101
|
|
|
|
102
|
|
|
""" |
103
|
|
|
|
104
|
|
|
self.__parameters = parameters if parameters is not None else antmean_clustering_params(); |
105
|
|
|
self.__clusters = []; |
106
|
|
|
self.__sample = sample; |
107
|
|
|
self.__count_clusters = count_clusters; |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def process(self): |
111
|
|
|
"""! |
112
|
|
|
@brief Performs cluster analysis using ant-mean colony. |
113
|
|
|
|
114
|
|
|
@see get_clusters |
115
|
|
|
|
116
|
|
|
""" |
117
|
|
|
|
118
|
|
|
self.__clusters = wrapper.antmean_clustering_process(self.__parameters, self.__count_clusters, self.__sample); |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
def get_clusters(self): |
122
|
|
|
"""! |
123
|
|
|
@brief Returns allocated clusters after processing. |
124
|
|
|
|
125
|
|
|
@see process |
126
|
|
|
|
127
|
|
|
""" |
128
|
|
|
return self.__clusters; |
129
|
|
|
|