-
Notifications
You must be signed in to change notification settings - Fork 196
/
mins_ndim.h
212 lines (212 loc) · 4.36 KB
/
mins_ndim.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
200
201
202
203
204
205
206
207
208
209
210
211
212
template <class T>
struct F1dim {
const VecDoub &p;
const VecDoub ξ
Int n;
T &func;
VecDoub xt;
F1dim(VecDoub_I &pp, VecDoub_I &xii, T &funcc) : p(pp),
xi(xii), n(pp.size()), func(funcc), xt(n) {}
Doub operator() (const Doub x)
{
for (Int j=0;j<n;j++)
xt[j]=p[j]+x*xi[j];
return func(xt);
}
};
template <class T>
struct Linemethod {
VecDoub p;
VecDoub xi;
T &func;
Int n;
Linemethod(T &funcc) : func(funcc) {}
Doub linmin()
{
Doub ax,xx,xmin;
n=p.size();
F1dim<T> f1dim(p,xi,func);
ax=0.0;
xx=1.0;
Brent brent;
brent.bracket(ax,xx,f1dim);
xmin=brent.minimize(f1dim);
for (Int j=0;j<n;j++) {
xi[j] *= xmin;
p[j] += xi[j];
}
return brent.fmin;
}
};
template <class T>
struct Df1dim {
const VecDoub &p;
const VecDoub ξ
Int n;
T &funcd;
VecDoub xt;
VecDoub dft;
Df1dim(VecDoub_I &pp, VecDoub_I &xii, T &funcdd) : p(pp),
xi(xii), n(pp.size()), funcd(funcdd), xt(n), dft(n) {}
Doub operator()(const Doub x)
{
for (Int j=0;j<n;j++)
xt[j]=p[j]+x*xi[j];
return funcd(xt);
}
Doub df(const Doub x)
{
Doub df1=0.0;
funcd.df(xt,dft);
for (Int j=0;j<n;j++)
df1 += dft[j]*xi[j];
return df1;
}
};
template <class T>
struct Dlinemethod {
VecDoub p;
VecDoub xi;
T &func;
Int n;
Dlinemethod(T &funcc) : func(funcc) {}
Doub linmin()
{
Doub ax,xx,xmin;
n=p.size();
Df1dim<T> df1dim(p,xi,func);
ax=0.0;
xx=1.0;
Dbrent dbrent;
dbrent.bracket(ax,xx,df1dim);
xmin=dbrent.minimize(df1dim);
for (Int j=0;j<n;j++) {
xi[j] *= xmin;
p[j] += xi[j];
}
return dbrent.fmin;
}
};
template <class T>
struct Powell : Linemethod<T> {
Int iter;
Doub fret;
using Linemethod<T>::func;
using Linemethod<T>::linmin;
using Linemethod<T>::p;
using Linemethod<T>::xi;
const Doub ftol;
Powell(T &func, const Doub ftoll=3.0e-8) : Linemethod<T>(func),
ftol(ftoll) {}
VecDoub minimize(VecDoub_I &pp)
{
Int n=pp.size();
MatDoub ximat(n,n,0.0);
for (Int i=0;i<n;i++) ximat[i][i]=1.0;
return minimize(pp,ximat);
}
VecDoub minimize(VecDoub_I &pp, MatDoub_IO &ximat)
{
const Int ITMAX=200;
const Doub TINY=1.0e-25;
Doub fptt;
Int n=pp.size();
p=pp;
VecDoub pt(n),ptt(n);
xi.resize(n);
fret=func(p);
for (Int j=0;j<n;j++) pt[j]=p[j];
for (iter=0;;++iter) {
Doub fp=fret;
Int ibig=0;
Doub del=0.0;
for (Int i=0;i<n;i++) {
for (Int j=0;j<n;j++) xi[j]=ximat[j][i];
fptt=fret;
fret=linmin();
if (fptt-fret > del) {
del=fptt-fret;
ibig=i+1;
}
}
if (2.0*(fp-fret) <= ftol*(abs(fp)+abs(fret))+TINY) {
return p;
}
if (iter == ITMAX) throw("powell exceeding maximum iterations.");
for (Int j=0;j<n;j++) {
ptt[j]=2.0*p[j]-pt[j];
xi[j]=p[j]-pt[j];
pt[j]=p[j];
}
fptt=func(ptt);
if (fptt < fp) {
Doub t=2.0*(fp-2.0*fret+fptt)*SQR(fp-fret-del)-del*SQR(fp-fptt);
if (t < 0.0) {
fret=linmin();
for (Int j=0;j<n;j++) {
ximat[j][ibig-1]=ximat[j][n-1];
ximat[j][n-1]=xi[j];
}
}
}
}
}
};
template <class T>
struct Frprmn : Linemethod<T> {
Int iter;
Doub fret;
using Linemethod<T>::func;
using Linemethod<T>::linmin;
using Linemethod<T>::p;
using Linemethod<T>::xi;
const Doub ftol;
Frprmn(T &funcd, const Doub ftoll=3.0e-8) : Linemethod<T>(funcd),
ftol(ftoll) {}
VecDoub minimize(VecDoub_I &pp)
{
const Int ITMAX=200;
const Doub EPS=1.0e-18;
const Doub GTOL=1.0e-8;
Doub gg,dgg;
Int n=pp.size();
p=pp;
VecDoub g(n),h(n);
xi.resize(n);
Doub fp=func(p);
func.df(p,xi);
for (Int j=0;j<n;j++) {
g[j] = -xi[j];
xi[j]=h[j]=g[j];
}
for (Int its=0;its<ITMAX;its++) {
iter=its;
fret=linmin();
if (2.0*abs(fret-fp) <= ftol*(abs(fret)+abs(fp)+EPS))
return p;
fp=fret;
func.df(p,xi);
Doub test=0.0;
Doub den=MAX(fp,1.0);
for (Int j=0;j<n;j++) {
Doub temp=abs(xi[j])*MAX(abs(p[j]),1.0)/den;
if (temp > test) test=temp;
}
if (test < GTOL) return p;
dgg=gg=0.0;
for (Int j=0;j<n;j++) {
gg += g[j]*g[j];
// dgg += xi[j]*xi[j];
dgg += (xi[j]+g[j])*xi[j];
}
if (gg == 0.0)
return p;
Doub gam=dgg/gg;
for (Int j=0;j<n;j++) {
g[j] = -xi[j];
xi[j]=h[j]=g[j]+gam*h[j];
}
}
throw("Too many iterations in frprmn");
}
};