IMP  2.0.1
The Integrative Modeling Platform
MonteCarloMover.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/MonteCarloMover.h
3  * \brief The base class for movers for MC optimization.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPCORE_MONTE_CARLO_MOVER_H
10 #define IMPCORE_MONTE_CARLO_MOVER_H
11 
12 #include <IMP/core/core_config.h>
13 
14 #include <IMP/ModelObject.h>
15 #include <IMP/Model.h>
16 #include <IMP/particle_index.h>
17 #include <IMP/base/tuple_macros.h>
18 
19 IMPCORE_BEGIN_NAMESPACE
20 
21 /** The MonteCarloMoverResult is used as the return value for the
22  MonteCarloMover::propose() function. The values are the list of
23  particle (indexes) moved and the ratio between the probability of
24  the backwards move and the probability of the forwards move (for
25  many or most move sets this is 1.0).
26 */
27 IMP_NAMED_TUPLE_2(MonteCarloMoverResult, MonteCarloMoverResults,
28  ParticleIndexes, moved_particles,
29  double, proposal_ratio,);
30 
31 //! A base class for classes which perturb particles.
32 /** Mover objects propose a move, which can then be either accepted or rejected
33  based on some criteria. For example, in a Monte-Carlo evaluation scheme.
34 
35  The output particles (ModelObject::do_get_outputs()) are assummed to be
36  equal to the inputs (ModelObject::do_get_inputs()).
37  */
38 class IMPCOREEXPORT MonteCarloMover: public ModelObject
39 {
40  unsigned int num_proposed_;
41  unsigned int num_rejected_;
42 public:
43  MonteCarloMover(Model *m, std::string name);
44 
45  //! propose a modification
46  /** The method should return the list of all particles that were
47  actually moved and the ratio between the backward move probability
48  and the forward move probability (for Metropolis-Hastings moves).
49  Just return 1.0 for this value if you are not sure.
50  */
53  set_was_used(true);
54  ++num_proposed_;
55  return do_propose();
56  }
57 
58  //! Roll back any changes made to the Particles
59  void reject() {
61  ++num_rejected_;
62  do_reject();
63  }
64 
65  //! Roll back any changes made to the Particles
66  void accept() {
68  do_accept();
69  }
70 
71  /** \name Statistics
72  Movers keep track of some statistics as they are used.
73  @{
74  */
75  unsigned int get_number_of_proposed() const {
76  return num_proposed_;
77  }
78  unsigned int get_number_of_accepted() const {
79  return num_proposed_ - num_rejected_;
80  }
81  void reset_statistics() {
82  num_proposed_ = 0;
83  num_rejected_ = 0;
84  }
85  /** @} */
86 protected:
87  //! Implement propose_move()
88  virtual MonteCarloMoverResult do_propose() = 0;
89  //! Implement reset_proposed_move()
90  virtual void do_reject() = 0;
91  //! Implement accept_proposed_move(), default impl is empty
92  virtual void do_accept() {}
93 
94  virtual ModelObjectsTemp do_get_outputs() const IMP_OVERRIDE {
95  return get_inputs();
96  }
97 
98  virtual void do_update_dependencies() IMP_OVERRIDE {}
99 };
100 
102 
103 IMPCORE_END_NAMESPACE
104 
105 #endif /* IMPCORE_MONTE_CARLO_MOVER_H */