IMP logo
IMP Reference Guide  2.11.1
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-2019 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 
16 IMPCORE_BEGIN_NAMESPACE
17 
18 //! Simple conjugate gradients optimizer.
19 /** Algorithm is as per Shanno and Phua, ACM Transactions On Mathematical
20  Software 6 (December 1980), 618-622
21 
22  Conjugate gradients optimization is sensitive to the scales of the
23  derivatives of the various attributes being optimized. By default,
24  the scales are estimated from the range of values found for the attribute
25  upon initialization. These estimates can be viewed either by calling
26  Model::get_range(my_float_key) or by turning on TERSE logging and
27  looking at logged messages. If this estimate does not accurately reflect
28  the scale, then you can use Model::set_range to set a more accurate
29  range for the parameters.
30 
31  \note Currently, rigid bodies are not handled (and will not be moved
32  by this optimizer).
33 */
34 class IMPCOREEXPORT ConjugateGradients : public AttributeOptimizer {
35  public:
37  std::string name = "ConjugateGradients%1%");
38 
39  //! Set the threshold for the minimum gradient
40  void set_gradient_threshold(Float t) { threshold_ = t; }
41 
42 #ifndef IMP_DOXYGEN
43  void set_threshold(Float t) { set_gradient_threshold(t); }
44 #endif
45 
46  //! Limit how far anything can change each time step
47  void set_max_change(Float t) { max_change_ = t; }
48 
49  virtual Float do_optimize(unsigned int max_steps) IMP_OVERRIDE;
51 
52  private:
53  typedef double NT;
54 
55  // Handle optimization failing badly
56  void failure();
57 
58  NT get_score(Vector<FloatIndex> float_indices, Vector<NT> &x,
59  Vector<NT> &dscore);
60  bool line_search(Vector<NT> &x, Vector<NT> &dx, NT &alpha,
61  const Vector<FloatIndex> &float_indices, int &ifun,
62  NT &f, NT &dg, NT &dg1, int max_steps,
63  const Vector<NT> &search,
64  const Vector<NT> &estimate);
65  Float threshold_;
66  Float max_change_;
67 };
68 
69 IMPCORE_END_NAMESPACE
70 
71 #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:72
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:20
Base class for all optimizers.
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.
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.