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
|
/**
\file powell_method.hpp
*/
#ifndef POWELL_METHOD
#define POWELL_METHOD
#include <core/optimizer.hpp>
//#include <blitz/array.h>
#include <limits>
#include <cassert>
#include <cmath>
#include "linmin.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 powell_method
: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;
//typedef blitz::Array<rT,2> array2d_type;
private:
array1d_type start_point;
array1d_type end_point;
private:
int ncom;
array1d_type pcom_p;
array1d_type xicom_p;
rT threshold;
T** xi;
T* xi_1d;
private:
rT func(const pT& x)
{
assert(p_fo!=0);
return p_fo->eval(x);
}
private:
void clear_xi()
{
if(xi_1d!=0)
{
delete[] xi_1d;
}
if(xi!=0)
{
delete[] xi;
}
}
void init_xi(int n)
{
clear_xi();
xi_1d=new T[n*n];
xi=new T*[n];
for(int i=0;i!=n;++i)
{
xi[i]=xi_1d+i*n;
}
for(int i=0;i!=n;++i)
{
for(int j=0;j!=n;++j)
{
xi[i][j]=(j==i?1:0);
}
}
}
void powell(array1d_type& p,const T ftol,
int& iter,T& fret)
{
const int ITMAX=200;
const T TINY=std::numeric_limits<T>::epsilon();
int i,j,ibig;
T del,fp,fptt,t;
int n=(int)get_size(p);
array1d_type pt(n);
array1d_type ptt(n);
array1d_type xit(n);
fret=p_fo->eval(p);
for(j=0;j<n;++j)
{
//get_element(pt,j)=get_element(p,j);
set_element(pt,j,get_element(p,j));
}
for(iter=0;;++iter)
{
fp=fret;
ibig=0;
del=0.0;
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
//get_element(xit,j)=xi[j][i];
set_element(xit,j,xi[j][i]);
}
fptt=fret;
linmin(p,xit,fret,(*p_fo));
if((fptt-fret)>del)
{
del=fptt-fret;
ibig=i+1;
}
}
if(T(2.)*(fp-fret)<=ftol*(tabs(fp)+tabs(fret))+TINY)
{
return;
}
if(iter==ITMAX)
{
std::cerr<<"powell exceeding maximun iterations."<<std::endl;
return;
}
for(j=0;j<n;++j)
{
//get_element(ptt,j)=T(2.)*get_element(p,j)-get_element(pt,j);
set_element(ptt,j,T(2.)*get_element(p,j)-get_element(pt,j));
//get_element(xit,j)=
//get_element(p,j)-get_element(pt,j);
set_element(xit,j,get_element(p,j)-get_element(pt,j));
//get_element(pt,j)=get_element(p,j);
set_element(pt,j,get_element(p,j));
}
fptt=func(ptt);
if(fptt<fp)
{
t=T(2.)*(fp-T(2.)*fret+fptt)*sqr(T(fp-fret-del))-del*sqr(T(fp-fptt));
if(t<T(0.))
{
linmin(p,xit,fret,*p_fo);
for(j=0;j<n;++j)
{
xi[j][ibig-1]=xi[j][n-1];
xi[j][n-1]=get_element(xit,j);
}
}
}
}
}
public:
powell_method()
:threshold(1e-4),xi(0),xi_1d(0)
{}
virtual ~powell_method()
{
clear_xi();
};
powell_method(const powell_method<rT,pT>& rhs)
:p_fo(rhs.p_fo),p_optimizer(rhs.p_optimizer),
start_point(rhs.start_point),
end_point(rhs.end_point),
ncom(rhs.ncom),
threshold(rhs.threshold),xi(0),xi_1d(0)
{
}
powell_method<rT,pT>& operator=(const powell_method<rT,pT>& rhs)
{
threshold=rhs.threshold;
xi=0;
xi_1d=0;
p_fo=rhs.p_fo;
p_optimizer=rhs.p_optimizer;
start_point=rhs.start_point;
end_point=rhs.end_point;
ncom=rhs.ncom;
threshold=rhs.threshold;
}
opt_method<rT,pT>* do_clone()const
{
return new powell_method<rT,pT>(*this);
}
void do_set_start_point(const array1d_type& p)
{
resize(start_point,get_size(p));
opt_eq(start_point,p);
}
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;
}
void do_set_optimizer(optimizer<rT,pT>& o)
{
p_optimizer=&o;
p_fo=p_optimizer->ptr_func_obj();
}
pT do_optimize()
{
init_xi((int)get_size(start_point));
for(int i=0;i<(int)get_size(start_point);++i)
{
for(int j=0;j<(int)get_size(start_point);++j)
{
xi[i][j]=(i==j)?1:0;
}
}
int iter=100;
opt_eq(end_point,start_point);
rT fret;
powell(end_point,threshold,iter,fret);
return end_point;
}
};
}
#endif
//EOF
|