30 #include <boost/bind.hpp>
31 #include <boost/foreach.hpp>
32 #include <boost/function.hpp>
33 #define foreach BOOST_FOREACH
37 #include <basic/Tracer.hh>
38 #include <ObjexxFCL/format.hh>
39 #include <numeric/random/random.hh>
40 #include <utility/tag/Tag.hh>
57 #include <utility/io/izstream.hh>
63 static basic::Tracer
TR(
"protocols.simple_moves.GenericMonteCarloMover");
64 static basic::Tracer
TR_energies(
"protocols.simple_moves.GenericMonteCarloMover.individual_energies");
65 static numeric::random::RandomGenerator
mc_RG(61452);
70 namespace simple_moves {
72 using namespace ObjexxFCL::fmt;
75 GenericMonteCarloMoverCreator::keyname()
const
77 return GenericMonteCarloMoverCreator::mover_name();
81 GenericMonteCarloMoverCreator::create_mover()
const {
86 GenericMonteCarloMoverCreator::mover_name()
88 return "GenericMonteCarlo";
92 GenericMonteCarloMover::GenericMonteCarloMover():
93 Mover(
"GenericMonteCarlo"),
99 sample_type_(
"low" ),
102 recover_low_( true ),
103 rank_by_filter_( 1 ),
104 boltz_rank_( false ),
105 last_accepted_pose_( NULL ),
106 lowest_score_pose_( NULL ),
107 stopping_condition_( NULL ),
108 mover_stopping_condition_( NULL ),
109 adaptive_movers_( false ),
110 adaptation_period_( 0 ),
111 saved_accept_file_name_(
"" ),
112 saved_trial_number_file_(
"" ),
114 reset_baselines_( true )
123 Size const maxtrials,
124 Size const task_scaling,
126 Real const temperature,
129 Super(
"GenericMonteCarlo"),
130 maxtrials_( maxtrials ),
131 task_scaling_( task_scaling ),
133 temperature_( temperature ),
134 sample_type_( sample_type ),
137 recover_low_( true ),
139 boltz_rank_( false ),
140 last_accepted_pose_( NULL ),
141 lowest_score_pose_( NULL ),
142 saved_accept_file_name_(
"" ),
144 reset_baselines_( true )
152 Size const maxtrials,
153 Size const task_scaling,
156 Real const temperature,
159 Super(
"GenericMonteCarlo"),
160 maxtrials_( maxtrials ),
161 task_scaling_( task_scaling ),
164 factory_ (factory_in),
165 temperature_( temperature ),
166 sample_type_( sample_type ),
169 recover_low_( true ),
171 boltz_rank_( false ),
172 saved_accept_file_name_(
"" ),
174 reset_baselines_( true )
209 TR <<
"WARNING: the sample type, " <<
sample_type_ <<
", is not defined." << std::endl;
210 runtime_assert(
false );
325 TR <<
"WARNING !! the sample type, " << type <<
", is not defined." << std::endl;
326 runtime_assert(
false );
364 out <<
"Higher score sampled: trial=" << I( 5,
trial_counter_ ) <<
", score(current/last_accepted/best)=";
366 out <<
"Lower score sampled: trial=" << I( 5,
trial_counter_ ) <<
", score(current/last_accepted/best)=";
380 evaluation =
"ScoreXXX";
386 out <<
"mover=" << LJ( 16, mover ) <<
" Score_eval=" << LJ( 16, evaluation ) <<
387 " trials= " << I( 5, ntrials ) <<
"; " <<
388 " accepts= " << F( 6, 3,
Real( accepts )/ntrials ) <<
"; " <<
389 " energy_gap/trial= " << F( 8, 3,
Real(
flip_sign_ * energy_gap ) / ntrials ) << std::endl;
391 out <<
"mover=" <<
A( 16, mover ) <<
" Score_eval=" <<
A( 16, evaluation )
392 <<
" trials= " << I( 6, ntrials ) <<
" NO ACCEPTS." << std::endl;
395 out <<
"Number of rejections per filter: ";
412 TR <<
" No lowest_score_pose_ exists, probably because all movers or filters failed. " << std::endl;
425 for(
Size index = 1; index <=
filters_.size(); ++index ){
442 TR <<
"Initialization done " << std::endl;
458 Size number_designable = 0;
459 TR <<
"Designable Residues: ";
461 if(task->design_residue(i)) {
466 TR << std::endl <<
"Calculated number designable residues" <<
": ";
467 TR << number_designable << std::endl;
468 return number_designable;
476 TR.Debug <<
"filters.size() "<<
filters_.size()<<std::endl;
481 provisional_scores.clear();
482 Real ranking_score( 0.0 );
484 TR.Debug <<
"Filter #"<<index<<std::endl;
486 bool const adaptive(
adaptive_[ index ] );
489 core::Real const filter_val( filter->report_sm( pose ));
490 TR<<
"Filter "<<index<<
" reports "<<filter_val<<std::endl;
492 provisional_scores.push_back( flip * filter_val );
497 TR_energies.Debug <<
"energy index, last_accepted_score, current_score "<<index<<
" "<<
last_accepted_scores_[ index ]<<
" "<<provisional_scores[ index ]<<std::endl;
498 TR.Debug <<
"Current, best, boltz "<<provisional_scores[ index ]<<
" "<<
last_accepted_scores_[ index ]<<
" "<<boltz_factor<<std::endl;
502 Real const probability = std::exp( std::min (40.0, std::max(-40.0,boltz_factor)) );
503 Real const random_num(
mc_RG.uniform() );
504 bool const reject_filter( provisional_scores[ index ] >
last_accepted_scores_[ index ] && random_num >= probability );
510 if( !reject_filter ) {
515 TR<<
"Accept"<<std::endl;
534 TR<<
"Adding accepted mover tag to pose comments"<<std::endl;
551 TR.Debug <<
"Reject"<<std::endl;
565 Real const probability = std::exp( std::min (40.0, std::max(-40.0,boltz_factor)) );
566 if (
mc_RG.uniform() >= probability ) {
603 if( end - begin != 0 ){
604 f.seekg( 0, ios::beg );
606 TR<<
"Loading trial number from checkpoint"<<std::endl;
609 std::istringstream line_stream( line );
610 line_stream >> trial;
611 TR<<
"Loaded trial number: "<<trial<<std::endl;
618 ifstream f_mover_tag( fname.c_str(), ios::in );
619 if( f_mover_tag.good() ){
621 TR<<
"Loaded mover_tag from checkpointing file: "<<
mover_tag_->obj<<std::endl;
624 TR<<
"File containing mover_tag "<<fname<<
" not found. Not loading movertag from checkpoint"<<std::endl;
628 bool call_reset(
false );
629 using namespace protocols::filters;
630 using namespace protocols::simple_filters;
632 if( filter->get_type() ==
"Operator" ){
633 TR<<
"Resetting Operator filter's baseline"<<std::endl;
634 OperatorOP operator_filter( dynamic_cast< Operator * >( filter() ) );
635 operator_filter->reset_baseline( pose, trial != 1 );
638 else if( filter->get_type() ==
"CompoundStatement" ){
639 CompoundFilterOP comp_filt_op( dynamic_cast< CompoundFilter * >( filter() ) );
640 runtime_assert( comp_filt_op );
641 for( CompoundFilter::CompoundStatement::iterator cs_it = comp_filt_op->begin(); cs_it != comp_filt_op->end(); ++cs_it ){
643 if( filt->get_type() ==
"Operator" ){
644 TR<<
"Resetting Operator filter's baseline"<<std::endl;
645 OperatorOP operator_filter( dynamic_cast< Operator * >( filt() ) );
646 operator_filter->reset_baseline( pose, trial != 1 );
653 TR<<
"Resetting Boltzmann's accepted values, in case filters have changed during loading"<<std::endl;
683 TR.Warning <<
"Mover is empty ! " << std::endl;
687 TR.Warning <<
"Both ScorefunctionOP and FilterOP are empty ! " << std::endl;
694 task =
factory_->create_task_and_apply_taskoperations( pose );
696 TR <<
"No task inputted" << std::endl;
701 TR <<
"Input number of trials is " <<
maxtrials_ << std::endl;
704 TR <<
"Resetting number of trials based on your input task to: " <<
maxtrials_ << std::endl;
706 TR <<
"The number of trials for this run is: " <<
maxtrials_ << std::endl;
710 TR<<
"MC stopping condition met at the start, so failing without retrying "<<std::endl;
716 if (
mover_->get_additional_output())
717 utility_exit_with_message(
"Movers returning multiple poses are unsupported by GenericMontoCarloMover.");
719 TR<<
"Saving initial pose entering the MC trajectory, for use in checkpoint recovery. Checkpointing filename: "<<
saved_accept_file_name_<<std::endl;
727 using namespace protocols::rosetta_scripts;
730 bool is_single_random( mover_pp->mode() ==
"single_random" );
731 if( mover_pp && !is_single_random ){
733 ParsedProtocolOP tmp( dynamic_cast< ParsedProtocol * >( mfp.first.first() ) );
734 if( tmp && tmp->mode() ==
"single_random" ){
736 is_single_random =
true;
740 runtime_assert( is_single_random );
743 mover_accepts.clear();
745 runtime_assert( mover_pp );
746 runtime_assert( mover_pp->mode() ==
"single_random" );
750 TR<<
"Trial number: "<<i<<std::endl;
756 sum_prev_accepts += a;
759 TR<<
"Adapting running probabilities: old/new probabilities per mover\n";
760 for(
core::Size ma = 1; ma<= mover_accepts.size(); ma++ ){
762 TR<<mover_pp->apply_probability()[ ma ]<<
"/"<<new_probabilities[ ma ]<<
'\n';
765 mover_pp->apply_probability( new_probabilities );
770 TR<<
"MC stopping condition met at trial "<<i<<std::endl;
773 Pose store_pose( pose );
776 ms =
mover_->get_last_move_status();
778 TR.Warning <<
"Mover failed. The mover, " <<
mover_->get_name() <<
", is performed again. " << std::endl;
782 TR.Error <<
"Mover failed. Exit from GenericMonteCarloMover." << std::endl;
796 mover_accepts[ mover_pp->last_attempted_mover_idx() ]++;
800 pose = (*initial_pose);
811 TR<<
"Finished MC. Out of "<<
maxtrials_<<
" "<<
accept<<
" accepted "<<
" and "<<reject<<
" rejected."<<std::endl;
844 if( data.
has(
"stopping_condition", user_defined_mover_name_ ) ){
845 TR<<user_defined_mover_name_<<
" defines its own stopping condition, and GenericMC will respect this stopping condition"<<std::endl;
849 String const filter_name( tag->getOption<
String >(
"filter_name",
"true_filter" ) );
850 Movers_map::const_iterator find_mover ( movers.find( user_defined_mover_name_ ));
851 Filters_map::const_iterator find_filter( filters.find( filter_name ));
852 if( find_mover == movers.end() && user_defined_mover_name_ !=
"" ) {
853 TR.Error <<
"ERROR !! mover not found in map: \n" << tag << std::endl;
854 runtime_assert( find_mover != movers.end() );
856 if( find_filter == filters.end() ) {
857 TR.Error <<
"ERROR !! filter not found in map: \n" << tag << std::endl;
858 runtime_assert( find_filter != filters.end() );
860 if( user_defined_mover_name_ !=
"" )
861 mover_ = find_mover->second;
864 runtime_assert( dynamic_cast< protocols::rosetta_scripts::ParsedProtocol * >(
mover_() ));
866 bool const adaptive( tag->getOption<
bool >(
"adaptive",
true ) );
868 String const sfxn ( tag->getOption<
String >(
"scorefxn_name",
"" ) );
872 TR <<
"Score evaluation during MC is done by" << sfxn <<
", ";
873 TR << filter_name <<
" is ignored." << std::endl;
881 if( filter_name ==
"true_filter" && !
scorefxn_ ){
882 TR.Error <<
"You need to set filter_name or scorefxn_name for MC criteria." << std::endl;
883 runtime_assert(
false );
887 TR <<
"Apply mover of " << user_defined_mover_name_ <<
", and evaluate score by " << sfxn
888 <<
" at Temperature=" << temperature_ <<
", ntrails= " <<
maxtrials_ << std::endl;
892 if( tag->hasOption(
"stopping_condition" ) )
893 TR<<
"Generic MC using stopping condition "<<
stopping_condition()->get_user_defined_name()<<std::endl;
894 drift_ = tag->getOption<
bool >(
"drift", 1 );
895 preapply_ = tag->getOption<
bool >(
"preapply", 1 );
896 recover_low_ = tag->getOption<
bool >(
"recover_low", 1 );
897 boltz_rank_ = tag->getOption<
bool >(
"bolz_rank", 0 );
901 foreach(
TagPtr const btag, branch_tags ){
902 if( btag->getName() ==
"Filters" ){
904 foreach(
TagPtr const ftag, filters_tags ){
905 String const filter_name( ftag->getOption<
String >(
"filter_name" ) );
906 Filters_map::const_iterator find_filt( filters.find( filter_name ));
907 if( find_filt == filters.end() ) {
908 TR.Error <<
"Error !! filter not found in map: \n" << tag << std::endl;
909 runtime_assert( find_filt != filters.end() );
911 Real const temp( ftag->getOption<
Real >(
"temperature", 1 ) );
912 bool const adap( ftag->getOption<
bool >(
"adaptive",
true ));
913 String const samp_type( ftag->getOption<
String >(
"sample_type",
"low" ));
914 bool const rank( ftag->getOption<
bool >(
"rank",
false ) );
917 TR.Warning <<
"WARNING Multiple filters set to rank! Using most recent (currently "<< filter_name <<
")." << std::endl;
920 TR.Warning <<
"WARNING Setting of rank on sub-filter "<< filter_name <<
" will be ignored as parent is set to Boltzmann rank all." << std::endl;
923 add_filter( find_filt->second, adap, temp, samp_type, rank );
927 utility_exit_with_message(
"tag name " + btag->getName() +
" unrecognized." );
931 TR <<
"Pose is set back to initial pose every after applying mover and score evaluation." << std::endl;
934 if( sample_type_ ==
"high" ){
935 TR <<
"Pose that have higher score is sampled." << sfxn << std::endl;
940 if( tag->hasOption(
"mover_tag" ) )
954 if ( ( tag->hasOption(
"task_operations") )){
955 TR <<
"Found a task operation" << std::endl;
957 if ( new_task_factory == 0)
return;
969 boost::unordered_map<Size, GenericMonteCarloMoverTrigger>::iterator i;
972 bool rescore =
t(cycle, num_cycles, pose, scoring);
988 boost::unordered_map<Size, GenericMonteCarloMoverTrigger>::iterator i =
triggers_.find(trigger_id);
990 TR.Warning <<
"Attempt to remove invalid trigger_id => " << trigger_id << std::endl;