Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GenericMonteCarloMover.hh
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 src/protocols/simple_moves/GenericMonteCarloMover.hh
11 /// @brief perform a given mover and sample structures by MonteCarlo
12 /// @detailed The "score" evaluation of pose during MC after applying mover is done by
13 /// ither FilterOP that can do report_sm() or ScoreFunctionOP you gave.
14 /// By setting sample_type_ to high, you can also sample the pose that have higher score.
15 /// @author Nobuyasu Koga ( nobuyasu@uw.edu )
16 /// @author Christopher Miles (cmiles@uw.edu)
17 
18 #ifndef INCLUDED_protocols_simple_moves_GenericMonteCarloMover_hh
19 #define INCLUDED_protocols_simple_moves_GenericMonteCarloMover_hh
20 
21 // Unit header
25 
26 // C/C++ headers
27 #include <string>
28 
29 // External headers
30 #include <boost/function.hpp>
31 #include <boost/unordered_map.hpp>
32 
33 // Utility headers
34 #include <utility/vector1.hh>
35 #include <utility/tag/Tag.fwd.hh>
36 
37 // Project Headers
38 #include <core/types.hh>
39 #include <core/pose/Pose.fwd.hh>
44 
45 // Package headers
47 #include <protocols/moves/Mover.hh>
48 
49 namespace protocols {
50 namespace simple_moves {
51 
52 using namespace protocols::moves;
53 
54 /// @brief Trigger API definition
55 typedef boost::function<bool(core::Size,
56  core::Size,
57  const core::pose::Pose&,
59 
60 
62 public:
65  typedef core::Size Size;
66  typedef core::Real Real;
77 
82 
83  /// @brief default constructor
85 
86  /// @brief value constructor without score function
88  Size const maxtrials,
89  Size const task_scaling,
90  MoverOP const & mover,
91  Real const temperature = 0.0,
92  String const sample_type = "low",
93  bool const drift = true
94  );
95 
96  /// @brief value constructor with score function
97  // Undefined, commenting out to fix PyRosetta build
98  /* GenericMonteCarloMover(
99  Size const maxtrials,
100  MoverOP const & mover,
101  ScoreFunctionOP const & sfxn,
102  Real const temperature = 0.0,
103  String const sample_type = "low",
104  bool const drift = true
105  ); */
106 
107 
108  /// @brief value constructor with task operation via TaskFactory
110  Size const maxtrials,
111  Size const task_scaling,
112  MoverOP const & mover,
113  TaskFactoryOP factory_in,
114  Real const temperature = 0.0,
115  String const sample_type = "low",
116  bool const drift = true
117  );
118 
119 
120  /// @brief destructor
122 
123  /// @brief create copy constructor
124  virtual MoverOP clone() const;
125 
126  /// @brief create this type of objectt
127  virtual MoverOP fresh_instance() const;
128 
129  /// @brief initialize object used in constructor
130  void initialize();
131 
132  /// @brief apply GenericMonteCarloMover (Mover)
133  virtual void apply( Pose & pose );
134 
135  virtual String get_name() const;
136 
137  /// @brief reset MC iterations, with pose used for the last & best structures
138  void reset( Pose & pose );
139 
140  /// @brief return the simulation state to the lowest energy structure we've seen
141  void recover_low( Pose & pose );
142 
143  /// @brief core of MC
144  bool boltzmann( Pose & pose );
145 
146  Size num_designable( Pose & pose, PackerTaskOP & task);
147 
148  public: // accessor
149 
150  /// @brief return the last accepted pose
151  PoseOP last_accepted_pose() const;
152 
153  /// @brief return the last accepted score
154  Real last_accepted_score() const;
155 
156  /// @brief return the lowest score pose
157  PoseOP lowest_score_pose() const;
158 
159  /// @brief return the lowest score
160  Real lowest_score() const;
161 
162  /// @brief return the lowest score
163  Real current_score() const;
164 
165  /// @brief return mc_accepted
166  MCA mc_accpeted() const;
167 
168  /// @brief Return the score function in use
170  return scorefxn_;
171  }
172 
173  /// @brief Adds a new trigger, returning its id.
174  ///
175  /// Example:
176  /// #include <boost/bind.hpp>
177  /// #include <boost/function.hpp>
178  ///
179  /// bool no_op(core::Size stage,
180  /// core::Size num_stages,
181  /// core::Size cycle,
182  /// core::Size num_cycles,
183  /// const core::pose::Pose&,
184  /// core::scoring::ScoreFunctionOP) {}
185  ///
186  /// Trigger callback = boost::bind(&no_op, STAGE, NUM_STAGES, _1, _2, _3, _4);
187  /// Size trigger_id = add_trigger(callback);
188  ///
189  /// The current stage and number of stages must be bound at creation time.
190  /// This information provides the triggers with context about the current
191  /// progress of the simulation as a whole.
192  ///
193  /// If the trigger returns true, rescoring occurs.
194  Size add_trigger(const GenericMonteCarloMoverTrigger& trigger);
195 
196  /// @brief Returns the number of triggers
197  Size num_triggers() const;
198 
199  public: // mutators
200 
201  /// @brief Removes the trigger with the specified id
202  void remove_trigger(Size trigger_id);
203 
204  /// @brief set max trials of MC trials
205  void set_maxtrials( Size const ntrial );
206 
207  /// @brief set task multiplier to calculate trials from task
208  void set_task_scaling( Size const scaling );
209 
210  /// @brief set mover
211  void set_mover( MoverOP const & mover );
212 
213  /// @brief Pose is evaluated by ScoreFunctionOP during MC trials
214  void set_scorefxn( ScoreFunctionOP const & sfxn );
215 
216  /// @brief set temperature
217  void set_temperature( Real const temp );
218 
219  /// @brief set sample type, max or min
220  /// when sample_type == max, sample pose which have higher score
221  /// when sample_type == min, sample pose which have lower score
222  void set_sampletype( String const & type );
223 
224  /// @brief if drift=false, the pose is set back to the initial pose at each MC trial
225  /// Of course, this is not MC sampling.
226  void set_drift( bool const drift );
227 
228  /// @brief if preapply=true, auto-accept the first application of the submover,
229  /// ignoring boltzman criteria.
230  void set_preapply( bool const preapply=false );
231 
232  /// @brief if recover_low=true, after apply() the structure
233  /// is the lowest energy structure, rather than the last accepted structure.
234  void set_recover_low( bool const recover_low );
235 
236  /// @brief if boltz_rank=true, rank structures by the temperature-weighted
237  /// sum of scores, rather than a single filter
238  /// @detailed The score used here is the effective combined energy function
239  /// that the Monte Carlo sampler is sampling over.
240  void set_boltz_rank( bool const boltz_rank );
241 
242  /// @brief show scores of last_accepted_score and "best_score" ( = flip_sign_ * lowest_score )
243  void show_scores( std::ostream & out ) const;
244 
245  /// @brief show counters of ntrial and acceptance ratio
246  void show_counters( std::ostream & out ) const;
247 
248  virtual void parse_my_tag(
249  TagPtr const tag,
250  DataMap & data,
251  Filters_map const & filters,
252  Movers_map const & movers,
253  Pose const &
254  );
255 
256  ///@brief parse "task_operations" XML option (can be employed virtually by derived Packing movers)
257  virtual void parse_task_operations(
258  TagPtr const,
259  DataMap const &,
260  Filters_map const &,
261  Movers_map const &
262 );
263 
264  void add_filter( FilterOP filter, bool const adaptive, Real const temp, String const sample_type, bool rank_by=false);
265 
266  void stopping_condition( protocols::filters::FilterOP filter );
267  protocols::filters::FilterOP stopping_condition() const;
268  void adaptive_movers( bool const a ){ adaptive_movers_ = a; }
269  bool adaptive_movers() const{ return adaptive_movers_; }
270  void adaptation_period( core::Size const a ) { adaptation_period_ = a; }
271  core::Size adaptation_period() const{ return adaptation_period_; }
272  std::string saved_accept_file_name() const;
273  void saved_accept_file_name( std::string const );
274  std::string saved_trial_number_file() const;
275  void saved_trial_number_file( std::string const );
276  core::Size load_trial_number_from_checkpoint( core::pose::Pose & );
277  void save_trial_number_to_checkpoint( core::Size const i ) const;
278  void reset_baselines( bool const r ){ reset_baselines_ = r; };
279  bool reset_baselines() const{ return reset_baselines_; }
280 private:
281  /// @brief evalute pose by ScoreFunctionOP or FilterOP
282  Real scoring( Pose & pose );
283 
284  /// @brief Executes all triggers. The order of trigger execution is undefined.
285  /// Do not assume, depend, or in any way rely on a particular ordering.
286  void fire_all_triggers(
287  Size cycle,
288  Size num_cycles,
289  const Pose& pose,
290  ScoreFunctionOP scoring);
291 
292  /// @brief max number of MC trials
294 
295  /// @brief number of designable positions
297 
298  /// @brief multiply the number_designable_ by task_scaling to get the number of trials
300 
301  /// @brief mover
303 
304  /// @brief task
306 
307  /// @brief task factory
309 
310  /// @brief Pose is evaluated by FilterOP which can do report_sm() during MC trials
312  utility::vector1< bool > adaptive_; // deflt true; are the filters adaptive or constant
313 
314  /// @brief acceptance criterion temperature
315  utility::vector1< Real > temperatures_; // temperature per filter.
318 
319  /// @brief Count the number of rejections each filter resulted in.
321 
322  /// @brief Pose is evaluated by ScoreFunctionOP during MC trials
324 
325  /// @brief setter
326  void task_factory( core::pack::task::TaskFactoryOP tf );
327 
328  /// @brief acceptance criterion temperature
329  Real temperature_;// temperature for non-filters
330 
331  /// @brief set sample type, max or min
332  /// when sample_type == max, sample pose which have higher score
333  /// when sample_type == min, sample pose which have lower score
335 
336  /// @brief if drift=false, the pose is set back to the initial pose at each MC trial
337  /// Of course, this is not MC sampling.
338  bool drift_;
339 
340  /// @brief Should we apply (and accept) the first application of the mover
341  /// regardless of boltzman criteria? (Defaults true for historical reasons.)
342  bool preapply_;
343 
344  /// @brief At the end of application, is the structure
345  /// the last accepted structure (recover_low_==false)
346  /// or is it the lowest energy structure (recover_low_=true)
348 
349  /// @brief By which filter (by index) are poses ranked by for non-Boltzman purposes
350  /// (e.g. recover_low())
352 
353  /// @brief If boltz_rank_=true, instead of ranking by a single filter, rank by the
354  /// temperature-weighted sum of all scores
356 
357  /// @brief current score
359 
360  /// @brief accepted structure
362 
363  /// @brief lowest energy structure we've seen
365 
366  /// @brief accepted structure
368 
369  /// @brief lowest energy structure we've seen
371 
372  /// @brief result of the last call to boltzmann
374 
375  /// @brief to change the sing of calculated "score"
377 
378  /// @brief diagnostics
382 
383  /// @brief Next trigger identifier to be assigned
385 
386  /// @brief Collection of function callbacks
387  boost::unordered_map<Size, GenericMonteCarloMoverTrigger> triggers_;
388  protocols::filters::FilterOP stopping_condition_; //dflt false_filter; use this to stop an MC trajectory before maxtrials_ (if filter evaluates to true)
389  utility::pointer::owning_ptr< protocols::moves::DataMapObj< bool > > mover_stopping_condition_; // dflt NULL; if the mover defined a stopping condition on the datamap then this assumes the mover's value. In this way, the Mover can tell GenericMC to stop execution, e.g., if it has iterated over all internal possibilities
390  bool adaptive_movers_; //dflt false; change the mover probabilities according to the accept rates?; only works if the mover is a ParsedProtocol type with mode=single_random
391  core::Size adaptation_period_; /// dflt max( 10, trials/10 ); only works with adaptive; how often should the run probabilities be adapted?
392  std::string saved_accept_file_name_; // dflt ""; if a file name is specified, after each accept a pdb file is dumped to disk. This is useful for checkpointing
393  std::string saved_trial_number_file_; // dflt ""; if specified checkpoints the current trial number and recovers from it
394  utility::pointer::owning_ptr< protocols::moves::DataMapObj< std::string > > mover_tag_; /// dflt NULL; this is used by the called movers to set a certain tag. If saved_accept_file_name_ is set, then at exit the tag coming from the chosen mover is written to disk as, <saved_accept_file_name>.mover_tag. To work, mover_tag_ must be exposed to the movers being called.
395  std::string user_defined_mover_name_; // dflt ""; the mover being called by GenericMC. Used to add values to the poses DataCache.
396  bool reset_baselines_; ///dflt true; reset the filters' baseline at trial=1?
397 };
398 
399 } // namespace simple_moves
400 } // namespace protocols
401 
402 #endif
403