-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvexOptimization.cpp
298 lines (262 loc) · 8.05 KB
/
ConvexOptimization.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* ConvexOptimization.cpp
*
* Created on: Sep 17, 2014
* Author: dailos
*/
#include <limits> //numeric_limit<double>::epsilon
#include <algorithm> // std::max
#include <cmath> //std::abs
#include <functional> //function objects
#include "ConvexOptimization.h"
//Rename as ConvexOptimization
ConvexOptimization::ConvexOptimization()
{
// TODO Auto-generated constructor stub
}
ConvexOptimization::~ConvexOptimization()
{
// TODO Auto-generated destructor stub
}
cv::Mat ConvexOptimization::gradient_diff(cv::Mat x, const std::function<double(cv::Mat)>& func)
{ //in case gradient function is not available, it could be build with a difference approximation
//make up gradient vector through slopes and tiny differences
double EPS(1.0e-4);
cv::Mat df = cv::Mat::zeros(x.size(), x.type());
for(unsigned int j = 0; j < x.total(); ++j)
{
cv::Mat xh = x.clone();
cv::Mat xl = x.clone();
xh.at<double>(j,0) = xh.at<double>(j,0) + EPS;
xl.at<double>(j,0) = xl.at<double>(j,0) - EPS;
double fh = func(xh);
double fl = func(xl);
df.at<double>(j,0) = (fh-fl)/(2.0*EPS);
}
return df;
};
//perform_BFGS
void ConvexOptimization::perform_BFGS(cv::Mat &p, std::function<double(cv::Mat)> &func, std::function<cv::Mat(cv::Mat)> &dfunc)
{
int n = p.total(); //Check the vector has only one column first
//Declare and initialize some variables: g = gradient, xi = direction, hessin = hessian matrix
cv::Mat g, xi;
cv::Mat hessin = cv::Mat::eye(n, n, cv::DataType<double>::type); //initialize to identity matrix
fret_ = func(p);
g = dfunc(p);
xi = -1 * g; //first direction is the opposite to the gradient
std::cout << "starting direction xi: " << xi.t() << std::endl;
//variables: p:[point], xi:[search direction], func:[function], dfunc:[gradient function], g:[gradient at p], h:[hessian at p],
double old_fret = 0.0;
for (int its=0;its<ITMAX;its++)
{
iter_ = its;
std::cout << "step " << iter_ << " to minimum. " << "fret: " << fret_ << std::endl;
std::cout << "p = " << p.t() << std::endl;
std::cout << "gradient_diff(p, func): " << gradient_diff(p, func).t() << std::endl;
std::cout << "dfunc(p): " << dfunc(p).t() << std::endl;
cv::Mat quotient;
cv::divide(gradient_diff(p, func), dfunc(p), quotient);
std::cout << "quotient gradient_diff(p, func)/dfunc(p): " << quotient.t() << std::endl;
if(nextStep(p, xi, g, hessin, fret_, func, dfunc)) return; //minimum reached
//if(std::abs(old_fret-fret_) < 1.0e-8) {std::cout << "Minimum reached. " << std::endl; return;}
else old_fret = fret_;
}
throw("too many iterations in dfpmin");
}
int ConvexOptimization::nextStep(cv::Mat &p, cv::Mat &xi, cv::Mat &g, cv::Mat &hessin, double &fret,
std::function<double(cv::Mat)> &func, std::function<cv::Mat(cv::Mat)> &dfunc)
{
const double EPS = std::numeric_limits<double>::epsilon();
//const double TOLX = 4 * EPS;
const double TOLX = 3.0e-8;
double den, fac, fad, fae, sumdg, sumxi;
cv::Mat dg, hdg;
//we use linmin uses brent method inside to look for the minimum in 1D
fret = brentLineSearch(p, xi, func);
cv::Mat temp;
cv::Mat abs_p = cv::abs(p);
abs_p.setTo(1.0, abs_p > 1.0);
cv::divide(cv::abs(xi), abs_p, temp);
//If all of temp elements are lower than TOLX, algorithm terminates
if ( cv::checkRange(temp, true, nullptr, 0.0, TOLX) ){std::cout << "minimum reached" << std::endl; return 1; } //minimum reached
g.copyTo(dg);
g = dfunc(p);
den = cv::max(fret, 1.0);
cv::multiply(cv::abs(g), abs_p / den, temp);
if ( cv::checkRange(temp, true, nullptr, 0.0, gtol) ){std::cout << "minimum reached" << std::endl; return 1; } //minimum reached
dg = g - dg;
hdg = hessin * dg;
fac = fae = sumdg = sumxi = 0.0;
fac = dg.dot(xi);
fae = dg.dot(hdg);
sumdg = dg.dot(dg);
sumxi = xi.dot(xi);
if (fac > std::sqrt(EPS * sumdg * sumxi))
{
fac = 1.0/fac;
fad = 1.0/fae;
dg = fac * xi - fad * hdg; //Vector that makes BFGS different form DFP method
hessin += fac * xi * xi.t() - fad * hdg * hdg.t() + fae * dg * dg.t();
}
xi = -hessin * g;
return 0; //minumum not found yet
}
double ConvexOptimization::brentLineSearch(cv::Mat& p, cv::Mat& xi, std::function<double(cv::Mat)> &func)
{
//Helpter function that turns a multidimensional functor into 1-dim, through point p and direction xi on function func
auto F1dim = [] (const double &x, const cv::Mat &p, const cv::Mat &xi, const std::function<double(cv::Mat)> &func) -> double
{ //could be implemented through function adaptors, read more about it
return func(p + x * xi);
};
std::function<double(double)> f1dim = std::bind(F1dim, std::placeholders::_1, p, xi, func);
bracket(0.0,1.0,f1dim); //initial bounds conditions a=0, b=1
xmin = brent(f1dim);
xi = xi * xmin;
p = p + xi;
return fmin;
}
double ConvexOptimization::brent(std::function<double(double)> &func)
{
//brent method suposses the minimum has been bracket before whithin points xa, xb, xc, member variables
auto shft3 = [](double &a, double &b, double &c, const double d){a=b; b=c; c=d;};
auto sign = [](double a, double b) {return b >= 0.0 ? std::abs(a) : -std::abs(a);};
const int ITMAX = 100;
const double CGOLD = 0.3819660;
const double ZEPS = std::numeric_limits<double>::epsilon() * 1.0e-3;
double a, b, d = 0.0, etemp, fu, fv, fw, fx;
double p, q, r, tol1, tol2, u, v, w, x, xm;
double e = 0.0;
a = (ax < cx ? ax : cx);
b = (ax > cx ? ax : cx);
x = w = v = bx;
fw = fv = fx = func(x);
for (unsigned int iter = 0; iter < ITMAX; ++iter)
{
xm = 0.5 * (a+b);
tol2 = 2.0 * (tol1 = tol * std::abs(x) + ZEPS);
if (std::abs(x-xm) <= (tol2-0.5*(b-a)))
{
fmin = fx;
return xmin = x;
}
if (std::abs(e) > tol1)
{
r = (x-w) * (fx-fv);
q = (x-v) * (fx-fw);
p = (x-v) * q - (x-w) * r;
q = 2.0 * (q-r);
if (q > 0.0) p = -p;
q = std::abs(q);
etemp = e;
e = d;
if (std::abs(p) >= std::abs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
{
d = CGOLD * (e=(x >= xm ? a-x : b-x));
}
else
{
d = p/q;
u = x+d;
if (u-a < tol2 || b-u < tol2)
{
d = sign(tol1,xm-x);
}
}
}
else
{
d = CGOLD*(e=(x >= xm ? a-x : b-x));
}
u = (std::abs(d) >= tol1 ? x+d : x + sign(tol1,d));
fu = func(u);
if (fu <= fx)
{
if (u >= x) a=x; else b=x;
shft3(v,w,x,u);
shft3(fv,fw,fx,fu);
}
else
{
if (u < x) a=u; else b=u;
if (fu <= fw || w == x)
{
v=w;
w=u;
fv=fw;
fw=fu;
}
else if (fu <= fv || v == x || v == w)
{
v=u;
fv=fu;
}
}
}
throw CustomException("Too many iterations in brent 1D minimization method.");
}
void ConvexOptimization::bracket(const double& a, const double& b, std::function<double(double)> &func)
{
auto shft3 = [](double &a, double &b, double &c, const double d){a=b; b=c; c=d;};
auto sign = [](double a, double b) {return b >= 0.0 ? std::abs(a) : -std::abs(a);};
const double GOLD = 1.618034, GLIMIT = 100.0, TINY = 1.0e-20;
ax=a; bx=b;
double fu;
fa = func(ax);
fb = func(bx);
if (fb > fa)
{
std::swap(ax, bx);
std::swap(fb, fa);
}
cx = bx + GOLD * (bx-ax);
fc = func(cx);
while (fb > fc)
{
double r = (bx-ax) * (fb-fc);
double q = (bx-cx) * (fb-fa);
double u = bx - ((bx-cx)*q-(bx-ax)*r)/(2.0*sign(std::max(std::abs(q-r),TINY),q-r));
double ulim = bx + GLIMIT * (cx-bx);
if ((bx-u) * (u-cx) > 0.0)
{
fu = func(u);
if (fu < fc)
{
ax = bx;
bx = u;
fa = fb;
fb = fu;
return;
}
else if (fu > fb)
{
cx = u;
fc = fu;
return;
}
u = cx + GOLD * (cx-bx);
fu = func(u);
}
else if ((cx-u) * (u-ulim) > 0.0)
{
fu = func(u);
if (fu < fc)
{
shft3(bx, cx, u, u + GOLD * (u-cx));
shft3(fb, fc, fu, func(u));
}
}
else if ((u-ulim) * (ulim-cx) >= 0.0)
{
u = ulim;
fu = func(u);
}
else
{
u = cx + GOLD * (cx-bx);
fu = func(u);
}
shft3(ax, bx, cx, u);
shft3(fa, fb, fc, fu);
}
}