void SOMClusteringSpace::getPartitionConfiguration() {
  vector<int> indices;
  //Create equivalent indices
  for ( int i = 0; i < (int)assignation.size();i++) {
    int val = assignation.at(i);
    if ( belongs_to( val, indices ) == -1 ) {
      indices.push_back( val );
    }
  }
  for ( int i = 0; i < (int)indices.size(); i++ ) {
    string group_name("");
    group_name.append(patterns_prop_name).append("_");
    group_name.append(static_cast<ostringstream*>(&(ostringstream()<<i))->str() );
    pGraph->addSubGraph(group_name);
  }
  //Now, define the groups
  //IntegerProperty* group = theGraph->getLocalProperty<IntegerProperty>(patterns_prop_name);
  string group_name("");
  Iterator<node>* node_iter = pGraph->getNodes();
  while ( node_iter->hasNext() ) {
    node n = node_iter->next();
    int id = n.id;
    int val = assignation.at(id);
    int new_index = belongs_to( val, indices );
    group_name.append(patterns_prop_name).append("_").append(static_cast<ostringstream*>(&(ostringstream()<<new_index))->str() );
    pGraph->getSubGraph(group_name)->addNode(n);
  	//group->setNodeValue(n,new_index);
    //group_name = "";
  }
  delete node_iter;
}
    virtual BOOL on_mouse(HELEMENT he, HELEMENT target, UINT event_type, POINT pt, UINT mouseButtons, UINT keyboardStates )
    {
      dom::element el = he;
     
      switch( event_type )
      {
        case MOUSE_DOWN: 
          {
            dom::element popup_el = el.find_first(".popup,popup"); // either class or element <popup>
            if( belongs_to(popup_el,target) )
              return true;

            if( popup_el.is_valid() && el.is_valid() )
            {
              el.set_attribute("popup-shown",L""); // set popup-shown attribute 
                     // to indicate that popup has been shown
                     // this may be used by CSS to indicate UI state
              el.update(true); // render state
              ::HTMLayoutShowPopup(popup_el,el,2); // show it below
              popup_el.set_state(0,STATE_FOCUS);
              el.set_attribute("popup-shown",0);   // remove popup-shown attribute 
            }
            return true;
          }
          break;
      }
      return false;
    }
bool belongs_to( dom::element parent, dom::element child )
{
  if( !child.is_valid()) 
    return false;
  if( parent == child )
    return true;
  return belongs_to( parent, child.parent() );
}
 virtual BOOL on_focus  (HELEMENT he, HELEMENT target, UINT event_type ) 
 { 
   if( (event_type == FOCUS_LOST) || (event_type == (FOCUS_LOST | SINKING)) )
   {
     if( !belongs_to( he, target ) )
       ::HTMLayoutHidePopup(he);
   }
   return TRUE; 
 }
Пример #5
0
void KMeans::train(const float * features, int feature_size, int M, int N) {
	n_center_ = N;
	feature_size_ = feature_size;
	center_.fill( 0.0f, N*feature_size );
	
	// Use K-Means to find the centers [initial seeds]
	for( int i=0; i<N; i++ )
		memcpy( center_.data() + feature_size*i, features + (random() % M)*feature_size, feature_size*sizeof(float) );
	
	// Happily iterate
	QVector<short> belongs_to( M, 0 );
	QVector<int> count( N, 0 );
	for( int it=0; it<MAX_IT; it++ ){
		int change = 0;
		// Assignment step
		initAnn();
		
		QVector<short> old_belongs_to = belongs_to;
		evaluateMany( features, feature_size, M, belongs_to.data() );
		for( int i=0; i<M; i++ ){
			if (belongs_to[i]!=old_belongs_to[i])
				change++;
		}
		if (!change)
			break;
		
		// Update step
		count.fill( 0 );
		center_.fill( 0 );
		for( int i=0; i<M; i++ ){
			for( int j=0; j<feature_size; j++ )
				center_[ belongs_to[i]*feature_size+j ] += features[i*feature_size+j];
			count[ belongs_to[i] ]++;
		}
		
		for( int i=0; i<N; i++ )
			if (count[i] == 0)
				// If the current cluster dies, choose a random feature
				memcpy( center_.data() + feature_size*i, features + (random() % M)*feature_size, feature_size*sizeof(float) );
			else
				// otherwise normalize
				for( int j=0; j<feature_size; j++ )
					center_[ i*feature_size+j ] *= 1.0 / count[i];
	}
}