TopolSegmentation.h

00001 #ifndef TopolSegmentation_H
00002 #define TopolSegmentation_H
00003 
00004 #include "SOMSegmentation.h"
00005 
00006 class TopolSegmentation : public SOMSegmentation {
00007 public:
00008         TopolSegmentation( const SOM * som ) : SOMSegmentation( som ) {};
00009         TIntVector Segment();
00010 
00011 private:
00012         bool element_in( TVecCluster vec1, TVecCluster vec2 );
00013 
00014         void vector_copy( TVecCluster & vec1, TVecCluster vec2 );
00015 
00016         void group( TMatrix group_dist, TMatrix mean_vector, TMatrix group_in );
00017 
00018         bool isActive( TMatrix mean_vector, int index, int d );
00019 };
00020 
00021 TIntVector 
00022 TopolSegmentation::Segment( const SOM * som ) {
00023         int i,k, j;
00024         Value_Type min, dist, diff;
00025         int m                           = netParams.getMapcode()->getMapSize();
00026         int n                           = netParams.getData()->getDataSize();
00027         int d                           = netParams.getData()->getDimension();
00028         int     lattice                 = netParams.getMapcode()->getLattice();
00029         int*    dimensions      = netParams.getMapcode()->getDimensions();
00030         TBMU * bmu                      = getLearningAlgorithm()->getBMU();
00031         TMatrix codebook        = netParams.getMapcode()->getCodebook();        
00032         TMatrix delta           = netParams.getDelta();
00033 
00034         // Initialize group_in[1..m][1..m] variable with zeros and ones in the diagonal
00035         TIntVector tmp = create_intvector(1, d);        
00036         TMatrix group_in = create_matrix(1,m,1,m);
00037         for (i=1; i<=m; i++) for (j=1; j<=m; j++)
00038                 if ( i == j ) group_in[i][j] = 1;
00039                 else group_in[i][j] = 0;
00040 
00041         // Initialize the mean_vector[1..m] for each group
00042         TvecMean mean_vector;
00043         for (i=1; i<=m; i++) {          
00044                 for (k=1; k<=d; k++) tmp[k] = codebook[i][k];
00045                 mean_vector.push_back( mean_node( tmp, 1 ) );           
00046         }
00047 
00048         // Sum of all code vectors for each group
00049         for (i=0; i<n; i++) {
00050                 if ( delta[bmu[0][i][0].i][bmu[0][i][1].i] < sqrt(2) ) {
00051                         for (k=1; k<=d; k++) {
00052                                 mean_vector[ bmu[0][i][0].i - 1 ].mean[k] += codebook[ bmu[0][i][1].i ][k];                     
00053                         }
00054                         ++mean_vector[ bmu[0][i][0].i - 1 ].num;
00055                         group_in[bmu[0][i][0].i][bmu[0][i][1].i] = 1;
00056                 }
00057         };
00058         
00059         // Calculates the mean for each group
00060         for (i=0; i<m; i++)
00061                 for (k=1; k<=d; k++ )
00062                         mean_vector[i].mean[k] /= mean_vector[i].num;
00063 
00064         // Decides for each node in what group it will be allocated
00065         int min_index;
00066         for (i=1; i<=m; i++ ) {
00067                 min = 100000000; min_index = -1;
00068                 for (j=1; j<=m; j++ )
00069                         if (group_in[i][j] == 1) {                                                              
00070                                 dist = 0;
00071                                 for (k=1; k<= d; k++) {
00072                                         diff = mean_vector[j-1].mean[k] - codebook[i][k]; 
00073                                         dist += diff*diff;
00074                                 }                               
00075                                 if ( dist < min ) {
00076                                         if ( min_index > 0 ) 
00077                                           group_in[i][min_index] = 0;
00078                                         min = dist;
00079                                     min_index = j;                                      
00080                                 }
00081                                 else
00082                                         group_in[i][j] = 0;
00083                         }
00084         }
00085         //printmatrix( group_in, 1, m, 1, m );
00086 
00087         // Initializes temporary variables              
00088         TMatrix  mean_tmp = create_matrix(1,m,1,d);
00089         for (i=1;i<=m;i++) for (j=1;j<=d;j++) mean_tmp[i][j] = 0;
00090         TVector  num_tmp  = create_vector(1, m );
00091         for (i=1;i<=m;i++) num_tmp[i] = 0;
00092 
00093         // Re-calculates the mean vector, considering the new groups formed
00094         for (i=1; i<=m; i++) {
00095                 //cout << endl << "Grupo " << i << ":";
00096                 for (j=1; j<=m; j++) {
00097                         if ( group_in[j][i] == 1 ) {
00098                                 //cout << j << " ";                             
00099                                 ++num_tmp[i];
00100                                 for (k=1; k<=d; k++)                                    
00101                                         mean_tmp[i][k] += codebook[j][k];                               
00102                         }
00103                 }
00104         }
00105         for (i=1; i<=m; i++)
00106                 if ( num_tmp[i] > 0 )
00107                         for (k=1; k<=d; k++)
00108                                 mean_tmp[i][k] /= num_tmp[i];
00109 
00110         //printmatrix( mean_tmp, 1,m,1,d);                       
00111         
00112         // Create the group distance matrix (group_dist[1..m][1..m])
00113         TMatrix group_dist = create_matrix(1,m,1,m);
00114         TMatrix distancia = create_matrix(1,m,1,m);
00115         for (i=1; i<=m; i++)
00116                 for (j=i; j<=m; j++) {
00117                         dist = 0;
00118                         for (k=1; k<= d; k++) {
00119                                 diff = mean_tmp[i][k] - mean_tmp[j][k]; 
00120                                 dist += diff*diff;
00121                         }                       
00122                         distancia[i][j] = distancia[j][i] = dist;
00123                         if (( dist <= limit ) && (i != j) && (isActive(mean_tmp,j,d)) && (isActive(mean_tmp,i,d)))
00124                            group_dist[i][j] = group_dist[j][i] = 1;
00125                         else
00126                            group_dist[i][j] = group_dist[j][i] = 0;
00127                 }
00128 
00129         //printmatrix( distancia,1,m,1,m);
00130 
00131         //printmatrix( group_dist,1,m,1,m);
00132 
00133         group( group_dist, mean_tmp, group_in );
00134 
00135         return 0;
00136 };
00137 
00138 bool 
00139 TopolSegmentation::isActive( TMatrix mean_vector, int index, int d ) {
00140         int k; 
00141         for (k=1; k<=d; k++) if ( mean_vector[index][k] != 0.0 ) return 1;
00142         return 0;
00143 };
00144 
00145 bool 
00146 TopolSegmentation::element_in( TVecCluster vec1, TVecCluster vec2 ) {
00147         TVecCluster::iterator iv1, iv2;
00148         for (iv1 = vec1.begin(); iv1 < vec1.end(); iv1++ ) 
00149                 for (iv2 = vec2.begin(); iv2 < vec2.end(); iv2++ )
00150                         if ( *iv1 == *iv2 )
00151                                 return 1;
00152         return 0;       
00153 }
00154 
00155 void 
00156 TopolSegmentation::vector_copy( TVecCluster & vec1, TVecCluster vec2 ) {        
00157         
00158         TVecCluster::iterator iv1 = vec1.begin();
00159         TVecCluster::iterator iv2 = vec2.begin();               
00160         int count = 0; int i;
00161 
00162         while (iv2 < vec2.end()) {
00163                 if ( *iv2 > *iv1 ) {            
00164                         ++iv1; ++count; }
00165                 else
00166                         if ( *iv2 == *iv1 ) 
00167                                 ++iv2;
00168                         else
00169                                 if ( *iv2 < *iv1 ) {
00170                                         vec1.push_back( *iv2 );
00171                                         ++iv2;
00172                                         iv1 = vec1.begin();
00173                                         for(i=1; i<=count; i++) ++iv1; 
00174                                 }
00175                 if ( iv1 == vec1.end() )
00176                         while ( iv2 < vec2.end() ) {
00177                                 if ( *iv2 != *iv1 )
00178                                         vec1.push_back( *iv2 );                                 
00179                                 ++iv2;
00180                         }
00181         };
00182 }
00183 
00184 
00185 void 
00186 TopolSegmentation::group( TMatrix group_dist,TMatrix mean_vector, TMatrix group_in ) {
00187 
00188         TCluster cluster;
00189         TCluster::iterator ic;
00190         TVecCluster vec_cluster;
00191         TVecCluster::iterator iv;
00192         int yet_included;
00193         int i, j, l;
00194         int m = netParams.getMapcode()->getMapSize();
00195         int d = netParams.getData()->getDimension();
00196         int* dimensions = netParams.getMapcode()->getDimensions();
00197 
00198         cluster.clear();
00199         TVecCluster tmpvec;
00200         TVecCluster ptrvec;
00201         for (j=1; j<=m; j++) {
00202           if (isActive( mean_vector, j, d )) {
00203                 vec_cluster.clear();
00204                 vec_cluster.push_back( j );
00205                 for (l=j; l<=m; l++)
00206                         if ( group_dist[l][j] == 1 )
00207                                 vec_cluster.push_back( l );
00208                 sort( vec_cluster.begin(), vec_cluster.end() ); 
00209                 if (cluster.empty()) 
00210                         cluster.push_back( vec_cluster );               
00211                 else
00212                 {
00213                         TCluster::iterator pointer = cluster.begin();
00214                         yet_included = 0;
00215 
00216                         for (ic=cluster.begin(); ic < cluster.end(); ic++) {
00217                                 tmpvec.clear();
00218                                 tmpvec = *ic;
00219                                 if ( yet_included == 0 ) {
00220                                         if ( element_in( vec_cluster, tmpvec ) ){
00221                                                 vector_copy( tmpvec, vec_cluster );
00222                                                 sort( tmpvec.begin(), tmpvec.end() );
00223                                                 *ic = tmpvec;
00224                                                 yet_included = 1; pointer = ic;
00225                                         }
00226                                 }
00227                                 else {
00228                                         ptrvec = *pointer;
00229                                         if (element_in( ptrvec, tmpvec )) {
00230                                                 vector_copy( tmpvec, ptrvec );
00231                                                 sort( tmpvec.begin(), tmpvec.end() );
00232                                                 *ic = tmpvec;
00233                                                 pointer->clear();
00234                                                 pointer = ic;
00235                                         }
00236                                 }
00237                         }
00238                         if ( yet_included == 0 )
00239                                 cluster.push_back( vec_cluster );
00240                 }
00241           }
00242         }
00243 
00244         int label = 0; int x, y;
00245         TMatrix cluster_label = create_matrix(1,dimensions[1],1,dimensions[0]);
00246         for (i=1; i<= dimensions[1]; i++) for (j=1; j<= dimensions[0]; j++)
00247                 cluster_label[i][j] = 0;
00248         
00249         for (ic=cluster.begin(); ic < cluster.end(); ic++) {
00250                 tmpvec = *ic;           
00251                 //if (!ic->end()) ++label; 
00252                 if (tmpvec.begin() != tmpvec.end()) ++label;
00253                 //cout << "Novo grupo" << endl;
00254                 for (iv=tmpvec.begin(); iv < tmpvec.end(); iv++ ) {
00255                         //cout << endl << *iv << ": ";
00256                         for (i=1; i<=m; i++)
00257                                 if ( group_in[i][*iv] == 1 ) {
00258                                         //cout << " " << i << " ";
00259                                         x = getLearningAlgorithm()->getTopology()->Coord( i, 1, dimensions );
00260                                         y = getLearningAlgorithm()->getTopology()->Coord( i, 2, dimensions );
00261                                         cluster_label[y+1][x+1] = label;
00262                                 }
00263                 }
00264         }
00265 
00266         netParams.getMapcode()->setClusterLabel( cluster_label );
00267 
00268 }
00269 
00270 #endif

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3