IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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-2026 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
15#include <cereal/access.hpp>
16#include <cereal/types/base_class.hpp>
17
18IMPCORE_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*/
33class IMPCOREEXPORT ConjugateGradients : public AttributeOptimizer {
34 public:
35 ConjugateGradients(Model *m,
36 std::string name = "ConjugateGradients%1%");
37 ConjugateGradients() {}
38
39 //! Set the threshold for the minimum gradient
40 void set_gradient_threshold(Float t) { threshold_ = t; }
41
42 //! Get the threshold for the minimum gradient
43 Float get_gradient_threshold() const { return threshold_; }
44
45 //! Limit how far anything can change each time step
46 void set_max_change(Float t) { max_change_ = t; }
47
48 //! Get the limit on how far anything can change each time step
49 Float get_max_change() const { return max_change_; }
50
51 virtual Float do_optimize(unsigned int max_steps) override;
52 IMP_OBJECT_METHODS(ConjugateGradients);
53
54 private:
55 typedef double NT;
56
57 // Handle optimization failing badly
58 void failure();
59
60 NT get_score(Vector<FloatIndex> float_indices, Vector<NT> &x,
61 Vector<NT> &dscore);
62 bool line_search(Vector<NT> &x, Vector<NT> &dx, NT &alpha,
63 const Vector<FloatIndex> &float_indices, int &ifun,
64 NT &f, NT &dg, NT &dg1, int max_steps,
65 const Vector<NT> &search,
66 const Vector<NT> &estimate);
67 Float threshold_;
68 Float max_change_;
69
70 friend class cereal::access;
71
72 template<class Archive> void serialize(Archive &ar) {
73 ar(cereal::base_class<AttributeOptimizer>(this), threshold_, max_change_);
74 }
75};
76
77IMPCORE_END_NAMESPACE
78
79#endif /* IMPCORE_CONJUGATE_GRADIENTS_H */
Base class for optimizers that act on individual attributes.
A more IMP-like version of the std::vector.
Definition Vector.h:60
virtual Float do_optimize(unsigned int max_steps) override
override this function to do actual optimization
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.
Float get_gradient_threshold() const
Get the threshold for the minimum gradient.
Float get_max_change() const
Get the limit on how far anything can change each time step.
double Float
Basic floating-point value (could be float, double...).
Definition types.h:19
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.