示例#1
0
bool LocalSearchB::chooseBestNeighbor (Movement* move)
{	
	bool   better_found = false;
	double best_improvement  = 0.0;		

	list<Movement*>* candidates = neighborhood();

	for (list<Movement*>::iterator i=candidates->begin(); i!=candidates->end(); i++)
	{
		Movement* m=(*i);
		
		if (m->_edgeInsert != m->_edgeRemove)
		{
			double improvement = differentialCost(m);
			
			if (improvement < best_improvement) 
			{
				better_found = true;
				if (m->_type == STATIC_MOVE) move->assign(m->_edgeInsert, m->_edgeRemove, m->_type, m->_cycle);
				else move->assign(m->_edgeInsert, m->_edgeRemove, m->_type);
				best_improvement = improvement;
	}	}	}
	
	if (better_found) computeNeighborhoodUpdate(move);
	
	for (list<Movement*>::iterator i=candidates->begin(); i!=candidates->end(); i++)
	{
		if (*i!=move) delete(*i);
	}

	delete(candidates);
	return better_found;
}
示例#2
0
	// ----------------------------------------------------------------------
	void 
		LocalizationDLSModule::
		estimate_position( void ) throw(){

			shawn::Vec* est_pos;
			double distance_r1,distance_r2;
			int count =0;
			distance_r1 = owner().estimate_distance(node(), *linearization_tool_);
			distance_r1 *=distance_r1;
			NeighborInfoList neighbors;
			collect_neighbors( neighborhood(), lat_anchors, neighbors );

			for( std::vector<shawn::Node*>::iterator iter = beacons_->begin(); iter!=beacons_->end();iter++,count++){
				for( ConstNeighborInfoListIterator iter1 = neighbors.begin(); iter1!=neighbors.end(); iter1++){
					if((*iter)->id() == (*iter1)->node().id() ){
						distance_r2 = (*iter1)->distance();
						//distance_r2 = owner().estimate_distance(node(), **iter);

						if(distance_r2 == std::numeric_limits<double>::max() ||distance_r2 == std::numeric_limits<double>::min())
							vector_r_->at(count,0) = 0;
						else    
							vector_r_->at(count,0) = 0.5*( distance_r1 - (distance_r2*distance_r2) + (vector_r_->at(count,0)));
						break;
					}
				}		
			}	
			SimpleMatrix<double>*  temp = new SimpleMatrix<double>(3,1);	
			*temp= *matrix_a_ * *vector_r_;
			est_pos = new shawn::Vec(temp->at(0,0),temp->at(1,0),temp->at(2,0));
			*est_pos += (linearization_tool_->has_est_position())?
				(linearization_tool_->est_position()):(linearization_tool_->real_position());
			node_w().set_est_position( *est_pos );
			delete temp;
	}
   // ----------------------------------------------------------------------
   void
   LocalizationMinMaxModule::
   work( void )
      throw()
   {
      if ( owner().is_anchor() )
         state_ = minmax_finished;

      if ( state_ == minmax_finished )
         return;

      if ( state_ == minmax_wait )
         state_ = minmax_work;

      shawn::Vec est_pos;
      NeighborInfoList neighbors;
      collect_neighbors( neighborhood(), lat_anchors, neighbors );

      if ( est_pos_min_max( neighbors, est_pos ) )
      {
         if ( !observer().check_residue() || check_residue( neighbors, est_pos, lat_anchors, observer().comm_range() ) )
            node_w().set_est_position( est_pos );
      }

      state_ = minmax_finished;
   }
示例#4
0
/********************************************************************
* Calculates the Direct Structure Reachability [DirREACH(v,w)]
* of a given node. Will return all w E N.
********************************************************************/
std::set<Edge> Scan::dirReach(uint v, const double epsilon,
                              const int mi) {
    std::set<Edge> s;
    if (isCore(v, epsilon, mi)) {
        s = neighborhood(v, epsilon);
    }
    return s;
}
 // ----------------------------------------------------------------------
 void
 LocalizationGPSfreeLCSModule::
 broadcast_neighborhood( void )
    throw()
 {
    // send info about own neighborhood
    send( new LocalizationGPSfreeLCSNeighborMessage(
       neighborhood().neighbor_distance_map() ) );
 }
示例#6
0
/********************************************************************
* Verifies if a node is a core
********************************************************************/
bool Scan::isCore(uint node, const double epsilon, const int mi) {
    std::set<Edge> blah;
    blah = neighborhood(node, epsilon);
    if (blah.size() >= mi) {
        return true;
    } else {
        return false;
    }
}
示例#7
0
文件: cf.cpp 项目: AmesianX/mlpack
// Predict the rating for a group of user/item combinations.
void CF::Predict(const arma::Mat<size_t>& combinations,
                 arma::vec& predictions) const
{
  // First, for nearest neighbor search, stretch the H matrix.
  arma::mat l = arma::chol(w.t() * w);
  arma::mat stretchedH = l * h; // Due to the Armadillo API, l is L^T.

  // Now, we must determine those query indices we need to find the nearest
  // neighbors for.  This is easiest if we just sort the combinations matrix.
  arma::Mat<size_t> sortedCombinations(combinations.n_rows,
                                       combinations.n_cols);
  arma::uvec ordering = arma::sort_index(combinations.row(0).t());
  for (size_t i = 0; i < ordering.n_elem; ++i)
    sortedCombinations.col(i) = combinations.col(ordering[i]);

  // Now, we have to get the list of unique users we will be searching for.
  arma::Col<size_t> users = arma::unique(combinations.row(0).t());

  // Assemble our query matrix from the stretchedH matrix.
  arma::mat queries(stretchedH.n_rows, users.n_elem);
  for (size_t i = 0; i < queries.n_cols; ++i)
    queries.col(i) = stretchedH.col(users[i]);

  // Now calculate the neighborhood of these users.
  neighbor::KNN a(stretchedH);
  arma::mat distances;
  arma::Mat<size_t> neighborhood;

  a.Search(queries, numUsersForSimilarity, neighborhood, distances);

  // Now that we have the neighborhoods we need, calculate the predictions.
  predictions.set_size(combinations.n_cols);

  size_t user = 0; // Cumulative user count, because we are doing it in order.
  for (size_t i = 0; i < sortedCombinations.n_cols; ++i)
  {
    // Could this be made faster by calculating dot products for multiple items
    // at once?
    double rating = 0.0;

    // Map the combination's user to the user ID used for kNN.
    while (users[user] < sortedCombinations(0, i))
      ++user;

    for (size_t j = 0; j < neighborhood.n_rows; ++j)
      rating += arma::as_scalar(w.row(sortedCombinations(1, i)) *
          h.col(neighborhood(j, user)));
    rating /= neighborhood.n_rows;

    predictions(ordering[i]) = rating;
  }
}
vector<vector<int>> cluster(vector<Segment> &D, double epsilon, int minlns){
	Segment L;
	vector<int> clus(D.size());
	fill(clus.begin(), clus.end(),-1);
	vector<int> temp_neigh;
	progress::begin();
	int clusterId = 0;
	for (int i = 0; i<D.size(); i++){
		progress::step("clustering: ", i, 1, D.size());
		if (clus[i]==-1){
			temp_neigh = neighborhood(D, i, epsilon);
			if(temp_neigh.size()>= minlns){
				clus[i]=clusterId;
				queue<int> Q;
				for(auto j : temp_neigh){
					clus[j]= clusterId;
					Q.push(j);
				}
				expandCluster(D, clus, Q, clusterId, epsilon, minlns);
				clusterId++;
			}
			else{
				clus[i]=-2;
			}
		}
	}
	progress::done();
	//collect and clean the clusters
	vector<vector<int>> ret(clusterId);
	vector<unordered_set<int>> participating(clusterId);
	for(int i = 0; i<clus.size(); i++){
		if (clus[i]>=0){
			ret[clus[i]].push_back(i);
			D[i].myclus = clus[i];
			participating[clus[i]].insert(D[i].mytraj);
		}
	}
	for(int i = 0; i<participating.size(); i++){
		if (participating[i].size()<=minlns){
			participating.erase(participating.begin() + i);
			ret.erase(ret.begin() + i);
			i--;
		}
	}
	sort(ret.begin(), ret.end(), cs);
	return ret;
}
   // ----------------------------------------------------------------------
   bool
   LocalizationIterLaterationModule::
   process_iter_lateration_sound_message( const LocalizationIterLaterationSoundMessage& lilsm )
      throw()
   {
      if ( owner().is_anchor() || state_ == il_finished )
         return true;

      neighborhood_w().add_sound( lilsm.source() );

      if ( !sound_ )
      {
         if ( (sound_ = neighborhood().is_sound()) )
            send( new LocalizationIterLaterationSoundMessage() );
      }

      return true;
   }
