IMP  2.3.0
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-2014 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/kernel/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:
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 succeeded in taking a step
84  /** \deprecated_at{2.2} Use get_number_of_accepted_steps() instead
85  */
86  IMPCORE_DEPRECATED_FUNCTION_DECL(2.2)
87  unsigned int get_number_of_forward_steps() const {
88  IMPCORE_DEPRECATED_FUNCTION_DEF(2.2,
89  "Use get_number_of_accepted_steps() instead.");
90  return get_number_of_accepted_steps();
91  }
92 
93  //! Return how many times the optimizer has stepped to lower score
94  unsigned int get_number_of_downward_steps() const {
95  return stat_downward_steps_taken_;
96  }
97  //! Return how many times the optimizer has stepped to higher score
98  unsigned int get_number_of_upward_steps() const {
99  return stat_upward_steps_taken_;
100  }
101  //! Get number of proposed moves
102  unsigned int get_number_of_proposed_steps() const {
103  return stat_downward_steps_taken_ + stat_upward_steps_taken_ +
104  stat_num_failures_;
105  }
106  //! Get number of accepted moves
107  unsigned int get_number_of_accepted_steps() const {
108  return stat_downward_steps_taken_ + stat_upward_steps_taken_;
109  }
110  void reset_statistics() {
111  stat_downward_steps_taken_ = 0;
112  stat_upward_steps_taken_ = 0;
113  stat_num_failures_ = 0;
114  }
115 
116  /** @} */
117 
118  //! Set the score threshold.
119  //* An optimization is terminated if the score drops below this value. */
120  void set_score_threshold(double s) { min_score_ = s; }
121 
122  //! Get the score threshold.
123  double get_score_threshold() const { return min_score_; }
124 
125  /** Computations can be accelerated by throwing out
126  the tails of the distribution of accepted moves. To
127  do this, specify a maximum acceptable difference
128  between the before and after scores.
129  */
130  void set_maximum_difference(double d) { max_difference_ = d; }
131 
132  double get_maximum_difference() const { return max_difference_; }
133  /** @name Movers
134 
135  The following methods are used to manipulate the list of Movers.
136  Each mover is called at each optimization step, giving it a chance
137  to change the current configuration.
138  @{
139  */
140  IMP_LIST_ACTION(public, Mover, Movers, mover, movers, MonteCarloMover *,
141  MonteCarloMovers, {}, {}, {});
142  /** @} */
143 
144  /** \name Incremental
145  Efficient evaluation of non-bonded list based restraints is
146  a bit tricky with incremental evaluation.
147  @{
148  */
149  /** Set whether to use incremental evaluate or evaluate all restraints
150  each time. This cannot be changed during optimization.
151  */
152  void set_incremental_scoring_function(IncrementalScoringFunction *isf);
153  bool get_use_incremental_scoring_function() const { return isf_; }
154  IncrementalScoringFunction *get_incremental_scoring_function() const {
155  return isf_;
156  }
157  /** @} */
158  protected:
159  /** Get all movable particles (those that can be moved by the current
160  movers.*/
161  kernel::ParticleIndexes get_movable_particles() const;
162  /** Note that if return best is true, this will save the current
163  state of the model. Also, if the move is accepted, the
164  optimizer states will be updated.
165  */
166  bool do_accept_or_reject_move(double score, double last,
167  double proposal_ratio);
168  bool do_accept_or_reject_move(double score, double proposal_ratio) {
169  return do_accept_or_reject_move(score, get_last_accepted_energy(),
170  proposal_ratio);
171  }
172 
173  MonteCarloMoverResult do_move();
174  //! a class that inherits from this should override this method
175  virtual void do_step();
176  //! Get the current energy
177  /** By default it just calls
178  Optimizer::get_scoring_function()->evaluate(false). However,
179  if an incremental scoring function is used, the list of moved
180  particles will be used to evaluate the score more efficiently.
181  Also, if there is a maximum allowed difference in scores
182  Optimizer::get_scoring_function()->evaluate_if_below()
183  will be called instead, allowing more efficient evaluation.
184  Classes which override this method should be similarly aware for
185  efficiency.
186 
187  The list of moved particles is passed.
188  */
189  virtual double do_evaluate(const kernel::ParticleIndexes &moved) const {
190  IMP_UNUSED(moved);
191  if (isf_) {
192  isf_->set_moved_particles(moved);
193  }
194  if (get_maximum_difference() < NO_MAX) {
195  return get_scoring_function()->evaluate_if_below(
196  false, last_energy_ + max_difference_);
197  } else {
198  return get_scoring_function()->evaluate(false);
199  }
200  }
201 
202  private:
203  double temp_;
204  double last_energy_;
205  double best_energy_;
206  double max_difference_;
207  unsigned int stat_downward_steps_taken_;
208  unsigned int stat_upward_steps_taken_;
209  unsigned int stat_num_failures_;
210  bool return_best_;
211  double min_score_;
213  ::boost::uniform_real<> rand_;
214 
216 };
217 
218 //! This variant of Monte Carlo that relaxes after each move
219 class IMPCOREEXPORT MonteCarloWithLocalOptimization : public MonteCarlo {
221  unsigned int num_local_;
222 
223  public:
224  MonteCarloWithLocalOptimization(Optimizer *opt, unsigned int steps);
225 
226  unsigned int get_number_of_steps() const { return num_local_; }
227 
228  Optimizer *get_local_optimizer() const { return opt_; }
229 
230  protected:
231  virtual void do_step() IMP_OVERRIDE;
233 };
234 
235 //! This variant of Monte Carlo uses basis hopping
236 /** Basin hopping is where, after a move, a local optimizer is used to relax
237  the model before the energy computation. However, the pre-relaxation state
238  of the model is used as the starting point for the next step. The idea
239  is that models are accepted or rejected based on the score of the nearest
240  local minima, but they can still climb the barriers in between as the model
241  is not reset to the minima after each step.
242  */
243 class IMPCOREEXPORT MonteCarloWithBasinHopping
245  public:
246  MonteCarloWithBasinHopping(Optimizer *opt, unsigned int ns);
247 
248  protected:
249  virtual void do_step() IMP_OVERRIDE;
251 };
252 
253 IMPCORE_END_NAMESPACE
254 
255 #endif /* IMPCORE_MONTE_CARLO_H */
double get_kt(double T)
Return kT for a given temperature.
A Monte Carlo optimizer.
Definition: MonteCarlo.h:45
const double NO_MAX
Use this value when you want to turn off maximum for restraint evaluation.
unsigned int get_number_of_upward_steps() const
Return how many times the optimizer has stepped to higher score.
Definition: MonteCarlo.h:98
Simple Monte Carlo optimizer.
double get_score_threshold() const
Get the score threshold.
Definition: MonteCarlo.h:123
double evaluate(bool derivatives)
Evaluate and return the score.
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:147
void set_maximum_difference(double d)
Definition: MonteCarlo.h:130
#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
A smart pointer to a reference counted object.
Definition: Pointer.h:87
IMP::base::Vector< IMP::base::Pointer< MonteCarloMover > > MonteCarloMovers
Import IMP/kernel/Optimizer.h in the namespace.
void set_score_threshold(double s)
Set the score threshold.
Definition: MonteCarlo.h:120
Import IMP/kernel/container_macros.h in the namespace.
#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:141
virtual void do_step()
a class that inherits from this should override this method
This variant of Monte Carlo uses basis hopping.
Definition: MonteCarlo.h:243
unsigned int get_number_of_downward_steps() const
Return how many times the optimizer has stepped to lower score.
Definition: MonteCarlo.h:94
#define IMP_UNUSED(variable)
The base class for movers for MC optimization.
unsigned int get_number_of_accepted_steps() const
Get number of accepted moves.
Definition: MonteCarlo.h:107
This variant of Monte Carlo that relaxes after each move.
Definition: MonteCarlo.h:219
double get_best_accepted_energy() const
If return best is on, returns the best energy found so far.
Definition: MonteCarlo.h:75
Base class for all optimizers.
virtual double do_evaluate(const kernel::ParticleIndexes &moved) const
Get the current energy.
Definition: MonteCarlo.h:189
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
ScoringFunction * get_scoring_function() const
Return the scoring function that is being used.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:170
virtual double do_optimize(unsigned int ns)=0
override this function to do actual optimization
virtual void do_step()
a class that inherits from this should override this method
Import IMP/kernel/Configuration.h in the namespace.
Functions to generate vectors.
unsigned int get_number_of_proposed_steps() const
Get number of proposed moves.
Definition: MonteCarlo.h:102
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73