tttrlib
A library for time-tagged time resolved data
Loading...
Searching...
No Matches
i_lbfgs.h
Go to the documentation of this file.
1#ifndef TTTRLIB_I_BFGS_H
2#define TTTRLIB_I_BFGS_H
3
4// minimize f(x,p) using BFGS algorithm
5
6#include <cmath> /* isfinite */
7#include <iostream>
8
9#include "alglib/ap.h"
10#include "lbfgs/lbfgs.h"
11
12
13// pointer to the target function
14typedef double(*TargetFP)(double*, void*);
15
16int fjac1(void (*)(double*, double*), double*, int, int, double, double*);
17
18int fgrad1(void (*)(double*, double&), double*, int, double, double*);
19
20int fjac2(void (*)(double*, double*), double*, int, int, double, double*);
21
22int fgrad2(void (*)(double*, double&), double*, int, double, double*);
23
24int fjac4(void (*)(double*, double*), double*, int, int, double, double*);
25
26int fgrad4(void (*)(double*, double&), double*, int, double, double*);
27
28
29class bfgs
30{
31
32 public:
33
34 bfgs(TargetFP fun) { setdefaults(); N = 0; f = fun; }
35 bfgs(TargetFP fun, int n) { setdefaults(); setN(n); f = fun; }
37 if (N>0) { delete[] xd; delete[] fixed; }
38 }
39
40 // change dimension
41 void setN(int n) {
42 if (N>0) { delete[] xd; delete[] fixed; }
43 N = n;
44 xd = new double[N];
45 fixed = new int[N];
46 for(int i=0; i<N; i++) fixed[i] = 0;
47 }
48
49 // set epsilon explicitly
50 void seteps(double e) {
51 eps = e;
52 sqrt_eps = sqrt(e);
53 }
54
55 // estimate epsilon
56 void seteps() {
57 double x1 = 1.0, x2 = 1.0, e = 1.e-18, estep = 1.001;
58 for (;;) {
59 x2 = x1 + e;
60 if (x2 > x1) break;
61 else e *= estep;
62 }
63 seteps(e);
64 }
65
66 // fix or unfix a parameter
67 void fix(int n) {
68 if (n>=N) return;
69 else fixed[n] = 1;
70 }
71 void free(int n) {
72 if (n>=N) return;
73 else fixed[n] = 0;
74 }
75
76 // max number of iterations
78
79 // minimize target f
80 int minimize(double* x, void* p)
81 {
82 if (N==0) return -1;
83
84 int info = 0, bfgsM, Nfree = 0, j = 1;
85 for(int i=0; i<N; i++) {
86 xd[i] = x[i];
87 Nfree += (!fixed[i]);
88 }
89 Nfree < 7 ? bfgsM = Nfree : bfgsM = 7;
90
91 // create "real_1d_array" X for lbfgsminimize
92 ap::real_1d_array X;
93 X.setbounds(1, Nfree);
94 for(int i=0; i<N; i++)
95 if (!fixed[i]) X(j++) = x[i];
96 pcopy = p;
97
98 // problem here - segfault
99 lbfgsminimize(Nfree, bfgsM, X, sqrt_eps, eps, sqrt_eps, maxiter, info);
100
101 // copy results back to x
102 j = 1;
103 for(int i=0; i<N; i++)
104 if (!fixed[i]) x[i] = X(j++);
105
106 return info;
107 }
108
109 private:
110
111 int N;
112 double eps;
113 double sqrt_eps;
114 TargetFP f;
115 void* pcopy;
116 double* xd;
117 int* fixed;
118
119 void setdefaults()
120 {
121 N = 0;
122 // seteps();
123 seteps(2.2e-16);
124 maxiter = 100;
125 }
126
127 // subroutines from lbfgs
128 void lbfgsminimize(const int&, const int&, ap::real_1d_array&, const double&, const double&,
129 const double&, const int&, int&);
130 void lbfgslincomb(const int&, const double&, const ap::real_1d_array&,
131 int, ap::real_1d_array&, int);
132 double lbfgsdotproduct(const int&, const ap::real_1d_array&, int,
133 const ap::real_1d_array&, int);
134 void lbfgsmcsrch(const int&, ap::real_1d_array&, double&, ap::real_1d_array&,
135 const ap::real_1d_array&, int, double&, const double&, const double&, const int&,
136 int&, int&, ap::real_1d_array&, const double&, const double&, const double&);
137 void lbfgsmcstep(double&, double&, double&, double&, double&, double&, double&,
138 const double&, const double&, bool&, const double&, const double&, int&);
139 void lbfgsnewiteration(const ap::real_1d_array&, double, const ap::real_1d_array&);
140
141 // f gradient: 1, 2, and 4-points approximations
142 void fgrad1(ap::real_1d_array&, double&, ap::real_1d_array&);
143 void fgrad2(ap::real_1d_array&, double&, ap::real_1d_array&);
144 void fgrad4(ap::real_1d_array&, double&, ap::real_1d_array&);
145 void funcgrad(ap::real_1d_array& x, double& fval, ap::real_1d_array& g) { fgrad2(x, fval, g); }
146
147};
148
149#endif //TTTRLIB_I_BFGS_H
Definition i_lbfgs.h:30
void seteps()
Definition i_lbfgs.h:56
void free(int n)
Definition i_lbfgs.h:71
int minimize(double *x, void *p)
Definition i_lbfgs.h:80
void fix(int n)
Definition i_lbfgs.h:67
void setN(int n)
Definition i_lbfgs.h:41
bfgs(TargetFP fun, int n)
Definition i_lbfgs.h:35
int maxiter
Definition i_lbfgs.h:77
void seteps(double e)
Definition i_lbfgs.h:50
bfgs(TargetFP fun)
Definition i_lbfgs.h:34
~bfgs()
Definition i_lbfgs.h:36
int fjac2(void(*)(double *, double *), double *, int, int, double, double *)
int fjac4(void(*)(double *, double *), double *, int, int, double, double *)
int fgrad2(void(*)(double *, double &), double *, int, double, double *)
int fgrad1(void(*)(double *, double &), double *, int, double, double *)
int fjac1(void(*)(double *, double *), double *, int, int, double, double *)
double(* TargetFP)(double *, void *)
Definition i_lbfgs.h:14
int fgrad4(void(*)(double *, double &), double *, int, double, double *)