void expandCluster(vector<Segment> &D, vector<int> &clus, queue<int> &Q, int clusterId, double epsilon, int minlns){
	int M;
	while(!Q.empty()){
		M = Q.front();
		vector<int> neigh_M = neighborhood(D, M, epsilon);
		if (neigh_M.size()>=minlns){
			for (auto X : neigh_M){
				if (clus[X] == -1 || clus[X]==-2){
					clus[X] = clusterId;
				}
				if (clus[X]==-1){
					Q.push(X);
				}
			}
		}
		Q.pop();
	}
}
bool CRelations::CheckForHostileAI(idVec3 point, int team) // grayman #3548
{
	// Determine if any AI are armed and hostile in
	// point's neighborhood.

	bool hostile = false;

	idClipModel *clipModels[ MAX_GENTITIES ];
	idBounds neighborhood(idVec3(point.x-256,point.y-256,point.z),
						  idVec3(point.x+256,point.y+256,point.z+128));

	int num = gameLocal.clip.ClipModelsTouchingBounds( neighborhood, MASK_MONSTERSOLID, clipModels, MAX_GENTITIES );
	for ( int i = 0 ; i < num ; i++ )
	{
		idClipModel *cm = clipModels[i];

		// don't check render entities
		if ( cm->IsRenderModel() )
		{
			continue;
		}

		idEntity *ent = cm->GetEntity();

		// is this an AI?
		if (ent && ent->IsType(idAI::Type))
		{
			// is the AI armed?
			idAI* ai = static_cast<idAI*>(ent);
			if ( ( ai->GetNumMeleeWeapons() > 0 ) || ( ai->GetNumRangedWeapons() > 0 ) )
			{
				// is the AI hostile to the given team?
				if (IsEnemy(ai->team, team))
				{
					hostile = true;
					break;
				}
			}
		}
	}

	return hostile; // return true = hostiles found; return false = no hostiles found
}
   // ----------------------------------------------------------------------
   void
   LocalizationIterLaterationModule::
   work( void )
      throw()
   {
      if ( state_ == il_finished )
         return;

      // do initial stuff;
      //    if anchor, send 'init-message' and set state to 'finished';
      //    if unknown, check whether the node is sound or not. if sound,
      //       send messages, if not, clear estimated position.
      if ( state_ == il_init )
      {
         if ( owner().is_anchor() )
         {
            send( new LocalizationIterLaterationMessage( observer().confidence() ) );
            state_ = il_finished;
         }
         else
         {
            sound_ = neighborhood().is_sound();

            if ( sound_ && node().has_est_position() )
            {
               send( new LocalizationIterLaterationMessage( observer().confidence() ) );
               send( new LocalizationIterLaterationSoundMessage() );
            }
            else
            {
               set_confidence( 0 );
               node_w().clear_est_position();
            }

            state_ = il_work;
         }
      }

      if ( state_ == il_work )
         iter_lateration_step();
   }
示例#13
0
文件: cf.cpp 项目: AmesianX/mlpack
// Predict the rating for a single user/item combination.
double CF::Predict(const size_t user, const size_t item) const
{
  // First, we need to find the nearest neighbors of the given user.
  // We'll use the same technique as for GetRecommendations().

  // We want to avoid calculating the full rating matrix, so we will do nearest
  // neighbor search only on the H matrix, using the observation that if the
  // rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i), W
  // H.col(j)).  This can be seen as nearest neighbor search on the H matrix
  // with the Mahalanobis distance where M^{-1} = W^T W.  So, we'll decompose
  // M^{-1} = L L^T (the Cholesky decomposition), and then multiply H by L^T.
  // Then we can perform nearest neighbor search.
  arma::mat l = arma::chol(w.t() * w);
  arma::mat stretchedH = l * h; // Due to the Armadillo API, l is L^T.

  // Now, we will use the decomposed w and h matrices to estimate what the user
  // would have rated items as, and then pick the best items.

  // Temporarily store feature vector of queried users.
  arma::mat query = stretchedH.col(user);

  // Temporary storage for neighborhood of the queried users.
  arma::Mat<size_t> neighborhood;

  // Calculate the neighborhood of the queried users.
  // This should be a templatized option.
  neighbor::KNN a(stretchedH, false, true /* single-tree mode */);
  arma::mat resultingDistances; // Temporary storage.

  a.Search(query, numUsersForSimilarity, neighborhood, resultingDistances);

  double rating = 0; // We'll take the average of neighborhood values.

  for (size_t j = 0; j < neighborhood.n_rows; ++j)
    rating += arma::as_scalar(w.row(item) * h.col(neighborhood(j, 0)));
  rating /= neighborhood.n_rows;

  return rating;
}
   // ----------------------------------------------------------------------
   void
   LocalizationGPSfreeLCSModule::
   build_lcs( void )
      throw()
   {
      for ( ConstNeighborhoodIterator
               it1 = neighborhood().begin_neighborhood();
               it1 != neighborhood().end_neighborhood();
               ++it1 )
         for ( ConstNeighborhoodIterator
                  it2 = it1;
                  it2 != neighborhood().end_neighborhood();
                  ++it2 )
         {
            if ( it1 == it2 )
               continue;

            LocalizationLocalCoordinateSystem lcs;
            lcs.update_basic_nodes( *it1->first, *it2->first, neighborhood() );

            for ( ConstNeighborhoodIterator
                     it3 = neighborhood().begin_neighborhood();
                     it3 != neighborhood().end_neighborhood();
                     ++it3 )
            {
               if ( it3 == it1 || it3 == it2 )
                  continue;

               lcs.update_node( *it3->first );
            }

            if (  lcs.is_valid() &&
                  ( !local_coord_sys().is_valid() ||
                      local_coord_sys().size() < lcs.size() ) )
               local_coord_sys_w() = lcs;
         }

      local_coord_sys_w().set_src_node( node() );
   }
   // ----------------------------------------------------------------------
   void
   LocalizationIterLaterationModule::
   iter_lateration_step( void )
      throw()
   { 
      if ( state_ == il_finished || !sound_ )
         return;

      if ( iteration_cnt_ >= iteration_limit_ )
         state_ = il_finished;
      ++iteration_cnt_;

      neighborhood_w().reassign_twins( twin_measure_ * observer().comm_range() );

	  if ( neighborhood().confident_neighbor_cnt() < min_confident_nbrs_ )
         return;

      Vec est_pos;
      NeighborInfoList neighbors;
      collect_neighbors( neighborhood(), lat_confident, neighbors );

	  // try to update position. if lateration fails, confidence is set to 0,
      // else position is updated and confidence is set to average of all
      // neighbor confidences
      if ( est_pos_lateration( neighbors, est_pos, lat_confident, false ) &&
            est_pos_lateration( neighbors, est_pos, lat_confident, true ) )
      {
         set_confidence( neighborhood().avg_neighbor_confidence() );

         // check validity of new position. if check fails, there is a chance
         // of 'res_acceptance_', which is normally set to 0.1, to accept
         // the position anyway. this is done to avoid being trapped in a
         // local minimum. moreover, if the bad position is accepted, the
         // confidence is reduced by 50%.
         if ( !check_residue( neighbors, est_pos, lat_confident, observer().comm_range() ) )
         {
            if ( res_acceptance_ > uniform_random_0i_1i() )
               set_confidence( observer().confidence() / 2 );
            else
            {
               if ( observer().confidence() == 0 ) return;
               set_confidence( 0 );
               node_w().clear_est_position();
               send( new LocalizationIterLaterationMessage( observer().confidence() ) );
               return;
            }
         }

         // check, whether the new position is very close to the old one or
         // not. if so, refinement is finished.
         if ( node().has_est_position() )
         {
            double pos_diff = euclidean_distance( est_pos, node().est_position() );

            if ( pos_diff < abort_pos_update_ * observer().comm_range() )
               state_ = il_finished;
         }

         node_w().set_est_position( est_pos );
	  }
      else
      {
	     if ( observer().confidence() == 0 ) return;
         set_confidence( 0 );
         node_w().clear_est_position();
      }

      send( new LocalizationIterLaterationMessage( observer().confidence() ) );
   }
