IMP logo
IMP Reference Guide  2.19.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 #ifndef IMP_DOXYGEN
46  IMPCORE_DEPRECATED_METHOD_DECL(2.19)
47  void set_threshold(Float t) {
48  IMPCORE_DEPRECATED_METHOD_DEF(2.19, "Use set_gradient_threshold().");
49  set_gradient_threshold(t);
50  }
51 #endif
52 
53  //! Limit how far anything can change each time step
54  void set_max_change(Float t) { max_change_ = t; }
55 
56  virtual Float do_optimize(unsigned int max_steps) override;
58 
59  private:
60  typedef double NT;
61 
62  // Handle optimization failing badly
63  void failure();
64 
65  NT get_score(Vector<FloatIndex> float_indices, Vector<NT> &x,
66  Vector<NT> &dscore);
67  bool line_search(Vector<NT> &x, Vector<NT> &dx, NT &alpha,
68  const Vector<FloatIndex> &float_indices, int &ifun,
69  NT &f, NT &dg, NT &dg1, int max_steps,
70  const Vector<NT> &search,
71  const Vector<NT> &estimate);
72  Float threshold_;
73  Float max_change_;
74 
75  friend class cereal::access;
76 
77  template<class Archive> void serialize(Archive &ar) {
78  ar(cereal::base_class<AttributeOptimizer>(this), threshold_, max_change_);
79  }
80 };
81 
82 IMPCORE_END_NAMESPACE
83 
84 #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.