Esempio n. 1
0
double GSLOptimizer::optimize(unsigned int iter,
                              const gsl_multimin_fminimizer_type*t,
                              double ms, double mxs) {
  fis_= get_optimized_attributes();
  best_score_=std::numeric_limits<double>::max();
  unsigned int n= get_dimension();
  if (n ==0) {
    IMP_LOG(TERSE, "Nothing to optimize" << std::endl);
    return get_scoring_function()->evaluate(false);
  }
  gsl_multimin_fminimizer *s=gsl_multimin_fminimizer_alloc (t, n);

  gsl_vector *x= gsl_vector_alloc(get_dimension());
  update_state(x);
  gsl_vector *ss= gsl_vector_alloc(get_dimension());
  gsl_vector_set_all(ss, mxs);

  gsl_multimin_function f= internal::create_f_function_data(this);
  gsl_multimin_fminimizer_set (s, &f, x, ss);
  try {
    int status;
    do {
      --iter;
      //update_state(x);
      status = gsl_multimin_fminimizer_iterate(s);
      if (status) {
        IMP_LOG(TERSE, "Ending optimization because of state " << s
                << std::endl);
        break;
      }
      double sz= gsl_multimin_fminimizer_size(s);
      status= gsl_multimin_test_size(sz, ms);
      update_states();
      if (status == GSL_SUCCESS) {
        IMP_LOG(TERSE, "Ending optimization because of small size " << sz
                << std::endl);
        break;
      }
    } while (status == GSL_CONTINUE && iter >0);
  } catch (AllDone){
  }
  gsl_vector *ret=gsl_multimin_fminimizer_x (s);
  best_score_=gsl_multimin_fminimizer_minimum (s);
  write_state(ret);
  gsl_multimin_fminimizer_free (s);
  gsl_vector_free (x);
  return best_score_;
}
Esempio n. 2
0
IMPCORE_BEGIN_NAMESPACE

