@@ -47,7 +47,7 @@ namespace libcmaes
47
47
// debug
48
48
49
49
pli le (k,samplesize,parameters._dim ,parameters._gp .pheno (x),minfvalue,fup,delta);
50
-
50
+
51
51
errstats<TGenoPheno>::profile_likelihood_search (func,parameters,le,cmasol,k,false ,samplesize,fup,delta,maxiters,curve); // positive direction
52
52
errstats<TGenoPheno>::profile_likelihood_search (func,parameters,le,cmasol,k,true ,samplesize,fup,delta,maxiters,curve); // negative direction
53
53
@@ -82,15 +82,16 @@ namespace libcmaes
82
82
while (true )
83
83
{
84
84
// get a new xk point.
85
- errstats<TGenoPheno>::take_linear_step (func,parameters,k,minfvalue,fup,delta,n,linit,d,x);
85
+ errstats<TGenoPheno>::take_linear_step (func,parameters,k,minfvalue,fup,delta,n,linit,cmasol. eigenvectors (), d,x);
86
86
linit = false ;
87
87
88
88
// debug
89
89
// std::cout << "new xk point: " << x.transpose() << std::endl;
90
90
// debug
91
91
92
92
// minimize.
93
- CMASolutions ncitsol = errstats<TGenoPheno>::optimize_pk (func,parameters,citsol,k,x[k]);
93
+ dVec nx;
94
+ CMASolutions ncitsol = errstats<TGenoPheno>::optimize_pk (func,parameters,citsol,k,x,nx);
94
95
if (ncitsol._run_status < 0 )
95
96
{
96
97
LOG (ERROR) << " profile likelihood linesearch: optimization error " << ncitsol._run_status << " -- " << ncitsol.status_msg () << std::endl;
@@ -117,13 +118,12 @@ namespace libcmaes
117
118
}
118
119
else // update current point and solution.
119
120
{
120
- // citsol = ncitsol; // XXX: doesn't work well, restarting from min works better.
121
- x = ncitsol.best_candidate ().get_x_dvec ();
121
+ x = nx;
122
122
nminfvalue = ncitsol.best_candidate ().get_fvalue ();
123
123
}
124
124
125
125
// store points.
126
- dVec phenobx = parameters._gp .pheno (ncitsol. best_candidate (). get_x_dvec () );
126
+ dVec phenobx = parameters._gp .pheno (nx );
127
127
if (curve)
128
128
{
129
129
le._fvaluem [samplesize+sign*(1 +i)] = ncitsol.best_candidate ().get_fvalue ();
@@ -148,7 +148,6 @@ namespace libcmaes
148
148
}
149
149
return ;
150
150
}
151
- // std::cout << "i=" << i << std::endl;
152
151
++i;
153
152
if (curve && i == samplesize)
154
153
break ;
@@ -166,6 +165,7 @@ namespace libcmaes
166
165
const double &delta,
167
166
const int &n,
168
167
const bool &linit,
168
+ const dMat &eigenve,
169
169
double &d,
170
170
dVec &x)
171
171
{
@@ -179,18 +179,20 @@ namespace libcmaes
179
179
// std::cerr << "d=" << d << " / fdiff=" << fdiff << " / xtmpk=" << xtmp[k] << std::endl;
180
180
// debug
181
181
182
+ dVec dv = dVec::Zero (parameters.dim ());
183
+ dv[k] = d;
182
184
int i = n;
183
185
if (fdiff > fup + fdelta) // above
184
186
{
185
- // std::cerr << "above\n";
186
- x[k] -= d;
187
+ x -= eigenve.transpose () * dv;
187
188
while (fdiff > fup + fdelta
188
189
&& i > 0
189
190
&& phenoxtmp[k] >= parameters._gp .get_boundstrategy ().getPhenoLBound (k)
190
191
&& phenoxtmp[k] <= parameters._gp .get_boundstrategy ().getPhenoUBound (k))
191
192
{
192
193
d /= 2.0 ;
193
- xtmp[k] = x[k] + d;
194
+ dv[k] = d;
195
+ xtmp = x + eigenve.transpose () * dv; // search in rotated space
194
196
phenoxtmp = parameters._gp .pheno (xtmp);
195
197
fvalue = func (phenoxtmp.data (),xtmp.size ());
196
198
fdiff = fabs (fvalue - minfvalue);
@@ -205,14 +207,14 @@ namespace libcmaes
205
207
}
206
208
else // below
207
209
{
208
- // std::cerr << "below\n";
209
210
while (fdiff < fup - fdelta
210
211
&& i > 0
211
212
&& phenoxtmp[k] >= parameters._gp .get_boundstrategy ().getPhenoLBound (k)
212
213
&& phenoxtmp[k] <= parameters._gp .get_boundstrategy ().getPhenoUBound (k))
213
214
{
214
215
d *= 2.0 ;
215
- xtmp[k] = x[k] + d;
216
+ dv[k] = d;
217
+ xtmp = x + eigenve.transpose () * dv; // search in rotated space
216
218
phenoxtmp = parameters._gp .pheno (xtmp);
217
219
fvalue = func (phenoxtmp.data (),xtmp.size ());
218
220
fdiff = fabs (fvalue - minfvalue);
@@ -234,30 +236,56 @@ namespace libcmaes
234
236
const CMAParameters<TGenoPheno> ¶meters,
235
237
const CMASolutions &cmasol,
236
238
const std::vector<int > &k,
237
- const std::vector<double > &vk)
239
+ const std::vector<double > &vk,
240
+ const dVec &x0,
241
+ dVec &nx)
238
242
{
239
- CMASolutions ncmasol = cmasol;
240
- CMAParameters<TGenoPheno> nparameters = parameters;
241
- nparameters._quiet = true ;
242
- ncmasol._sigma = nparameters._sigma_init ;
243
+ dVec rx0 = x0;
244
+ double sigma = 0.0 ;
243
245
for (size_t i=0 ;i<k.size ();i++)
244
246
{
245
- nparameters. set_fixed_p ( k[i], vk[i]);
246
- nparameters. _sigma_init = ncmasol. _sigma = std::max (ncmasol. _sigma , fabs (cmasol. best_candidate (). get_x_dvec_ref ()[ k[i]]-vk[i])); // XXX: here sigma as max distance to best candidate in parameter space. There might be better selection schemes.
247
+ sigma = std::max (sigma, fabs (cmasol. best_candidate (). get_x_dvec_ref ()[ k[i]]- vk[i]) );
248
+ removeElement (rx0, k[i]);
247
249
}
248
- return cmaes<TGenoPheno>(func,nparameters,CMAStrategy<CovarianceUpdate,TGenoPheno>::_defaultPFunc,nullptr ,ncmasol); // XXX: we're not explicitely setting the initial covariance because current set of experiments shows that it doesn't work well.
250
+ CMAParameters<TGenoPheno> nparameters (rx0.size (),rx0.data (),sigma,parameters.lambda ());
251
+ nparameters.set_ftarget (parameters.get_ftarget ());
252
+
253
+ FitFunc rfunc = [func,k,vk](const double *x, const int N)
254
+ {
255
+ std::vector<double > nx;
256
+ nx.reserve (N+1 );
257
+ for (size_t j=0 ;j<k.size ();j++)
258
+ {
259
+ for (int i=0 ;i<N+1 ;i++)
260
+ {
261
+ if (i < k[j])
262
+ nx.push_back (x[i]);
263
+ else if (i == k[j])
264
+ nx.push_back (vk[j]);
265
+ else nx.push_back (x[i-1 ]);
266
+ }
267
+ }
268
+ return func (&nx.front (),N+1 );
269
+ };
270
+
271
+ CMASolutions cms = cmaes<TGenoPheno>(rfunc,nparameters);
272
+ nx = cms.best_candidate ().get_x_dvec ();
273
+ for (size_t i=0 ;i<k.size ();i++)
274
+ addElement (nx,k[i],vk[i]);
275
+ return cms;
249
276
}
250
277
251
278
template <class TGenoPheno >
252
279
CMASolutions errstats<TGenoPheno>::optimize_pk(FitFunc &func,
253
280
const CMAParameters<TGenoPheno> ¶meters,
254
281
const CMASolutions &cmasol,
255
282
const int &k,
256
- const double &vk)
283
+ const dVec &vk,
284
+ dVec &nx)
257
285
{
258
286
std::vector<int > tk = {k};
259
- std::vector<double > tvk = {vk};
260
- return errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,tk,tvk);
287
+ std::vector<double > tvk = {vk[k] };
288
+ return errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,tk,tvk,vk,nx );
261
289
}
262
290
263
291
template <class TGenoPheno >
@@ -423,13 +451,14 @@ namespace libcmaes
423
451
double tlf = ftol*fup;
424
452
int nfcn = 0 ;
425
453
unsigned int maxitr = 15 , ipt = 0 ;
454
+ dVec nx;
426
455
427
456
// debug
428
457
// std::cout << "aminsv=" << aminsv << " / aim=" << aim << " / tla=" << tla << " / tlf=" << tlf << std::endl;
429
458
// debug
430
459
431
- // get a first optimized point.
432
- CMASolutions cmasol1 = errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,par,pmid);
460
+ // get a first optimized point.
461
+ CMASolutions cmasol1 = errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,par,pmid,parameters. get_x0min (),nx );
433
462
alsb[0 ] = 0.0 ;
434
463
flsb[0 ] = cmasol1.best_candidate ().get_fvalue ();
435
464
flsb[0 ] = std::max (flsb[0 ],aminsv+0.1 *fup);
@@ -448,7 +477,7 @@ namespace libcmaes
448
477
else if (aopt < -0.5 )
449
478
aopt = 0.5 ;
450
479
std::vector<double > pmiddir2 = {pmid[0 ]+aopt*pdir[0 ],pmid[1 ]+aopt*pdir[1 ]};
451
- CMASolutions cmasol2 = errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,par,pmiddir2);
480
+ CMASolutions cmasol2 = errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,par,pmiddir2,parameters. get_x0min (),nx );
452
481
alsb[1 ] = aopt;
453
482
flsb[1 ] = cmasol2.best_candidate ().get_fvalue ();
454
483
nfcn += cmasol2._nevals ;
@@ -477,7 +506,7 @@ namespace libcmaes
477
506
flsb[0 ] = flsb[1 ];
478
507
aopt = alsb[0 ] + 0.2 *(it+1 );
479
508
std::vector<double > pmidt = {pmid[0 ]+aopt*pdir[0 ],pmid[1 ]+aopt*pdir[1 ]};
480
- CMASolutions cmasolt = errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,par,pmidt);
509
+ CMASolutions cmasolt = errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,par,pmidt,parameters. get_x0min (),nx );
481
510
alsb[1 ] = aopt;
482
511
flsb[1 ] = cmasolt.best_candidate ().get_fvalue ();
483
512
dfda = (flsb[1 ]-flsb[0 ])/(alsb[1 ]-alsb[0 ]);
@@ -517,7 +546,7 @@ namespace libcmaes
517
546
518
547
// get third point.
519
548
std::vector<double > pmiddir3 = {pmid[0 ]+aopt*pdir[0 ],pmid[1 ]+aopt*pdir[1 ]};
520
- CMASolutions cmasol3 = errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,par,pmiddir3);
549
+ CMASolutions cmasol3 = errstats<TGenoPheno>::optimize_vpk (func,parameters,cmasol,par,pmiddir3,parameters. get_x0min (),nx );
521
550
alsb[2 ] = aopt;
522
551
flsb[2 ] = cmasol3.best_candidate ().get_fvalue ();
523
552
nfcn += cmasol3._nevals ;
@@ -609,7 +638,7 @@ namespace libcmaes
609
638
// debug
610
639
611
640
std::vector<double > vxk = {xstart (par[0 ]),xstart (par[1 ])};
612
- CMASolutions ncitsol = errstats<TGenoPheno>::optimize_vpk (func,parameters,citsol,par,vxk);
641
+ CMASolutions ncitsol = errstats<TGenoPheno>::optimize_vpk (func,parameters,citsol,par,vxk,parameters. get_x0min (),nx );
613
642
if (ncitsol._run_status < 0 )
614
643
{
615
644
LOG (WARNING) << " contour linesearch: optimization error " << ncitsol._run_status << std::endl;
0 commit comments