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
|
/**
\file conjugate_gradient.hpp
\brief powerll optimization method
\author Junhua Gu
*/
#ifndef CONJUGATE_GRADIENT
#define CONJUGATE_GRADIENT
#define OPT_HEADER
#include <core/optimizer.hpp>
//#include <blitz/array.h>
#include <limits>
#include <cassert>
#include <cmath>
#include "../linmin/linmin.hpp"
#include <math/num_diff.hpp>
#include <algorithm>
#include <iostream>
namespace opt_utilities
{
/**
\brief Impliment of an optimization method
\tparam rT return type of the object function
\tparam pT parameter type of the object function
*/
template <typename rT,typename pT>
class conjugate_gradient
:public opt_method<rT,pT>
{
public:
typedef pT array1d_type;
typedef rT T;
private:
func_obj<rT,pT>* p_fo;
optimizer<rT,pT>* p_optimizer;
volatile bool bstop;
//typedef blitz::Array<rT,2> array2d_type;
const char* do_get_type_name()const
{
return "conjugate gradient method";
}
private:
array1d_type start_point;
array1d_type end_point;
private:
rT threshold;
array1d_type g;
array1d_type h;
array1d_type xi;
private:
rT func(const pT& x)
{
assert(p_fo!=0);
return p_fo->eval(x);
}
private:
void clear_xi()
{
}
void init_xi(int n)
{
clear_xi();
g=array1d_type(n);
h=array1d_type(n);
xi=array1d_type(n);
}
void cg(array1d_type& p,const T ftol,
int& iter,T& fret)
{
const int ITMAX=200;
const T EPS=std::numeric_limits<T>::epsilon();
int j,its;
int n=p.size();
T gg,gam,fp,dgg;
fp=func(p);
xi=gradient(*p_fo,p);
for(j=0;j<n;++j)
{
g[j]=-xi[j];
xi[j]=h[j]=g[j];
}
for(its=1;its<=ITMAX;++its)
{
iter=its;
linmin(p,xi,fret,*p_fo);
//std::cerr<<"######:"<<its<<"\t"<<abs(fret-fp)/(abs(fret)+fabs(fp)+EPS)<<std::endl;
if(2.0*abs(fret-fp)<=ftol*(abs(fret)+fabs(fp)+EPS))
{
return;
}
fp=func(p);
xi=gradient(*p_fo,p);
dgg=gg=0;
for(j=0;j<n;++j)
{
gg+=g[j]*g[j];
//dgg+=(xi[j]+g[j])*xi[j];
dgg+=xi[j]*xi[j];
}
std::cerr<<its<<"\t"<<gg<<std::endl;
if(gg==0.0)
{
return;
}
gam=dgg/gg;
for(j=0;j<n;++j)
{
g[j]=-xi[j];
xi[j]=h[j]=g[j]+gam*h[j];
}
}
std::cerr<<"Too many iterations in cg"<<std::endl;
}
public:
conjugate_gradient()
:threshold(1e-4),g(0),h(0),xi(0)
{}
virtual ~conjugate_gradient()
{
clear_xi();
};
conjugate_gradient(const conjugate_gradient<rT,pT>& rhs)
:opt_method<rT,pT>(rhs),p_fo(rhs.p_fo),p_optimizer(rhs.p_optimizer),
start_point(rhs.start_point),
end_point(rhs.end_point),
threshold(rhs.threshold),g(0),h(0),xi(0)
{
}
conjugate_gradient<rT,pT>& operator=(const conjugate_gradient<rT,pT>& rhs)
{
threshold=rhs.threshold;
p_fo=rhs.p_fo;
p_optimizer=rhs.p_optimizer;
start_point=rhs.start_point;
end_point=rhs.end_point;
threshold=rhs.threshold;
}
opt_method<rT,pT>* do_clone()const
{
return new conjugate_gradient<rT,pT>(*this);
}
void do_set_start_point(const array1d_type& p)
{
resize(start_point,get_size(p));
opt_eq(start_point,p);
}
array1d_type do_get_start_point()const
{
return start_point;
}
void do_set_lower_limit(const array1d_type& p)
{}
void do_set_upper_limit(const array1d_type& p)
{}
void do_set_precision(rT t)
{
threshold=t;
}
rT do_get_precision()const
{
return threshold;
}
void do_set_optimizer(optimizer<rT,pT>& o)
{
p_optimizer=&o;
p_fo=p_optimizer->ptr_func_obj();
}
pT do_optimize()
{
bstop=false;
init_xi((int)get_size(start_point));
int iter=100;
opt_eq(end_point,start_point);
rT fret;
#if 0
for(int i=0;i<get_size(start_point);++i)
{
array1d_type direction(start_point.size());
direction[i]=1;
linmin(end_point,direction,fret,(*p_fo));
}
#endif
cg(end_point,threshold,iter,fret);
return end_point;
}
void do_stop()
{
bstop=true;
}
};
}
#endif
//EOF
|