int main()
{
	// image matching

	// 1. Read input images
	cv::Mat image1= cv::imread("church01.jpg",CV_LOAD_IMAGE_GRAYSCALE);
	cv::Mat image2= cv::imread("church02.jpg",CV_LOAD_IMAGE_GRAYSCALE);

	// 2. Define keypoints vector
	std::vector<cv::KeyPoint> keypoints1;
	std::vector<cv::KeyPoint> keypoints2;

	// 3. Define feature detector
	cv::FastFeatureDetector fastDet(80);

	// 4. Keypoint detection
	fastDet.detect(image1,keypoints1);
	fastDet.detect(image2,keypoints2);

	std::cout << "Number of keypoints (image 1): " << keypoints1.size() << std::endl; 
	std::cout << "Number of keypoints (image 2): " << keypoints2.size() << std::endl; 

	// 5. Define a square neighborhood
	const int nsize(11); // size of the neighborhood
	cv::Rect neighborhood(0, 0, nsize, nsize); // 11x11
	cv::Mat patch1;
	cv::Mat patch2;

	// 6. For all keypoints in first image
	//    find best match in second image
	cv::Mat result;
	std::vector<cv::DMatch> matches;

	//for all keypoints in image 1
	for (int i=0; i<keypoints1.size(); i++) {
	
		// define image patch
		neighborhood.x = keypoints1[i].pt.x-nsize/2;
		neighborhood.y = keypoints1[i].pt.y-nsize/2;

		// if neighborhood of points outside image, then continue with next point
		if (neighborhood.x<0 || neighborhood.y<0 ||
			neighborhood.x+nsize >= image1.cols || neighborhood.y+nsize >= image1.rows)
			continue;

		//patch in image 1
		patch1 = image1(neighborhood);

		// reset best correlation value;
		cv::DMatch bestMatch;

		//for all keypoints in image 2
	    for (int j=0; j<keypoints2.size(); j++) {

			// define image patch
			neighborhood.x = keypoints2[j].pt.x-nsize/2;
			neighborhood.y = keypoints2[j].pt.y-nsize/2;

			// if neighborhood of points outside image, then continue with next point
			if (neighborhood.x<0 || neighborhood.y<0 ||
				neighborhood.x + nsize >= image2.cols || neighborhood.y + nsize >= image2.rows)
				continue;

			// patch in image 2
			patch2 = image2(neighborhood);

			// match the two patches
			cv::matchTemplate(patch1,patch2,result,CV_TM_SQDIFF_NORMED);

			// check if it is a best match
			if (result.at<float>(0,0) < bestMatch.distance) {

				bestMatch.distance= result.at<float>(0,0);
				bestMatch.queryIdx= i;
				bestMatch.trainIdx= j;
			}
		}

		// add the best match
		matches.push_back(bestMatch);
	}

	std::cout << "Number of matches: " << matches.size() << std::endl; 

	// extract the 50 best matches
	std::nth_element(matches.begin(),matches.begin()+50,matches.end());
	matches.erase(matches.begin()+50,matches.end());

	std::cout << "Number of matches (after): " << matches.size() << std::endl; 

	// Draw the matching results
	cv::Mat matchImage;
	cv::drawMatches(image1,keypoints1, // first image
                    image2,keypoints2, // second image
                    matches,     // vector of matches
                    matchImage,  // produced image
	                cv::Scalar(255,255,255),  // line color
		  		    cv::Scalar(255,255,255)); // point color

    // Display the image of matches
	cv::namedWindow("Matches");
	cv::imshow("Matches",matchImage);

	// Match template

	// define a template
	cv::Mat target(image1,cv::Rect(80,105,30,30));
    // Display the template
	cv::namedWindow("Template");
	cv::imshow("Template",target);

	// define search region
	cv::Mat roi(image2, 
		// here top half of the image
		cv::Rect(0,0,image2.cols,image2.rows/2)); 
			
	// perform template matching
	cv::matchTemplate(
		roi,    // search region
		target, // template
		result, // result
		CV_TM_SQDIFF); // similarity measure

	// find most similar location
	double minVal, maxVal;
	cv::Point minPt, maxPt;
	cv::minMaxLoc(result, &minVal, &maxVal, &minPt, &maxPt);

	// draw rectangle at most similar location
	// at minPt in this case
	cv::rectangle(roi, cv::Rect(minPt.x, minPt.y, target.cols , target.rows), 255);
	
    // Display the template
	cv::namedWindow("Best");
	cv::imshow("Best",image2);

	cv::waitKey();
	return 0;
}
示例#17
0
void CF<FactorizerType>::GetRecommendations(const size_t numRecs,
                                            arma::Mat<size_t>& recommendations,
                                            arma::Col<size_t>& users)
{
  // Generate new table by multiplying approximate values.
  rating = w * h;

  // Now, we will use the decomposed w and h matrices to estimate what the user
  // would have rated items as, and then pick the best items.

  // Temporarily store feature vector of queried users.
  arma::mat query(rating.n_rows, users.n_elem);

  // Select feature vectors of queried users.
  for (size_t i = 0; i < users.n_elem; i++)
    query.col(i) = rating.col(users(i));

  // Temporary storage for neighborhood of the queried users.
  arma::Mat<size_t> neighborhood;

  // Calculate the neighborhood of the queried users.
  // This should be a templatized option.
  neighbor::AllkNN a(rating);
  arma::mat resultingDistances; // Temporary storage.
  a.Search(query, numUsersForSimilarity, neighborhood, resultingDistances);

  // Temporary storage for storing the average rating for each user in their
  // neighborhood.
  arma::mat averages = arma::zeros<arma::mat>(rating.n_rows, query.n_cols);

  // Iterate over each query user.
  for (size_t i = 0; i < neighborhood.n_cols; ++i)
  {
    // Iterate over each neighbor of the query user.
    for (size_t j = 0; j < neighborhood.n_rows; ++j)
      averages.col(i) += rating.col(neighborhood(j, i));
    // Normalize average.
    averages.col(i) /= neighborhood.n_rows;
  }

  // Generate recommendations for each query user by finding the maximum numRecs
  // elements in the averages matrix.
  recommendations.set_size(numRecs, users.n_elem);
  recommendations.fill(cleanedData.n_rows); // Invalid item number.
  arma::mat values(numRecs, users.n_elem);
  values.fill(-DBL_MAX); // The smallest possible value.
  for (size_t i = 0; i < users.n_elem; i++)
  {
    // Look through the averages column corresponding to the current user.
    for (size_t j = 0; j < averages.n_rows; ++j)
    {
      // Ensure that the user hasn't already rated the item.
      if (cleanedData(j, users(i)) != 0.0)
        continue; // The user already rated the item.

      // Is the estimated value better than the worst candidate?
      const double value = averages(j, i);
      if (value > values(values.n_rows - 1, i))
      {
        // It should be inserted.  Which position?
        size_t insertPosition = values.n_rows - 1;
        while (insertPosition > 0)
        {
          if (value <= values(insertPosition - 1, i))
            break; // The current value is the right one.
          insertPosition--;
        }

        // Now insert it into the list.
        InsertNeighbor(i, insertPosition, j, value, recommendations,
            values);
      }
    }

    // If we were not able to come up with enough recommendations, issue a
    // warning.
    if (recommendations(values.n_rows - 1, i) == cleanedData.n_rows + 1)
      Log::Warn << "Could not provide " << values.n_rows << " recommendations "
          << "for user " << users(i) << " (not enough un-rated items)!"
          << std::endl;
  }
}
示例#18
0
文件: cf.cpp 项目: AmesianX/mlpack
void CF::GetRecommendations(const size_t numRecs,
                            arma::Mat<size_t>& recommendations,
                            arma::Col<size_t>& users)
{
  // We want to avoid calculating the full rating matrix, so we will do nearest
  // neighbor search only on the H matrix, using the observation that if the
  // rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i), W
  // H.col(j)).  This can be seen as nearest neighbor search on the H matrix
  // with the Mahalanobis distance where M^{-1} = W^T W.  So, we'll decompose
  // M^{-1} = L L^T (the Cholesky decomposition), and then multiply H by L^T.
  // Then we can perform nearest neighbor search.
  arma::mat l = arma::chol(w.t() * w);
  arma::mat stretchedH = l * h; // Due to the Armadillo API, l is L^T.

  // Now, we will use the decomposed w and h matrices to estimate what the user
  // would have rated items as, and then pick the best items.

  // Temporarily store feature vector of queried users.
  arma::mat query(stretchedH.n_rows, users.n_elem);

  // Select feature vectors of queried users.
  for (size_t i = 0; i < users.n_elem; i++)
    query.col(i) = stretchedH.col(users(i));

  // Temporary storage for neighborhood of the queried users.
  arma::Mat<size_t> neighborhood;

  // Calculate the neighborhood of the queried users.
  // This should be a templatized option.
  neighbor::KNN a(stretchedH);
  arma::mat resultingDistances; // Temporary storage.
  a.Search(query, numUsersForSimilarity, neighborhood, resultingDistances);

  // Generate recommendations for each query user by finding the maximum numRecs
  // elements in the averages matrix.
  recommendations.set_size(numRecs, users.n_elem);
  recommendations.fill(cleanedData.n_rows); // Invalid item number.
  arma::mat values(numRecs, users.n_elem);
  values.fill(-DBL_MAX); // The smallest possible value.
  for (size_t i = 0; i < users.n_elem; i++)
  {
    // First, calculate average of neighborhood values.
    arma::vec averages;
    averages.zeros(cleanedData.n_rows);

    for (size_t j = 0; j < neighborhood.n_rows; ++j)
      averages += w * h.col(neighborhood(j, i));
    averages /= neighborhood.n_rows;

    // Look through the averages column corresponding to the current user.
    for (size_t j = 0; j < averages.n_rows; ++j)
    {
      // Ensure that the user hasn't already rated the item.
      if (cleanedData(j, users(i)) != 0.0)
        continue; // The user already rated the item.

      // Is the estimated value better than the worst candidate?
      const double value = averages[j];
      if (value > values(values.n_rows - 1, i))
      {
        // It should be inserted.  Which position?
        size_t insertPosition = values.n_rows - 1;
        while (insertPosition > 0)
        {
          if (value <= values(insertPosition - 1, i))
            break; // The current value is the right one.
          insertPosition--;
        }

        // Now insert it into the list.
        InsertNeighbor(i, insertPosition, j, value, recommendations,
            values);
      }
    }

    // If we were not able to come up with enough recommendations, issue a
    // warning.
    if (recommendations(values.n_rows - 1, i) == cleanedData.n_rows + 1)
      Log::Warn << "Could not provide " << values.n_rows << " recommendations "
          << "for user " << users(i) << " (not enough un-rated items)!"
          << std::endl;
  }
}
示例#19
0
htd::ConstCollection<htd::vertex_t> htd::MinFillOrderingAlgorithm::computeOrdering(const htd::IHypergraph & graph) const
{
    htd::VectorAdapter<htd::vertex_t> ret;

    std::vector<htd::vertex_t> & ordering = ret.container();

    std::size_t size = graph.vertexCount();

    std::size_t tmp = 0;

    std::size_t minFill = (std::size_t)-1;
    
    std::size_t minDegree = (std::size_t)-1;
        
    std::size_t currentDegree = (std::size_t)-1;
        
    std::vector<htd::vertex_t> minDegreePool;

    std::unordered_set<htd::vertex_t> pool(size);

    std::vector<htd::vertex_t> vertices;
    vertices.reserve(size);

    std::copy(graph.vertices().begin(), graph.vertices().end(), std::back_inserter(vertices));

    std::vector<char> updateStatus(size, 0);
    
    std::vector<std::size_t> requiredFillAmount(size, (std::size_t)-1);
    
    std::vector<htd::vertex_container> neighborhood(size, htd::vertex_container());

    std::vector<std::vector<htd::vertex_t>> existingNeighbors(size, htd::vertex_container());
    std::vector<std::vector<htd::vertex_t>> additionalNeighbors(size, htd::vertex_container());
    std::vector<std::vector<htd::vertex_t>> unaffectedNeighbors(size, htd::vertex_container());
    
    htd::vertex_container newNeighborhood;
    
    htd::vertex_container affectedVertices;
    affectedVertices.reserve(size);

    for (htd::vertex_t vertex : graph.vertices())
    {
        auto & currentNeighborhood = neighborhood[vertex - htd::Vertex::FIRST];

        const htd::ConstCollection<htd::vertex_t> & neighborCollection = graph.neighbors(vertex);

        currentNeighborhood.reserve(neighborCollection.size());

        std::copy(neighborCollection.begin(), neighborCollection.end(), std::back_inserter(currentNeighborhood));

        auto position = std::lower_bound(currentNeighborhood.begin(), currentNeighborhood.end(), vertex);
        
        if (position == currentNeighborhood.end() || *position != vertex)
        {
            currentNeighborhood.insert(position, vertex);
        }
    }

    for (htd::vertex_t vertex : vertices)
    {
        auto & currentNeighborhood = neighborhood[vertex - htd::Vertex::FIRST];
        
        tmp = ((currentNeighborhood.size() * (currentNeighborhood.size() - 1)) / 2) - computeEdgeCount(neighborhood, currentNeighborhood);
        
        if (tmp <= minFill)
        {
            if (tmp < minFill)
            {
                minFill = tmp;

                pool.clear();
            }
            
            pool.insert(vertex);
        }
        
        requiredFillAmount[vertex - htd::Vertex::FIRST] = tmp;
        
        DEBUGGING_CODE_LEVEL2(
        std::cout << "Vertex " << vertex << ":" << std::endl;
        htd::print(currentNeighborhood, false);
        std::cout << std::endl;
        std::size_t neighborhoodSize = currentNeighborhood.size();
        std::cout << "   INITIAL EDGE COUNT " << vertex << ": " << computeEdgeCount(neighborhood, neighborhood[vertex]) << std::endl;
        std::cout << "   MAXIMUM EDGE COUNT " << vertex << ": " << (neighborhoodSize * (neighborhoodSize - 1)) / 2 << std::endl;
        std::cout << "   INITIAL FILL VALUE " << vertex << ": " << requiredFillAmount[vertex] << std::endl;
        )
    }
    
    while (size > 0)
    {
        if (pool.size() == 0)
        {
            minFill = (std::size_t)-1;
    
            for (htd::vertex_t vertex : vertices)
            {
                tmp = requiredFillAmount[vertex - htd::Vertex::FIRST];

                if (tmp <= minFill)
                {
                    if (tmp < minFill)
                    {
                        minFill = tmp;

                        pool.clear();
                    }

                    pool.insert(vertex);
                } 
            }
        }
        
        DEBUGGING_CODE_LEVEL2(
        std::cout << "POOL (FILL=" << min << "): ";
        htd::print(pool, false);
        std::cout << std::endl;
        )
        
        minDegree = (std::size_t)-1;
        
        for (htd::vertex_t vertex : pool)
        {
            currentDegree = neighborhood[vertex - htd::Vertex::FIRST].size() - 1;
            
            if (currentDegree <= minDegree)
            {
                if (currentDegree < minDegree)
                {
                    minDegreePool.clear();

                    minDegree = currentDegree;
                }
                
                minDegreePool.push_back(vertex);
            }
        }
        
        htd::vertex_t selectedVertex = minDegreePool[rand() % minDegreePool.size()];
        auto & selectedNeighborhood = neighborhood[selectedVertex - htd::Vertex::FIRST];
        
        pool.erase(pool.find(selectedVertex));
        
        updateStatus[selectedVertex - htd::Vertex::FIRST] = 4;
        
        affectedVertices.clear();
        
        if (requiredFillAmount[selectedVertex - htd::Vertex::FIRST] == 0)
        {
            for (auto vertex : selectedNeighborhood)
            {
                if (vertex != selectedVertex)
                {
                    auto & currentNeighborhood = neighborhood[vertex - htd::Vertex::FIRST];
                    
                    currentNeighborhood.erase(std::lower_bound(currentNeighborhood.begin(), currentNeighborhood.end(), selectedVertex));
                }
            }
        }
        else
        {
            for (auto neighbor : selectedNeighborhood)
            {
                if (neighbor != selectedVertex)
                {
                    if (updateStatus[neighbor - htd::Vertex::FIRST] == 0)
                    {
                        decompose_sets(selectedNeighborhood, neighborhood[neighbor - htd::Vertex::FIRST], selectedVertex,
                                       additionalNeighbors[neighbor - htd::Vertex::FIRST],
                                       unaffectedNeighbors[neighbor - htd::Vertex::FIRST],
                                       existingNeighbors[neighbor - htd::Vertex::FIRST]);
                    }

                    updateStatus[neighbor - htd::Vertex::FIRST] |= 1;

                    for (auto affectedVertex : neighborhood[neighbor - htd::Vertex::FIRST])
                    {
                        //TODO Change into status_t
                        char currentUpdateStatus = updateStatus[affectedVertex - htd::Vertex::FIRST];

                        if (currentUpdateStatus < 2)
                        {
                            if (currentUpdateStatus == 0)
                            {
                                decompose_sets(selectedNeighborhood, neighborhood[affectedVertex - htd::Vertex::FIRST], selectedVertex,
                                               additionalNeighbors[affectedVertex - htd::Vertex::FIRST],
                                               unaffectedNeighbors[affectedVertex - htd::Vertex::FIRST],
                                               existingNeighbors[affectedVertex - htd::Vertex::FIRST]);
                            }

                            affectedVertices.push_back(affectedVertex);
                            
                            updateStatus[affectedVertex - htd::Vertex::FIRST] |= 2;
                        }
                    }
                }
            }

            for (auto vertex : selectedNeighborhood)
            {
                if (vertex != selectedVertex)
                {
                    auto & currentNeighborhood = neighborhood[vertex - htd::Vertex::FIRST];
                    auto & currentUnaffectedNeighborhood = unaffectedNeighbors[vertex - htd::Vertex::FIRST];
                    auto & currentAdditionalNeighborhood = additionalNeighbors[vertex - htd::Vertex::FIRST];

                    std::size_t additionalNeighborCount = currentAdditionalNeighborhood.size();

                    if (additionalNeighborCount > 0)
                    {
                        if (additionalNeighborCount == 1)
                        {
                            auto first = currentNeighborhood.begin();
                            auto last = currentNeighborhood.end();
                            
                            htd::vertex_t newVertex = currentAdditionalNeighborhood[0];

                            if (newVertex < selectedVertex)
                            {
                                if (selectedVertex - newVertex == 1)
                                {
                                    *std::lower_bound(first, last, selectedVertex) = newVertex;
                                }
                                else
                                {
                                    htd::index_t position1 = std::distance(first, std::lower_bound(first, last, newVertex));
                                    htd::index_t position2 = std::distance(first, std::lower_bound(first + position1, last, selectedVertex));
                                    
                                    currentNeighborhood.erase(first + position2);
                                    currentNeighborhood.insert(currentNeighborhood.begin() + position1, newVertex);
                                }
                            }
                            else
                            {
                                if (newVertex - selectedVertex == 1)
                                {
                                    *std::lower_bound(first, last, selectedVertex) = newVertex;
                                }
                                else
                                {
                                    htd::index_t position1 = std::distance(first, std::lower_bound(first, last, selectedVertex));
                                    htd::index_t position2 = std::distance(first, std::lower_bound(first + position1, last, newVertex));
                                    
                                    currentNeighborhood.erase(first + position1);
                                    currentNeighborhood.insert(currentNeighborhood.begin() + position2 - 1, newVertex);
                                }
                            }
                        }
                        else
                        {
                            set_union(currentNeighborhood, currentAdditionalNeighborhood, selectedVertex, newNeighborhood);
                            
                            std::swap(currentNeighborhood, newNeighborhood);

                            newNeighborhood.clear();
                        }
                    }
                    else
                    {
                        currentNeighborhood.erase(std::lower_bound(currentNeighborhood.begin(), currentNeighborhood.end(), selectedVertex));
                    }

                    tmp = requiredFillAmount[vertex - htd::Vertex::FIRST];

                    if (additionalNeighborCount > 0 || tmp > 0)
                    {
                        std::size_t unaffectedNeighborCount = currentUnaffectedNeighborhood.size();

                        if (unaffectedNeighborCount > 0)
                        {
                            if (additionalNeighborCount == 0)
                            {
                                auto & relevantNeighborhood = existingNeighbors[vertex - htd::Vertex::FIRST];

                                auto last = relevantNeighborhood.end();

                                std::size_t count = relevantNeighborhood.size();
                                htd::index_t index = 0;
    
                                for (auto it = relevantNeighborhood.begin(); index < count && tmp > unaffectedNeighborCount; index++)
                                {
                                    htd::vertex_t vertex2 = *it;

                                    auto & currentAdditionalNeighborhood2 = additionalNeighbors[vertex2 - htd::Vertex::FIRST];

                                    it++;

                                    tmp -= htd::compute_set_intersection_size(it, last, std::upper_bound(currentAdditionalNeighborhood2.begin(), currentAdditionalNeighborhood2.end(), vertex2), currentAdditionalNeighborhood2.end());
                                }

                                tmp -= unaffectedNeighborCount;

                                //TODO
                                updateStatus[vertex - htd::Vertex::FIRST] = 0;

                                if (tmp <= minFill)
                                {
                                    if (tmp < minFill)
                                    {
                                        minFill = tmp;

                                        pool.clear();
                                    }

                                    pool.insert(vertex);
                                }
                            }
                            else
                            {
                                auto first = currentAdditionalNeighborhood.begin();
                                auto last = currentAdditionalNeighborhood.end();

                                for (htd::vertex_t unaffectedVertex : currentUnaffectedNeighborhood)
                                {
                                    auto & affectedVertices = existingNeighbors[unaffectedVertex - htd::Vertex::FIRST];

                                    tmp += htd::compute_set_difference_size(first, last, affectedVertices.begin(), affectedVertices.end());

                                    tmp--;
                                }

                                updateStatus[vertex - htd::Vertex::FIRST] &= ~1;

                                if (updateStatus[vertex - htd::Vertex::FIRST] == 0)
                                {
                                    if (tmp <= minFill)
                                    {
                                        if (tmp < minFill)
                                        {
                                            minFill = tmp;

                                            pool.clear();
                                        }

                                        pool.insert(vertex);
                                    }
                                }
                            }
                        }
                        else
                        {
                            tmp = 0;

                            //TODO
                            updateStatus[vertex - htd::Vertex::FIRST] = 0;

                            if (tmp < minFill)
                            {
                                minFill = tmp;

                                pool.clear();
                            }

                            pool.insert(vertex);
                        }

                        requiredFillAmount[vertex - htd::Vertex::FIRST] = tmp;
                    }
                    else
                    {
                        updateStatus[vertex - htd::Vertex::FIRST] = 0;
                    }
                }
            }

    #ifdef TESTOUTPUT
            std::cout << "SELECTED VERTEX: " << selectedVertex << std::endl;
            std::cout << "   DIRECT NEIGHBORS:  ";
            htd::print(selectedNeighborhood, false);
            std::cout << std::endl;
            for (auto vertex : selectedNeighborhood)
            {
                if (vertex != selectedVertex)
                {
                    std::cout << "      NEIGHBORHOOD " << vertex << ": ";
                    htd::print(neighborhood[vertex], false);
                    std::cout << std::endl;
                    std::cout << "         EXISTING:   ";
                    htd::print(existingNeighbors[vertex], false);
                    std::cout << std::endl;
                    std::cout << "         ADDITIONAL: ";
                    htd::print(additionalNeighbors[vertex], false);
                    std::cout << std::endl;
                    std::cout << "         UNAFFECTED: ";
                    htd::print(unaffectedNeighbors[vertex], false);
                    std::cout << std::endl;
                }
            }
            std::cout << "   ----- ----- -----" << std::endl;
            std::cout << "   AFFECTED VERTICES: ";
            htd::print(affectedVertices, false);
            std::cout << std::endl;
            for (auto vertex : affectedVertices)
            {
                std::cout << "      NEIGHBORHOOD " << vertex << ": ";
                htd::print(neighborhood[vertex], false);
                std::cout << std::endl;
                std::cout << "         EXISTING:   ";
                htd::print(existingNeighbors[vertex], false);
                std::cout << std::endl;
                std::cout << "         UNAFFECTED: ";
                htd::print(unaffectedNeighbors[vertex], false);
                std::cout << std::endl;
            }
            std::cout << std::endl;

            std::cout << "STATUS: ";
            std::vector<int> statusBefore;
            std::copy(updateStatus.begin(), updateStatus.end(), std::back_inserter(statusBefore));
            htd::print(statusBefore, false);
            std::cout << std::endl;
    #endif

            for (auto vertex : affectedVertices)
            {
                if (updateStatus[vertex - htd::Vertex::FIRST] == 2)
                {
                    tmp = requiredFillAmount[vertex - htd::Vertex::FIRST];

                    if (unaffectedNeighbors[vertex - htd::Vertex::FIRST].size() > 0 && tmp > 0)
                    {
                        auto & relevantNeighborhood = existingNeighbors[vertex - htd::Vertex::FIRST];

                        auto last = relevantNeighborhood.end();

                        std::size_t count = relevantNeighborhood.size();
                        htd::index_t index = 0;
                        
                        for (auto it = relevantNeighborhood.begin(); index < count && tmp > 0;)
                        {
                            htd::vertex_t vertex2 = *it;

                            auto & currentAdditionalNeighborhood2 = additionalNeighbors[vertex2 - htd::Vertex::FIRST];

                            it++;
                            index++;

                            tmp -= htd::compute_set_intersection_size(it, last, std::upper_bound(currentAdditionalNeighborhood2.begin(), currentAdditionalNeighborhood2.end(), vertex2), currentAdditionalNeighborhood2.end());
                        }

                        if (tmp <= minFill)
                        {
                            if (tmp < minFill)
                            {
                                minFill = tmp;

                                pool.clear();
                            }

                            pool.insert(vertex);
                        }
                    }
                    else
                    {    
                        tmp = 0;

                        if (tmp < minFill)
                        {
                            minFill = tmp;

                            pool.clear();
                        }

                        pool.insert(vertex);
                    }

                    requiredFillAmount[vertex - htd::Vertex::FIRST] = tmp;
                }
            }

            for (auto vertex : selectedNeighborhood)
            {
                additionalNeighbors[vertex - htd::Vertex::FIRST].clear();
                unaffectedNeighbors[vertex - htd::Vertex::FIRST].clear();
                existingNeighbors[vertex - htd::Vertex::FIRST].clear();
            }

            for (auto vertex : affectedVertices)
            {
                if (updateStatus[vertex - htd::Vertex::FIRST] == 2)
                {
                    additionalNeighbors[vertex - htd::Vertex::FIRST].clear();
                    unaffectedNeighbors[vertex - htd::Vertex::FIRST].clear();
                    existingNeighbors[vertex - htd::Vertex::FIRST].clear();

                    updateStatus[vertex - htd::Vertex::FIRST] = 0;
                }
            }
        }
        
        selectedNeighborhood.clear();
        
        vertices.erase(std::lower_bound(vertices.begin(), vertices.end(), selectedVertex));
        
        size--;

        ordering.push_back(selectedVertex);
        
#ifdef TESTOUTPUT
        std::cout << "STATUS AFTER: ";
        std::vector<int> statusAfter;
        std::copy(updateStatus.begin(), updateStatus.end(), std::back_inserter(statusAfter));
        htd::print(statusAfter, false);
        std::cout << "   AVAILABLE: " << std::count(updateStatus.begin(), updateStatus.end(), 0) << std::endl;
#endif
        
//#define VERIFY
#ifdef VERIFY
        long size = 0;
        
        //TODO
        //std::cout << "CHECK (ELIMINATED=" << selectedVertex << "): " << std::endl;
        
        for (htd::vertex_t vertex : vertices)
        {
            if (updateStatus[vertex] == 0)
            {
                auto & currentNeighborhood = neighborhood[vertex];

                size = currentNeighborhood.size();
                
                long actual = requiredFillAmount[vertex];
                
                long maximumEdges = (size * (size - 1)) / 2;
                long existingEdges = computeEdgeCount(neighborhood, currentNeighborhood);
                
                long expected = maximumEdges - existingEdges;
                
                if (actual != expected)
                {
                    std::cout << "ERROR!!! Vertex " << vertex << " (Expected: " << expected << ", Actual: " << actual << ")" << std::endl;

                    std::cout << "VERTEX " << vertex << ":" << std::endl;
                    std::cout << "   NEIGHBORHOOD:   ";
                    htd::print(currentNeighborhood, false);
                    std::cout << std::endl;
                    for (auto vertex2 : currentNeighborhood)
                    {
                        if (vertex2 != vertex)
                        {
                            std::cout << "   NEIGHBORHOOD " << vertex2 << ": ";
                            htd::print(neighborhood[vertex2], false);
                            std::cout << std::endl;
                        }
                    }
                    std::cout << "EDGES " << vertex << ": " << existingEdges << "/" << maximumEdges << std::endl;
                
                    return;
                }
            }
        }
            
        //TODO
        //std::cout << std::endl;
#endif
    }
示例#20
0
void Cosisim::init_neighborhoods( Geostat_grid* hard_grid, Geostat_grid* soft_grid, 
                                  const GsTLTripletTmpl<double>& hard_ranges, 
                                  const GsTLTripletTmpl<double>& hard_angles,
                                  const GsTLTripletTmpl<double>& soft_ranges, 
                                  const GsTLTripletTmpl<double>& soft_angles,
                                  int hard_max_neighbors, int soft_max_neighbors,
                                  std::vector< CovarianceType >& covariances, 
                                  Search_filter* prim_filter, Search_filter* soft_filter) {

  int nb_indicators = indicators_.size();

  // get the neighborhoods on the primary variable
  for( int i = 0 ; i < nb_indicators ; i++ ) {


   SmartPtr<Neighborhood> neighborhood;
    if( dynamic_cast<Point_set*>(hard_grid) ) {
      neighborhood = SmartPtr<Neighborhood>(
        hard_grid->neighborhood( hard_ranges, hard_angles,
                                 &covariances[i])  );
    } 
    else {
      neighborhood = SmartPtr<Neighborhood>(
        hard_grid->neighborhood( hard_ranges, hard_angles,
                                 &covariances[i] )  );
    }


    neighborhood->select_property( indicators_[i]->name() );
    neighborhood->max_size( hard_max_neighbors );
    if(prim_filter) neighborhood->search_neighborhood_filter(prim_filter->clone());

    hard_neighborhoods_.push_back( NeighorhoodType( neighborhood ) );
  }

  // get the neighborhoods on the secondary variable
  if( soft_grid ) {
    for( int j = 0 ; j < nb_indicators ; j++ ) {
      appli_assert( nb_indicators == secondary_indicators_.size() );


     SmartPtr<Neighborhood> neighborhood;
      if( dynamic_cast<Point_set*>(hard_grid) ) {
        neighborhood = SmartPtr<Neighborhood>(
          soft_grid->neighborhood( soft_ranges, soft_angles,
                                   &covariances[j], true )  );
      } 
      else {
        neighborhood = SmartPtr<Neighborhood>(
          soft_grid->neighborhood( soft_ranges, soft_angles,
                                   &covariances[j] )  );
      }

       neighborhood->select_property( secondary_indicators_[j]->name() );
      neighborhood->max_size( soft_max_neighbors );
      if(soft_filter) neighborhood->search_neighborhood_filter(soft_filter->clone());
      soft_neighborhoods_.push_back( NeighorhoodType( neighborhood ) );
    }
  }
  else {
    // there are no soft data. Use DummyNeighborhoods
    for( int j = 0 ; j < nb_indicators ; j++ ) {
      SmartPtr<Neighborhood> neighborhood( new DummyNeighborhood );
      soft_neighborhoods_.push_back( NeighorhoodType( neighborhood ) );
    }
  }
}
示例#21
0
/*==================================
  MAIN
  ================================*/
int main(int argc, char *argv[]){

  if (argc < 3){
    printf("This program takes 2 arguments:\n");
    printf("positions_file and velocities_file.\n");
    printf("You only passed %d arguments.\n",argc-1);
    printf("You fool.\n");
    exit(1);
  }
  
  //argv[1]: positions file
  //argv[2]: velocities file
  
  //Opens the files

  FILE *posfile, *velfile;
  
  char *namepos = argv[1];
  char *namevel = argv[2];
  
  posfile = fopen(namepos,"r");
  velfile = fopen(namevel,"r");

  long n_pos = countlines(posfile);
  long n_vel = countlines(velfile);
  
  if (n_pos != n_vel)
    {
      printf("Position file and velocity files have different lengths\n");
      printf("Position file has %ld lines and vel. file has %ld lines.\n"
	     ,n_pos,n_vel);
      printf("You fool.\n");
      exit(1);
    }

  printf("Given files %s, %s\n",namepos,namevel);

  printf("length of file = %ld\n",n_pos);

  //Create array of particles

  struct point *masses;
  masses = ( struct point * ) malloc( n_pos * sizeof(struct point) ); 

  //Import data from posfile, velfile into particle array
  
  get_data_from_files( posfile, velfile, n_pos, masses );


  /*=====
    Neighborhoods
    =====*/

  int i = 0, j = 0;
  double mean_dist = 0.0;
  
  printf("Number of neighbors = %d\n",N_NEIGH);
  printf("Obtaining neighborhoods...\n");

  for (i=0; i < n_pos; ++i)
    {
      if (i%250 == 0)
	{
	  printf("i = %d\n",i);
	}

      masses[i].neigh = (size_t * ) malloc( N_NEIGH * sizeof(size_t) );
      neighborhood( masses[i].neigh, N_NEIGH, i, n_pos, masses, &mean_dist );
    }

  mean_dist = 2.0* (mean_dist) / (n_pos*(n_pos + 1.0));

  printf("Mean distance = %lf\n",mean_dist);
  /*=====
    Densities
    ===== */
  
  printf("Calculating densities... ");

  size_t neigh_index = 1;
  double density = 0.0;
  for ( i = 0; i < n_pos; ++i){
    density = 0.0;
    for ( j = 0; j < N_NEIGH; ++j){
      
      neigh_index = masses[i].neigh[j];
      
      density += density_kernel(masses[i], masses[neigh_index], 0.5* (mean_dist));
    }
    masses[i].rho = 4.0/(M_PI * mean_dist * mean_dist) * density;
  }

  /*for (i = 0; i < n_pos; ++i){
    masses[i].rho = 0.0;
  }

  i = 10;
  masses[i].rho = 10.0;
  for (j = 0; j < N_NEIGH; ++j){
    neigh_index = masses[i].neigh[j];
    masses[neigh_index].rho = 10.0;
    }*/

  printf("done!\n");

  /*=====
    Accelerations
    =====*/


  
  /*=====
    Writing out
    =====*/


  FILE * pf;
  pf = fopen("sph.dat","w");

  fprintf(pf,"ID\t m\t x\t y\t vx\t vy\t ax\t ay\t rho\n");

  for ( i = 0; i < n_pos; ++i){
    //printf("Writing %d...\n",i);
    fprintf(pf,
	    "%d\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\n",
	    (int) masses[i].id,
	    masses[i].mass,
	    masses[i].x,
	    masses[i].y,
	    masses[i].vx,
	    masses[i].vy,
	    masses[i].ax,
	    masses[i].ay,
	    masses[i].rho);
  }
  
  fclose(pf);

  return 0;
}
示例#22
0
文件: query.cpp 项目: multiphase/DSC
 std::set<NodeKey> Query::neighborhood(NodeKey from_node, double max_distance) {
     vec3 from_pos = mesh->get(from_node).get_pos();
     return neighborhood(from_pos, max_distance);
 }
示例#23
0
文件: cf.cpp 项目: thejonan/mlpack
void CF::GetRecommendations(const size_t numRecs,
                            arma::Mat<size_t>& recommendations,
                            arma::Col<size_t>& users)
{
  // We want to avoid calculating the full rating matrix, so we will do nearest
  // neighbor search only on the H matrix, using the observation that if the
  // rating matrix X = W*H, then d(X.col(i), X.col(j)) = d(W H.col(i), W
  // H.col(j)).  This can be seen as nearest neighbor search on the H matrix
  // with the Mahalanobis distance where M^{-1} = W^T W.  So, we'll decompose
  // M^{-1} = L L^T (the Cholesky decomposition), and then multiply H by L^T.
  // Then we can perform nearest neighbor search.
  arma::mat l = arma::chol(w.t() * w);
  arma::mat stretchedH = l * h; // Due to the Armadillo API, l is L^T.

  // Now, we will use the decomposed w and h matrices to estimate what the user
  // would have rated items as, and then pick the best items.

  // Temporarily store feature vector of queried users.
  arma::mat query(stretchedH.n_rows, users.n_elem);

  // Select feature vectors of queried users.
  for (size_t i = 0; i < users.n_elem; i++)
    query.col(i) = stretchedH.col(users(i));

  // Temporary storage for neighborhood of the queried users.
  arma::Mat<size_t> neighborhood;

  // Calculate the neighborhood of the queried users.
  // This should be a templatized option.
  neighbor::KNN a(stretchedH);
  arma::mat resultingDistances; // Temporary storage.
  a.Search(query, numUsersForSimilarity, neighborhood, resultingDistances);

  // Generate recommendations for each query user by finding the maximum numRecs
  // elements in the averages matrix.
  recommendations.set_size(numRecs, users.n_elem);
  arma::mat values(numRecs, users.n_elem);

  for (size_t i = 0; i < users.n_elem; i++)
  {
    // First, calculate average of neighborhood values.
    arma::vec averages;
    averages.zeros(cleanedData.n_rows);

    for (size_t j = 0; j < neighborhood.n_rows; ++j)
      averages += w * h.col(neighborhood(j, i));
    averages /= neighborhood.n_rows;

    // Let's build the list of candidate recomendations for the given user.
    // Default candidate: the smallest possible value and invalid item number.
    const Candidate def = std::make_pair(-DBL_MAX, cleanedData.n_rows);
    std::vector<Candidate> vect(numRecs, def);
    typedef std::priority_queue<Candidate, std::vector<Candidate>, CandidateCmp>
        CandidateList;
    CandidateList pqueue(CandidateCmp(), std::move(vect));

    // Look through the averages column corresponding to the current user.
    for (size_t j = 0; j < averages.n_rows; ++j)
    {
      // Ensure that the user hasn't already rated the item.
      if (cleanedData(j, users(i)) != 0.0)
        continue; // The user already rated the item.


      // Is the estimated value better than the worst candidate?
      if (averages[i] > pqueue.top().first)
      {
        Candidate c = std::make_pair(averages[j], j);
        pqueue.pop();
        pqueue.push(c);
      }
    }

    for (size_t p = 1; p <= numRecs; p++)
    {
      recommendations(numRecs - p, i) = pqueue.top().second;
      values(numRecs - p, i) = pqueue.top().first;
      pqueue.pop();
    }

    // If we were not able to come up with enough recommendations, issue a
    // warning.
    if (recommendations(numRecs - 1, i) == def.second)
      Log::Warn << "Could not provide " << numRecs << " recommendations "
          << "for user " << users(i) << " (not enough un-rated items)!"
          << std::endl;
  }
}
示例#24
0
/********************************************************************
* Main SCAN algorithm
********************************************************************/
void Scan::run(const double epsilon, const int mi) {
    // All vertices begin unclassified
    // So let's begin. We will iterate the labels list. In no moment
    // can a label became "unclassified" again, so this loop will
    // do the trick.
    hmap::iterator node;
    uint cnt = 0;
    for (node = g.graph_map.begin(); node != g.graph_map.end(); ++node) {
        // Making sure this node was not yet evaluated
        if (getClusterLabel(node->first) == -1) {
            // Is it a core?
            if (isCore(node->first, epsilon, mi)) {
                //std::cout << node->first << " is a core!\n";
                // Will begin a new cluster
                int cid = getNewClusterID();
                // Put all the e-neighborhood of the node in a queue
                std::set<Edge> x;
                x = neighborhood(node->first, epsilon);
                std::queue<uint> q;
                std::set<Edge>::iterator it;
                //std::cout << node->first << " nhood is:";
                for (it = x.begin(); it != x.end(); ++it) {
                    //std::cout << "   " << it->getNode() << std::endl;
                    q.push(it->getNode());
                }
                while (!q.empty()) {
                    uint y = q.front();
                    q.pop();
                    std::set<Edge> r;
                    r = dirReach(y, epsilon, mi);
                    std::set<Edge>::iterator setIt;
                    for (setIt = r.begin(); setIt != r.end(); ++setIt) {
                        // If the node is unclassified
                        if (getClusterLabel(setIt->getNode()) == -1) {
                            addToCluster(setIt->getNode(), cid);
                            q.push(setIt->getNode());
                        }
                        // If the node is a non-member of the cluster
                        if (getClusterLabel(setIt->getNode()) == 0) {
                            addToCluster(setIt->getNode(), cid);
                        }
                    }

                }
            } else {
                // Not a Core, so it will be labeled as non-member (0)
                setClusterLabel(node->first, 0);
            }
        } else {
            // Node already evaluated. Move along.
            continue;
        }
    }
    // Further classifies non-members

    hmap_ii::iterator nmnode;
    for (nmnode = cluster_label.begin();
            nmnode != cluster_label.end(); ++nmnode) {
        if (nmnode->second == 0) {
            std::set<uint> tmp;
            tmp = getNeighborClusters(nmnode->first);
            if (tmp.size() > 1) {
                addHub(nmnode->first, tmp);
            } else {
                std::pair<uint, uint>
                o(nmnode->first,*tmp.begin());
                outliers.push_back(o);
            }
        }
    }
    std::cout << "SCAN finished.";
    std::cout << std::endl;
}
示例#25
0
LandModel::LandModel(const std::string& _propsFile, int _argc, char** _argv,
		boost::mpi::communicator* _world) :
		props(_propsFile, _argc, _argv, _world), agents(_world) {

	repast::initializeRandom(props, _world);

	repast::RepastProcess* rp = repast::RepastProcess::instance();
	rank = rp->rank();
	world = _world;

	// Payoff information
	payoffT = repast::strToInt(props.getProperty(PAYOFF_T));
	payoffR = repast::strToInt(props.getProperty(PAYOFF_R));
	payoffP = repast::strToInt(props.getProperty(PAYOFF_P));
	payoffS = repast::strToInt(props.getProperty(PAYOFF_S));

	// Model information
	rounds = repast::strToInt(props.getProperty(MODEL_ROUNDS));
	tax = repast::strToDouble(props.getProperty(MODEL_TAX));
	considerTrust = repast::strToDouble(
			props.getProperty(MODEL_CONSIDER_TRUST));
	deltaTrust = repast::strToDouble(props.getProperty(MODEL_DELTA_TRUST));
	trustThreshold = repast::strToDouble(
			props.getProperty(MODEL_TRUST_THRESHOLD));
	strategyType = repast::strToInt(props.getProperty(MODEL_STRATEGY_TYPE));
	neighborhoodType = repast::strToInt(props.getProperty(MODEL_NEIGHBORHOOD));
	topologyType = repast::strToInt(props.getProperty(MODEL_TOPOLOGY));

	genStrategy = repast::Random::instance()->getGenerator("strategy");
	genConsiderTrust = repast::Random::instance()->getGenerator(
			"considerTrust");

	// Grid size
	sizeX = repast::strToInt(props.getProperty(GRID_MAX_X))
			- repast::strToInt(props.getProperty(GRID_MIN_X)) + 1;
	sizeY = repast::strToInt(props.getProperty(GRID_MAX_Y))
			- repast::strToInt(props.getProperty(GRID_MIN_Y)) + 1;

	int procX = repast::strToInt(props.getProperty(PROC_X));
	int procY = repast::strToInt(props.getProperty(PROC_Y));

	std::vector<int> procDim;
	procDim.push_back(procX);
	procDim.push_back(procY);

	int gridBuffer = repast::strToInt(props.getProperty(GRID_BUFFER));

	// Create Grid
	grid = new repast::SharedSpaces<LandAgent>::SharedWrappedDiscreteSpace(
			"grid ",
			repast::GridDimensions(repast::Point<double>(sizeX, sizeY)),
			procDim, gridBuffer, world);
	agents.addProjection(grid);

	// Grid size managed by each process
	dimX = sizeX / procX;
	dimY = sizeY / procY;

	int originX = grid->dimensions().origin().getX();
	int originY = grid->dimensions().origin().getY();

	// Create the agents
	int strategy = strategyType;
	bool cTrust;
	LandAgent* agent;
	for (int i = 0; i < (dimX * dimY); i++) {
		repast::AgentId id(i, rank, AGENT_TYPE);

		// Define the strategy
		if (strategyType == 3) {
			strategy = (int) genStrategy->next();
		}

		// Define if the agent should consider trust
		if (genConsiderTrust->next() <= considerTrust) {
			cTrust = true;
		} else {
			cTrust = false;
		}

		agent = new LandAgent(id, strategy, cTrust, deltaTrust, trustThreshold);
		agents.addAgent(agent);

		// Move the agent to the position in the grid
		grid->moveTo(agent,
				repast::Point<int>(originX + (i / dimX), originY + (i % dimY)));
		agent->setXY((originX + (i / dimX)), (originY + (i % dimY)));
	}

	world->barrier();

	rp->synchronizeProjectionInfo<LandAgent, LandAgentPackage>(agents, *this,
			*this, *this);

	// Set agents neighbors
	for (int i = 0; i < (dimX * dimY); i++) {
		agent = grid->getObjectAt(
				repast::Point<int>(originX + (i / dimX), originY + (i % dimY)));

		agent->setNeighbors(neighborhood(agent));
	}

	// Request agents from other processes
	int numProcesses = repast::RepastProcess::instance()->worldSize();

	repast::AgentRequest request(rank);
	for (int p = 0; p < numProcesses; p++) {
		if (p != rank) {
			for (int i = 0; i < (dimX * dimY); i++) {
				request.addRequest(repast::AgentId(i, p, AGENT_TYPE));
			}
		}
	}
	repast::RepastProcess::instance()->requestAgents<LandAgent, LandAgentPackage>(
			agents, request, *this, *this, *this);

	repast::RepastProcess::instance()->synchronizeAgentStates<LandAgentPackage>(
			*this, *this);
}