FixedRefiner::FixedRefiner(const ParticlesTemp &ps):
  Refiner("FixedRefiner%d"), ps_(ps){
  IMP_LOG(VERBOSE, "Created fixed particle refiner with " << ps.size()
          << " particles" << std::endl);
}
  void MultivariateFNormalSufficient::set_epsilon(const VectorXd& eps)
{
    IMP_LOG(TERSE, "MVN:   set epsilon to new vector"<< std::endl);
    epsilon_ = eps;
    flag_epsilon_ = true;
    flag_Peps_ = false;
}
void ClusterSet::do_join_clusters(unsigned int cluster_id1,
                       unsigned int cluster_id2,
                       double distance_between_clusters) {
  IMP_LOG(VERBOSE,"Joining clusters " << cluster_id1 << " and "
          << cluster_id2 << std::endl);

  joined_ids1_.push_back(cluster_id1);
  joined_ids2_.push_back(cluster_id2);
  cluster_distances_.push_back(distance_between_clusters);


  Ints ids;
  ids.push_back(cluster_id1);
  ids.push_back(cluster_id2);
  Ints new_cluster;
  for (unsigned int i=0;i<2;++i) {
    if( (unsigned int )ids[i]<n_elements_) {
      new_cluster.push_back(ids[i]);
    } else {
      unsigned int s=get_step_from_id(ids[i]);
      new_cluster.insert(new_cluster.end(),
                       clusters_elements_[s].begin(),
                       clusters_elements_[s].end());
    }
  }
  clusters_elements_.push_back(new_cluster);
  steps_++;
}
Esempio n. 5
0
void FitRestraint::store_particles(ParticlesTemp ps) {
  all_ps_=get_as<Particles>(ps);
  add_particles(ps);
  //sort to rigid and not rigid members
  if (use_rigid_bodies_) {
    for(Particles::iterator it = all_ps_.begin();it != all_ps_.end(); it++) {
      if (core::RigidMember::particle_is_instance(*it)) {
        core::RigidBody rb=core::RigidMember(*it).get_rigid_body();
        part_of_rb_.push_back(*it);
        if (member_map_.find(rb) == member_map_.end()) {
          member_map_[rb]=Particles();
          rbs_.push_back(rb);
        }
        member_map_[rb].push_back(*it);
      }
      else {
        not_part_of_rb_.push_back(*it);
      }
    }
  }
  else {
    not_part_of_rb_=all_ps_;
  }
  IMP_LOG(TERSE,"number of"
          <<" particles that are not rigid bodies is:"
          <<not_part_of_rb_.size()<<", "<<part_of_rb_.size()<<" particles "<<
          " are part of "<<rbs_.size()<<" rigid bodies"<<std::endl);
}
ParticlesTemp RigidBodyMover::propose_move(Float f) {
    IMP_OBJECT_LOG;
    {
        ::boost::uniform_real<> rand(0,1);
        double fc =rand(random_number_generator);
        if (fc > f) return ParticlesTemp();
    }
    last_transformation_= d_.get_reference_frame().get_transformation_to();
    algebra::Vector3D translation
        = algebra::get_random_vector_in(algebra::Sphere3D(d_.get_coordinates(),
                                        max_translation_));
    algebra::Vector3D axis =
        algebra::get_random_vector_on(algebra::Sphere3D(algebra::Vector3D(0.0,
                                      0.0,
                                      0.0),
                                      1.));
    ::boost::uniform_real<> rand(-max_angle_,max_angle_);
    Float angle =rand(random_number_generator);
    algebra::Rotation3D r
        = algebra::get_rotation_about_axis(axis, angle);
    algebra::Rotation3D rc
        = r*d_.get_reference_frame().get_transformation_to().get_rotation();
    algebra::Transformation3D t(rc, translation);
    IMP_LOG(VERBOSE,"proposed move " << t << std::endl);
    d_.set_reference_frame(algebra::ReferenceFrame3D(t));
    return ParticlesTemp(1, d_);
}
double Fine2DRegistrationRestraint::unprotected_evaluate(
    DerivativeAccumulator *accum) const {
  IMP_UNUSED(accum);
  calls_++;
  IMP_USAGE_CHECK(accum == nullptr,
                  "Fine2DRegistrationRestraint: This restraint does not "
                  "provide derivatives ");

  // projection_ needs to be mutable, son this const function can change it.
  // project_particles changes the matrix of projection_
  ProjectingOptions options(params_.pixel_size, params_.resolution);
  double score = 0;
  try {
    do_project_particles(ps_, projection_->get_data(), PP_.get_rotation(),
                         PP_.get_translation(), options, masks_);
    score = score_function_->get_score(subject_, projection_);
  }
  catch (cv::Exception &e) {
    IMP_LOG(WARNING,
            "Fine2DRegistration. Error computing the score: "
            "Returning 1 (maximum score). Most probably because projecting "
            "out of the image size."
                << e.what() << std::endl);
    score = 1.0;
  }
  IMP_LOG_VERBOSE("Fine2DRegistration. Score: " << score << std::endl);
  return score;
}
IMPISD_BEGIN_NAMESPACE

