Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TrialMover.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file TrialMover
11 /// @brief performs a move and accepts it according to Monte Carlo accept/reject criterion.
12 /// @author Monica Berrondo
13 
15 
16 // Rosetta Headers
17 #include <core/pose/Pose.hh>
18 // AUTO-REMOVED #include <core/conformation/Residue.hh>
19 #include <core/scoring/Energies.hh>
21 // AUTO-REMOVED #include <basic/basic.hh>
22 #include <basic/Tracer.hh>
23 #include <utility/tag/Tag.hh>
24 
26 #include <protocols/moves/Mover.hh>
28 
29 // Utility headers
30 
31 // C++ headers
32 #include <string>
33 
34 #include <utility/vector0.hh>
35 #include <utility/excn/Exceptions.hh>
36 #include <utility/vector1.hh>
37 
38 
39 namespace protocols {
40 namespace moves {
41 
42 basic::Tracer tr("protocols.TrialMover");
43 
44 using namespace core;
45 
47 {
48 
49 }
50 
52 {
53 
54 }
55 
57 
59 {
60  if(mode_ == "reset")
61  {
62  mc_->reset(pose);
63  }else if(mode_ == "recover_low")
64  {
65  mc_->recover_low(pose);
66  }else
67  {
68  utility_exit_with_message("MonteCarloUtil mode must be 'reset' or 'recover_low', this should have been caught earlier, dying");
69  }
70 
71 }
72 
74  utility::tag::TagPtr const tag,
76  protocols::filters::Filters_map const & /* filters */,
77  protocols::moves::Movers_map const & /* movers */,
78  core::pose::Pose const & /* pose */)
79 {
80  if(!tag->hasOption("mode"))
81  {
82  throw utility::excn::EXCN_RosettaScriptsOption("you must specify option mode in MonteCarloUtil");
83  }
84  if(!tag->hasOption("montecarlo"))
85  {
86  throw utility::excn::EXCN_RosettaScriptsOption("you must specify the option montecarlo in MonteCarloUtil");
87  }
88 
89  mode_ = tag->getOption<std::string>("mode");
90  std::string const mc_name(tag->getOption<std::string>("montecarlo"));
91 
92  if(mode_ != "reset" && mode_ != "recover_low")
93  {
94  throw utility::excn::EXCN_RosettaScriptsOption("the option mode must be set to either reset or recover_low in MonteCarloUtil");
95  }
96 
97  mc_ = *data.get<protocols::moves::MonteCarlo*>( "montecarlos",mc_name);
98 }
99 
101 {
102  return new MonteCarloUtil(mc_);
103 }
104 
105 
106 
108 {
109  return "MonteCarloUtil";
110 }
111 
112 
114  start_weight_( 0.0 ),
115  original_weight( 0.0 ),
116  ramp_weight( 0.0 ),
117  delta( 0.0 ),
118  stats_type_( all_stats )
119 {}
120 
121 // constructor with arguments
123  start_weight_( 0.0 ),
124  original_weight( 0.0 ),
125  ramp_weight( 0.0 ),
126  delta( 0.0 ),
127  stats_type_( all_stats )
128 {
129  mover_ = mover_in;
130  mc_ = mc_in;
131 }
132 
134 
135 // set the weights for the score_type for ramping
137  Real const start_weight,
138  Real const end_weight,
139  core::scoring::ScoreType score_type,
140  int const ramp_cycles
141 ) {
142  original_weight = mc_->score_function().get_weight( score_type );
143  delta = ( end_weight - start_weight ) / ramp_cycles;
144  ramp_weight = start_weight;
145  start_weight_ = start_weight;
146 }
147 
149  mc_ = mc_in;
150 }
151 
152 
153 /// @begin TrialMover::apply()
154 /// @brief:
155 /// the apply function for a trial
156 /// @detailed:
157 /// the trial object is created with an mc object
158 /// the mover is applied before doing an mc.boltzmann
159 ///
160 /// @author: Monica Berrondo
162 {
163  using scoring::total_score;
164 
165  /// get the initial scores
166  if ( keep_stats_type() == all_stats ) {
167  stats_.add_score( mc_->last_accepted_score() ); ///< initial_last_accepted_score
168  stats_.add_score( pose.energies().total_energy() ); ///< initial_pose_score
169  }
170 
171  /// make the move
172  mover_->apply( pose );
173 
174  // if ( keep_stats_type() == all_stats ) { //// score and get residue energies
175  // Stupid and wasteful. The structure will be scored inside mc_->boltzman. mc_->score_function()( pose );
176  // Unneccessary since revision 23846 --- mc_->score_function().accumulate_residue_total_energies( pose );
177  // WAIT FOR IT. stats_.add_score( pose.energies().total_energy() ); ///< score_after_move
178  // }
179 
180  /// test if MC accepts or rejects it
181  bool accepted_move = mc_->boltzmann( pose, mover_->type() );
182 
183  if ( keep_stats_type() == all_stats ) {
184  stats_.add_score( mc_->total_score_of_last_considered_pose() );
185  }
186 
187  if ( stats_type_ <= accept_reject ) {
188  stats_.accepted( accepted_move );
189  }
190  if ( keep_stats_type() > no_stats ) {
191  stats_.print( mc_, mover_->type() );
192  }
193 }
194 
197  return "TrialMover";
198 }
199 
200 
202 {
203  tr << "Acceptance rate: " << stats_.acceptance_rate() << std::endl;
204  return stats_.acceptance_rate();
205 }
206 
208 {
209  return stats_.num_accepted();
210 }
211 
212 // sets the input pose also for the contained mover (barak)
214 {
215  this->Mover::set_input_pose( pose );
216  if(mover_)
217  mover_->set_input_pose( pose );
218 }
219 
220 // sets the native pose also for the contained mover (barak)
222 {
223  this->Mover::set_native_pose( pose );
224  if(mover_)
225  mover_->set_native_pose( pose );
226 }
227 
229  TagPtr const tag,
230  DataMap & data,
231  Filters_map const &,
232  Movers_map const & movers,
233  Pose const &
234 )
235 {
236  // 1. MonteCarlo object's name
237  std::string const mc_name( tag->getOption< std::string > ( "montecarlo", "" ));
238  if ( mc_name == "" ) {
239  throw utility::excn::EXCN_RosettaScriptsOption( "TrialMover requires the 'montecarlo' option which was not provided" );
240  }
241 
242  // 2. Mover
243  std::string const movername( tag->getOption< std::string > ( "mover", "" ));
244  if ( movername == "" ) {
245  throw utility::excn::EXCN_RosettaScriptsOption( "TrialMover requires the 'mover' option which was not provided" );
246  }
247  Movers_map::const_iterator find_mover ( movers.find( movername ));
248  if( find_mover == movers.end() && movername != "" ) {
249  throw utility::excn::EXCN_RosettaScriptsOption( "TrialMover was not able to find the mover named '" + movername + "' in the Movers_map" );
250  }
251  mover_ = find_mover->second;
252  mc_ = *data.get< protocols::moves::MonteCarlo * >( "montecarlos", mc_name );
253 
254  // 3. stats_type.
255  std::string const statstype( tag->getOption< std::string > ( "keep_stats", "no_stats" ));
256  if ( statstype != "no_stats" && statstype != "accept_reject" && statstype != "all_stats" ) {
257  throw utility::excn::EXCN_RosettaScriptsOption( "TrialMover keep_stats may only be given the values:\n'no_stats', 'accept_reject', and 'all_stats'.\nRead value '" + statstype + "'" );
258  }
259  if ( statstype == "no_stats" ) {
261  } else if ( statstype == "accept_reject" ) {
263  } else {
265  }
266 
267 }
268 
269 std::ostream &operator<< (std::ostream &os, TrialMover const &mover)
270 {
271  //moves::operator<<(os, mover); // this line causes unexpected segmentation fault.
272  os << "Mover name: " << mover.get_name() << ", Mover type: " << mover.get_type() << ", Mover current tag: " << mover.get_current_tag() << std::endl <<
273  "Mover being tried: " << mover.mover() << std::endl <<
274  "Moves were accepted: " << mover.num_accepts() << " times." << std::endl <<
275  "Acceptance rate: " << mover.stats_.acceptance_rate() << std::endl;
276  os << "MonteCarlo: ";
277  if ( mover.mc_ != 0 ) { os << mover.mc_ << std::endl; }
278  else { os << "none" << std::endl; }
279 
280  return os;
281 }
282 
283 } // namespace moves
284 } // namespace protocols