Completed
Push — master ( 55f4eb...4814fa )
by Andrei
03:31
created

antmean   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 74
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 9 1
A get_clusters() 0 8 1
A __init__() 0 17 2
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
           This algorithm uses CCORE by default - C/C++ shared library due to lack Python implementation of the algorithm.
63
    
64
    @details
65
    
66
    Code example:
67
    @code
68
        # Define ant-colony parameters
69
        params = antmean_clustering_params();
70
        params.iterations = 300;
71
        params.count_ants = 200;
72
        params.pheramone_init = 0.1;
73
        params.ro = 0.9;
74
        
75
        # Read data from text file
76
        sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE2);
77
        
78
        # Create instance of the algorithm
79
        algo = antmean(sample, 3, params);
80
        
81
        # Start clustering process
82
        algo.process();
83
        
84
        # Obtain allocated clusters by the colony
85
        res = algo.get_clusters();
86
        
87
        # Display output result (allocated clusters)
88
        print(res);
89
    @endcode
90
    
91
    """
92
    
93
    def __init__(self, sample, count_clusters, parameters):
94
        """!
95
        @brief Construct ant mean clustering algorithm using colony parameters.
96
        @details This algorithm is implemented on core side only (C/C++ part of the library).
97
        
98
        @warning Ant-Mean is working using core of the library - CCORE.
99
        
100
        @param[in] sample (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
101
        @param[in] count_clusters (uint): Amount of clusters that should be allocated.
102
        @param[in] parameters (antmean_clustering_params): Ant colony parameters.
103
        
104
        """
105
        
106
        self.__parameters = parameters if parameters is not None else antmean_clustering_params();
107
        self.__clusters = [];
108
        self.__sample = sample;
109
        self.__count_clusters = count_clusters;
110
111
112
    def process(self):
113
        """!
114
        @brief Performs cluster analysis using ant-mean colony.
115
        
116
        @see get_clusters
117
        
118
        """
119
        
120
        self.__clusters = wrapper.antmean_clustering_process(self.__parameters, self.__count_clusters, self.__sample);
121
122
123
    def get_clusters(self):
124
        """!
125
        @brief Returns allocated clusters after processing.
126
        
127
        @see process
128
        
129
        """
130
        return self.__clusters;
131