void HybridMonteCarlo::do_step() { //gibbs sampler on x and v //persistence=p samples p times x and once v //However because it's constant E, a rejected move //will result in recalculating the same move up to p times //until it is either accepted or the velocities are redrawn //since p has to be independent of the outcome (markov property) //we avoid recalculating the trajectory on rejection by just trying //it over and over without doing the md again. persistence_counter_ += 1; if (persistence_counter_ == persistence_) { persistence_counter_ = 0; //boltzmann constant in kcal/mol static const double kB = 8.31441 / 4186.6; md_->assign_velocities(get_kt() / kB); } double last = do_evaluate(get_model()->get_particles()); ParticlesTemp moved = do_move(get_move_probability()); double energy = do_evaluate(moved); bool accepted = do_accept_or_reject_move(energy, last); while ((!accepted) && (persistence_counter_ < persistence_-1)) { persistence_counter_ += 1; accepted = do_accept_or_reject_move(energy, last); } /*std::cout << "hmc" << " old " << last << " new " << energy << " delta " << energy-last << " accepted " << accepted <<std::endl;*/ }
double MonteCarlo::do_optimize(unsigned int max_steps) { IMP_OBJECT_LOG; IMP_CHECK_OBJECT(this); if (get_number_of_movers() == 0) { IMP_THROW("Running MonteCarlo without providing any" << " movers isn't very useful.", ValueException); } ParticleIndexes movable = get_movable_particles(); // provide a way of feeding in this value last_energy_ = do_evaluate(movable); if (return_best_) { best_ = new Configuration(get_model()); best_energy_ = last_energy_; } reset_statistics(); update_states(); IMP_LOG_TERSE("MC Initial energy is " << last_energy_ << std::endl); for (unsigned int i = 0; i < max_steps; ++i) { if (get_stop_on_good_score() && get_scoring_function()->get_had_good_score()) { break; } do_step(); if (best_energy_ < get_score_threshold()) break; } IMP_LOG_TERSE("MC Final energy is " << last_energy_ << std::endl); if (return_best_) { // std::cout << "Final score is " << get_model()->evaluate(false) //<< std::endl; best_->swap_configuration(); IMP_LOG_TERSE("MC Returning energy " << best_energy_ << std::endl); IMP_IF_CHECK(USAGE) { IMP_LOG_TERSE("MC Got " << do_evaluate(get_movable_particles()) << std::endl); /*IMP_INTERNAL_CHECK((e >= std::numeric_limits<double>::max() && best_energy_ >= std::numeric_limits<double>::max()) || std::abs(best_energy_ - e) < .01+.1* std::abs(best_energy_ +e), "Energies do not match " << best_energy_ << " vs " << e << std::endl);*/ } return do_evaluate(movable); } else { return last_energy_;
ExpressionResult do_evaluate(const Node* node) { if(node->isLeaf()) { //Leaf evaluation assert(node->getTerm().getType() == Term::Number); if(node->getTerm().numberCanBeEvaluated()) { ExpressionResult res(node->getTerm().toString(), node->getTerm().toNumber()); return res; } //We can't evaluate it return ExpressionResult(); } else { const Node* left = node->a(); const Node* right = node->b(); ExpressionResult left_eval = do_evaluate(left); ExpressionResult right_eval = do_evaluate(right); if(left_eval.isEvaluated() && right_eval.isEvaluated()) { //We can evaluate this node MInt a = left_eval.result(); MInt b = right_eval.result(); MInt res = 0; Symbol::OperationType op = node->getTerm().getSymbol().getOperation(); if(op == Symbol::Add) res = a + b; else if(op == Symbol::Substract) res = a - b; else if(op == Symbol::Multiply) res = a * b; std::string expr_str = left_eval.expression(); expr_str += node->getTerm().toString(); expr_str += right_eval.expression(); ExpressionResult expr_res(expr_str, res); if(_save_all_evaluations) _all_evaluations.push_back(expr_res); return expr_res; } return ExpressionResult(); //We can't evaluate this node } }
ski::node eval(ski::node const& n_orig) { arg_vector args; auto n = n_orig; while (true) { warn(n_orig); while (auto* app = n.get<ski::application>()) { args.push_back(n); warn("back:", args.back()); n = app->f; } bool id = is_identity(n); auto result = boost::apply_visitor(do_evaluate(args), n); if (!result) break; if (args.empty()) n = n_orig.update(*result); else if (id) n = get_fun(args.back()) = *result; else if (result->get<ski::application>()) n = get_fun(args.back()).update(*result); else n = get_fun(args.back()).update(ski::application{ ski::combinator{'I'}, *result}); } while (!args.empty()) n = ski::application{n, get_arg(extract(args))}; return n; }
void MonteCarlo::do_step() { MonteCarloMoverResult moved = do_move(); MoverCleanup cleanup(this); double energy = do_evaluate(moved.get_moved_particles()); do_accept_or_reject_move(energy, moved.get_proposal_ratio()); cleanup.reset(); }
void HybridMonteCarlo::do_step() { //gibbs sampler on x and v //persistence=p samples p times x and once v //However because it's constant E, a rejected move //will result in recalculating the same move up to p times //until it is either accepted or the velocities are redrawn //since p has to be independent of the outcome (markov property) //we avoid recalculating the trajectory on rejection by just trying //it over and over without doing the md again. persistence_counter_ += 1; if (persistence_counter_ == persistence_) { persistence_counter_ = 0; //boltzmann constant in kcal/mol static const double kB = 8.31441 / 4186.6; md_->assign_velocities(get_kt() / kB); } ParticleIndexes all_optimized_particles; { ModelObjectsTemp op = get_model()->get_optimized_particles(); for (unsigned int i = 0; i< op.size(); ++i) { all_optimized_particles.push_back(dynamic_cast<Particle*>(op[i].get()) ->get_index()); } } double last = do_evaluate(all_optimized_particles); core::MonteCarloMoverResult moved = do_move(); double energy = do_evaluate(all_optimized_particles); bool accepted = do_accept_or_reject_move(energy, last, moved.get_proposal_ratio()); while ((!accepted) && (persistence_counter_ < persistence_-1)) { persistence_counter_ += 1; accepted = do_accept_or_reject_move(energy, last, moved.get_proposal_ratio()); } /*std::cout << "hmc" << " old " << last << " new " << energy << " delta " << energy-last << " accepted " << accepted <<std::endl;*/ }
void param_t::evaluate( float frame) { do_evaluate( frame);}
ExpressionResult evaluate() { return do_evaluate(_node); }