IMP logo
IMP Reference Guide  2.6.1
The Integrative Modeling Platform
MonteCarlo.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/MonteCarlo.h \brief Simple Monte Carlo optimizer.
3  *
4  * Copyright 2007-2016 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPCORE_MONTE_CARLO_H
9 #define IMPCORE_MONTE_CARLO_H
10 
11 #include <IMP/core/core_config.h>
12 #include "MonteCarloMover.h"
14 #include <IMP/Optimizer.h>
15 #include <IMP/container_macros.h>
16 #include <IMP/internal/container_helpers.h>
18 #include <IMP/Configuration.h>
19 
20 #include <boost/random/uniform_real.hpp>
21 
22 IMPCORE_BEGIN_NAMESPACE
23 
24 /** Allow code to test for the changes in MC interface.*/
25 #define IMP_CORE_HAS_MONTE_CARLO_MOVER 1
26 
27 //! A Monte Carlo optimizer.
28 /** The optimizer uses a set of Mover objects to propose steps. At
29  each sampling iteration, all Movers added to MonteCarlo are called to
30  generate a new proposed configuration.
31 
32  The movers propose some modification, which is then accepted or
33  rejected based on the Metropolis criterion. Optionally, a number
34  of local optimization steps are taken before the MonteCarlo step
35  is accepted or rejected.
36 
37  If you want to sequentially call one mover at every iteration, wrap
38  all movers into a SerialMover first, and then add the SerialMover to
39  MonteCarlo.
40 
41  By default, the lowest score state encountered is returned.
42 
43  \see Mover
44  */
45 class IMPCOREEXPORT MonteCarlo : public Optimizer {
46  public:
47  MonteCarlo(Model *m);
48 
49  protected:
50  virtual Float do_optimize(unsigned int max_steps);
52  public:
53  /** By default, the optimizer returns the lowest scoring state
54  found so far. If, instead, you wish to return the last accepted
55  state, set return best to false.
56  */
57  void set_return_best(bool tf) { return_best_ = tf; }
58 
59  /** \name kT
60  The kT value has to be on the same scale as the differences
61  in energy between good and bad states (and so the default is
62  likely to not be a good choice).
63  @{
64  */
65  void set_kt(Float t) {
66  IMP_INTERNAL_CHECK(t > 0, "Temperature must be positive");
67  temp_ = t;
68  }
69  Float get_kt() const { return temp_; }
70  /** @} */
71  //! Return the energy of the last accepted state.
72  double get_last_accepted_energy() const { return last_energy_; }
73 
74  //! If return best is on, returns the best energy found so far.
75  double get_best_accepted_energy() const {
76  IMP_USAGE_CHECK(return_best_, "Getting the best energy"
77  << " requires return best being on.");
78  return best_energy_;
79  }
80  /** \name Statistics
81  @{
82  */
83  //! Return how many times the optimizer has stepped to lower score
84  unsigned int get_number_of_downward_steps() const {
85  return stat_downward_steps_taken_;
86  }
87  //! Return how many times the optimizer has stepped to higher score
88  unsigned int get_number_of_upward_steps() const {
89  return stat_upward_steps_taken_;
90  }
91  //! Get number of proposed moves
92  unsigned int get_number_of_proposed_steps() const {
93  return stat_downward_steps_taken_ + stat_upward_steps_taken_ +
94  stat_num_failures_;
95  }
96  //! Get number of accepted moves
97  unsigned int get_number_of_accepted_steps() const {
98  return stat_downward_steps_taken_ + stat_upward_steps_taken_;
99  }
100  void reset_statistics() {
101  stat_downward_steps_taken_ = 0;
102  stat_upward_steps_taken_ = 0;
103  stat_num_failures_ = 0;
104  }
105 
106  /** @} */
107 
108  //! Set the score threshold.
109  //* An optimization is terminated if the score drops below this value. */
110  void set_score_threshold(double s) { min_score_ = s; }
111 
112  //! Get the score threshold.
113  double get_score_threshold() const { return min_score_; }
114 
115  /** Computations can be accelerated by throwing out
116  the tails of the distribution of accepted moves. To
117  do this, specify a maximum acceptable difference
118  between the before and after scores.
119  */
120  void set_maximum_difference(double d) { max_difference_ = d; }
121 
122  double get_maximum_difference() const { return max_difference_; }
123  /** @name Movers
124 
125  The following methods are used to manipulate the list of Movers.
126  Each mover is called at each optimization step, giving it a chance
127  to change the current configuration.
128  @{
129  */
130  IMP_LIST_ACTION(public, Mover, Movers, mover, movers, MonteCarloMover *,
131  MonteCarloMovers, {}, {}, {});
132  /** @} */
133 
134  /** \name Incremental
135  Efficient evaluation of non-bonded list based restraints is
136  a bit tricky with incremental evaluation.
137  @{
138  */
139  /** Set whether to use incremental evaluate or evaluate all restraints
140  each time. This cannot be changed during optimization.
141  */
142  void set_incremental_scoring_function(IncrementalScoringFunction *isf);
143  bool get_use_incremental_scoring_function() const { return isf_; }
144  IncrementalScoringFunction *get_incremental_scoring_function() const {
145  return isf_;
146  }
147  /** @} */
148  protected:
149  /** Get all movable particles (those that can be moved by the current
150  movers.*/
151  ParticleIndexes get_movable_particles() const;
152  /** Note that if return best is true, this will save the current
153  state of the model. Also, if the move is accepted, the
154  optimizer states will be updated.
155  */
156  bool do_accept_or_reject_move(double score, double last,
157  double proposal_ratio);
158  bool do_accept_or_reject_move(double score, double proposal_ratio) {
159  return do_accept_or_reject_move(score, get_last_accepted_energy(),
160  proposal_ratio);
161  }
162 
163  MonteCarloMoverResult do_move();
164  //! a class that inherits from this should override this method
165  virtual void do_step();
166  //! Get the current energy
167  /** By default it just calls
168  Optimizer::get_scoring_function()->evaluate(false). However,
169  if an incremental scoring function is used, the list of moved
170  particles will be used to evaluate the score more efficiently.
171  Also, if there is a maximum allowed difference in scores
172  Optimizer::get_scoring_function()->evaluate_if_below()
173  will be called instead, allowing more efficient evaluation.
174  Classes which override this method should be similarly aware for
175  efficiency.
176 
177  The list of moved particles is passed.
178  */
179  virtual double do_evaluate(const ParticleIndexes &moved) const {
180  IMP_UNUSED(moved);
181  if (isf_) {
182  isf_->set_moved_particles(moved);
183  }
184  if (get_maximum_difference() < NO_MAX) {
185  return get_scoring_function()->evaluate_if_below(
186  false, last_energy_ + max_difference_);
187  } else {
188  return get_scoring_function()->evaluate(false);
189  }
190  }
191 
192  private:
193  double temp_;
194  double last_energy_;
195  double best_energy_;
196  double max_difference_;
197  unsigned int stat_downward_steps_taken_;
198  unsigned int stat_upward_steps_taken_;
199  unsigned int stat_num_failures_;
200  bool return_best_;
201  double min_score_;
203  ::boost::uniform_real<> rand_;
204 
206 };
207 
208 //! This variant of Monte Carlo that relaxes after each move
209 class IMPCOREEXPORT MonteCarloWithLocalOptimization : public MonteCarlo {
211  unsigned int num_local_;
212 
213  public:
214  MonteCarloWithLocalOptimization(Optimizer *opt, unsigned int steps);
215 
216  unsigned int get_number_of_steps() const { return num_local_; }
217 
218  Optimizer *get_local_optimizer() const { return opt_; }
219 
220  protected:
221  virtual void do_step() IMP_OVERRIDE;
223 };
224 
225 //! This variant of Monte Carlo uses basis hopping
226 /** Basin hopping is where, after a move, a local optimizer is used to relax
227  the model before the energy computation. However, the pre-relaxation state
228  of the model is used as the starting point for the next step. The idea
229  is that models are accepted or rejected based on the score of the nearest
230  local minima, but they can still climb the barriers in between as the model
231  is not reset to the minima after each step.
232  */
233 class IMPCOREEXPORT MonteCarloWithBasinHopping
235  public:
236  MonteCarloWithBasinHopping(Optimizer *opt, unsigned int ns);
237 
238  protected:
239  virtual void do_step() IMP_OVERRIDE;
241 };
242 
243 IMPCORE_END_NAMESPACE
244 
245 #endif /* IMPCORE_MONTE_CARLO_H */
double get_kt(double T)
Return kT for a given temperature.
A Monte Carlo optimizer.
Definition: MonteCarlo.h:45
unsigned int get_number_of_upward_steps() const
Return how many times the optimizer has stepped to higher score.
Definition: MonteCarlo.h:88
Score model efficiently when a small number of particles are changed.
double get_score_threshold() const
Get the score threshold.
Definition: MonteCarlo.h:113
const double NO_MAX
Use this value when you want to turn off maximum for restraint evaluation.
IMP::Vector< IMP::Pointer< MonteCarloMover > > MonteCarloMovers
void set_maximum_difference(double d)
Definition: MonteCarlo.h:120
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
double get_last_accepted_energy() const
Return the energy of the last accepted state.
Definition: MonteCarlo.h:72
Base class for all optimizers.
void set_score_threshold(double s)
Set the score threshold.
Definition: MonteCarlo.h:110
Macros to define containers of objects.
A smart pointer to a reference counted object.
Definition: Pointer.h:87
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:139
virtual void do_step()
a class that inherits from this should override this method
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
This variant of Monte Carlo uses basis hopping.
Definition: MonteCarlo.h:233
unsigned int get_number_of_downward_steps() const
Return how many times the optimizer has stepped to lower score.
Definition: MonteCarlo.h:84
virtual double do_optimize(unsigned int ns)=0
override this function to do actual optimization
#define IMP_UNUSED(variable)
virtual double do_evaluate(const ParticleIndexes &moved) const
Get the current energy.
Definition: MonteCarlo.h:179
Base class for all optimizers.
Definition: Optimizer.h:46
The base class for movers for MC optimization.
unsigned int get_number_of_accepted_steps() const
Get number of accepted moves.
Definition: MonteCarlo.h:97
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:146
This variant of Monte Carlo that relaxes after each move.
Definition: MonteCarlo.h:209
double get_best_accepted_energy() const
If return best is on, returns the best energy found so far.
Definition: MonteCarlo.h:75
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
virtual void do_step()
a class that inherits from this should override this method
Store a set of configurations of the model.
Functions to generate vectors.
double evaluate(bool derivatives)
Evaluate and return the score.
unsigned int get_number_of_proposed_steps() const
Get number of proposed moves.
Definition: MonteCarlo.h:92
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
ScoringFunction * get_scoring_function() const
Return the scoring function that is being used.
Definition: Optimizer.h:90