Permalink
Browse files

don't insert empty elements into scores cache in incremental monte carlo

git-svn-id: https://svn.salilab.org/imp/trunk@12828 e91f8c7b-0e32-4433-a9ac-4502d39a4eab
  • Loading branch information...
1 parent a2bcbbb commit 3c511b14c21a21c9c1a862d8a2476fddb25be261 drussel@SALILAB.ORG committed Feb 24, 2012
Showing with 22 additions and 17 deletions.
  1. +4 −4 modules/core/include/MonteCarlo.h
  2. +18 −13 modules/core/src/MonteCarlo.cpp
@@ -240,15 +240,15 @@ class IMPCOREEXPORT MonteCarlo: public Optimizer
mutable Floats incremental_scores_;
mutable Floats old_incremental_scores_;
mutable Ints old_incremental_score_indexes_;
- vector<Ints> incremental_used_;
+ base::IndexVector<ParticleIndexTag, Ints> incremental_used_;
struct NBLScore {
OwnerPointer<PairScore> score_;
typedef std::pair<ParticleIndex, double> ScorePair;
typedef vector<ScorePair> ScorePairs;
// first on the particle index then list the neighbors
- mutable vector<ScorePairs> cache_;
+ mutable base::IndexVector<ParticleIndexTag, ScorePairs> cache_;
double distance_;
mutable double prior_, old_prior_;
ParticleIndexes pis_;
@@ -268,8 +268,8 @@ class IMPCOREEXPORT MonteCarlo: public Optimizer
void initialize(Model *m, ParticleIndexPairs all);
};
Ints nbl_incremental_used_;
- Ints to_dnn_;
- Ints from_dnn_;
+ base::IndexVector<ParticleIndexTag, int> to_dnn_;
+ ParticleIndexes from_dnn_;
mutable ParticleIndex moved_;
OwnerPointer<algebra::DynamicNearestNeighbor3D> dnn_;
NBLScore nbl_;
@@ -201,12 +201,13 @@ void MonteCarlo::setup_incremental() {
dg);
ParticleIndex pi= ap[i]->get_index();
incremental_used_.resize(std::max<unsigned int>(incremental_used_.size(),
- pi+1));
+ get_as_unsigned_int(pi)+1));
for (unsigned int j=0; j< cur.size(); ++j) {
IMP_INTERNAL_CHECK(index.find(cur[j]) != index.end(),
"Cannot find restraints " << cur[j]->get_name()
<< " in index");
- incremental_used_[pi].push_back(index.find(cur[j])->second);
+ incremental_used_[pi]
+ .push_back(index.find(cur[j])->second);
IMP_LOG(VERBOSE, "Restraint " << cur[j]->get_name()
<< " depends on particle " << ap[i]->get_name() << std::endl);
}
@@ -259,7 +260,7 @@ double MonteCarlo::evaluate_incremental(const ParticleIndexes &moved) const {
++incremental_evals_;
Ints allr;
for (unsigned int i=0; i< moved.size(); ++i) {
- if (moved[i] < static_cast<int>(incremental_used_.size())) {
+ if (get_as_unsigned_int(moved[i]) < incremental_used_.size()) {
allr.insert(allr.end(), incremental_used_[moved[i]].begin(),
incremental_used_[moved[i]].end());
}
@@ -362,11 +363,11 @@ MonteCarlo::NBLScore::NBLScore(PairScore *score,
const PairFilters &filters) {
score_=score;
distance_=distance;
- ParticleIndex mi=0;
+ unsigned int mi(0);
pis_.resize(ps.size());
filters_=filters;
for (unsigned int i=0; i< ps.size(); ++i) {
- mi=std::max(mi, ps[i]->get_index());
+ mi=std::max(mi, get_as_unsigned_int(ps[i]->get_index()));
pis_[i]=ps[i]->get_index();
}
cache_.resize(mi+1);
@@ -377,17 +378,19 @@ double MonteCarlo::NBLScore::get_score(Model *m, ParticleIndex moved,
IMP_IF_CHECK(USAGE_AND_INTERNAL) {
double nscore=0;
for (unsigned int i=0; i< cache_.size(); ++i) {
- for (unsigned int j=0; j< cache_[i].size(); ++j) {
- if (static_cast<unsigned int>(cache_[i][j].first) < i) {
- nscore+=cache_[i][j].second;
+ ParticleIndex pi(i);
+ for (unsigned int j=0; j< cache_[pi].size(); ++j) {
+ if (cache_[pi][j].first != base::get_invalid_index<ParticleIndexTag>()
+ && cache_[pi][j].first < pi) {
+ nscore+=cache_[pi][j].second;
}
}
}
IMP_INTERNAL_CHECK(std::abs(nscore-prior_) < .05*(nscore+prior_)+.1,
"Cached and stored scores don't match: " << prior_
<< " vs " << nscore);
}
- if (moved==-1) return prior_;
+ if (moved==base::get_invalid_index<ParticleIndexTag>()) return prior_;
double old=0;
removed_.clear();
@@ -414,7 +417,7 @@ double MonteCarlo::NBLScore::get_score(Model *m, ParticleIndex moved,
filters_[i]->filter_in_place(m, all);
}
double nscore=0;
- cache_[moved].resize(nearby.size());
+ cache_[moved].reserve(nearby.size());
for (unsigned int i=0; i< all.size(); ++i) {
double cur= score_->evaluate_index(m, ParticleIndexPair(moved, all[i][1]),
nullptr);
@@ -432,7 +435,7 @@ double MonteCarlo::NBLScore::get_score(Model *m, ParticleIndex moved,
}
void MonteCarlo::NBLScore::roll_back(Model *, ParticleIndex moved) {
- IMP_LOG(VERBOSE, "Rolling back nbl on " << moved << std::endl);
+ IMP_LOG(TERSE, "Rolling back nbl on " << moved << std::endl);
prior_=old_prior_;
cache_[moved].clear();
for (unsigned int i=0; i< added_.size(); ++i) {
@@ -455,7 +458,7 @@ void MonteCarlo::NBLScore::initialize(Model *m, ParticleIndexPairs all) {
filters_[i]->filter_in_place(m, all);
}
for (unsigned int i=0; i< cache_.size(); ++i) {
- cache_[i].clear();
+ cache_[ParticleIndex(i)].clear();
}
prior_=0;
for (unsigned int i=0; i< all.size(); ++i) {
@@ -471,7 +474,9 @@ void MonteCarlo::NBLScore::initialize(Model *m, ParticleIndexPairs all) {
double MonteCarlo::evaluate_non_bonded(const ParticleIndexes &moved) const {
if (!dnn_) return 0;
if (moved.empty()) {
- return nbl_.get_score(get_model(), -1, ParticleIndexes());
+ return nbl_.get_score(get_model(),
+ base::get_invalid_index<ParticleIndexTag>(),
+ ParticleIndexes());
} else {
IMP_USAGE_CHECK(moved.size()==1, "Only one at a time supported");
Ints nearby= dnn_->get_in_ball(to_dnn_[moved[0]],

0 comments on commit 3c511b1

Please sign in to comment.