-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktrackinglinesearch.cpp
76 lines (66 loc) · 2.84 KB
/
backtrackinglinesearch.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
#include <limits>
#include <vector>
#include <math.h>
#include <iostream>
#include "backtrackinglinesearch.h"
#include "utils.h"
using namespace std;
backtrackingLineSearch::backtrackingLineSearch(vector<double> x){
// NOT Implemented Yet
}
// TODO : Initialization list
backtrackingLineSearch::backtrackingLineSearch(vector<double> x,\
double alpha,\
double alpha_dec,\
double alpha_inc,\
double wolfe_para,\
double tol,\
Condition_Type type,\
double (&f)(vector<double>),\
vector<double> (&df)(vector<double>),
int verbose,
string logFileName): x(x), alpha(alpha),\
alpha_dec(alpha_dec),\
alpha_inc(alpha_inc),\
wolfe_para(wolfe_para),\
tol(tol),\
type(type),
f(&f),\
df(&df)
{
this->verbose=verbose;
if(verbose!=0) logFile.open(logFileName);
}
void backtrackingLineSearch:: set_x0(vector<double> x0){
x=x0;
}
vector<double> backtrackingLineSearch::optimize(){
uint count=0;
//local variable for that optimization problem
double alpha_=alpha;
double alpha_dec_=alpha_dec;
double alpha_inc_=alpha_inc;
vector<double> delta;
if (verbose!=0){
cout << "Initial SOLUTION : ("<< x[0] << ","<< x[1]<<")"<<endl;
logFile<<x[0]<<" "<<x[1]<<" "<<f(x)<<endl;
}
while (count<10){
delta=utils::Delta(df(x));
while (f({x[0]+alpha_*delta[0],x[1]+alpha_*delta[1]})>f(x)+wolfe_para*(df(x)[0]*alpha_*delta[0]+df(x)[1]*alpha_*delta[1])){
alpha_*=alpha_dec_;
if(verbose!=0) cout<<"Dec Alpha to: "<<alpha<<endl;
}
x[0]+=alpha_*delta[0];
x[1]+=alpha_*delta[1];
alpha_*=alpha_inc_;
if (verbose!=0){
cout << "########### NEW SOLUTION ########### : ("<< x[0] << ","<< x[1]<<")"<<endl;
logFile<<x[0]<<" "<<x[1]<<" "<<f(x)<<endl;
}
// terminaltion rule for the inner loop
if(sqrt(pow(alpha_*delta[0],2)+pow(alpha_*delta[1],2))<tol)count++;
}
logFile.close();
return x;
}