IMP logo
IMP Reference Guide  2.20.0
The Integrative Modeling Platform
core/ConjugateGradients.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/ConjugateGradients.h
3  * \brief Simple conjugate gradients optimizer.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPCORE_CONJUGATE_GRADIENTS_H
10 #define IMPCORE_CONJUGATE_GRADIENTS_H
11 
12 #include <IMP/core/core_config.h>
13 
14 #include <IMP/AttributeOptimizer.h>
15 #include <cereal/access.hpp>
16 #include <cereal/types/base_class.hpp>
17 
18 IMPCORE_BEGIN_NAMESPACE
19 
20 //! Simple conjugate gradients optimizer.
21 /** Algorithm is as per Shanno and Phua, ACM Transactions On Mathematical
22  Software 6 (December 1980), 618-622
23 
24  Conjugate gradients optimization is sensitive to the scales of the
25  derivatives of the various attributes being optimized. By default,
26  the scales are estimated from the range of values found for the attribute
27  upon initialization. These estimates can be viewed either by calling
28  Model::get_range(my_float_key) or by turning on TERSE logging and
29  looking at logged messages. If this estimate does not accurately reflect
30  the scale, then you can use Model::set_range to set a more accurate
31  range for the parameters.
32 
33  \note Currently, rigid bodies are not handled (and will not be moved
34  by this optimizer).
35 */
36 class IMPCOREEXPORT ConjugateGradients : public AttributeOptimizer {
37  public:
39  std::string name = "ConjugateGradients%1%");
41 
42  //! Set the threshold for the minimum gradient
43  void set_gradient_threshold(Float t) { threshold_ = t; }
44 
45  //! Limit how far anything can change each time step
46  void set_max_change(Float t) { max_change_ = t; }
47 
48  virtual Float do_optimize(unsigned int max_steps) override;
50 
51  private:
52  typedef double NT;
53 
54  // Handle optimization failing badly
55  void failure();
56 
57  NT get_score(Vector<FloatIndex> float_indices, Vector<NT> &x,
58  Vector<NT> &dscore);
59  bool line_search(Vector<NT> &x, Vector<NT> &dx, NT &alpha,
60  const Vector<FloatIndex> &float_indices, int &ifun,
61  NT &f, NT &dg, NT &dg1, int max_steps,
62  const Vector<NT> &search,
63  const Vector<NT> &estimate);
64  Float threshold_;
65  Float max_change_;
66 
67  friend class cereal::access;
68 
69  template<class Archive> void serialize(Archive &ar) {
70  ar(cereal::base_class<AttributeOptimizer>(this), threshold_, max_change_);
71  }
72 };
73 
74 IMPCORE_END_NAMESPACE
75 
76 #endif /* IMPCORE_CONJUGATE_GRADIENTS_H */
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Simple conjugate gradients optimizer.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
virtual double do_optimize(unsigned int ns)=0
override this function to do actual optimization
Base class for optimizers that act on individual attributes.
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
Base class for optimizers that act on individual attributes.
void set_gradient_threshold(Float t)
Set the threshold for the minimum gradient.
void set_max_change(Float t)
Limit how far anything can change each time step.