MultivariateFNormalSufficientSparse::MultivariateFNormalSufficientSparse(
        const MatrixXd& FX, double JF, const VectorXd& FM,
        const SparseMatrix<double>& Sigma, cholmod_common *c, double cutoff) :
    Object("Multivariate Normal distribution %1%")
{
        c_ = c;
        W_=nullptr;
        Sigma_=nullptr;
        P_=nullptr;
        PW_=nullptr;
        epsilon_=nullptr;
        L_=nullptr;
        N_=FX.rows();
        M_=FX.cols();
        IMP_LOG(TERSE, "MVNsparse: direct init with N=" << N_
                << " and M=" << M_ << std::endl);
        IMP_USAGE_CHECK( N_ > 0,
            "please provide at least one observation per dimension");
        IMP_USAGE_CHECK( M_ > 0,
            "please provide at least one variable");
        FM_=FM;
        set_FX(FX,cutoff); //also computes W, Fbar and epsilon.
        set_JF(JF);
        set_Sigma(Sigma); //computes the Cholesky decomp.
}
void MultivariateFNormalSufficient::set_norms(double norm, double lnorm)
{
    norm_ = norm;
    lnorm_ = lnorm;
    IMP_LOG(TERSE, "MVN:   set norms" << std::endl);
    flag_norms_ = true;
}
MultivariateFNormalSufficientSparse::MultivariateFNormalSufficientSparse(
        const VectorXd& Fbar, double JF, const VectorXd& FM, int Nobs,
        const SparseMatrix<double>& W, const SparseMatrix<double>& Sigma,
        cholmod_common *c)
        : Object("Multivariate Normal distribution %1%")
{
        c_ = c;
        W_=nullptr;
        Sigma_=nullptr;
        P_=nullptr;
        PW_=nullptr;
        epsilon_=nullptr;
        L_=nullptr;
        N_=Nobs;
        M_=Fbar.rows();
        IMP_LOG(TERSE, "MVNsparse: sufficient statistics init with N=" << N_
                << " and M=" << M_ << std::endl);
        IMP_USAGE_CHECK( N_ > 0,
            "please provide at least one observation per dimension");
        IMP_USAGE_CHECK( M_ > 0,
            "please provide at least one variable");
        FM_=FM;
        set_Fbar(Fbar); //also computes epsilon
        set_W(W);
        set_JF(JF);
        set_Sigma(Sigma);
}
void MultivariateFNormalSufficient::set_Peps(const VectorXd& Peps)
{
    Peps_ = Peps;
    //std::cout << "Peps: " << Peps_ << std::endl << std::endl;
    IMP_LOG(TERSE, "MVN:   set P*epsilon to new matrix" << std::endl);
    flag_Peps_ = true;
}
void MultivariateFNormalSufficient::set_PW(const MatrixXd& PW)
{
    PW_ = PW;
    //std::cout << "PW: " << PW_ << std::endl << std::endl;
    IMP_LOG(TERSE, "MVN:   set PW to new matrix" << std::endl);
    flag_PW_ = true;
}
 void MultivariateFNormalSufficientSparse::set_JF(double f)
 {
   JF_=f;
   lJF_=-log(JF_);
   IMP_LOG(TERSE, "MVNsparse:   set JF = " << JF_ << " lJF_ = " << lJF_
           <<std::endl);
 }
Esempio n. 14
0
IMPSTATISTICS_BEGIN_INTERNAL_NAMESPACE


KMCentersTree::KMCentersTree( KMData *data_points,KMCenters *centers,
  KMPoint *bb_lo, KMPoint *bb_hi) : centers_(centers){
  data_points_ = data_points;
  Ints pid;
  skeleton_tree(pid,bb_lo, bb_hi);
  root_ = build_tree(0, data_points_->get_number_of_points()-1,0);
  IMP_LOG(VERBOSE,"KMCentersTree const end build tree "<< std::endl);
  root_->compute_sums();
  IMP_LOG(VERBOSE,"KMCentersTree const end compute sums "<< std::endl);
  //TODO - should we use the ignore stuff
  //  root_->compute_sums(ignoreMe1, ignoreMe2, ignoreMe3);
  //  IMP_INTERNAL_CHECK(ignoreMe1 == data_points_->get_number_of_points(),
  // "calculate sums should have included all of the points");
}
  /* energy (score) functions, aka -log(p) */
double MultivariateFNormalSufficientSparse::evaluate() const
  {
      //std::cout << " mean " << double(N_)*mean_dist();
      //std::cout << " WP " << trace_WP();
      double e = lnorm_ + lJF_ + 0.5*( trace_WP() + double(N_)*mean_dist()) ;
      IMP_LOG(TERSE, "MVNsparse: evaluate() = " << e << std::endl);
      return e;
  }
