-
Notifications
You must be signed in to change notification settings - Fork 0
/
Optimizer.cpp
442 lines (404 loc) · 14.3 KB
/
Optimizer.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
// Optimizer.cpp
// Numerical_Optimization
//
// Created by Balaji Krishnamurthy on 12/20/20.
// Copyright © 2020 Balaji Krishnamurthy. All rights reserved.
//
#include "Optimizer.hpp"
// Function to compute derivative, used for Gradient and Hessian
std::unordered_map<std::string, int> Rosenbrock::Derivate(const char& dx) const
{
std::unordered_map<std::string, int> fmap;
std::string temp;
bool nflag = false;
bool dflag = false;
int coeff = 1;
int mul = 1;
const auto sz = this->fn.length();
for(int i = 0; i <= sz; i++)
{
if(i == sz || this->fn[i] == '-' || this->fn[i] == '+')
{
if(nflag)
{
coeff *= -1;
nflag = !nflag;
}
if(dflag)
{
if(temp.back() == dx && (this->fn[i-1] == dx || this->fn[i-2] == dx))
{
temp.pop_back();
if(temp.empty())
{
temp = "1";
}
}
fmap[temp] += coeff;
dflag = !dflag;
}
coeff = 1;
temp.clear();
mul = 1;
if(i != sz && this->fn[i] == '-')
{
nflag = !nflag;
}
}
else if(this->fn[i] == dx)
{
dflag = !dflag;
temp += this->fn[i];
}
else if(std::isdigit(this->fn[i]))
{
const int t = this->fn[i] - '0';
if(dflag)
{
coeff *= t;
if(t > 2)
{
temp += (t - 1 + '0');
}
else
{
temp.pop_back();
}
}
else if(temp.size())
{
temp += this->fn[i];
}
else
{
coeff *= mul;
if(coeff == 1)
{
coeff = 0;
mul = 10;
}
coeff += t;
}
}
else if(!std::isspace(this->fn[i]))
{
if(dflag)
{
if(this->fn[i] != '^' && this->fn[i-1] == dx)
{
temp.pop_back();
}
}
temp += this->fn[i];
}
}
return fmap;
}
// This function computes value of a polynomial at given points
double Rosenbrock::computePoly(const std::vector<std::pair<std::string,float>>& vect, const std::pair<float, float>& pt) const
{
double grad_sum = 0;
for(const auto& x : vect)
{
double ans = 1;
float last = 0;
for(const auto& p : x.first)
{
if(p == this->xyz.front())
{
ans *= pt.first;
last = pt.first;
}
else if(p == this->xyz.back())
{
ans *= pt.second;
last = pt.second;
}
else if (std::isdigit(p))
{
int u = p - '0';
while(--u)
{
ans *= last;
}
}
}
ans *= x.second;
grad_sum += ans;
}
return grad_sum;
}
// Function to compute Gradient
std::vector<float> Rosenbrock::Gradient(const std::pair<float, float>& point)
{
std::vector<float> ftemp;
if(this->gradvec.empty())
{
for(const char& c : this->xyz)
{
const auto fd = this->Derivate(c);
std::vector<std::pair<std::string, float>> vec;
for(const auto& x : fd)
{
vec.emplace_back(x.first, x.second);
}
this->gradvec.emplace_back(vec);
}
}
for(const auto& vect : this->gradvec)
{
ftemp.emplace_back(this->computePoly(vect, point));
}
return ftemp;
}
// Function to compute f(x)
void Rosenbrock::computeFx()
{
std::string temp;
bool nflag = false;
int coeff = 1;
int mul = 1;
const auto sz = this->fn.size();
for(int i = 0; i <= sz; i++)
{
if(i == sz || this->fn[i] == '-' || this->fn[i] == '+')
{
if(nflag)
{
coeff *= -1;
nflag = !nflag;
}
if(temp.empty())
{
temp = "1";
}
this->fvec.emplace_back(temp, coeff);
temp.clear();
coeff = 1;
mul = 1;
if(i != sz && this->fn[i] == '-')
{
nflag = !nflag;
}
}
else if(std::isdigit(this->fn[i]))
{
if(temp.size())
{
temp += this->fn[i];
}
else
{
const int t = this->fn[i] - '0';
coeff *= mul;
if(coeff == 1)
{
coeff = 0;
mul = 10;
}
coeff += t;
}
}
else if(!std::isspace(this->fn[i]))
{
temp += this->fn[i];
}
}
}
// Function to bactrack for Steepest Descent
void Rosenbrock::SD_BackTrack(const std::pair<float, float>& btpt)
{
if(this->fvec.empty())
{
this->computeFx();
}
std::vector<float> btgrad;
std::pair<float, float> btfx;
double btfdx;
float mul = 1;
double bttemp = 0;
double left = 1;
double right = 0;
while(left > right)
{
this->step *= mul;
btgrad = this->Gradient(btpt);
btfx = {btpt.first - this->step * btgrad[0], btpt.second - this->step * btgrad[1]};
left = this->computePoly(this->fvec, btfx);
btfdx = this->computePoly(this->fvec, btpt);
for(const auto& bt : btgrad)
{
bttemp += bt * bt;
}
bttemp = 0.5 * this->step * bttemp;
right = btfdx - bttemp;
mul = this->rho;
}
}
// Function for the Steepest Descent algorithm
std::pair<float,float> Rosenbrock::steepestDescent()
{
double sd_curr_fx = DOUBLE_MAX/100;
double sd_prev_fx = DOUBLE_MAX;
const auto sd_start = this->start;
std::pair<float, float> sd_nstate = this->start;
std::pair<float, float> sd_residue;
std::vector<float> sd_grad;
int sd_iter = 0;
this->SD_ans.emplace_back(sd_nstate);
while((sd_prev_fx - sd_curr_fx) > slimit)
{
this->start = sd_nstate;
sd_prev_fx = sd_curr_fx;
sd_grad = this->Gradient(this->start);
// update learning rate until backtrack inequality is true;
this->SD_BackTrack(this->start);
sd_residue = {-1* sd_grad.front() * this->step, -1 * sd_grad.back() * this->step};
sd_nstate = {this->start.first + sd_residue.first, this->start.second + sd_residue.second};
sd_curr_fx = this->computePoly(this->fvec, sd_nstate);
std::cout << "iteration : " << sd_iter << ",\t" << "f(x) : " << sd_curr_fx << ",\t" << "learning rate : " << this->step << "\n\n";
sd_iter++;
this->step = 1;
this->SD_ans.emplace_back(sd_nstate);
}
std::cout << "\nOptimal points \t x1 : " << sd_nstate.first << ",\t x2 : " << sd_nstate.second << "\n\n\n\n" << std::endl;
this->start = sd_start;
return sd_nstate;
}
// Function to compute Hessian matrix
std::vector<std::vector<double>> Rosenbrock::Hessian(const std::pair<float, float>& Hpt)
{
std::vector<std::vector<double>> Hess;
if(this->hessmat.empty())
{
const std::string Hfn = this->fn;
for(const auto& vect : this->gradvec)
{
this->fn.clear();
const auto sz = vect.size();
for(int i = 0; i < sz; i++)
{
if(vect[i].second < 0)
{
if(this->fn.size())
{
this->fn.pop_back();
}
this->fn += '-';
}
this->fn += ' ';
this->fn += std::to_string(std::abs(int(vect[i].second)));
if(vect[i].first != "1")
{
this->fn += vect[i].first;
}
if(i < sz - 1)
{
this->fn += " +";
}
}
std::vector<std::vector<std::pair<std::string, float>>> Htemp;
for(const char& c : this->xyz)
{
const auto Hd = this->Derivate(c);
std::vector<std::pair<std::string, float>> Hvec;
for(const auto& x : Hd)
{
Hvec.emplace_back(x.first, x.second);
}
Htemp.emplace_back(Hvec);
}
this->hessmat.emplace_back(Htemp);
}
this->fn = Hfn;
}
for(const auto& hvect : this->hessmat)
{
std::vector<double> htemp;
for(const auto& hv : hvect)
{
double grad_sum = this->computePoly(hv, Hpt);
htemp.emplace_back(grad_sum);
}
Hess.emplace_back(htemp);
}
return Hess;
}
// Function to compute Inverse of the Hessian matrix
void Rosenbrock::Inverse(std::vector<std::vector<double>>& Inv) const
{
const double det = Inv.front().front() * Inv.back().back() - Inv.front().back() * Inv.back().front();
std::swap(Inv.front().front(), Inv.back().back());
Inv.front().front() /= det;
Inv.back().back() /= det;
Inv.front().back() *= (-1/det);
Inv.back().front() *= (-1/det);
}
// Function to bactrack for Newton's Method
void Rosenbrock::NM_BackTrack(const std::pair<float, float>& nmbtpt)
{
if(this->fvec.empty())
{
this->computeFx();
}
std::vector<float> nm_grad;
std::vector<std::vector<double>> nm_hess;
std::pair<float, float> left_res;
std::pair<float, float> left_arg;
std::vector<float> nm_grad_tr;
double right_res;
double right_fx;
float mul = 1;
double left = 1;
double right = 0;
while(left > right)
{
this->step *= mul;
nm_grad = this->Gradient(this->start);
nm_hess = this->Hessian(this->start);
this->Inverse(nm_hess);
left_res = {(nm_hess.front().front() * nm_grad.front() + nm_hess.front().back() * nm_grad.back()),
(nm_hess.back().front() * nm_grad.front() + nm_hess.back().back() * nm_grad.back())};
left_arg = {this->start.first - this->step * left_res.first, this->start.second - this->step * left_res.second};
left = this->computePoly(this->fvec, left_arg);
nm_grad_tr = nm_grad;
right_res = nm_grad_tr.front() * left_res.first + nm_grad_tr.back() * left_res.second;
right_fx = this->computePoly(this->fvec, nmbtpt);
right = right_fx - 0.5 * this->step * right_res;
mul = this->rho;
}
}
// Function to implement Newton's algorithm
std::pair<float,float> Rosenbrock::NewtonMethod()
{
double nm_curr_fx = DOUBLE_MAX/100;
double nm_prev_fx = DOUBLE_MAX;
std::vector<std::vector<double>> nm_hess;
std::pair<float, float> nm_nstate = this->start;
std::pair<float, float> nm_residue;
std::vector<float> nm_gradient;
int nm_iter = 0;
this->NM_ans.emplace_back(nm_nstate);
while(std::abs(nm_prev_fx - nm_curr_fx) > this->slimit)
{
this->start = nm_nstate;
nm_prev_fx = nm_curr_fx;
nm_gradient = this->Gradient(this->start);
nm_hess = this->Hessian(this->start);
this->Inverse(nm_hess);
// update step unti backtrack inequality is true;
this->NM_BackTrack(start);
nm_residue = {this->step * (nm_hess.front().front() * nm_gradient.front() + nm_hess.front().back() * nm_gradient.back()),
this->step * (nm_hess.back().front() * nm_gradient.front() + nm_hess.back().back() * nm_gradient.back())};
nm_nstate = {this->start.first - nm_residue.first, this->start.second - nm_residue.second};
nm_curr_fx = this->computePoly(this->fvec, nm_nstate);
std::cout << "iteration : " << nm_iter << ",\t" << "f(x) : " << nm_curr_fx << ",\t" << "learning rate : " << this->step << "\n\n";
nm_iter++;
this->step = 1;
this->NM_ans.emplace_back(nm_nstate);
}
std::cout << "\nOptimal points \t x1 : " << nm_nstate.first << ",\t x2 : " << nm_nstate.second << "\n" << std::endl;
return nm_nstate;
}
/* Optimizer_cpp */