Esempio n. 1
0
TEST(Neighbors,CoverTreeDistanceNeighbors)
{
	typedef std::vector<float> Floats;
	const int N = 100;
	const int k = 10;

	Floats floats;
	for (int i=0;i<N;i++) 
		floats.push_back(float(i));

	float_distance_callback fdc;
	tapkee::tapkee_internal::Neighbors neighbors = 
		tapkee::tapkee_internal::find_neighbors(tapkee::CoverTree, floats.begin(), floats.end(),
				tapkee::tapkee_internal::PlainDistance<Floats::iterator,float_distance_callback>(fdc), k, true);

	for (int i=0;i<N;i++)
	{
		// total number of found neighbors is k
		ASSERT_EQ(neighbors[i].size(),k);
		std::set<float> neighbors_set;
		for (int j=0;j<k;j++) 
			neighbors_set.insert(neighbors[i][j]);
		// there are no repeated values
		ASSERT_EQ(neighbors_set.size(),k);
		// the vector is not a neighbor of itself
		ASSERT_EQ(neighbors_set.find(floats[i]),neighbors_set.end());
		// check neighbors
		int k_left = std::min(i,k/2);
		int k_right = std::min(N-i-1,k/2);
		for (int j=0; j<k_left; j++) 
			ASSERT_NE(neighbors_set.find(floats[i-j-1]),neighbors_set.end());
		for (int j=0; j<k_right; j++) 
			ASSERT_NE(neighbors_set.find(floats[i+j+1]),neighbors_set.end());
	}
}
Esempio n. 2
0
core::MonteCarloMoverResult WeightMover::do_propose() {
  IMP_OBJECT_LOG;

  // store the old weights in case of reject
  oldweights_ = w_.get_weights();

  // Draw weights from a uniform distribution of variables that sum to one
  // taken from http://stats.stackexchange.com/questions/14059/
  //                     generate-uniformly-distributed-weights-that-sum-to-unity

  // get the old x
  Floats x;
  x.push_back(oldweights_[0]);
  for (unsigned i = 1; i < oldweights_.get_dimension() - 1; ++i) {
    x.push_back(oldweights_[i] + x[i - 1]);
  }

  // zero vector in D dimension
  algebra::VectorKD zero = algebra::get_zero_vector_kd(x.size());

  // generate random perturbation of old components
  algebra::VectorKD dX =
      algebra::get_random_vector_in(algebra::SphereKD(zero, radius_));

  // add perturbation to x and apply reflective boundaries
  for (unsigned i = 0; i < x.size(); ++i) {
    if (x[i] + dX[i] > 1.0)
      x[i] = 2.0 - x[i] - dX[i];
    else if (x[i] + dX[i] < 0.0)
      x[i] = -x[i] - dX[i];
    else
      x[i] += dX[i];
  }

  // sort the new x here
  std::sort(x.begin(), x.end(), std::less<double>());

  // get the new weights
  algebra::VectorKD newweights =
      algebra::get_zero_vector_kd(oldweights_.get_dimension());

  newweights[0] = x[0];
  for (unsigned i = 1; i < newweights.get_dimension() - 1; ++i) {
    newweights[i] = x[i] - x[i - 1];
  }
  newweights[newweights.get_dimension() - 1] = 1.0 - x[x.size() - 1];

  // set the new weights
  w_.set_weights(newweights);

  return core::MonteCarloMoverResult(
      ParticleIndexes(1, w_.get_particle()->get_index()), 1.0);
}
Esempio n. 3
0
 algebra::VectorKD get_embedding(const Subset &s,
                                 const Assignment &a,
                                 ParticleStatesTable *pst){
   Floats embed;
  for (unsigned int i=0; i< s.size(); ++i) {
    algebra::VectorKD cur
        = pst->get_particle_states(s[i])->get_embedding(a[i]);
    embed.insert(embed.end(), cur.coordinates_begin(),
                 cur.coordinates_end());
  }
  return embed;
 }