void
SingleParticleScoringFunction::do_update_dependencies(const DependencyGraph &dg,
                                     const DependencyGraphVertexIndex &index) {
  IMP_OBJECT_LOG;
  compatibility::map<Restraint*, int> mp;
  IMP_LOG(TERSE, "All restraints are " << all_restraints_ << std::endl);
  for (unsigned int i=0; i< all_restraints_.size(); ++i) {
    mp[all_restraints_[i]]= i;
  }
  Restraints mr;
  boost::tie(indexes_, mr)= get_my_restraints(get_model()->get_particle(pi_),
                                             mp, dg, index);
  IMP_LOG(TERSE, "Found " << mr << " for particle "
          << Showable(get_model()->get_particle(pi_)) << std::endl);
  IMP::internal::RestraintsScoringFunction::set_restraints(mr);
  IMP::internal::RestraintsScoringFunction::do_update_dependencies(dg, index);
}
void MultivariateFNormalSufficient::set_P(const MatrixXd& P)
{
    P_ = P;
    if (use_cg_) precond_ = P;
    //std::cout << "P: " << P_ << std::endl << std::endl;
    IMP_LOG(TERSE, "MVN:   set P to new matrix" << std::endl);
    flag_P_ = true;
}
 bool operator()(double max_centrality, DGEdge e,
                 const DensityGraph& g)
 {
   IMP_LOG(TERSE,"Iter: " << iter << " Max Centrality: "
           << (max_centrality / dividend) << std::endl);
   ++iter;
   return inherited::operator()(max_centrality, e, g);
 }
  // O(1) if up to date, O(M^2) if epsilon new, O(M^3) if Sigma new
