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