IMP logo
IMP Reference Guide  2.5.0
The Integrative Modeling Platform
Optimizer.h
Go to the documentation of this file.
1 /**
2  * \file IMP/Optimizer.h \brief Base class for all optimizers.
3  *
4  * Copyright 2007-2015 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPKERNEL_OPTIMIZER_H
9 #define IMPKERNEL_OPTIMIZER_H
10 
11 #include <IMP/kernel_config.h>
12 #include "base_types.h"
13 #include <IMP/Object.h>
14 #include "utility.h"
15 #include "Model.h"
16 #include "Particle.h"
17 #include "ModelObject.h"
18 #include <IMP/Pointer.h>
19 #include "OptimizerState.h"
20 #include <IMP/Vector.h>
21 #include <limits>
22 #include <cmath>
23 
24 IMPKERNEL_BEGIN_NAMESPACE
25 
26 //! Base class for all optimizers.
27 /** An optimizer attempts to improve the current configuration of the
28  Model by moving particles around so as to lower the score.
29 
30  The Optimizer maintains a list of OptimizerStates which are
31  updated each time the conformation is changed.
32 
33  The optimizers have one key method Optimizer::optimize() which takes
34  the number of steps to perform. The optimizers can have other
35  stopping conditions as appropriate.
36 
37  A typical optimization loop proceeds by:
38  - the optimizer calls Model::evaluate() to compute the score
39  (and possibly the derivatives) of the
40  current conformation of the Model.
41  - the optimizer uses this information to update the optimizable
42  parameters of the Particles contained in the Model.
43 
44  \see Sampler
45 */
46 class IMPKERNELEXPORT Optimizer : public ModelObject {
47  mutable Floats widths_;
48  Pointer<Model> my_model_;
49  bool stop_on_good_score_;
50  mutable Pointer<ScoringFunction> cache_;
51  Pointer<ScoringFunction> scoring_function_;
52 
53  static void set_optimizer_state_optimizer(OptimizerState *os, Optimizer *o);
54 
55  protected:
56 #ifndef IMP_DOXYGEN
57  void set_is_optimizing_states(bool tf) const;
58 #endif
59  ModelObjectsTemp get_optimizer_state_inputs() const;
61  return get_optimizer_state_inputs();
62  }
63  //! don't return anything here to avoid pointless dependencies
64  virtual ModelObjectsTemp do_get_outputs() const { return ModelObjectsTemp(); }
65 
66  public:
67  Optimizer(Model *m, std::string name = "Optimizer %1%");
68 
69  //! Optimize the model for up to max_steps iterations
70  /** Optimize the model
71 
72  @param[in] max_steps The maximum number of iterations of the
73  optimizer to perform. Increasing this number will generally make
74  the optimizer spend more time searching for a solution, but
75  beyond that, the details of what changes will vary.
76 
77  @return The final score.
78  */
79  double optimize(unsigned int max_steps);
80 
81  /** Optimization can be stopped if all the thresholds in the Model are
82  satisfied. */
83  void set_stop_on_good_score(bool tf) { stop_on_good_score_ = tf; }
84  bool get_stop_on_good_score() const { return stop_on_good_score_; }
85  //! Return the score found in the last evaluate
86  double get_last_score() const { return cache_->get_last_score(); }
87 
88  //! Return the scoring function that is being used
90  if (scoring_function_) {
91  return scoring_function_;
92  } else if (cache_) {
93  return cache_;
94  } else {
95 /* Don't warn about deprecated model scoring function every time someone
96  includes Optimizer.h */
97 IMP_HELPER_MACRO_PUSH_WARNINGS
98 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)
99 IMP_GCC_PRAGMA(diagnostic ignored "-Wdeprecated-declarations")
100 #endif
101  return cache_ = get_model()->create_model_scoring_function();
102 IMP_HELPER_MACRO_POP_WARNINGS
103  }
104  }
105 
106  /** @name States
107 
108  The stored OptimizerState objects are updated each time the
109  Optimizer decides to accept a new configuration of the Model.
110  To manipulate the list of optimizer states use the methods below.
111  */
112  /**@{*/
113  IMP_LIST_ACTION(public, OptimizerState, OptimizerStates, optimizer_state,
114  optimizer_states, OptimizerState *, OptimizerStates,
115  set_optimizer_state_optimizer(obj, this);
116  , {},
117  { Optimizer::set_optimizer_state_optimizer(obj, nullptr); });
118  /**@}*/
119 
120  /** By default, the Optimizer uses the scoring function provided by
121  the model, but you can use another scoring function instead.
122  */
123  virtual void set_scoring_function(ScoringFunctionAdaptor sf);
124 
126 
127  // swig needs this at the end for some reason I don't understand
128  protected:
129  //! override this function to do actual optimization
130  virtual double do_optimize(unsigned int ns) = 0;
131  //! Update optimizer states, should be called at each successful step
132  /** Make sure the scoring function restraints are up to date before this is
133  called (eg by calling evaluate).*/
134  void update_states() const;
135 };
136 
138 
139 IMPKERNEL_END_NAMESPACE
140 
141 #endif /* IMPKERNEL_OPTIMIZER_H */
virtual ModelObjectsTemp do_get_outputs() const
don't return anything here to avoid pointless dependencies
Definition: Optimizer.h:64
Storage of a model, its restraints, constraints and particles.
virtual ModelObjectsTemp do_get_inputs() const
Definition: Optimizer.h:60
A smart pointer to a reference counted object.
Definition: Pointer.h:87
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition: base_types.h:82
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
Base class for all optimizers.
Definition: Optimizer.h:46
A class for storing lists of IMP items.
For backwards compatibility.
Single variable function.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing sets of objects.
Definition: object_macros.h:42
A nullptr-initialized pointer to an IMP Object.
Represents a scoring function on the model.
A shared base class to help in debugging and things.
Shared optimizer state that is invoked upon commitment of new coordinates.
#define IMP_REF_COUNTED_NONTRIVIAL_DESTRUCTOR(Name)
Shared optimizer state.
void set_stop_on_good_score(bool tf)
Definition: Optimizer.h:83
double get_last_score() const
Return the score found in the last evaluate.
Definition: Optimizer.h:86
#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:89