VectorXd MultivariateFNormalSufficient::evaluate_derivative_FM() const
{
      timer_.start(DFM);
      // d(-log(p))/d(FM) = - N * P * epsilon
      IMP_LOG(TERSE, "MVN: evaluate_derivative_FM() = " << std::endl);
      VectorXd tmp(-N_ * get_Peps()/IMP::square(factor_));
      timer_.stop(DFM);
      return tmp;
}
Esempio n. 20
0
/* Writes the grid of values of an EM map to a MRC file */
void MRCReaderWriter::write_data(std::ofstream &s,const float *pt)
{

  s.write((char *)pt,sizeof(float)*header.nx * header.ny * header.nz);
  IMP_USAGE_CHECK(!s.bad(),
            "MRCReaderWriter::write_data >> Error writing MRC data.");
  IMP_LOG(TERSE,"MRC file written: grid " << header.nx << "x" << header.ny
          << "x" << header.nz << std::endl);
}
Esempio n. 21
0
ParticlesTemp SerialMover::propose_move(Float f) {
  IMP_OBJECT_LOG;
  ++imov_;
  if(imov_==static_cast<int>(mvs_.size())) imov_=0;
  mvs_[imov_]->set_was_used(true);
  IMP_LOG(VERBOSE,"Propose move using "
          << mvs_[imov_]->get_name() <<std::endl);
  return mvs_[imov_]->propose_move(f);
}
  MatrixXd MultivariateFNormalSufficient::compute_PTP() const
{
  timer_.start(PTP);
  IMP_LOG(TERSE, "MVN:   computing PTP" << std::endl);
  VectorXd peps(get_Peps());
  MatrixXd tmp(peps*peps.transpose());
  timer_.stop(PTP);
  return tmp;
}
Esempio n. 23
0
Atom get_atom(Residue rd, AtomType at) {
  Hierarchy mhd(rd.get_particle());
  for (unsigned int i=0; i< mhd.get_number_of_children(); ++i) {
    Atom a(mhd.get_child(i));
    if (a.get_atom_type() == at) return a;
  }
  IMP_LOG(VERBOSE, "Atom not found " << at << std::endl);
  return Atom();
}
  cholmod_sparse *MultivariateFNormalSufficientSparse::compute_PTP() const
{
  IMP_LOG(TERSE, "MVNsparse:   computing PTP" << std::endl);
  cholmod_sparse *eps = cholmod_dense_to_sparse(epsilon_, true, c_);
  cholmod_sparse *tmp = cholmod_spsolve(CHOLMOD_A, L_, eps, c_);
  cholmod_sparse *ptp = cholmod_aat(tmp, nullptr, 0, 1, c_);
  cholmod_free_sparse(&eps, c_);
  cholmod_free_sparse(&tmp, c_);
  return ptp;
}
Esempio n. 25
0
void KMLProxy::log_header()  const{
  IMP_LOG(TERSE, "\n[Run_k-means:\n"
      << "  data_size       = " << data_->get_number_of_points() << "\n"
      << "  kcenters        = " << kcenters_ << "\n"
      << "  dim             = " << dim_ << "\n"
    //TODO - should we add this back ?
    //      << "  max_tot_stage   = " << term_.getMaxTotStage(kcenters_,
    //data_size_) << "\n"
      << "  max_run_stage   = " << term_.get_max_num_of_stages_for_run() << "\n"
      << "  min_accum_rdl   = " << term_.get_min_accumulated_rdl() << "\n");
}
VectorOfFloats ClusterSet::get_linkage_matrix() const {
  IMP_LOG(VERBOSE,"ClusterSet: Building linkage  matrix" << std::endl);
  VectorOfFloats mat(steps_);
  for (unsigned int i=0;i<steps_;++i) {
    mat[i].resize(3);
    mat[i][0] = static_cast<double>(joined_ids1_[i]);
    mat[i][1] = static_cast<double>(joined_ids2_[i]);
    mat[i][2] = cluster_distances_[i];
  }
  return mat;
}
void MultivariateFNormalSufficient::set_ldlt(
          const Eigen::LDLT<MatrixXd, Eigen::Upper>& ldlt)
{
    IMP_LOG(TERSE, "MVN:   set ldlt factorization"<< std::endl);
    ldlt_ = ldlt;
    flag_ldlt_ = true;
    flag_P_ = false;
    flag_PW_ = false;
    flag_norms_ = false;
    flag_Peps_ = false;
}
  double MultivariateFNormalSufficient::get_mean_square_residuals() const
{
    timer_.start(MD);
    //std::cout << "P " << std::endl << P_ << std::endl;
    //std::cout << "epsilon " << std::endl << get_epsilon() << std::endl;
    VectorXd Peps(get_Peps());
    VectorXd epsilon(get_epsilon());
    double dist = epsilon.transpose()*Peps;
    IMP_LOG(TERSE, "MVN:   get_mean_square_residuals = " << dist << std::endl);
    timer_.stop(MD);
    return dist;
}
Float SphereDistanceToSingletonScore::evaluate(Particle *b,
                                         DerivativeAccumulator *da) const
{
  Float v= internal::evaluate_distance_pair_score(XYZR(b),
                                                  StaticD(pt_), da,
                                                  f_.get(),
                                                  boost::lambda::_1
                                                  - XYZR(b).get_radius());
  IMP_LOG(VERBOSE, "SphereDistanceTo from " << XYZR(b) << " to "
          << pt_ << " scored " << v << std::endl);
  return v;
}
 void MultivariateFNormalSufficientSparse::set_FM(const VectorXd& FM)
 {
   if (FM.rows() != FM_.rows() || FM.cols() != FM_.cols() || FM != FM_){
       if (FM.rows() != M_) {
           IMP_THROW("size mismatch for FM: got "
                   <<FM.rows() << " instead of " << M_, ModelException);
           }
       FM_=FM;
       IMP_LOG(TERSE, "MVNsparse:   set FM to new vector" << std::endl);
       compute_epsilon();
   }
 }