IMP  2.3.0
The Integrative Modeling Platform
kernel/OptimizerState.h
Go to the documentation of this file.
1 /**
2  * \file IMP/kernel/OptimizerState.h \brief Shared optimizer state.
3  *
4  * Copyright 2007-2014 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPKERNEL_OPTIMIZER_STATE_H
9 #define IMPKERNEL_OPTIMIZER_STATE_H
10 
11 #include <IMP/kernel/kernel_config.h>
12 #include "ModelObject.h"
13 #include <IMP/base/WeakPointer.h>
14 #include <IMP/base/Object.h>
15 
16 #include <iostream>
17 
18 IMPKERNEL_BEGIN_NAMESPACE
19 
20 class Optimizer;
21 
22 //! Shared optimizer state that is invoked upon commitment of new coordinates.
23 /** An OptimizerState update() method is called every time that an
24  owning Optimizer commits to a new set of coordinates. The update()
25  method, in turn, invokes do_update(), which can be overridden by
26  inheriting classes.
27 
28  @note An OptimizerState may have periodicity by its set_period() method
29 
30  @note An OptimizerState is added to an Optimizer object by calling
31  Optimizer::add_optimizer_state().
32 
33  @note Optimizer states may change the values of particle
34  attributes. However, changes to whether an attribute is optimized
35  or not may not be picked up by the Optimizer until the next call
36  to optimize.
37 
38  \note When logging is VERBOSE, state should print enough information
39  in evaluate to reproduce the the entire flow of data in update. When
40  logging is TERSE the restraint should print out only a constant number
41  of lines per update call.
42  */
43 class IMPKERNELEXPORT OptimizerState : public ModelObject {
44  friend class Optimizer;
45  unsigned int period_, call_number_, update_number_;
46 
47  void set_optimizer(Optimizer* optimizer);
48 
49  public:
50  /** constructs an optimizer state whose update() method is invoked
51  every time that a set of model coordinates is committed
52  by an optimizer.
53 
54  @param m the model to which this optimizer state is associated
55  @param name the name of the object
56  @note An optimizer state may become periodic via its set_period()
57  method.
58  */
59  OptimizerState(kernel::Model* m, std::string name);
60 
61  //! Called when the Optimizer accepts a new conformation
62  /**
63  This method is called by owning optimizers every time they commit.
64  However, if set_period(p) was invoked, it calls do_update() only
65  every p times it is called (by any optimizer).
66 
67  @note Overriding this method is deprecated, override do_update() instead.
68  */
69  virtual void update();
70 
71  /** Called with true at the beginning of an optimizing run and with
72  false at the end.
73 
74  \note Do not override, override do_set_is_optimizing() instead.
75  */
76  virtual void set_is_optimizing(bool);
77 
78  Optimizer* get_optimizer() const {
79  IMP_INTERNAL_CHECK(optimizer_,
80  "Must call set_optimizer before get_optimizer on state");
81  return optimizer_.get();
82  }
83 
84  /**
85  Causes update() invoke do_update() only every p calls to update()
86  instead of the default p=1. Note that this periodicity is shared
87  by all optimizers that own this OptimizerState object.
88 
89  @param p periodicity
90  */
91  void set_period(unsigned int p);
92 
93  /**
94  @return the periodicity of the optimizer (how many calls to update()
95  are required to invoke do_update()
96  */
97  unsigned int get_period() const { return period_; }
98 
99  /** Reset the phase to 0 and set the call number to 0 too.*/
100  virtual void reset();
101 
102  /** Force the optimizer state to perform its action now, ignoring the
103  phase.
104  */
105  void update_always();
106 
107  //! Return the number of times update has been called
108  unsigned int get_number_of_updates() const { return update_number_; }
109 
110  //! Set the counter
111  void set_number_of_updates(unsigned int n) { update_number_ = n; }
112 
114 
115  protected:
116  /** This method is called every get_period() update calls. The number of
117  times this method has been called since the last reset or start of the
118  optimization run is passed.*/
119  virtual void do_update(unsigned int) { update(); }
120 
121  virtual void do_set_is_optimizing(bool) {}
122 
124  return ModelObjectsTemp();
125  }
127  return ModelObjectsTemp();
128  }
129 
130  private:
132  bool is_optimizing_;
133 };
134 
135 IMPKERNEL_END_NAMESPACE
136 
137 #endif /* IMPKERNEL_OPTIMIZER_STATE_H */
A weak pointer to an Object or RefCountedObject.
Definition: WeakPointer.h:32
IMP::base::Vector< IMP::base::WeakPointer< kernel::ModelObject > > ModelObjectsTemp
virtual void do_update(unsigned int)
unsigned int get_period() const
#define IMP_FINAL
Have the compiler report an error if anything overrides this method.
Shared optimizer state that is invoked upon commitment of new coordinates.
IMP::kernel::Optimizer Optimizer
#define IMP_REF_COUNTED_DESTRUCTOR(Name)
Ref counted objects should have private destructors.
virtual ModelObjectsTemp do_get_inputs() const
#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
Single variable function.
A nullptr-initialized pointer to an Object.
Base class for all optimizers.
unsigned int get_number_of_updates() const
Return the number of times update has been called.
A shared base class to help in debugging and things.
virtual ModelObjectsTemp do_get_outputs() const
IMP::kernel::OptimizerState OptimizerState
#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
void set_number_of_updates(unsigned int n)
Set the counter.