aimsalgo 6.0.0
Neuroimaging image processing
dynamicstrategy_d.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34
35#ifndef DYNAMICSTRATEGY_D_H
36#define DYNAMICSTRATEGY_D_H
37
43#include <vector>
44#include <list>
45#include <stdlib.h>
46
47
48template <class T>
50 aims::KmeansStrategy<T>( dynamicStrat )
51{
52 this->myMeanVector = dynamicStrat.myMeanVector ;
53 this->myVarianceVector = dynamicStrat.myVarianceVector ;
54 this->myDistance = dynamicStrat.myDistance ;
55 this->myBeginIndex = dynamicStrat.myBeginIndex ;
56 this->myEndIndex = dynamicStrat.myEndIndex ;
57}
58
59
60template <class T>
62 int beginIndex , int endIndex ,
63 const std::vector< aims::Individuals<T> >& codeVector ) :
64 aims::KmeansStrategy<T>( nbIterations, distanceType, beginIndex, endIndex, codeVector )
65{
66}
67
68
69template <class T>
73
74
75template <class T>
80
81
82template <class T>
83double aims::DynamicStrategy<T>::iterate( int& nbOfIterations,
84 std::vector< std::list< Individuals<T> > >& classes )
85{
86 std::cout << "Dynamic clustering - Iteration n° " << nbOfIterations << std::endl ;
87 int nbOfClasses = classes.size() ;
88 int indMin = 0 ;
89 unsigned int classCardNew, classCardOld ;
90 int nbChanges = 0 ;
91
92/* std::cout << "nb d'individus ds les classes en entrant ds iterate:" ; */
93/* for( int c = 0 ; c < nbOfClasses ; ++c ) */
94/* std::cout << " " << classes[c].size() ; */
95/* std::cout << std::endl ; */
96
97 std::vector< std::list< aims::Individuals<T> > >* newClasses ;
98 newClasses = &classes ;
99
100 // QUAND LES INDIVIDUS SONT DS LA CLASSE 0, FAIRE 1 ITERATION DE KMEANS
101 if( !nbOfIterations && ( classes[0].size() != 0 ) ){
102 // dispatcher les individus de la classe 0 dans les classes 1 à c
103 typename std::list< aims::Individuals<T> >::iterator iter( classes[0].begin() ),
104 last( classes[0].end() ) ;
105 while( iter != last ){
106 int indMin = aggregate( *iter ) ;
107 (*newClasses)[indMin].push_back( *iter ) ;
108 ++iter ;
109 }
110 (*newClasses)[0].clear() ;
111 // faire l'analyse des classes
112 analyse( *newClasses ) ;
113 }
114 else{
115 for( int c = 0 ; c < nbOfClasses ; ++c ){
116 typename std::list< aims::Individuals<T> >::iterator iter( classes[c].begin() ),
117 last( classes[c].end() ), iterToMove ;
118 while( iter != last ){
119 iterToMove = iter ;
120 indMin = aggregate( *iter ) ;
121 ++iter ;
122 // si l'individu change de classe, faire les modifs des classes et le calcul des centres de ces classes
123 if( c != indMin ){
124 ++nbChanges ;
125 classCardNew = (*newClasses)[indMin].size() ;
126 classCardOld = (*newClasses)[c].size() ;
127
128 centerComputation( indMin, c, classCardNew, classCardOld, *iterToMove ) ;
129
130 (*newClasses)[indMin].push_back( *iterToMove ) ;
131 (*newClasses)[c].erase( iterToMove ) ;
132 }
133 }
134 }
135 }
136
137 classes = *newClasses ;
138
139 std::cout << "nb d'individus - iterate end :" ;
140 for( int c = 0 ; c < nbOfClasses ; ++c )
141 std::cout << " " << classes[c].size() ;
142 std::cout << std::endl ;
143
144 double new_inertia = globInertia( classes ) ; // calcul de l'inertie globale */
145
146 ++nbOfIterations ; // compteur nb d'iterations
147
148 return new_inertia ;
149}
150
151
152template <class T>
153void aims::DynamicStrategy<T>::centerComputation( int cNew, int cOld, unsigned int cardNew,
154 unsigned int cardOld, const Individuals<T>& ind )
155{
156 // récupérer le vecteur code moyen de la classe
157 Individuals<T> meanIndivNew = this->getMeanValue( cNew ) ;
158 Individuals<T> meanIndivOld = this->getMeanValue( cOld ) ;
159
160 Point3df meanIndivPosNew = meanIndivNew.position() ;
161 Point3df meanIndivPosOld = meanIndivOld.position() ;
162 Point3df indPos = ind.position() ;
163
164 for ( int i = 0 ; i < 3 ; ++i ){
165 meanIndivPosNew[i] = ( ( meanIndivPosNew[i] * cardNew ) + indPos[i] ) / ( cardNew + 1 ) ;
166 meanIndivPosOld[i] = ( ( meanIndivPosOld[i] * cardOld ) - indPos[i] ) / ( cardOld - 1 ) ;
167 }
168
169 std::vector<T> indVal = ind.value() ;
170 unsigned int valIndSize = indVal.size() ;
171
172 std::vector<T> meanIndivValNew = meanIndivNew.value() ;
173 std::vector<T> meanIndivValOld = meanIndivOld.value() ;
174 std::vector<T> meanIndivValNewClass = meanIndivValNew ;
175 std::vector<T> meanIndivValOldClass = meanIndivValOld ;
176
177 for( unsigned int k = 0 ; k < valIndSize ; ++k ){
178 meanIndivValNewClass[k] = ( ( meanIndivValNew[k] * cardNew ) + indVal[k] ) / ( cardNew + 1 ) ;
179 meanIndivValOldClass[k] = ( ( meanIndivValOld[k] * cardOld ) - indVal[k] ) / ( cardOld - 1 ) ;
180 }
181
182 Individuals<T> newMeanIndiv( meanIndivPosNew, meanIndivValNewClass ) ;
183 this->myMeanVector[cNew] = newMeanIndiv ;
184 Individuals<T> oldMeanIndiv( meanIndivPosOld, meanIndivValOldClass ) ;
185 this->myMeanVector[cOld] = oldMeanIndiv ;
186}
187
188
189#endif
DynamicStrategy(const DynamicStrategy< T > &dynamicStrat)
virtual ClassifStrategy< T > * clone() const
virtual double iterate(int &nbOfIterations, std::vector< std::list< Individuals< T > > > &classes)
void centerComputation(int cNew, int cOld, unsigned int cardNew, unsigned int cardOld, const Individuals< T > &ind)
const std::vector< T > & value() const
Definition individuals.h:51
const Point3df & position() const
Definition individuals.h:50
virtual int aggregate(const Individuals< T > &individual)
float(* myDistance)(const std::vector< T > &ind1, const std::vector< T > &ind2, unsigned int beginIndex, unsigned int endIndex)
virtual double globInertia(const std::vector< std::list< Individuals< T > > > &classes)
virtual Individuals< T > getMeanValue(int classe)
KmeansStrategy(const KmeansStrategy< T > &kmeanStrat)
virtual void analyse(const std::vector< std::list< Individuals< T > > > &classes)
std::vector< Individuals< T > > myMeanVector
std::vector< Individuals< T > > myVarianceVector
AimsVector< float, 3 > Point3df