Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MonteCarlo.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
11 /// @brief
12 /// @author
13 
14 
15 #ifndef INCLUDED_protocols_moves_MonteCarlo_hh
16 #define INCLUDED_protocols_moves_MonteCarlo_hh
17 
18 
19 // type headers
20 #include <core/types.hh>
21 
22 // unit headers
26 // AUTO-REMOVED #include <protocols/moves/MonteCarloExceptionConverge.hh>
27 
28 // package headers
29 #include <core/pose/Pose.fwd.hh>
31 
32 // utility headers
33 #include <utility/pointer/ReferenceCount.hh>
34 // #include "utility/basic_sys_util.h"
35 // AUTO-REMOVED #include <utility/vector1.hh>
36 
37 // C++ headers
38 #include <map>
39 // AUTO-REMOVED #include <string>
40 
42 #include <utility/vector1.hh>
43 #include <string>
44 
45 #ifdef WIN32
47 #endif
48 
49 
50 // Forward declarations
51 
52 namespace protocols {
53 namespace moves {
54 
55 /// @brief This object is responsible for all of the major functions needed in
56 /// a Monte Carlo simulation. Its main purpose is to apply the Metropolis
57 /// Criterion on a pose, based on a ScoreFunction, temperature, and the
58 /// previously accepted pose. It stores the lowest-energy pose ecountered,
59 /// the last-accepted pose in the simulation, and various other statistics.
60 ///
61 ///
62 /// Output Methods:
63 /// MonteCarlo.show_counters()
64 /// MonteCarlo.show_scores()
65 /// MonteCarlo.show_state()
66 /// Common Methods:
67 /// MonteCarlo.last_accepted_score
68 /// MonteCarlo.last_accepted_pose
69 /// MonteCarlo.lowest_score
70 /// MonteCarlo.lowest_score_pose
71 /// MonteCarlo.score_function
72 /// MonteCarlo.set_temperature
73 /// MonteCarlo.temperature
75 public:
82  typedef core::Real Real;
83 
84 
85 public:
86 
87  /// @brief Copy constructor
88  MonteCarlo( MonteCarlo const & );
89 
90  /// @brief Constructs a useable MonteCarlo object
91  ///
92  /// mc = MonteCarlo( init_pose , scorefxn , temp )
93  ///
94  /// Pose init_pose /manipulated during the simulation
95  /// ScoreFunction scorefxn /evaluates pose scores
96  /// Real (float) temp /used in the Metropolis Criterion
97  MonteCarlo(
98  Pose const & init_pose, // PoseCOP init_pose,
99  ScoreFunction const & scorefxn, // ScoreFunctionCOP scorefxn,
100  Real const temperature
101  );
102 
103 
104  /// @brief Constructor without Pose -- call reset(pose) before first use
105  MonteCarlo(
106  ScoreFunction const & scorefxn, // ScoreFunctionCOP scorefxn,
107  Real const temperature
108  );
109 
110  /// @brief Empty destructor in C++ file to reduce number of necessary includes
111  virtual ~MonteCarlo();
112 
113 
114  /// @brief Resets the ScoreFunction
115  void
117  Pose const & init_pose,
118  ScoreFunction const & scorefxn
119  );
120 
121  /// @brief Sets the temperature value used in the Metropolis Criterion to <temp>
122  ///
123  /// example(s):
124  /// mc.set_temperature( temp )
125  /// See also:
126  /// MonteCarlo
127  /// MonteCarlo.temperature
128  /// MonteCarlo.show_state
129  void
130  set_temperature( Real const temp );
131 
132 
133  /// @brief Returns the temperature value used in the Metropolis Criterion
134  ///
135  /// example(s):
136  /// mc.temperature()
137  /// See also:
138  /// MonteCarlo
139  /// MonteCarlo.set_temperature
140  /// MonteCarlo.show_state
141  Real
142  temperature() const {
143  return temperature_;
144  }
145 
146  /// @brief Sets autotemp to quench_temp
147  /// example(s):
148  /// See also:
149  /// MonteCarlo
150  /// MonteCarlo.autotemp
151  /// MonteCarlo.show_state
152  void
153  set_autotemp(
154  bool const setting,
155  core::Real const quench_temp
156  );
157 
158 
159  /// @brief Applies the Metropolis Criterion on pose based on
160  /// the ScoreFunction, temperature, and the last accepted
161  /// pose. This method evaluates the change in score, compares
162  /// the trial pose to the last accepted pose, and updates the
163  /// pose structure and simulation statistics appropriately
164  ///
165  /// example(s):
166  /// mc.boltzmann( pose )
167  /// See also:
168  /// MonteCarlo
169  /// MonteCarlo.last_accepted_score
170  /// MonteCarlo.lowest_score
171  virtual bool
172  boltzmann(
173  Pose & pose,//PoseOP pose,
174  std::string const & move_type = "unk",
175  core::Real const proposal_density_ratio = 1,
176  core::Real const inner_score_temperature_delta = 0
177  );
178 
179 
180  /// @brief Applies the Metropolis Criterion on the inputted
181  /// pose based on the supplied score delta
182  ///
183  /// example(s):
184  ///
185  /// See also:
186  /// MonteCarlo
187  virtual bool
188  boltzmann(
189  core::Real score_delta,
190  std::string const & move_type = "unk",
191  core::Real const proposal_density_ratio = 1
192  );
193 
194 
195  /// @brief Sets lowest score pose and last accepted pose to
196  /// the score of <pose>
197  /// @note (does not reset counters)
198  ///
199  /// example(s):
200  /// mc.reset(pose)
201  /// See also:
202  /// MonteCarlo
203  /// MonteCarlo.last_accepted_pose
204  /// MonteCarlo.last_accepted_score
205  /// MonteCarlo.lowest_score
206  /// MonteCarlo.lowest_scored_pose
207  void
208  reset( Pose const & pose );
209 
210 
211 
212  /// @brief Sets the last accepted pose to the score of <pose>
213  /// @note (does not reset counters)
214  ///
215  /// example(s):
216  /// mc.reset_last_accepted( pose )
217  /// See also:
218  /// MonteCarlo
219  /// MonteCarlo.reset
220  void
221  reset_last_accepted( Pose const & pose );
222 
223 
224  /// @brief Returns the last accepted pose
225  ///
226  /// example(s):
227  /// mc.last_accepted_pose()
228  /// See also:
229  /// MonteCarlo
230  /// MonteCarlo.last_accept
231  /// MonteCarlo.last_accepted_score
232  Pose const &
234  {
235  return *last_accepted_pose_;
236  }
237 
238 
239  /// @brief Returns the lowest score pose encountered
240  ///
241  /// example(s):
242  /// mc.lowest_score_pose()
243  /// See also:
244  /// MonteCarlo
245  /// MonteCarlo.last_accepted_pose
246  /// MonteCarlo.lowest_score
247  Pose const &
249  {
250  return *lowest_score_pose_;
251  }
252 
253 
254  /// @brief Sets the last accepted pose to the score of <pose>
255  /// @note (for recovering from a checkpoint)
256  ///
257  /// example(s):
258  /// mc.set_last_accepted_pose( pose )
259  /// See also:
260  /// MonteCarlo
261  /// MonteCarlo.last_accept
262  /// MonteCarlo.last_accepted_pose
263  /// MonteCarlo.last_accepted_score
264  void set_last_accepted_pose( Pose const & pose );
265 
266 
267  /// @brief Sets the lowest score pose to the score of <pose>
268  /// @note (for recovering from a checkpoint)
269  ///
270  /// example(s):
271  /// mc.set_lowest_score_pose(_pose_)
272  /// See also:
273  /// MonteCarlo
274  /// MonteCarlo.lowest_score
275  /// MonteCarlo.lowest_score_pose
276  void set_lowest_score_pose( Pose const & pose );
277 
278 
279  /// @brief attach observer to last accepted conformation
280  /// @tparam ConformationObserver any class implementing <tt> void attach_to( Conformation & ) </tt>
281  template< typename ConformationObserver >
282  void
283  attach_observer_to_last_accepted_conformation( ConformationObserver & obs );
284 
285 
286  /// @brief attach observer to lowest score conformation
287  /// @tparam ConformationObserver any class implementing <tt> void attach_to( Conformation & ) </tt>
288  template< typename ConformationObserver >
289  void
290  attach_observer_to_lowest_score_conformation( ConformationObserver & obs );
291 
292 
293  /// @brief attach observer to last accepted pose
294  /// @tparam PoseObserver any class implementing <tt> void attach_to( Pose & ) </tt>
295  template< typename PoseObserver >
296  void
297  attach_observer_to_last_accepted_pose( PoseObserver & obs );
298 
299 
300  /// @brief attach observer to lowest score pose
301  /// @tparam PoseObserver any class implementing <tt> void attach_to( Pose & ) </tt>
302  template< typename PoseObserver >
303  void
304  attach_observer_to_lowest_score_pose( PoseObserver & obs );
305 
306 
307  /// @brief Sets the input <pose> and last accepted pose to
308  /// the lowest score pose
309  ///
310  /// example(s):
311  /// mc.recover_low( pose )
312  /// See also:
313  /// MonteCarlo
314  /// MonteCarlo.last_accept
315  /// MonteCarlo.last_accepted_pose
316  /// MonteCarlo.last_accepted_score
317  /// MonteCarlo.lowest_score
318  /// MonteCarlo.lowest_score_pose
319  void
320  recover_low( Pose & pose );
321 
322 
323  /// @brief Sets the ScoreFunction to <scorefxn> , re-scores
324  /// last accepted pose and lowest score pose
325  ///
326  /// example(s):
327  /// mc.score_function( scorefxn )
328  /// See also:
329  /// MonteCarlo
330  /// MonteCarlo.boltzmann
331  /// MonteCarlo.set_temperature
332  /// MonteCarlo.temperature
333  /// ScoreFunction
334  /// create_score_function
335  void
336  score_function( ScoreFunction const & scorefxn ); // ScoreFunctionCOP scorefxn )
337 
338 
339  /// @brief Returns the MonteCarlo ScoreFunction
340  ///
341  /// example(s):
342  /// mc.score_function()
343  /// mc.score_function()( pose )
344  /// See also:
345  /// MonteCarlo
346  /// MonteCarlo.boltzmann
347  /// MonteCarlo.set_temperature
348  /// MonteCarlo.temperature
349  /// ScoreFunction
350  /// create_score_function
351  ScoreFunction const & score_function() const;
352 
353 
354  /// @brief Displays the last accepted score and the lowest score
355  ///
356  /// example(s):
357  /// mc.show_scores()
358  /// Output as:
359  /// protocols.moves.MonteCarlo: MonteCarlo:: last_accepted_score,lowest_score: X Y
360  /// See also:
361  /// MonteCarlo
362  /// MonteCarlo.last_accepted_score
363  /// MonteCarlo.lowest_score
364  /// MonteCarlo.show_counters
365  /// MonteCarlo.show_state
366  void show_scores() const;
367 
368 
369  /// @brief Resets the mover counters
370  ///
371  /// example(s):
372  /// mc.reset_counters()
373  /// See alse:
374  /// MonteCarlo
375  /// MonteCarlo.show_counters
376  void reset_counters();
377 
378 
379  /// @brief Displays the entire MonteCarlo state
380  /// temperature, scores, annealing settings,
381  /// move statistics, move counters (show_counters)
382  ///
383  /// example(s):
384  /// mc.show_state()
385  /// Output as:
386  /// protocols.moves.MonteCarlo: MC: t l1 l2 las lws la au qu mca
387  /// t= temperature
388  /// l1= (*score_function_)(*last_accepted_pose_)
389  /// l2= (*score_function_)(*lowest_score_pose_)
390  /// las= last accepted score
391  /// lws= lowest score
392  /// la= last_accept_
393  /// au= autotemp_
394  /// qu= quench_temp_
395  /// mca= mc_accepted_
396  /// See also:
397  /// MonteCarlo
398  /// MonteCarlo.show_counters
399  /// MonteCarlo.show_scores
400  /// MonteCarlo.last_accepted_score
401  /// MonteCarlo.lowest_score
402  /// MonteCarlo.temperature
403  void show_state() const;
404 
405 
406  /// @brief Displays the number of trials performed, fraction
407  /// of trial moves accepted, and the average energy drop per
408  /// accepted trial by mover types applied (unknown movers or
409  /// perturbations are listed as "unktrials")
410  ///
411  /// example(s):
412  /// mc.show_counters()
413  /// Output as:
414  /// protocols.moves.MonteCarlo: unk trials= X; accepts= Y; energy_drop/trial= Z
415  /// See also:
416  /// MonteCarlo
417  /// MonteCarlo.show_scores
418  /// MonteCarlo.show_state
419  void show_counters() const;
420 
421 
422  /// @brief Returns the total number of trials since the last reset
423  /// @note: MonteCarlo.boltzmann(pose) updates the number of trials
424  ///
425  /// example(s):
426  /// mc.total_trials()
427  /// See also:
428  /// MonteCarlo
429  /// MonteCarlo.last_accept
430  /// MonteCarlo.show_counters
431  /// MonteCarlo.show_state
432  Size total_trials() const;
433 
434 
435  /// @brief Returns the score value of the last accepted pose
436  ///
437  /// example(s):
438  /// mc.last_accepted_score()
439  /// See also:
440  /// MonteCarlo
441  /// MonteCarlo.last_accept
442  /// MonteCarlo.last_accepted_pose
443  /// MonteCarlo.show_counters
444  /// MonteCarlo.show_scores
445  /// MonteCarlo.show_state
446  Real last_accepted_score() const;
447 
448 
449  /// @brief Returns the score value of the lowest score pose encountered
450  ///
451  /// example(s):
452  /// mc.lowest_score()
453  /// See also:
454  /// MonteCarlo
455  /// MonteCarlo.lowest_score_pose
456  /// MonteCarlo.show_counters
457  /// MonteCarlo.show_scores
458  /// MonteCarlo.show_state
459  Real lowest_score() const;
460 
461 
462  /// @brief Returns mc_accepted, informative of the last move applied
463  ///
464  /// Note: Returns true for an accept, false otherwise
465  /// 3 = accepted:score beat low score and last_accepted score
466  /// 2 = accepted:score beat last_accepted score
467  /// 1 = thermally accepted: score worse than last_accepted score
468  /// 0 = not accepted
469  /// example(s):
470  /// mc.mc_accepted()
471  /// See also:
472  /// MonteCarlo
473  /// MonteCarlo.show_state
474  MCA mc_accepted() const;
475 
476 
477  /// @brief Removes last accepted pose and lowest score pose
478  ///
479  /// example(s):
480  /// mc.clear_poses()
481  /// See also:
482  /// MonteCarlo
483  /// MonteCarlo.last_accepted_pose
484  /// MonteCarlo.lowest_score_pose
485  /// MonteCarlo.recover_low
486  /// MonteCarlo.reset
487  /// MonteCarlo.set_last_accepted_pose
488  /// MonteCarlo.set_lowest_score_pose
489  void clear_poses();// remove last_accepted and lowest_score
490 
491 
492  /// no brief for now
493  void set_update_boinc( bool setting ){ update_boinc_ = setting; }
494 
495 
497 
498  /// @brief Returns the number of trials since last acceptance
499  ///
500  /// example(s):
501  /// mc.last_accept()
502  /// See also:
503  /// MonteCarlo
504  /// MonteCarlo.show_counters
505  /// MonteCarlo.last_accepted_pose
506  /// MonteCarlo.last_accepted_score
508  return last_accept_;
509  }
510 
511 
512  /// no brief for now
514  return heat_after_cycles_;
515  }
516 
517 
518  /// no brief for now
520  heat_after_cycles_ = setting;
521  }
522 
523 
524  /// no brief for now
526  /////////////////////////////////////////////////////////////////////////////
527  // private methods
528 private:
529 
530  /// @brief for managing the temperature, if we need to do so
531  void
532  autotemp_reject();
533 
534  void
535  autotemp_accept();
536 
537  void
538  evaluate_convergence_checks( core::pose::Pose const& pose, bool reject, bool final );
539 
540 public:
541  Size
542  check_frequency() const {
543  return check_frequency_;
544  }
545 
546 private:
547  /// unimplemented -- do not use
548  MonteCarlo const & operator = ( MonteCarlo const & ); // assignment operator -- do not use.
549 
550  /////////////////////////////////////////////////////////////////////////////
551  // data
552 private:
553 
554  /// @brief Latest accepted pose
556 
557  /// @brief Lowest score pose encountered
559 
560  /// @brief Acceptance criterion temperature
562 
563  /// @brief Internal scoring function
565 
566  /// @brief For abinitio-style increasing the temperature after a large number of rejects:
567  bool autotemp_;
570 
571  /// @brief Result of the last call to boltzmann
573 
574  /// @brief diagnostics
576 
578 
582 
584 
588 };
589 
590 // for Python bindings
591 std::ostream & operator << ( std::ostream & os, MonteCarlo const & mc);
592 
593 } // moves
594 } // rosetta
595 
596 #endif