IMP  2.1.0
The Integrative Modeling Platform
kernel/AttributeOptimizer.h
Go to the documentation of this file.
1 /**
2  * \file IMP/kernel/AttributeOptimizer.h
3  * \brief Base class for all optimizers.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_ATTRIBUTE_OPTIMIZER_H
10 #define IMPKERNEL_ATTRIBUTE_OPTIMIZER_H
11 
12 #include <IMP/kernel/kernel_config.h>
13 #include "Optimizer.h"
14 
15 IMPKERNEL_BEGIN_NAMESPACE
16 
17 //! Base class for optimizers that act on individual attributes.
18 /** AttributeOptimizers optimize the collection of optimized
19  attributes (see Model::set_is_optimized()) in contrast to,
20  say molecular dynamics where the fundamental entity is a Particle.
21 */
22 class IMPKERNELEXPORT AttributeOptimizer : public Optimizer {
23  public:
24  AttributeOptimizer(kernel::Model *m, std::string name = "Optimizer %1%");
25 
26  /** \deprecated_at{2.1} Use the constructor with a Model and a name.*/
27  IMPKERNEL_DEPRECATED_METHOD_DECL(2.1)
29 
30  protected:
31 
32  /** @name Methods for getting and setting optimized attributes
33  Optimizers don't have to go through the particles themselves
34  looking for values to optimize unless they care about special
35  properties of the optimized values. Instead they can iterate
36  through the list of optimized attributes, each of which is
37  identified by a FloatIndex. With these FloatIndex objects
38  they can get and set the values and derivatives as needed.
39  */
40  //!@{
42  return get_model()->get_optimized_attributes();
43  }
44  void set_value(FloatIndex fi, double v) const {
45  get_model()->set_attribute(fi.get_key(), fi.get_particle(), v);
46  }
47 
48  Float get_value(FloatIndex fi) const {
49  return get_model()->get_attribute(fi.get_key(), fi.get_particle());
50  }
51 
52  Float get_derivative(FloatIndex fi) const {
53  return get_model()->get_derivative(fi.get_key(), fi.get_particle());
54  }
55 
56  //!@}
57 
58  double get_width(FloatKey k) const {
59  if (widths_.size() <= k.get_index() || widths_[k.get_index()] == 0) {
60  FloatRange w = get_model()->get_range(k);
61  double wid = static_cast<double>(w.second) - w.first;
62  widths_.resize(std::max(widths_.size(), size_t(k.get_index() + 1)), 0.0);
63  if (wid > .0001) {
64  // double nwid= std::pow(2, std::ceil(log2(wid)));
65  widths_[k.get_index()] = wid;
66  } else {
67  widths_[k.get_index()] = 1.0;
68  }
69  }
70  return widths_[k.get_index()];
71  }
72 
73  /** @name Methods to get and set scaled optimizable values
74  Certain optimizers benefit from having all the optimized values
75  scaled to vary over a similar range. These accessors use the
76  Model::get_range ranges to scale the values before returning
77  them and unscale them before setting them.
78  */
79  //{@
80  void set_scaled_value(FloatIndex fi, Float v) const {
81  double wid = get_width(fi.get_key());
82  set_value(fi, v * wid);
83  }
84 
85  double get_scaled_value(FloatIndex fi) const {
86  double uv = get_value(fi);
87  double wid = get_width(fi.get_key());
88  return uv / wid;
89  }
90 
91  double get_scaled_derivative(FloatIndex fi) const {
92  double uv = get_derivative(fi);
93  double wid = get_width(fi.get_key());
94  return uv * wid;
95  }
96 
97  //! Clear the cache of range information. Do this at the start of
98  // optimization
99  void clear_range_cache() { widths_.clear(); }
100  //!@}
101  private:
102  mutable Floats widths_;
103 };
104 
106 
107 IMPKERNEL_END_NAMESPACE
108 
109 #endif /* IMPKERNEL_ATTRIBUTE_OPTIMIZER_H */
double get_scaled_value(FloatIndex fi) const
Float get_value(FloatIndex fi) const
FloatIndexes get_optimized_attributes() const
void set_scaled_value(FloatIndex fi, Float v) const
std::pair< Float, Float > FloatRange
A pair representing the allowed range for a Float attribute.
Definition: base/types.h:31
void set_value(FloatIndex fi, double v) const
Float get_derivative(FloatIndex fi) const
Base class for all optimizers.
void clear_range_cache()
Clear the cache of range information. Do this at the start of.
Base class for all optimizers.
Base class for optimizers that act on individual attributes.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing sets of objects.
double get_scaled_derivative(FloatIndex fi) const
double get_width(FloatKey k) const
double Float
Basic floating-point value (could be float, double...)
Definition: base/types.h:20
Class for storing model, its restraints, constraints, and particles.