NPL
Neurological Programs and Libraries
opt.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2014 Micah C Chambers (micahc.vt@gmail.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * @file opt.h Contains base class for all optimizers, defines Function types,
17  * and StopReason
18  *
19  *****************************************************************************/
20 
21 #ifndef OPT_H
22 #define OPT_H
23 
24 #include <functional>
25 #include <Eigen/Dense>
26 
27 namespace npl
28 {
29 
34 using std::function;
35 using Eigen::MatrixXd;
36 using Eigen::VectorXd;
37 
41 typedef function<int(const VectorXd& x, double& v, VectorXd& g)> ValGradFunc;
42 
46 typedef function<int(const VectorXd& x, VectorXd& g)> GradFunc;
47 
51 typedef function<int(const VectorXd& x, double& v)> ValFunc;
52 
56 typedef function<int(const VectorXd& x, double v, const VectorXd& g, size_t iter)> CallBackFunc;
57 
72 int testgrad(double& error, const VectorXd& x, double stepsize, double tol,
73  const ValFunc& valfunc, const ValGradFunc& valgradfunc);
74 
89 int testgrad(double& error, const VectorXd& x, double stepsize, double tol,
90  const ValFunc& valfunc, const GradFunc& gradfunc);
91 
100 int gRosenbrock_G(const VectorXd& x, VectorXd& gradient);
101 
110 int gRosenbrock_V(const VectorXd& x, double& v);
111 
119 void gRosenbrock_callCounts(size_t& vcalls, size_t& gcalls);
120 
131 inline
132 int noopCallback(const VectorXd& x, double value, const VectorXd& grad, size_t iter)
133 {
134  (void)(x);
135  (void)(value);
136  (void)(grad);
137  (void)(iter);
138  return 0;
139 }
140 
142 {
143  ENDGRAD, // end due to gradient below threshold
144  ENDSTEP, // end due to step size below threshold
145  ENDVALUE, // end due to change in value below threshold
146  ENDABSVALUE, // end due to surpassing value threshold
147  ENDITERS, // end due to number iterations
148  ENDFAIL // end tue to some error
149 };
150 
151 using std::max;
152 using std::abs;
153 
154 
156 {
157 
158 public:
162  VectorXd state_x;
163 
167  double stop_G;
168 
172  double stop_X;
173 
177  double stop_F;
178 
182  double stop_F_over;
183 
187  double stop_F_under;
188 
192  int stop_Its;
193 
208  Optimizer(size_t dim, const ValFunc& valfunc, const GradFunc& gradfunc,
209  const ValGradFunc& valgradfunc,
210  const CallBackFunc& callback = noopCallback);
211 
223  Optimizer(size_t dim, const ValFunc& valfunc, const GradFunc& gradfunc,
224  const CallBackFunc& callback = noopCallback);
225 
231  virtual
232  StopReason optimize() { return ENDFAIL; };
233 
239  static std::string explainStop(StopReason r);
240 protected:
241 
242  ValGradFunc m_compFG;
243  GradFunc m_compG;
244  ValFunc m_compF;
245  CallBackFunc m_callback;
246 };
247 
250 } // npl
251 
252 #endif //OPT_H
int testgrad(double &error, const VectorXd &x, double stepsize, double tol, const ValFunc &valfunc, const ValGradFunc &valgradfunc)
Tests a gradient function using the value function.
Definition: accessors.h:29
function< int(const VectorXd &x, double &v, VectorXd &g)> ValGradFunc
Value and Gradient Computation Function.
Definition: opt.h:41
int noopCallback(const VectorXd &x, double value, const VectorXd &grad, size_t iter)
Callback that does nothing.
Definition: opt.h:132
function< int(const VectorXd &x, VectorXd &g)> GradFunc
Gradient Only Computation Function.
Definition: opt.h:46
static std::string explainStop(StopReason r)
Provides a string that describes the stop reason.
Optimizer(size_t dim, const ValFunc &valfunc, const GradFunc &gradfunc, const ValGradFunc &valgradfunc, const CallBackFunc &callback=noopCallback)
Constructor for optimizer function.
function< int(const VectorXd &x, double &v)> ValFunc
Value Only Computation Function.
Definition: opt.h:51
CallBackFunc m_callback
Definition: opt.h:245
ValFunc m_compF
Definition: opt.h:244
double stop_F_over
Stop immediately when value goes above this.
Definition: opt.h:182
virtual StopReason optimize()
Perform optimization.
Definition: opt.h:232
double stop_F
Stop when change in function value drops below this value.
Definition: opt.h:177
ValGradFunc m_compFG
Definition: opt.h:242
int gRosenbrock_G(const VectorXd &x, VectorXd &gradient)
Implements generized rosenbrock gradient.
StopReason
Definition: opt.h:141
int gRosenbrock_V(const VectorXd &x, double &v)
Implements generized rosenbrock value.
VectorXd state_x
State variable, set to initialize.
Definition: opt.h:162
double stop_X
Stop when step size drops below this value.
Definition: opt.h:172
double stop_F_under
Stop immediately when value goes below this.
Definition: opt.h:187
int stop_Its
Stop after this many iterations (does not include linesearch)
Definition: opt.h:192
void gRosenbrock_callCounts(size_t &vcalls, size_t &gcalls)
Returns the number of times the Value and Gradient functions for the Generalized Rosenbrock Function ...
GradFunc m_compG
Definition: opt.h:243
function< int(const VectorXd &x, double v, const VectorXd &g, size_t iter)> CallBackFunc
Callback function.
Definition: opt.h:56
double stop_G
Stop when graient magnitde falls below this value.
Definition: opt.h:167