-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathxregLineSearchOpt.h
189 lines (152 loc) · 7.18 KB
/
xregLineSearchOpt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* MIT License
*
* Copyright (c) 2020 Robert Grupp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef XREGLINESEARCHOPTIMIZATION_H_
#define XREGLINESEARCHOPTIMIZATION_H_
#include <boost/variant/variant.hpp>
#include <Eigen/Eigen>
#include "xregCommon.h"
#include "xregObjWithOStream.h"
namespace xreg
{
/// \brief Generic line search optimization aggregate.
///
/// Template parameters specify function object classes for computing objective
/// functions (including gradients and Hessians), computing a search direction,
/// and determining step length.
struct LineSearchOptimization : public ObjWithOStream
{
using Scalar = CoordScalar;
using Pt = PtN;
using Mat = MatMxN;
// Inputs : ( x , compute grad, compute Hessian )
// Outputs: ( f(x) , grad f(x) , Hessian f(x) )
using ObjFn = std::function<std::tuple<Scalar,Pt,Mat>(const Pt&,const bool,const bool)>;
// Inputs: ( grad f(x) )
// Outputs: search direction in parameter space
using SearchDirFnFirstOrder = std::function<Pt(const Pt&)>;
// Inputs: ( grad f(x) , Hessian f(x) )
// Outputs: search direction in parameter space
using SearchDirFnSecOrder = std::function<Pt(const Pt&,const Mat&)>;
using SearchDirFn = boost::variant<SearchDirFnFirstOrder,SearchDirFnSecOrder>;
// Input: ( f , search dir p , x_0, grad f(x_0), compute Hessian )
// Outputs: ( x_1, f(x_1) , grad f(x_1) , Hessian f(x_1) )
using BacktrackFn = std::function<std::tuple<Pt,Scalar,Pt,Mat>(const ObjFn&, // f
const Pt&, // search dir, p
const Pt&, // x_0
const Scalar, // f(x_0)
const Pt&, // grad f(x_0)
const bool // compute Hessian
)>;
enum TermStatus
{
kGRAD_CONVERGED,
kMAX_ITS_PERFORMED,
kNO_POS_CHANGE,
kOTHER
};
ObjFn obj_fn;
SearchDirFn search_dir_fn;
BacktrackFn backtrack_fn;
Scalar param_tol = 1.0e-8;
Scalar grad_tol = 1.0e-8;
// 0 --> no limit
size_type max_its = 0;
LineSearchOptimization() = default;
// no copying
LineSearchOptimization(const LineSearchOptimization&) = delete;
LineSearchOptimization& operator=(const LineSearchOptimization&) = delete;
std::tuple<TermStatus,Pt,Scalar,size_type> solve(const Pt& init_x) const;
static std::string TermStatusString(const TermStatus status);
};
/// \brief Move in the direction of the negative gradient.
///
/// This would be used for a gradient descent search
LineSearchOptimization::Pt
NegativeGradSearchDir(const LineSearchOptimization::Pt& grad,
const LineSearchOptimization::Mat& hessian);
/// \brief Move in the direction of the Newton step.
///
/// Solves for step p: H p = -g
LineSearchOptimization::Pt
NewtonSearchDir(const LineSearchOptimization::Pt& grad,
const LineSearchOptimization::Mat& hessian);
/// \brief Move in a modified Netwon direction, replaces sufficiently
/// negative definite Hessian with a positive definite version, B.
///
/// Solves for step p: B p = -g
struct ModNewtonSearchDir
{
using Scalar = LineSearchOptimization::Scalar;
using Pt = LineSearchOptimization::Pt;
using Mat = LineSearchOptimization::Mat;
/// This assumes symmetric input, therefore this implementation is limited
/// (mainly) to C2 functions (where the Hessian is symmetric)
using SpectralDecomp = Eigen::SelfAdjointEigenSolver<Mat>;
/// \brief Bound on the condition number of the modified Hessian.
///
/// The general rule of thumb is if beta = 10^k, then k digits of precision
/// are lost. 64-bit double precision number give 15-17 digits of precision,
/// so a beta on the order of 10^6, should still yield 10 digits of precision.
/// https://en.wikipedia.org/wiki/Condition_number
/// https://en.wikipedia.org/wiki/Double-precision_floating-point_format
Scalar beta = 1.0e6;
SpectralDecomp spectral_dcomp;
Pt mod_eigen_vals;
Mat B;
Pt operator()(const Pt& grad, const Mat& hessian);
LineSearchOptimization::SearchDirFn callback_fn();
};
/// \brief Move in a constant, fixed-length, step - no backtracking conditions.
std::tuple<LineSearchOptimization::Pt,
LineSearchOptimization::Scalar,
LineSearchOptimization::Pt,
LineSearchOptimization::Mat>
FixedStepNoBacktracking(const LineSearchOptimization::ObjFn& obj_fn,
const LineSearchOptimization::Pt& p,
const LineSearchOptimization::Pt& x,
const bool compute_hessian,
const LineSearchOptimization::Scalar alpha);
xreg::LineSearchOptimization::BacktrackFn
MakeFixedStepNoBacktrackingCallback(const LineSearchOptimization::Scalar alpha);
/// \brief Determine search length via backtracking until the Armijo condition is satisfied.
std::tuple<LineSearchOptimization::Pt,
LineSearchOptimization::Scalar,
LineSearchOptimization::Pt,
LineSearchOptimization::Mat>
BacktrackingArmijo(const LineSearchOptimization::ObjFn& obj_fn,
const LineSearchOptimization::Pt& p,
const LineSearchOptimization::Pt& x,
const LineSearchOptimization::Scalar F,
const LineSearchOptimization::Pt& g,
const bool compute_hessian,
const LineSearchOptimization::Scalar init_alpha, ///< Initial fraction of step.
const LineSearchOptimization::Scalar eta, ///< Slope of line definiing Armijo condition
const LineSearchOptimization::Scalar tau ///< Backtracking scale factor (e.g. 0.5 cuts steps in half)
);
xreg::LineSearchOptimization::BacktrackFn
MakeBacktrackingArmijoCallback(const LineSearchOptimization::Scalar alpha = 1,
const LineSearchOptimization::Scalar eta = 0.001,
const LineSearchOptimization::Scalar tau = 0.5);
} // xreg
#endif