-
Notifications
You must be signed in to change notification settings - Fork 2
/
bisOptimizer.h
199 lines (144 loc) · 6.38 KB
/
bisOptimizer.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
190
191
192
193
194
195
196
197
198
199
/* License
_This file is Copyright 2018 by the Image Processing and Analysis Group (BioImage Suite Team). Dept. of Radiology & Biomedical Imaging, Yale School of Medicine._ It is released under the terms of the GPL v2.
----
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
See also http: www.gnu.org/licenses/gpl.html
If this software is modified please retain this statement and add a notice
that it had been modified (and by whom).
Endlicense */
#include <string>
#include <iostream>
#ifndef _bis_Optimizer_h
#define _bis_Optimizer_h
#include <vector>
#include "bisObject.h"
/**
* Parent class for all classes that provide the "Optimizable" Interface
* These can be "exercised using the bisOptimizer class
*/
class bisOptimizableAlgorithm : public bisObject {
public:
/** Constructor
* @param n used to set class name
*/
bisOptimizableAlgorithm(std::string n) : bisObject(n) { this->enable_feedback=1;}
/** Compute the value being optimized at this position. Called by bisOptimizer.
* @param position this contains the current set of parameters
* @returns the computed value
*/
virtual float computeValue(std::vector<float>& position)=0;
/** Compute the gradient of the value being optimized at this position. Called by bisOptimizer.
* @param position this contains the current set of parameters
* @param gradient the output gradient at the current position
* @returns the magnitude of the gradient vector
*/
virtual float computeGradient(std::vector<float>& position,std::vector<float>& gradient)=0;
/** Called at the beginning of each iteration by bisOptimizer.
* @param position this contains the current set of parameters
* @param iterno the current iteration number
*/
virtual void beginIteration(std::vector<float>& position,int iterno);
/** Called by bisOptimizer to generate output (essentially print)
* @param input the current message
*/
virtual void generateFeedback(std::string input) { if (enable_feedback) std::cout << input << std::endl; }
/** Returns the current step size */
virtual float getCurrentStepSize() { return -1.0;}
protected:
/** If zero then no print statements during optimization */
int enable_feedback;
};
#ifndef DOXYGEN_SKIP
class optParams {
public:
float ax,bx,cx;
float fa,fb,fc;
float xmin;
};
#endif // DOXYGEN_SKIP
/**
* Optimizer class. This can be used to optimize a functional implemented in a class
* derived from bisOptimizableAlgorithm
*/
class bisOptimizer : public bisObject {
public:
/** Constructor
* @param algorithm the class to optimize
* @param n used to set class name
*/
bisOptimizer(bisOptimizableAlgorithm* algorithm,std::string n="optimizer");
/** Destructor */
virtual ~bisOptimizer();
/** Compute Gradient Descent optimization (of the underlying algorithm)
* @param position on input the initial set of parameters, on output the optimal set)
* @param iterations maximum number of iterations
* @param tolerance if change is below this the optimization stops
* @returns the optimal value of the functional
*/
float computeGradientDescent(std::vector<float>& position,int iterations,float tolerance);
/** Compute Hill Climbing optimization (of the underlying algorithm)
* @param position on input the initial set of parameters, on output the optimal set)
* @param step the current step size to move around by
* @param iterations maximum number of iterations
* @returns the optimal value of the functional
*/
float computeSlowClimb(std::vector<float>& position,float step,int iterations);
/** Compute Conjugate Gradient Descent optimization (of the underlying algorithm)
* @param position on input the initial set of parameters, on output the optimal set)
* @param iterations maximum number of iterations
* @param tolerance if change is below this the optimization stops
* @returns the optimal value of the functional
*/
float computeConjugateGradient(std::vector<float>& position,int iterations,float tolerance);
protected:
#ifndef DOXYGEN_SKIP
std::vector<float> pcom,xicom,xtemp,gradient;
#endif
/** Number of degrees of freedom being optimized -- length of position vector */
unsigned int NumDOF;
/** Number of function evaluation since we begun */
int NumEvaluations;
/** Number of gradient computations since we begun */
int NumGradients;
/** The algorithm to optimize */
bisOptimizableAlgorithm* algorithm;
/** Allocate internal storage */
void allocateTempArrays(unsigned int sz);
/** reset internal statistics NumGradients and NumEvaluations to zero */
void resetStatistics();
/** Generate Print Output
* @param prefix1 string to prefix the output
* @param prefix2 string to follow prefix 1 in the output
* @param position current set of parameters
* @param measure current output measure
* @param iter current number of iteration
*/
void generateOutput(std::string prefix1,std::string prefix2,std::vector<float>& position,float measure,int iter=0);
/** Generate Statistics print output
* @param method name of optimization method
* @param position current set of parameters
*/
void generateStatistics(std::string method,std::vector<float>& position);
#ifndef DOXYGEN_SKIP
float lineFunction(float x);
void bracketMinimum(optParams& params);
float minimizeGivenBounds(optParams& params,float tol=0.0);
float lineMinimization(std::vector<float>& p,std::vector<float>& xi,int iterno,float tolerance,std::string method="");
#endif
private:
/** Copy constructor disabled to maintain shared/unique ptr safety */
bisOptimizer(const bisOptimizer&);
/** Assignment disabled to maintain shared/unique ptr safety */
void operator=(const bisOptimizer&);
};
#endif