IMP logo
IMP Reference Guide  develop.97bab1d8fd,2021/09/21
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-2021 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 
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(Model *m, std::string name = "Optimizer %1%");
25 
26  protected:
27  /** @name Methods for getting and setting optimized attributes
28  Optimizers don't have to go through the particles themselves
29  looking for values to optimize unless they care about special
30  properties of the optimized values. Instead they can iterate
31  through the list of optimized attributes, each of which is
32  identified by a FloatIndex. With these FloatIndex objects
33  they can get and set the values and derivatives as needed.
34  */
35  //!@{
36  FloatIndexes get_optimized_attributes() const {
37  return get_model()->get_optimized_attributes();
38  }
39  void set_value(FloatIndex fi, double v) const {
40  get_model()->set_attribute(fi.get_key(), fi.get_particle(), v);
41  }
42 
43  Float get_value(FloatIndex fi) const {
44  return get_model()->get_attribute(fi.get_key(), fi.get_particle());
45  }
46 
47  Float get_derivative(FloatIndex fi) const {
48  return get_model()->get_derivative(fi.get_key(), fi.get_particle());
49  }
50 
51  //!@}
52 
53  double get_width(FloatKey k) const {
54  if (widths_.size() <= k.get_index() || widths_[k.get_index()] == 0) {
55  FloatRange w = get_model()->get_range(k);
56  double wid = static_cast<double>(w.second) - w.first;
57  widths_.resize(std::max(widths_.size(), size_t(k.get_index() + 1)), 0.0);
58  if (wid > .0001) {
59  // double nwid= std::pow(2, std::ceil(log2(wid)));
60  widths_[k.get_index()] = wid;
61  } else {
62  widths_[k.get_index()] = 1.0;
63  }
64  }
65  return widths_[k.get_index()];
66  }
67 
68  /** @name Methods to get and set scaled optimizable values
69  Certain optimizers benefit from having all the optimized values
70  scaled to vary over a similar range. These accessors use the
71  Model::get_range ranges to scale the values before returning
72  them and unscale them before setting them.
73  */
74  //{@
75  void set_scaled_value(FloatIndex fi, Float v) const {
76  double wid = get_width(fi.get_key());
77  set_value(fi, v * wid);
78  }
79 
80  double get_scaled_value(FloatIndex fi) const {
81  double uv = get_value(fi);
82  double wid = get_width(fi.get_key());
83  return uv / wid;
84  }
85 
86  double get_scaled_derivative(FloatIndex fi) const {
87  double uv = get_derivative(fi);
88  double wid = get_width(fi.get_key());
89  return uv * wid;
90  }
91 
92  //! Clear the cache of range information. Do this at the start of
93  // optimization
94  void clear_range_cache() { widths_.clear(); }
95  //!@}
96  private:
97  mutable Floats widths_;
98 };
99 
101 
102 IMPKERNEL_END_NAMESPACE
103 
104 #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:31
Base class for all optimizers.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:73
Base class for all optimizers.
Definition: Optimizer.h:46
#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:20
void clear_range_cache()
Clear the cache of range information. Do this at the start of.