Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
cmaes.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/* --------------------------------------------------------- */
19/* --- File: cmaes.c -------- Author: Nikolaus Hansen --- */
20/* --------------------------------------------------------- */
21/*
22 CMA-ES for non-linear function minimization.
23
24 Copyright 1996, 2003, 2007, 2013 Nikolaus Hansen
25 e-mail: hansen .AT. lri.fr
26
27 SOURCE:
28 https://github.com/cma-es/c-cma-es
29 https://github.com/cma-es/c-cma-es/blob/master/src/cmaes.c
30
31 LICENSE: this library is free/open software and may be used
32 either under the
33
34 Apache License 2.0
35
36 or under the
37
38 GNU Lesser General Public License 2.1 or later
39
40 whichever suits best.
41
42 See also the LICENSE file
43 https://github.com/cma-es/c-cma-es/blob/master/LICENSE
44*/
45
46/* --- Changes : ---
47 03/03/21: argument const double *rgFunVal of
48 cmaes_ReestimateDistribution() was treated incorrectly.
49 03/03/29: restart via cmaes_resume_distribution() implemented.
50 03/03/30: Always max std dev / largest axis is printed first.
51 03/08/30: Damping is adjusted for large mueff.
52 03/10/30: Damping is adjusted for large mueff always.
53 04/04/22: Cumulation time and damping for step size adjusted.
54 No iniphase but conditional update of pc.
55 05/03/15: in ccov-setting mucov replaced by mueff.
56 05/10/05: revise comment on resampling in example.c
57 05/10/13: output of "coorstddev" changed from sigma * C[i][i]
58 to correct sigma * sqrt(C[i][i]).
59 05/11/09: Numerical problems are not anymore handled by increasing
60 sigma, but lead to satisfy a stopping criterion in
61 cmaes_Test().
62 05/11/09: Update of eigensystem and test for numerical problems
63 moved right before sampling.
64 06/02/24: Non-ansi array definitions replaced (thanks to Marc
65 Toussaint).
66 06/02/25: Overflow in time measurement for runs longer than
67 2100 seconds. This could lead to stalling the
68 covariance matrix update for long periods.
69 Time measurement completely rewritten.
70 06/02/26: Included population size lambda as parameter to
71 cmaes_init (thanks to MT).
72 06/02/26: Allow no initial reading/writing of parameters via
73 "non" and "writeonly" keywords for input parameter
74 filename in cmaes_init.
75 06/02/27: Optimized code regarding time spent in updating the
76 covariance matrix in function Adapt_C2().
77 07/08/03: clean up and implementation of an exhaustive test
78 of the eigendecomposition (via #ifdef for now)
79 07/08/04: writing of output improved
80 07/08/xx: termination criteria revised and more added,
81 damp replaced by damps=damp*cs, documentation improved.
82 Interface significantly changed, evaluateSample function
83 and therefore the function pointer argument removed.
84 Renaming of functions in accordance with Java code.
85 Clean up of parameter names, mainly in accordance with
86 Matlab conventions. Most termination criteria can be
87 changed online now. Many more small changes, but not in
88 the core procedure.
89 07/10/29: ReSampleSingle() got a better interface. ReSampleSingle()
90 is now ReSampleSingle_old only for backward
91 compatibility. Also fixed incorrect documentation. The new
92 function SampleSingleInto() has an interface similar to
93 the old ReSampleSingle(), but is not really necessary.
94 07/11/20: bug: stopMaxIter did not translate into the correct default
95 value but into -1 as default. This lead to a too large
96 damps and the termination test became true from the first
97 iteration. (Thanks to Michael Calonder)
98 07/11/20: new default stopTolFunHist = 1e-13; (instead of zero)
99 08/09/26: initial diagonal covariance matrix in code, but not
100 yet in interface
101 08/09/27: diagonalCovarianceMatrix option in initials.par provided
102 08/10/17: uncertainty handling implemented in example3.c.
103 PerturbSolutionInto() provides the optional small
104 perturbations before reevaluation.
105 10/10/16: TestForTermination changed such that diagonalCovarianceMatrix
106 option now yields linear time behavior
107 12/05/28: random seed > 2e9 prohibited to avoid an infinite loop on 32bit systems
108 12/10/21: input parameter file values "no", "none" now work as "non".
109 12/10/xx: tentative implementation of cmaes_Optimize
110 12/10/xx: some small changes with char * mainly to prevent warnings in C++
111 12/10/xx: added some string convenience functions isNoneStr, new_string, assign_string
112 13/01/03: rename files example?, initials.par, signals.par
113 14/04/29: removed bug, au = t->al[...], from the (new) boundary handling
114 code (thanks to Emmanuel Benazera for the hint)
115 14/06/16: test of un-initialized version number removed (thanks to Paul Zimmermann)
116 14/10/18: splitted cmaes_init() into cmaes_init_para() and cmaes_init_final(),
117 such that parameters can be set also comparatively safely within the
118 code, namely after calling cmaes_init_para(). The order of parameters
119 of readpara_init() has changed to be the same as for cmaes_init().
120 14/11/25: fix warnings from Microsoft C compiler (sherm)
121 14/11/26: renamed exported symbols so they begin with a cmaes_prefix.
122
123 Wish List
124 o make signals_filename part of cmaes_t using assign_string()
125
126 o as writing time is measure for all files at once, the display
127 cannot be independently written to a file via signals.par, while
128 this would be desirable.
129
130 o clean up sorting of eigenvalues and vectors which is done repeatedly.
131
132 o either use cmaes_Get() in cmaes_WriteToFilePtr(): revise the
133 cmaes_write that all keywords available with get and getptr are
134 recognized. Also revise the keywords, keeping backward
135 compatibility. (not only) for this it would be useful to find a
136 way how cmaes_Get() signals an unrecognized keyword. For GetPtr
137 it can return NULL.
138
139 o or break cmaes_Get() into single getter functions, being a nicer
140 interface, and compile instead of runtime error, and faster. For
141 file signals.par it does not help.
142
143 o writing data depending on timing in a smarter way, e.g. using 10%
144 of all time. First find out whether clock() is useful for measuring
145 disc writing time and then cmaes_timings_t class can be utilized.
146 For very large dimension the default of 1 seconds waiting might
147 be too small.
148
149 o allow modification of best solution depending on delivered f(xmean)
150
151 o re-write input and output procedures
152*/
153
154/* Prevent Microsoft compiler from complaining that common library functions
155 like strncpy(), ctime(), sprintf(), fopen(), fscanf(), etc. "may be unsafe".
156 This must come before the first #include.
157*/
158#ifdef _MSC_VER
159#define _CRT_SECURE_NO_WARNINGS
160#endif
161
162#include <math.h> /* sqrt() */
163#include <stddef.h> /* size_t */
164#include <stdlib.h> /* NULL, free */
165#include <string.h> /* strlen() */
166#include <stdio.h> /* sprintf(), NULL? */
167#include "cmaes_interface.h" /* <time.h> via cmaes.h */
168
169/* --------------------------------------------------------- */
170/* ------------------- Declarations ------------------------ */
171/* --------------------------------------------------------- */
172
173/* ------------------- External Visibly -------------------- */
174
175/* see cmaes_interface.h for those, not listed here */
176
177long cmaes_random_init(cmaes_random_t *, long unsigned seed /* 0==clock */);
179double cmaes_random_Gauss(cmaes_random_t *); /* (0,1)-normally distributed */
181long cmaes_random_Start(cmaes_random_t *, long unsigned seed /* 0==1 */);
182
184void cmaes_timings_start(cmaes_timings_t *timing); /* fields totaltime and tictoctime */
187double cmaes_timings_toc(cmaes_timings_t *timing);
188
189void cmaes_readpara_init (cmaes_readpara_t *, int dim, const double * xstart,
190 const double * sigma, int seed, int lambda,
191 const char * filename);
193void cmaes_readpara_ReadFromFile(cmaes_readpara_t *, const char *szFileName);
195void cmaes_readpara_SetWeights(cmaes_readpara_t *, const char * mode);
196void cmaes_readpara_WriteToFile(cmaes_readpara_t *, const char *filenamedest);
197
198const double * cmaes_Optimize( cmaes_t *, double(*pFun)(double const *, int dim),
199 long iterations);
200double const * cmaes_SetMean(cmaes_t *, const double *xmean);
201double * cmaes_PerturbSolutionInto(cmaes_t *t, double *xout,
202 double const *xin, double eps);
203void cmaes_WriteToFile(cmaes_t *, const char *key, const char *name);
204void cmaes_WriteToFileAW(cmaes_t *t, const char *key, const char *name,
205 const char * append);
206void cmaes_WriteToFilePtr(cmaes_t *, const char *key, FILE *fp);
207void cmaes_ReadFromFilePtr(cmaes_t *, FILE *fp);
208void cmaes_FATAL(char const *s1, char const *s2,
209 char const *s3, char const *s4);
210
211
212/* ------------------- Locally visibly ----------------------- */
213
214static char * getTimeStr(void);
215static void TestMinStdDevs( cmaes_t *);
216/* static void WriteMaxErrorInfo( cmaes_t *); */
217
218static void Eigen( int N, double **C, double *diag, double **Q,
219 double *rgtmp);
220static int Check_Eigen( int N, double **C, double *diag, double **Q);
221static void QLalgo2 (int n, double *d, double *e, double **V);
222static void Householder2(int n, double **V, double *d, double *e);
223static void Adapt_C2(cmaes_t *t, int hsig);
224
225static void FATAL(char const *sz1, char const *s2,
226 char const *s3, char const *s4);
227static void ERRORMESSAGE(char const *sz1, char const *s2,
228 char const *s3, char const *s4);
229static int isNoneStr(const char * filename);
230static void Sorted_index( const double *rgFunVal, int *index, int n);
231static int SignOfDiff( const void *d1, const void * d2);
232static double douSquare(double);
233static double rgdouMax( const double *rgd, int len);
234static double rgdouMin( const double *rgd, int len);
235static double douMax( double d1, double d2);
236static double douMin( double d1, double d2);
237static int MaxIdx( const double *rgd, int len);
238static int MinIdx( const double *rgd, int len);
239static double myhypot(double a, double b);
240static double * new_double( int n);
241static void * new_void( int n, size_t size);
242static char * new_string( const char *);
243static void assign_string( char **, const char*);
244
245static const char * c_cmaes_version = "3.20.00.beta";
246
247/* --------------------------------------------------------- */
248/* ---------------- Functions: cmaes_t --------------------- */
249/* --------------------------------------------------------- */
250
251static char *
253 time_t tm = time(NULL);
254 static char s[33];
255
256 /* get time */
257 strncpy(s, ctime(&tm), 24); /* TODO: hopefully we read something useful */
258 s[24] = '\0'; /* cut the \n */
259 return s;
260}
261
262char *
264{
265 /* write initial message */
266 sprintf(t->sOutString,
267 "(%d,%d)-CMA-ES(mu_eff=%.1f), Ver=\"%s\", dimension=%d, diagonalIterations=%ld, randomSeed=%d (%s)",
268 t->sp.mu, t->sp.lambda, t->sp.mueff, t->version, t->sp.N, (long)t->sp.diagonalCov,
269 t->sp.seed, getTimeStr());
270
271 return t->sOutString;
272}
273
274/* --------------------------------------------------------- */
275/* --------------------------------------------------------- */
276void
277cmaes_init_para(cmaes_t *t, /* "this" */
278 int dimension,
279 double *inxstart,
280 double *inrgstddev, /* initial stds */
281 long int inseed,
282 int lambda,
283 const char *input_parameter_filename)
284{
286 cmaes_readpara_init(&t->sp, dimension, inxstart, inrgstddev, inseed,
287 lambda, input_parameter_filename);
288}
289
290double *
291cmaes_init_final(cmaes_t *t /* "this" */)
292/*
293 * */
294{
295 int i, j, N;
296 double dtest, trace;
297
298 if (t->version == NULL) {
299 ERRORMESSAGE("cmaes_init_final called (probably) without calling cmaes_init_para first",
300 ", which will likely lead to unexpected results",0,0);
301 printf("Error: cmaes_init_final called (probably) without calling cmaes_init_para first\n");
302 }
303 if (strcmp(c_cmaes_version, t->version) != 0) {
304 ERRORMESSAGE("cmaes_init_final called twice, which will lead to a memory leak",
305 "; use cmaes_exit() first",0,0);
306 printf("Error: cmaes_init_final called twice, which will lead to a memory leak; use cmaes_exit first\n");
307 }
308 /* assign_string(&t->signalsFilename, "cmaes_signals.par"); */
309
310 if (!t->sp.flgsupplemented) {
312 if (!isNoneStr(t->sp.filename)) /* TODO: should this be done in readpara_SupplementDefaults? */
313 cmaes_readpara_WriteToFile(&t->sp, "actparcmaes.par");
314 }
315
316 t->sp.seed = cmaes_random_init( &t->rand, (long unsigned int) t->sp.seed);
317
318 N = t->sp.N; /* for convenience */
319
320 /* initialization */
321 for (i = 0, trace = 0.; i < N; ++i)
322 trace += t->sp.rgInitialStds[i]*t->sp.rgInitialStds[i];
323 t->sigma = sqrt(trace/N); /* t->sp.mueff/(0.2*t->sp.mueff+sqrt(N)) * sqrt(trace/N); */
324
325 t->chiN = sqrt((double) N) * (1. - 1./(4.*N) + 1./(21.*N*N));
327 t->flgCheckEigen = 0;
328 t->genOfEigensysUpdate = 0;
330 t->flgIniphase = 0; /* do not use iniphase, hsig does the job now */
331 t->flgresumedone = 0;
332 t->flgStop = 0;
333
334 for (dtest = 1.; dtest && dtest < 1.1 * dtest; dtest *= 2.)
335 if (dtest == dtest + 1.)
336 break;
337 t->dMaxSignifKond = dtest / 1000.; /* not sure whether this is really save, 100 does not work well enough */
338
339 t->gen = 0;
340 t->countevals = 0;
341 t->state = 0;
342 t->dLastMinEWgroesserNull = 1.0;
343 t->printtime = t->writetime = t->firstwritetime = t->firstprinttime = 0;
344
345 t->rgpc = new_double(N);
346 t->rgps = new_double(N);
347 t->rgdTmp = new_double(N+1);
348 t->rgBDz = new_double(N);
349 t->rgxmean = new_double(N+2); t->rgxmean[0] = N; ++t->rgxmean;
350 t->rgxold = new_double(N+2); t->rgxold[0] = N; ++t->rgxold;
351 t->rgxbestever = new_double(N+3); t->rgxbestever[0] = N; ++t->rgxbestever;
352 t->rgout = new_double(N+2); t->rgout[0] = N; ++t->rgout;
353 t->rgD = new_double(N);
354 t->C = (double**)new_void(N, sizeof(double*));
355 t->B = (double**)new_void(N, sizeof(double*));
357 t->rgFuncValue = new_double(t->sp.lambda+1);
358 t->rgFuncValue[0]=t->sp.lambda; ++t->rgFuncValue;
359 t->arFuncValueHist = new_double(10+(int)ceil(3.*10.*N/t->sp.lambda)+1);
360 t->arFuncValueHist[0] = (double)(10+(int)ceil(3.*10.*N/t->sp.lambda));
361 t->arFuncValueHist++;
362
363 for (i = 0; i < N; ++i) {
364 t->C[i] = new_double(i+1);
365 t->B[i] = new_double(N);
366 }
367 t->index = (int *) new_void(t->sp.lambda, sizeof(int));
368 for (i = 0; i < t->sp.lambda; ++i)
369 t->index[i] = i; /* should not be necessary */
370 t->rgrgx = (double **)new_void(t->sp.lambda, sizeof(double*));
371 for (i = 0; i < t->sp.lambda; ++i) {
372 t->rgrgx[i] = new_double(N+2);
373 t->rgrgx[i][0] = N;
374 t->rgrgx[i]++;
375 }
376
377 /* Initialize newed space */
378
379 for (i = 0; i < N; ++i)
380 for (j = 0; j < i; ++j)
381 t->C[i][j] = t->B[i][j] = t->B[j][i] = 0.;
382
383 for (i = 0; i < N; ++i)
384 {
385 t->B[i][i] = 1.;
386 t->C[i][i] = t->rgD[i] = t->sp.rgInitialStds[i] * sqrt(N / trace);
387 t->C[i][i] *= t->C[i][i];
388 t->rgpc[i] = t->rgps[i] = 0.;
389 }
390
391 t->minEW = rgdouMin(t->rgD, N); t->minEW = t->minEW * t->minEW;
392 t->maxEW = rgdouMax(t->rgD, N); t->maxEW = t->maxEW * t->maxEW;
393
394 t->maxdiagC=t->C[0][0]; for(i=1;i<N;++i) if(t->maxdiagC<t->C[i][i]) t->maxdiagC=t->C[i][i];
395 t->mindiagC=t->C[0][0]; for(i=1;i<N;++i) if(t->mindiagC>t->C[i][i]) t->mindiagC=t->C[i][i];
396
397 /* set xmean */
398 for (i = 0; i < N; ++i)
399 t->rgxmean[i] = t->rgxold[i] = t->sp.xstart[i];
400 /* use in case xstart as typicalX */
401 if (t->sp.typicalXcase)
402 for (i = 0; i < N; ++i)
403 t->rgxmean[i] += t->sigma * t->rgD[i] * cmaes_random_Gauss(&t->rand);
404
405 if (strcmp(t->sp.resumefile, "_no_") != 0)
407
408 return (t->publicFitness);
409
410} /* cmaes_init_final() */
411
412
413/* --------------------------------------------------------- */
414/* --------------------------------------------------------- */
415double *
416cmaes_init(cmaes_t *t, /* "this" */
417 int dimension,
418 double *inxstart,
419 double *inrgstddev, /* initial stds */
420 long int inseed,
421 int lambda,
422 const char *input_parameter_filename)
423{
424 cmaes_init_para(t, dimension, inxstart, inrgstddev, inseed,
425 lambda, input_parameter_filename);
426 return cmaes_init_final(t);
427}
428
429/* --------------------------------------------------------- */
430/* --------------------------------------------------------- */
431
432#ifdef __GNUC__
433 #pragma GCC diagnostic push
434 #pragma GCC diagnostic ignored "-Wunused-result"
435#endif
436
437void
439{
440 int i, j, res, n;
441 double d;
442 FILE *fp = fopen( filename, "r");
443 if(fp == NULL) {
444 ERRORMESSAGE("cmaes_resume_distribution(): could not open '",
445 filename, "'",0);
446 return;
447 }
448 /* count number of "resume" entries */
449 i = 0; res = 0;
450 while (1) {
451 if ((res = fscanf(fp, " resume %lg", &d)) == EOF)
452 break;
453 else if (res==0)
454 fscanf(fp, " %*s");
455 else if(res > 0)
456 i += 1;
457 }
458
459 /* go to last "resume" entry */
460 n = i; i = 0; res = 0; rewind(fp);
461 while (i<n) {
462 if ((res = fscanf(fp, " resume %lg", &d)) == EOF)
463 FATAL("cmaes_resume_distribution(): Unexpected error, bug",0,0,0);
464 else if (res==0)
465 fscanf(fp, " %*s");
466 else if(res > 0)
467 ++i;
468 }
469 if (d != t->sp.N)
470 FATAL("cmaes_resume_distribution(): Dimension numbers do not match",0,0,0);
471
472 /* find next "xmean" entry */
473 while (1) {
474 if ((res = fscanf(fp, " xmean %lg", &d)) == EOF)
475 FATAL("cmaes_resume_distribution(): 'xmean' not found",0,0,0);
476 else if (res==0)
477 fscanf(fp, " %*s");
478 else if(res > 0)
479 break;
480 }
481
482 /* read xmean */
483 t->rgxmean[0] = d; res = 1;
484 for(i = 1; i < t->sp.N; ++i)
485 res += fscanf(fp, " %lg", &t->rgxmean[i]);
486 if (res != t->sp.N)
487 FATAL("cmaes_resume_distribution(): xmean: dimensions differ",0,0,0);
488
489 /* find next "path for sigma" entry */
490 while (1) {
491 if ((res = fscanf(fp, " path for sigma %lg", &d)) == EOF)
492 FATAL("cmaes_resume_distribution(): 'path for sigma' not found",0,0,0);
493 else if (res==0)
494 fscanf(fp, " %*s");
495 else if(res > 0)
496 break;
497 }
498
499 /* read ps */
500 t->rgps[0] = d; res = 1;
501 for(i = 1; i < t->sp.N; ++i)
502 res += fscanf(fp, " %lg", &t->rgps[i]);
503 if (res != t->sp.N)
504 FATAL("cmaes_resume_distribution(): ps: dimensions differ",0,0,0);
505
506 /* find next "path for C" entry */
507 while (1) {
508 if ((res = fscanf(fp, " path for C %lg", &d)) == EOF)
509 FATAL("cmaes_resume_distribution(): 'path for C' not found",0,0,0);
510 else if (res==0)
511 fscanf(fp, " %*s");
512 else if(res > 0)
513 break;
514 }
515 /* read pc */
516 t->rgpc[0] = d; res = 1;
517 for(i = 1; i < t->sp.N; ++i)
518 res += fscanf(fp, " %lg", &t->rgpc[i]);
519 if (res != t->sp.N)
520 FATAL("cmaes_resume_distribution(): pc: dimensions differ",0,0,0);
521
522 /* find next "sigma" entry */
523 while (1) {
524 if ((res = fscanf(fp, " sigma %lg", &d)) == EOF)
525 FATAL("cmaes_resume_distribution(): 'sigma' not found",0,0,0);
526 else if (res==0)
527 fscanf(fp, " %*s");
528 else if(res > 0)
529 break;
530 }
531 t->sigma = d;
532
533 /* find next entry "covariance matrix" */
534 while (1) {
535 if ((res = fscanf(fp, " covariance matrix %lg", &d)) == EOF)
536 FATAL("cmaes_resume_distribution(): 'covariance matrix' not found",0,0,0);
537 else if (res==0)
538 fscanf(fp, " %*s");
539 else if(res > 0)
540 break;
541 }
542 /* read C */
543 t->C[0][0] = d; res = 1;
544 for (i = 1; i < t->sp.N; ++i)
545 for (j = 0; j <= i; ++j)
546 res += fscanf(fp, " %lg", &t->C[i][j]);
547 if (res != (t->sp.N*t->sp.N+t->sp.N)/2)
548 FATAL("cmaes_resume_distribution(): C: dimensions differ",0,0,0);
549
550 fclose(fp);
551
552 t->flgIniphase = 0;
554 t->flgresumedone = 1;
556
557} /* cmaes_resume_distribution() */
558#ifdef __GNUC__
559 #pragma GCC diagnostic pop
560#endif
561/* --------------------------------------------------------- */
562/* --------------------------------------------------------- */
563
564void
566{
567 int i, N = t->sp.N;
568 t->version = NULL;
569 /* free(t->signals_filename) */
570 t->state = -1; /* not really useful at the moment */
571 free( t->rgpc);
572 free( t->rgps);
573 free( t->rgdTmp);
574 free( t->rgBDz);
575 free( --t->rgxmean);
576 free( --t->rgxold);
577 free( --t->rgxbestever);
578 free( --t->rgout);
579 free( t->rgD);
580 for (i = 0; i < N; ++i) {
581 free( t->C[i]);
582 free( t->B[i]);
583 }
584 for (i = 0; i < t->sp.lambda; ++i)
585 free( --t->rgrgx[i]);
586 free( t->rgrgx);
587 free( t->C);
588 free( t->B);
589 free( t->index);
590 free( t->publicFitness);
591 free( --t->rgFuncValue);
592 free( --t->arFuncValueHist);
594 cmaes_readpara_exit (&t->sp);
595} /* cmaes_exit() */
596
597
598/* --------------------------------------------------------- */
599/* --------------------------------------------------------- */
600double const *
601cmaes_SetMean(cmaes_t *t, const double *xmean)
602/*
603 * Distribution mean could be changed before SamplePopulation().
604 * This might lead to unexpected behaviour if done repeatedly.
605 */
606{
607 int i, N=t->sp.N;
608
609 if (t->state >= 1 && t->state < 3)
610 FATAL("cmaes_SetMean: mean cannot be set inbetween the calls of ",
611 "SamplePopulation and UpdateDistribution",0,0);
612
613 if (xmean != NULL && xmean != t->rgxmean)
614 for(i = 0; i < N; ++i)
615 t->rgxmean[i] = xmean[i];
616 else
617 xmean = t->rgxmean;
618
619 return xmean;
620}
621
622/* --------------------------------------------------------- */
623/* --------------------------------------------------------- */
624double * const *
626{
627 int iNk, i, j, N=t->sp.N;
628 int flgdiag = ((t->sp.diagonalCov == 1) || (t->sp.diagonalCov >= t->gen));
629 double sum;
630 double const *xmean = t->rgxmean;
631
632 /* cmaes_SetMean(t, xmean); * xmean could be changed at this point */
633
634 /* calculate eigensystem */
635 if (!t->flgEigensysIsUptodate) {
636 if (!flgdiag)
638 else {
639 for (i = 0; i < N; ++i)
640 t->rgD[i] = sqrt(t->C[i][i]);
641 t->minEW = douSquare(rgdouMin(t->rgD, N));
642 t->maxEW = douSquare(rgdouMax(t->rgD, N));
645 }
646 }
647
648 /* treat minimal standard deviations and numeric problems */
649 TestMinStdDevs(t);
650
651 for (iNk = 0; iNk < t->sp.lambda; ++iNk)
652 { /* generate scaled cmaes_random vector (D * z) */
653 for (i = 0; i < N; ++i)
654 if (flgdiag)
655 t->rgrgx[iNk][i] = xmean[i] + t->sigma * t->rgD[i] * cmaes_random_Gauss(&t->rand);
656 else
657 t->rgdTmp[i] = t->rgD[i] * cmaes_random_Gauss(&t->rand);
658 if (!flgdiag)
659 /* add mutation (sigma * B * (D*z)) */
660 for (i = 0; i < N; ++i) {
661 for (j = 0, sum = 0.; j < N; ++j)
662 sum += t->B[i][j] * t->rgdTmp[j];
663 t->rgrgx[iNk][i] = xmean[i] + t->sigma * sum;
664 }
665 }
666 if(t->state == 3 || t->gen == 0)
667 ++t->gen;
668 t->state = 1;
669
670 return(t->rgrgx);
671} /* SamplePopulation() */
672
673/* --------------------------------------------------------- */
674/* --------------------------------------------------------- */
675double const *
677{
678 int i, j, N=t->sp.N;
679 double sum;
680
681 if (rgx == NULL)
682 FATAL("cmaes_ReSampleSingle(): Missing input double *x",0,0,0);
683
684 for (i = 0; i < N; ++i)
685 t->rgdTmp[i] = t->rgD[i] * cmaes_random_Gauss(&t->rand);
686 /* add mutation (sigma * B * (D*z)) */
687 for (i = 0; i < N; ++i) {
688 for (j = 0, sum = 0.; j < N; ++j)
689 sum += t->B[i][j] * t->rgdTmp[j];
690 rgx[i] = t->rgxmean[i] + t->sigma * sum;
691 }
692 return rgx;
693}
694
695/* --------------------------------------------------------- */
696/* --------------------------------------------------------- */
697double * const *
699{
700 int i, j, N=t->sp.N;
701 double *rgx;
702 double sum;
703 static char s[99];
704
705 if (iindex < 0 || iindex >= t->sp.lambda) {
706 sprintf(s, "index==%d must be between 0 and %d", iindex, t->sp.lambda);
707 FATAL("cmaes_ReSampleSingle(): Population member ",s,0,0);
708 }
709 rgx = t->rgrgx[iindex];
710
711 for (i = 0; i < N; ++i)
712 t->rgdTmp[i] = t->rgD[i] * cmaes_random_Gauss(&t->rand);
713 /* add mutation (sigma * B * (D*z)) */
714 for (i = 0; i < N; ++i) {
715 for (j = 0, sum = 0.; j < N; ++j)
716 sum += t->B[i][j] * t->rgdTmp[j];
717 rgx[i] = t->rgxmean[i] + t->sigma * sum;
718 }
719 return(t->rgrgx);
720}
721
722/* --------------------------------------------------------- */
723/* --------------------------------------------------------- */
724double *
726{
727 int i, j, N=t->sp.N;
728 double sum;
729
730 if (rgx == NULL)
731 rgx = new_double(N);
732
733 for (i = 0; i < N; ++i)
734 t->rgdTmp[i] = t->rgD[i] * cmaes_random_Gauss(&t->rand);
735 /* add mutation (sigma * B * (D*z)) */
736 for (i = 0; i < N; ++i) {
737 for (j = 0, sum = 0.; j < N; ++j)
738 sum += t->B[i][j] * t->rgdTmp[j];
739 rgx[i] = t->rgxmean[i] + t->sigma * sum;
740 }
741 return rgx;
742}
743
744/* --------------------------------------------------------- */
745/* --------------------------------------------------------- */
746double *
747cmaes_PerturbSolutionInto( cmaes_t *t, double *rgx, double const *xmean, double eps)
748{
749 int i, j, N=t->sp.N;
750 double sum;
751
752 if (rgx == NULL)
753 rgx = new_double(N);
754 if (xmean == NULL)
755 FATAL("cmaes_PerturbSolutionInto(): xmean was not given",0,0,0);
756
757 for (i = 0; i < N; ++i)
758 t->rgdTmp[i] = t->rgD[i] * cmaes_random_Gauss(&t->rand);
759 /* add mutation (sigma * B * (D*z)) */
760 for (i = 0; i < N; ++i) {
761 for (j = 0, sum = 0.; j < N; ++j)
762 sum += t->B[i][j] * t->rgdTmp[j];
763 rgx[i] = xmean[i] + eps * t->sigma * sum;
764 }
765 return rgx;
766}
767
768/* --------------------------------------------------------- */
769/* --------------------------------------------------------- */
770const double *
771cmaes_Optimize( cmaes_t *evo, double(*pFun)(double const *, int dim), long iterations)
772/* TODO: make signals.par another argument or, even better, part of cmaes_t */
773{
774 const char * signalsFilename = "cmaes_signals.par";
775 double *const*pop; /* sampled population */
776 const char *stop;
777 int i;
778 double startiter = evo->gen;
779
780 while(!(stop=cmaes_TestForTermination(evo)) &&
781 (evo->gen < startiter + iterations || !iterations))
782 {
783 /* Generate population of new candidate solutions */
784 pop = cmaes_SamplePopulation(evo); /* do not change content of pop */
785
786 /* Compute fitness value for each candidate solution */
787 for (i = 0; i < cmaes_Get(evo, "popsize"); ++i) {
788 evo->publicFitness[i] = (*pFun)(pop[i], evo->sp.N);
789 }
790
791 /* update search distribution */
793
794 /* read control signals for output and termination */
795 if (signalsFilename)
796 cmaes_ReadSignals(evo, signalsFilename);
797 fflush(stdout);
798 } /* while !cmaes_TestForTermination(evo) */
799
800 /* write some data */
801 cmaes_WriteToFile(evo, "all", "allcmaes.dat");
802
803 return cmaes_GetPtr(evo, "xbestever");
804}
805
806
807/* --------------------------------------------------------- */
808/* --------------------------------------------------------- */
809double *
810cmaes_UpdateDistribution( cmaes_t *t, const double *rgFunVal)
811{
812 int i, j, iNk, hsig, N=t->sp.N;
813 int flgdiag = ((t->sp.diagonalCov == 1) || (t->sp.diagonalCov >= t->gen));
814 double sum;
815 double psxps;
816
817 if(t->state == 3)
818 FATAL("cmaes_UpdateDistribution(): You need to call \n",
819 "SamplePopulation() before update can take place.",0,0);
820 if(rgFunVal == NULL)
821 FATAL("cmaes_UpdateDistribution(): ",
822 "Fitness function value array input is missing.",0,0);
823
824 if(t->state == 1) /* function values are delivered here */
825 t->countevals += t->sp.lambda;
826 else
827 ERRORMESSAGE("cmaes_UpdateDistribution(): unexpected state",0,0,0);
828
829 /* assign function values */
830 for (i=0; i < t->sp.lambda; ++i)
831 t->rgrgx[i][N] = t->rgFuncValue[i] = rgFunVal[i];
832
833
834 /* Generate index */
835 Sorted_index(rgFunVal, t->index, t->sp.lambda);
836
837 /* Test if function values are identical, escape flat fitness */
838 if (t->rgFuncValue[t->index[0]] ==
839 t->rgFuncValue[t->index[(int)t->sp.lambda/2]]) {
840 t->sigma *= exp(0.2+t->sp.cs/t->sp.damps);
841 ERRORMESSAGE("Warning: sigma increased due to equal function values\n",
842 " Reconsider the formulation of the objective function",0,0);
843 }
844
845 /* update function value history */
846 for(i = (int)*(t->arFuncValueHist-1)-1; i > 0; --i) /* for(i = t->arFuncValueHist[-1]-1; i > 0; --i) */
847 t->arFuncValueHist[i] = t->arFuncValueHist[i-1];
848 t->arFuncValueHist[0] = rgFunVal[t->index[0]];
849
850 /* update xbestever */
851 if (t->rgxbestever[N] > t->rgrgx[t->index[0]][N] || t->gen == 1)
852 for (i = 0; i <= N; ++i) {
853 t->rgxbestever[i] = t->rgrgx[t->index[0]][i];
854 t->rgxbestever[N+1] = t->countevals;
855 }
856
857 /* calculate xmean and rgBDz~N(0,C) */
858 for (i = 0; i < N; ++i) {
859 t->rgxold[i] = t->rgxmean[i];
860 t->rgxmean[i] = 0.;
861 for (iNk = 0; iNk < t->sp.mu; ++iNk)
862 t->rgxmean[i] += t->sp.weights[iNk] * t->rgrgx[t->index[iNk]][i];
863 t->rgBDz[i] = sqrt(t->sp.mueff)*(t->rgxmean[i] - t->rgxold[i])/t->sigma;
864 }
865
866 /* calculate z := D^(-1) * B^(-1) * rgBDz into rgdTmp */
867 for (i = 0; i < N; ++i) {
868 if (!flgdiag)
869 for (j = 0, sum = 0.; j < N; ++j)
870 sum += t->B[j][i] * t->rgBDz[j];
871 else
872 sum = t->rgBDz[i];
873 t->rgdTmp[i] = sum / t->rgD[i];
874 }
875
876 /* TODO?: check length of t->rgdTmp and set an upper limit, e.g. 6 stds */
877 /* in case of manipulation of arx,
878 this can prevent an increase of sigma by several orders of magnitude
879 within one step; a five-fold increase in one step can still happen.
880 */
881 /*
882 for (j = 0, sum = 0.; j < N; ++j)
883 sum += t->rgdTmp[j] * t->rgdTmp[j];
884 if (sqrt(sum) > chiN + 6. * sqrt(0.5)) {
885 rgdTmp length should be set to upper bound and hsig should become zero
886 }
887 */
888
889 /* cumulation for sigma (ps) using B*z */
890 for (i = 0; i < N; ++i) {
891 if (!flgdiag)
892 for (j = 0, sum = 0.; j < N; ++j)
893 sum += t->B[i][j] * t->rgdTmp[j];
894 else
895 sum = t->rgdTmp[i];
896 t->rgps[i] = (1. - t->sp.cs) * t->rgps[i] +
897 sqrt(t->sp.cs * (2. - t->sp.cs)) * sum;
898 }
899
900 /* calculate norm(ps)^2 */
901 for (i = 0, psxps = 0.; i < N; ++i)
902 psxps += t->rgps[i] * t->rgps[i];
903
904 /* cumulation for covariance matrix (pc) using B*D*z~N(0,C) */
905 hsig = sqrt(psxps) / sqrt(1. - pow(1.-t->sp.cs, 2*t->gen)) / t->chiN
906 < 1.4 + 2./(N+1);
907 for (i = 0; i < N; ++i) {
908 t->rgpc[i] = (1. - t->sp.ccumcov) * t->rgpc[i] +
909 hsig * sqrt(t->sp.ccumcov * (2. - t->sp.ccumcov)) * t->rgBDz[i];
910 }
911
912 /* stop initial phase */
913 if (t->flgIniphase &&
914 t->gen > douMin(1/t->sp.cs, 1+N/t->sp.mucov))
915 {
916 if (psxps / t->sp.damps / (1.-pow((1. - t->sp.cs), t->gen))
917 < N * 1.05)
918 t->flgIniphase = 0;
919 }
920
921#if 0
922 /* remove momentum in ps, if ps is large and fitness is getting worse */
923 /* This is obsolete due to hsig and harmful in a dynamic environment */
924 if(psxps/N > 1.5 + 10.*sqrt(2./N)
925 && t->arFuncValueHist[0] > t->arFuncValueHist[1]
926 && t->arFuncValueHist[0] > t->arFuncValueHist[2]) {
927 double tfac = sqrt((1 + douMax(0, log(psxps/N))) * N / psxps);
928 for (i=0; i<N; ++i)
929 t->rgps[i] *= tfac;
930 psxps *= tfac*tfac;
931 }
932#endif
933
934 /* update of C */
935
936 Adapt_C2(t, hsig);
937
938 /* Adapt_C(t); not used anymore */
939
940#if 0
941 if (t->sp.ccov != 0. && t->flgIniphase == 0) {
942 int k;
943
945
946 /* update covariance matrix */
947 for (i = 0; i < N; ++i)
948 for (j = 0; j <=i; ++j) {
949 t->C[i][j] = (1 - t->sp.ccov) * t->C[i][j]
950 + t->sp.ccov * (1./t->sp.mucov)
951 * (t->rgpc[i] * t->rgpc[j]
952 + (1-hsig)*t->sp.ccumcov*(2.-t->sp.ccumcov) * t->C[i][j]);
953 for (k = 0; k < t->sp.mu; ++k) /* additional rank mu update */
954 t->C[i][j] += t->sp.ccov * (1-1./t->sp.mucov) * t->sp.weights[k]
955 * (t->rgrgx[t->index[k]][i] - t->rgxold[i])
956 * (t->rgrgx[t->index[k]][j] - t->rgxold[j])
957 / t->sigma / t->sigma;
958 }
959 }
960#endif
961
962
963 /* update of sigma */
964 t->sigma *= exp(((sqrt(psxps)/t->chiN)-1.)*t->sp.cs/t->sp.damps);
965
966 t->state = 3;
967
968 return (t->rgxmean);
969
970} /* cmaes_UpdateDistribution() */
971
972
973/* --------------------------------------------------------- */
974/* --------------------------------------------------------- */
975static void
976Adapt_C2(cmaes_t *t, int hsig)
977{
978 int i, j, k, N=t->sp.N;
979 int flgdiag = ((t->sp.diagonalCov == 1) || (t->sp.diagonalCov >= t->gen));
980
981 if (t->sp.ccov != 0. && t->flgIniphase == 0) {
982
983 /* definitions for speeding up inner-most loop */
984 double ccov1 = douMin(t->sp.ccov * (1./t->sp.mucov) * (flgdiag ? (N+1.5) / 3. : 1.), 1.);
985 double ccovmu = douMin(t->sp.ccov * (1-1./t->sp.mucov)* (flgdiag ? (N+1.5) / 3. : 1.), 1.-ccov1);
986 double sigmasquare = t->sigma * t->sigma;
987
989
990 /* update covariance matrix */
991 for (i = 0; i < N; ++i)
992 for (j = flgdiag ? i : 0; j <= i; ++j) {
993 t->C[i][j] = (1 - ccov1 - ccovmu) * t->C[i][j]
994 + ccov1
995 * (t->rgpc[i] * t->rgpc[j]
996 + (1-hsig)*t->sp.ccumcov*(2.-t->sp.ccumcov) * t->C[i][j]);
997 for (k = 0; k < t->sp.mu; ++k) { /* additional rank mu update */
998 t->C[i][j] += ccovmu * t->sp.weights[k]
999 * (t->rgrgx[t->index[k]][i] - t->rgxold[i])
1000 * (t->rgrgx[t->index[k]][j] - t->rgxold[j])
1001 / sigmasquare;
1002 }
1003 }
1004 /* update maximal and minimal diagonal value */
1005 t->maxdiagC = t->mindiagC = t->C[0][0];
1006 for (i = 1; i < N; ++i) {
1007 if (t->maxdiagC < t->C[i][i])
1008 t->maxdiagC = t->C[i][i];
1009 else if (t->mindiagC > t->C[i][i])
1010 t->mindiagC = t->C[i][i];
1011 }
1012 } /* if ccov... */
1013}
1014
1015
1016/* --------------------------------------------------------- */
1017/* --------------------------------------------------------- */
1018static void
1020 /* increases sigma */
1021{
1022 int i, N = t->sp.N;
1023 if (t->sp.rgDiffMinChange == NULL)
1024 return;
1025
1026 for (i = 0; i < N; ++i)
1027 while (t->sigma * sqrt(t->C[i][i]) < t->sp.rgDiffMinChange[i])
1028 t->sigma *= exp(0.05+t->sp.cs/t->sp.damps);
1029
1030} /* cmaes_TestMinStdDevs() */
1031
1032
1033/* --------------------------------------------------------- */
1034/* --------------------------------------------------------- */
1035void cmaes_WriteToFile(cmaes_t *t, const char *key, const char *name)
1036{
1037 cmaes_WriteToFileAW(t, key, name, "a"); /* default is append */
1038}
1039
1040/* --------------------------------------------------------- */
1041/* --------------------------------------------------------- */
1042void cmaes_WriteToFileAW(cmaes_t *t, const char *key, const char *name,
1043 const char *appendwrite)
1044{
1045 const char *s = "tmpcmaes.dat";
1046 FILE *fp;
1047
1048 if (name == NULL)
1049 name = s;
1050
1051 fp = fopen( name, appendwrite);
1052
1053 if(fp == NULL) {
1054 ERRORMESSAGE("cmaes_WriteToFile(): could not open '", name,
1055 "' with flag ", appendwrite);
1056 return;
1057 }
1058
1059 if (appendwrite[0] == 'w') {
1060 /* write a header line, very rudimentary */
1061 fprintf(fp, "%% # %s (randomSeed=%d, %s)\n", key, t->sp.seed, getTimeStr());
1062 } else
1063 if (t->gen > 0 || strncmp(name, "outcmaesfit", 11) != 0)
1064 cmaes_WriteToFilePtr(t, key, fp); /* do not write fitness for gen==0 */
1065
1066 fclose(fp);
1067
1068} /* WriteToFile */
1069
1070/* --------------------------------------------------------- */
1071void cmaes_WriteToFilePtr(cmaes_t *t, const char *key, FILE *fp)
1072
1073/* this hack reads key words from input key for data to be written to
1074 * a file, see file signals.par as input file. The length of the keys
1075 * is mostly fixed, see key += number in the code! If the key phrase
1076 * does not match the expectation the output might be strange. for
1077 * cmaes_t *t == NULL it solely prints key as a header line. Input key
1078 * must be zero terminated.
1079 */
1080{
1081 int i, k, N=(t ? t->sp.N : 0);
1082 char const *keyend; /* *keystart; */
1083 const char *s = "few";
1084 if (key == NULL)
1085 key = s;
1086 /* keystart = key; for debugging purpose */
1087 keyend = key + strlen(key);
1088
1089 while (key < keyend)
1090 {
1091 if (strncmp(key, "axisratio", 9) == 0)
1092 {
1093 fprintf(fp, "%.2e", sqrt(t->maxEW/t->minEW));
1094 while (*key != '+' && *key != '\0' && key < keyend)
1095 ++key;
1096 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1097 }
1098 if (strncmp(key, "idxminSD", 8) == 0)
1099 {
1100 int mini=0; for(i=N-1;i>0;--i) if(t->mindiagC==t->C[i][i]) mini=i;
1101 fprintf(fp, "%d", mini+1);
1102 while (*key != '+' && *key != '\0' && key < keyend)
1103 ++key;
1104 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1105 }
1106 if (strncmp(key, "idxmaxSD", 8) == 0)
1107 {
1108 int maxi=0; for(i=N-1;i>0;--i) if(t->maxdiagC==t->C[i][i]) maxi=i;
1109 fprintf(fp, "%d", maxi+1);
1110 while (*key != '+' && *key != '\0' && key < keyend)
1111 ++key;
1112 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1113 }
1114 /* new coordinate system == all eigenvectors */
1115 if (strncmp(key, "B", 1) == 0)
1116 {
1117 /* int j, index[N]; */
1118 int j, *iindex=(int*)(new_void(N,sizeof(int))); /* MT */
1119 Sorted_index(t->rgD, iindex, N); /* should not be necessary, see end of QLalgo2 */
1120 /* One eigenvector per row, sorted: largest eigenvalue first */
1121 for (i = 0; i < N; ++i)
1122 for (j = 0; j < N; ++j)
1123 fprintf(fp, "%g%c", t->B[j][iindex[N-1-i]], (j==N-1)?'\n':'\t');
1124 ++key;
1125 free(iindex); /* MT */
1126 }
1127 /* covariance matrix */
1128 if (strncmp(key, "C", 1) == 0)
1129 {
1130 int j;
1131 for (i = 0; i < N; ++i)
1132 for (j = 0; j <= i; ++j)
1133 fprintf(fp, "%g%c", t->C[i][j], (j==i)?'\n':'\t');
1134 ++key;
1135 }
1136 /* (processor) time (used) since begin of execution */
1137 if (strncmp(key, "clock", 4) == 0)
1138 {
1140 fprintf(fp, "%.1f %.1f", t->eigenTimings.totaltotaltime,
1142 while (*key != '+' && *key != '\0' && key < keyend)
1143 ++key;
1144 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1145 }
1146 /* ratio between largest and smallest standard deviation */
1147 if (strncmp(key, "stddevratio", 11) == 0) /* std dev in coordinate axes */
1148 {
1149 fprintf(fp, "%g", sqrt(t->maxdiagC/t->mindiagC));
1150 while (*key != '+' && *key != '\0' && key < keyend)
1151 ++key;
1152 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1153 }
1154 /* standard deviations in coordinate directions (sigma*sqrt(C[i,i])) */
1155 if (strncmp(key, "coorstddev", 10) == 0
1156 || strncmp(key, "stddev", 6) == 0) /* std dev in coordinate axes */
1157 {
1158 for (i = 0; i < N; ++i)
1159 fprintf(fp, "%s%g", (i==0) ? "":"\t", t->sigma*sqrt(t->C[i][i]));
1160 while (*key != '+' && *key != '\0' && key < keyend)
1161 ++key;
1162 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1163 }
1164 /* diagonal of D == roots of eigenvalues, sorted */
1165 if (strncmp(key, "diag(D)", 7) == 0)
1166 {
1167 for (i = 0; i < N; ++i)
1168 t->rgdTmp[i] = t->rgD[i];
1169 qsort(t->rgdTmp, (unsigned) N, sizeof(double), &SignOfDiff); /* superfluous */
1170 for (i = 0; i < N; ++i)
1171 fprintf(fp, "%s%g", (i==0) ? "":"\t", t->rgdTmp[i]);
1172 while (*key != '+' && *key != '\0' && key < keyend)
1173 ++key;
1174 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1175 }
1176 if (strncmp(key, "dim", 3) == 0)
1177 {
1178 fprintf(fp, "%d", N);
1179 while (*key != '+' && *key != '\0' && key < keyend)
1180 ++key;
1181 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1182 }
1183 if (strncmp(key, "eval", 4) == 0)
1184 {
1185 fprintf(fp, "%.0f", t->countevals);
1186 while (*key != '+' && *key != '\0' && key < keyend)
1187 ++key;
1188 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1189 }
1190 if (strncmp(key, "few(diag(D))", 12) == 0)/* between four and six axes */
1191 {
1192 int add = (int)(0.5 + (N + 1.) / 5.);
1193 for (i = 0; i < N; ++i)
1194 t->rgdTmp[i] = t->rgD[i];
1195 qsort(t->rgdTmp, (unsigned) N, sizeof(double), &SignOfDiff);
1196 for (i = 0; i < N-1; i+=add) /* print always largest */
1197 fprintf(fp, "%s%g", (i==0) ? "":"\t", t->rgdTmp[N-1-i]);
1198 fprintf(fp, "\t%g\n", t->rgdTmp[0]); /* and smallest */
1199 break; /* number of printed values is not determined */
1200 }
1201 if (strncmp(key, "fewinfo", 7) == 0) {
1202 fprintf(fp," Iter Fevals Function Value Sigma ");
1203 fprintf(fp, "MaxCoorDev MinCoorDev AxisRatio MinDii Time in eig\n");
1204 while (*key != '+' && *key != '\0' && key < keyend)
1205 ++key;
1206 }
1207 if (strncmp(key, "few", 3) == 0) {
1208 fprintf(fp, " %4.0f ", t->gen);
1209 fprintf(fp, " %5.0f ", t->countevals);
1210 fprintf(fp, "%.15e", t->rgFuncValue[t->index[0]]);
1211 fprintf(fp, " %.2e %.2e %.2e", t->sigma, t->sigma*sqrt(t->maxdiagC),
1212 t->sigma*sqrt(t->mindiagC));
1213 fprintf(fp, " %.2e %.2e", sqrt(t->maxEW/t->minEW), sqrt(t->minEW));
1214 while (*key != '+' && *key != '\0' && key < keyend)
1215 ++key;
1216 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1217 }
1218 if (strncmp(key, "funval", 6) == 0 || strncmp(key, "fitness", 6) == 0)
1219 {
1220 fprintf(fp, "%.15e", t->rgFuncValue[t->index[0]]);
1221 while (*key != '+' && *key != '\0' && key < keyend)
1222 ++key;
1223 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1224 }
1225 if (strncmp(key, "fbestever", 9) == 0)
1226 {
1227 fprintf(fp, "%.15e", t->rgxbestever[N]); /* f-value */
1228 while (*key != '+' && *key != '\0' && key < keyend)
1229 ++key;
1230 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1231 }
1232 if (strncmp(key, "fmedian", 7) == 0)
1233 {
1234 fprintf(fp, "%.15e", t->rgFuncValue[t->index[(int)(t->sp.lambda/2)]]);
1235 while (*key != '+' && *key != '\0' && key < keyend)
1236 ++key;
1237 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1238 }
1239 if (strncmp(key, "fworst", 6) == 0)
1240 {
1241 fprintf(fp, "%.15e", t->rgFuncValue[t->index[t->sp.lambda-1]]);
1242 while (*key != '+' && *key != '\0' && key < keyend)
1243 ++key;
1244 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1245 }
1246 if (strncmp(key, "arfunval", 8) == 0 || strncmp(key, "arfitness", 8) == 0)
1247 {
1248 for (i = 0; i < N; ++i)
1249 fprintf(fp, "%s%.10e", (i==0) ? "" : "\t",
1250 t->rgFuncValue[t->index[i]]);
1251 while (*key != '+' && *key != '\0' && key < keyend)
1252 ++key;
1253 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1254 }
1255 if (strncmp(key, "gen", 3) == 0)
1256 {
1257 fprintf(fp, "%.0f", t->gen);
1258 while (*key != '+' && *key != '\0' && key < keyend)
1259 ++key;
1260 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1261 }
1262 if (strncmp(key, "iter", 4) == 0)
1263 {
1264 fprintf(fp, "%.0f", t->gen);
1265 while (*key != '+' && *key != '\0' && key < keyend)
1266 ++key;
1267 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1268 }
1269 if (strncmp(key, "sigma", 5) == 0)
1270 {
1271 fprintf(fp, "%.4e", t->sigma);
1272 while (*key != '+' && *key != '\0' && key < keyend)
1273 ++key;
1274 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1275 }
1276 if (strncmp(key, "minSD", 5) == 0) /* minimal standard deviation */
1277 {
1278 fprintf(fp, "%.4e", sqrt(t->mindiagC));
1279 while (*key != '+' && *key != '\0' && key < keyend)
1280 ++key;
1281 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1282 }
1283 if (strncmp(key, "maxSD", 5) == 0)
1284 {
1285 fprintf(fp, "%.4e", sqrt(t->maxdiagC));
1286 while (*key != '+' && *key != '\0' && key < keyend)
1287 ++key;
1288 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1289 }
1290 if (strncmp(key, "mindii", 6) == 0)
1291 {
1292 fprintf(fp, "%.4e", sqrt(t->minEW));
1293 while (*key != '+' && *key != '\0' && key < keyend)
1294 ++key;
1295 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1296 }
1297 if (strncmp(key, "0", 1) == 0)
1298 {
1299 fprintf(fp, "0");
1300 ++key;
1301 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1302 }
1303 if (strncmp(key, "lambda", 6) == 0 || strncmp(key, "popsi", 5) == 0 || strncmp(key, "populationsi", 12) == 0)
1304 {
1305 fprintf(fp, "%d", t->sp.lambda);
1306 while (*key != '+' && *key != '\0' && key < keyend)
1307 ++key;
1308 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1309 }
1310 if (strncmp(key, "N", 1) == 0)
1311 {
1312 fprintf(fp, "%d", N);
1313 ++key;
1314 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1315 }
1316 if (strncmp(key, "resume", 6) == 0)
1317 {
1318 fprintf(fp, "\n# resume %d\n", N);
1319 fprintf(fp, "xmean\n");
1320 cmaes_WriteToFilePtr(t, "xmean", fp);
1321 fprintf(fp, "path for sigma\n");
1322 for(i=0; i<N; ++i)
1323 fprintf(fp, "%g%s", t->rgps[i], (i==N-1) ? "\n":"\t");
1324 fprintf(fp, "path for C\n");
1325 for(i=0; i<N; ++i)
1326 fprintf(fp, "%g%s", t->rgpc[i], (i==N-1) ? "\n":"\t");
1327 fprintf(fp, "sigma %g\n", t->sigma);
1328 /* note than B and D might not be up-to-date */
1329 fprintf(fp, "covariance matrix\n");
1330 cmaes_WriteToFilePtr(t, "C", fp);
1331 while (*key != '+' && *key != '\0' && key < keyend)
1332 ++key;
1333 }
1334 if (strncmp(key, "xbest", 5) == 0) { /* best x in recent generation */
1335 for(i=0; i<N; ++i)
1336 fprintf(fp, "%s%g", (i==0) ? "":"\t", t->rgrgx[t->index[0]][i]);
1337 while (*key != '+' && *key != '\0' && key < keyend)
1338 ++key;
1339 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1340 }
1341 if (strncmp(key, "xmean", 5) == 0) {
1342 for(i=0; i<N; ++i)
1343 fprintf(fp, "%s%g", (i==0) ? "":"\t", t->rgxmean[i]);
1344 while (*key != '+' && *key != '\0' && key < keyend)
1345 ++key;
1346 fprintf(fp, "%c", (*key=='+') ? '\t':'\n');
1347 }
1348 if (strncmp(key, "all", 3) == 0)
1349 {
1350 time_t ti = time(NULL);
1351 fprintf(fp, "\n# --------- %s\n", asctime(localtime(&ti)));
1352 fprintf(fp, " N %d\n", N);
1353 fprintf(fp, " seed %d\n", t->sp.seed);
1354 fprintf(fp, "function evaluations %.0f\n", t->countevals);
1355 fprintf(fp, "elapsed (CPU) time [s] %.2f\n", t->eigenTimings.totaltotaltime);
1356 fprintf(fp, "function value f(x)=%g\n", t->rgrgx[t->index[0]][N]);
1357 fprintf(fp, "maximal standard deviation %g\n", t->sigma*sqrt(t->maxdiagC));
1358 fprintf(fp, "minimal standard deviation %g\n", t->sigma*sqrt(t->mindiagC));
1359 fprintf(fp, "sigma %g\n", t->sigma);
1360 fprintf(fp, "axisratio %g\n", rgdouMax(t->rgD, N)/rgdouMin(t->rgD, N));
1361 fprintf(fp, "xbestever found after %.0f evaluations, function value %g\n",
1362 t->rgxbestever[N+1], t->rgxbestever[N]);
1363 for(i=0; i<N; ++i)
1364 fprintf(fp, " %12g%c", t->rgxbestever[i],
1365 (i%5==4||i==N-1)?'\n':' ');
1366 fprintf(fp, "xbest (of last generation, function value %g)\n",
1367 t->rgrgx[t->index[0]][N]);
1368 for(i=0; i<N; ++i)
1369 fprintf(fp, " %12g%c", t->rgrgx[t->index[0]][i],
1370 (i%5==4||i==N-1)?'\n':' ');
1371 fprintf(fp, "xmean \n");
1372 for(i=0; i<N; ++i)
1373 fprintf(fp, " %12g%c", t->rgxmean[i],
1374 (i%5==4||i==N-1)?'\n':' ');
1375 fprintf(fp, "Standard deviation of coordinate axes (sigma*sqrt(diag(C)))\n");
1376 for(i=0; i<N; ++i)
1377 fprintf(fp, " %12g%c", t->sigma*sqrt(t->C[i][i]),
1378 (i%5==4||i==N-1)?'\n':' ');
1379 fprintf(fp, "Main axis lengths of mutation ellipsoid (sigma*diag(D))\n");
1380 for (i = 0; i < N; ++i)
1381 t->rgdTmp[i] = t->rgD[i];
1382 qsort(t->rgdTmp, (unsigned) N, sizeof(double), &SignOfDiff);
1383 for(i=0; i<N; ++i)
1384 fprintf(fp, " %12g%c", t->sigma*t->rgdTmp[N-1-i],
1385 (i%5==4||i==N-1)?'\n':' ');
1386 fprintf(fp, "Longest axis (b_i where d_ii=max(diag(D))\n");
1387 k = MaxIdx(t->rgD, N);
1388 for(i=0; i<N; ++i)
1389 fprintf(fp, " %12g%c", t->B[i][k], (i%5==4||i==N-1)?'\n':' ');
1390 fprintf(fp, "Shortest axis (b_i where d_ii=max(diag(D))\n");
1391 k = MinIdx(t->rgD, N);
1392 for(i=0; i<N; ++i)
1393 fprintf(fp, " %12g%c", t->B[i][k], (i%5==4||i==N-1)?'\n':' ');
1394 while (*key != '+' && *key != '\0' && key < keyend)
1395 ++key;
1396 } /* "all" */
1397
1398#if 0 /* could become generic part */
1399 s0 = key;
1400 d = cmaes_Get(t, key); /* TODO find way to detect whether key was found */
1401 if (key == s0) /* this does not work, is always true */
1402 {
1403 /* write out stuff, problem: only generic format is available */
1404 /* move in key until "+" or end */
1405 }
1406#endif
1407
1408 if (*key == '\0')
1409 break;
1410 else if (*key != '+') { /* last key was not recognized */
1411 ERRORMESSAGE("cmaes_t:WriteToFilePtr(): unrecognized key '", key, "'", 0);
1412 while (*key != '+' && *key != '\0' && key < keyend)
1413 ++key;
1414 }
1415 while (*key == '+')
1416 ++key;
1417 } /* while key < keyend */
1418
1419 if (key > keyend)
1420 FATAL("cmaes_t:WriteToFilePtr(): BUG regarding key sequence",0,0,0);
1421
1422} /* WriteToFilePtr */
1423
1424/* --------------------------------------------------------- */
1425double
1426cmaes_Get( cmaes_t *t, char const *s)
1427{
1428 int N=t->sp.N;
1429
1430 if (strncmp(s, "axisratio", 5) == 0) { /* between lengths of longest and shortest principal axis of the distribution ellipsoid */
1431 return (rgdouMax(t->rgD, N)/rgdouMin(t->rgD, N));
1432 }
1433 else if (strncmp(s, "eval", 4) == 0) { /* number of function evaluations */
1434 return (t->countevals);
1435 }
1436 else if (strncmp(s, "fctvalue", 6) == 0
1437 || strncmp(s, "funcvalue", 6) == 0
1438 || strncmp(s, "funvalue", 6) == 0
1439 || strncmp(s, "fitness", 3) == 0) { /* recent best function value */
1440 return(t->rgFuncValue[t->index[0]]);
1441 }
1442 else if (strncmp(s, "fbestever", 7) == 0) { /* ever best function value */
1443 return(t->rgxbestever[N]);
1444 }
1445 else if (strncmp(s, "generation", 3) == 0
1446 || strncmp(s, "iteration", 4) == 0) {
1447 return(t->gen);
1448 }
1449 else if (strncmp(s, "maxeval", 4) == 0
1450 || strncmp(s, "MaxFunEvals", 8) == 0
1451 || strncmp(s, "stopMaxFunEvals", 12) == 0) { /* maximal number of function evaluations */
1452 return(t->sp.stopMaxFunEvals);
1453 }
1454 else if (strncmp(s, "maxgen", 4) == 0
1455 || strncmp(s, "MaxIter", 7) == 0
1456 || strncmp(s, "stopMaxIter", 11) == 0) { /* maximal number of generations */
1457 return(ceil(t->sp.stopMaxIter));
1458 }
1459 else if (strncmp(s, "maxaxislength", 5) == 0) { /* sigma * max(diag(D)) */
1460 return(t->sigma * sqrt(t->maxEW));
1461 }
1462 else if (strncmp(s, "minaxislength", 5) == 0) { /* sigma * min(diag(D)) */
1463 return(t->sigma * sqrt(t->minEW));
1464 }
1465 else if (strncmp(s, "maxstddev", 4) == 0) { /* sigma * sqrt(max(diag(C))) */
1466 return(t->sigma * sqrt(t->maxdiagC));
1467 }
1468 else if (strncmp(s, "minstddev", 4) == 0) { /* sigma * sqrt(min(diag(C))) */
1469 return(t->sigma * sqrt(t->mindiagC));
1470 }
1471 else if (strncmp(s, "N", 1) == 0 || strcmp(s, "n") == 0 ||
1472 strncmp(s, "dimension", 3) == 0) {
1473 return (N);
1474 }
1475 else if (strncmp(s, "lambda", 3) == 0
1476 || strncmp(s, "samplesize", 8) == 0
1477 || strncmp(s, "popsize", 7) == 0) { /* sample size, offspring population size */
1478 return(t->sp.lambda);
1479 }
1480 else if (strncmp(s, "sigma", 3) == 0) {
1481 return(t->sigma);
1482 }
1483 FATAL( "cmaes_Get(cmaes_t, char const * s): No match found for s='", s, "'",0);
1484 return(0);
1485} /* cmaes_Get() */
1486
1487/* --------------------------------------------------------- */
1488double *
1489cmaes_GetInto( cmaes_t *t, char const *s, double *res)
1490{
1491 int i, N = t->sp.N;
1492 double const * res0 = cmaes_GetPtr(t, s);
1493 if (res == NULL)
1494 res = new_double(N);
1495 for (i = 0; i < N; ++i)
1496 res[i] = res0[i];
1497 return res;
1498}
1499
1500/* --------------------------------------------------------- */
1501double *
1502cmaes_GetNew( cmaes_t *t, char const *s)
1503{
1504 return (cmaes_GetInto(t, s, NULL));
1505}
1506
1507/* --------------------------------------------------------- */
1508const double *
1509cmaes_GetPtr( cmaes_t *t, char const *s)
1510{
1511 int i, N=t->sp.N;
1512
1513 /* diagonal of covariance matrix */
1514 if (strncmp(s, "diag(C)", 7) == 0) {
1515 for (i = 0; i < N; ++i)
1516 t->rgout[i] = t->C[i][i];
1517 return(t->rgout);
1518 }
1519 /* diagonal of axis lengths matrix */
1520 else if (strncmp(s, "diag(D)", 7) == 0) {
1521 return(t->rgD);
1522 }
1523 /* vector of standard deviations sigma*sqrt(diag(C)) */
1524 else if (strncmp(s, "stddev", 3) == 0) {
1525 for (i = 0; i < N; ++i)
1526 t->rgout[i] = t->sigma * sqrt(t->C[i][i]);
1527 return(t->rgout);
1528 }
1529 /* bestever solution seen so far */
1530 else if (strncmp(s, "xbestever", 7) == 0)
1531 return(t->rgxbestever);
1532 /* recent best solution of the recent population */
1533 else if (strncmp(s, "xbest", 5) == 0)
1534 return(t->rgrgx[t->index[0]]);
1535 /* mean of the recent distribution */
1536 else if (strncmp(s, "xmean", 1) == 0)
1537 return(t->rgxmean);
1538
1539 return(NULL);
1540}
1541
1542/* --------------------------------------------------------- */
1543/* tests stopping criteria
1544 * returns a string of satisfied stopping criterion for each line
1545 * otherwise NULL
1546*/
1547const char *
1549{
1550 double range, fac;
1551 int iAchse, iKoo;
1552 int flgdiag = ((t->sp.diagonalCov == 1) || (t->sp.diagonalCov >= t->gen));
1553 static char sTestOutString[3024];
1554 char * cp = sTestOutString;
1555 int i, cTemp, N=t->sp.N;
1556 cp[0] = '\0';
1557
1558 /* function value reached */
1559 if ((t->gen > 1 || t->state > 1) && t->sp.stStopFitness.flg &&
1560 t->rgFuncValue[t->index[0]] <= t->sp.stStopFitness.val)
1561 cp += sprintf(cp, "Fitness: function value %7.2e <= stopFitness (%7.2e)\n",
1562 t->rgFuncValue[t->index[0]], t->sp.stStopFitness.val);
1563
1564 /* TolFun */
1566 rgdouMax(t->rgFuncValue, t->sp.lambda)) -
1567 douMin(rgdouMin(t->arFuncValueHist, (int)douMin(t->gen, *(t->arFuncValueHist-1))),
1568 rgdouMin(t->rgFuncValue, t->sp.lambda));
1569
1570 if (t->gen > 0 && range <= t->sp.stopTolFun) {
1571 cp += sprintf(cp,
1572 "TolFun: function value differences %7.2e < stopTolFun=%7.2e\n",
1573 range, t->sp.stopTolFun);
1574 }
1575
1576 /* TolFunHist */
1577 if (t->gen > *(t->arFuncValueHist-1)) {
1578 range = rgdouMax(t->arFuncValueHist, (int)*(t->arFuncValueHist-1))
1579 - rgdouMin(t->arFuncValueHist, (int)*(t->arFuncValueHist-1));
1580 if (range <= t->sp.stopTolFunHist)
1581 cp += sprintf(cp,
1582 "TolFunHist: history of function value changes %7.2e stopTolFunHist=%7.2e",
1583 range, t->sp.stopTolFunHist);
1584 }
1585
1586 /* TolX */
1587 for(i=0, cTemp=0; i<N; ++i) {
1588 cTemp += (t->sigma * sqrt(t->C[i][i]) < t->sp.stopTolX) ? 1 : 0;
1589 cTemp += (t->sigma * t->rgpc[i] < t->sp.stopTolX) ? 1 : 0;
1590 }
1591 if (cTemp == 2*N) {
1592 cp += sprintf(cp,
1593 "TolX: object variable changes below %7.2e \n",
1594 t->sp.stopTolX);
1595 }
1596
1597 /* TolUpX */
1598 for(i=0; i<N; ++i) {
1599 if (t->sigma * sqrt(t->C[i][i]) > t->sp.stopTolUpXFactor * t->sp.rgInitialStds[i])
1600 break;
1601 }
1602 if (i < N) {
1603 cp += sprintf(cp,
1604 "TolUpX: standard deviation increased by more than %7.2e, larger initial standard deviation recommended \n",
1605 t->sp.stopTolUpXFactor);
1606 }
1607
1608 /* Condition of C greater than dMaxSignifKond */
1609 if (t->maxEW >= t->minEW * t->dMaxSignifKond) {
1610 cp += sprintf(cp,
1611 "ConditionNumber: maximal condition number %7.2e reached. maxEW=%7.2e,minEW=%7.2e,maxdiagC=%7.2e,mindiagC=%7.2e\n",
1612 t->dMaxSignifKond, t->maxEW, t->minEW, t->maxdiagC, t->mindiagC);
1613 } /* if */
1614
1615 /* Principal axis i has no effect on xmean, ie.
1616 x == x + 0.1 * sigma * rgD[i] * B[i] */
1617 if (!flgdiag) {
1618 for (iAchse = 0; iAchse < N; ++iAchse)
1619 {
1620 fac = 0.1 * t->sigma * t->rgD[iAchse];
1621 for (iKoo = 0; iKoo < N; ++iKoo){
1622 if (t->rgxmean[iKoo] != t->rgxmean[iKoo] + fac * t->B[iKoo][iAchse])
1623 break;
1624 }
1625 if (iKoo == N)
1626 {
1627 /* t->sigma *= exp(0.2+t->sp.cs/t->sp.damps); */
1628 cp += sprintf(cp,
1629 "NoEffectAxis: standard deviation 0.1*%7.2e in principal axis %d without effect\n",
1630 fac/0.1, iAchse);
1631 break;
1632 } /* if (iKoo == N) */
1633 } /* for iAchse */
1634 } /* if flgdiag */
1635 /* Component of xmean is not changed anymore */
1636 for (iKoo = 0; iKoo < N; ++iKoo)
1637 {
1638 if (t->rgxmean[iKoo] == t->rgxmean[iKoo] +
1639 0.2*t->sigma*sqrt(t->C[iKoo][iKoo]))
1640 {
1641 /* t->C[iKoo][iKoo] *= (1 + t->sp.ccov); */
1642 /* flg = 1; */
1643 cp += sprintf(cp,
1644 "NoEffectCoordinate: standard deviation 0.2*%7.2e in coordinate %d without effect\n",
1645 t->sigma*sqrt(t->C[iKoo][iKoo]), iKoo);
1646 break;
1647 }
1648
1649 } /* for iKoo */
1650 /* if (flg) t->sigma *= exp(0.05+t->sp.cs/t->sp.damps); */
1651
1652 if(t->countevals >= t->sp.stopMaxFunEvals)
1653 cp += sprintf(cp, "MaxFunEvals: conducted function evaluations %.0f >= %g\n",
1655 if(t->gen >= t->sp.stopMaxIter)
1656 cp += sprintf(cp, "MaxIter: number of iterations %.0f >= %g\n",
1657 t->gen, t->sp.stopMaxIter);
1658 if(t->flgStop)
1659 cp += sprintf(cp, "Manual: stop signal read\n");
1660
1661#if 0
1662 else if (0) {
1663 for(i=0, cTemp=0; i<N; ++i) {
1664 cTemp += (sigma * sqrt(C[i][i]) < stopdx) ? 1 : 0;
1665 cTemp += (sigma * rgpc[i] < stopdx) ? 1 : 0;
1666 }
1667 if (cTemp == 2*N)
1668 flgStop = 1;
1669 }
1670#endif
1671
1672 if (cp - sTestOutString>320)
1673 ERRORMESSAGE("Bug in cmaes_t:Test(): sTestOutString too short",0,0,0);
1674
1675 if (cp != sTestOutString) {
1676 return sTestOutString;
1677 }
1678
1679 return(NULL);
1680
1681} /* cmaes_Test() */
1682
1683/* --------------------------------------------------------- */
1684void cmaes_ReadSignals(cmaes_t *t, char const *filename)
1685{
1686 const char *s = "cmaes_signals.par";
1687 FILE *fp;
1688 if (filename == NULL)
1689 filename = s;
1690/* if (filename) assign_string(&(t->signalsFilename), filename)*/
1691 fp = fopen( filename, "r");
1692 if(fp == NULL) {
1693 return;
1694 }
1695 cmaes_ReadFromFilePtr( t, fp);
1696 fclose(fp);
1697}
1698/* --------------------------------------------------------- */
1700/* reading commands e.g. from signals.par file
1701*/
1702{
1703 const char *keys[15]; /* key strings for scanf */
1704 char s[199], sin1[99], sin2[129], sin3[99], sin4[99];
1705 int ikey, ckeys, nb;
1706 double d;
1707 static int flglockprint = 0;
1708 static int flglockwrite = 0;
1709 static long countiterlastwritten;
1710 static long maxdiffitertowrite; /* to prevent long gaps at the beginning */
1711 int flgprinted = 0;
1712 int flgwritten = 0;
1713 double deltaprinttime = (double)(time(NULL)-t->printtime); /* using clock instead might not be a good */
1714 double deltawritetime = (double)(time(NULL)-t->writetime); /* idea as disc time is not CPU time? */
1715 double deltaprinttimefirst = (double)(t->firstprinttime ? time(NULL)-t->firstprinttime : 0); /* time is in seconds!? */
1716 double deltawritetimefirst = (double)(t->firstwritetime ? time(NULL)-t->firstwritetime : 0);
1717 if (countiterlastwritten > t->gen) { /* probably restarted */
1718 maxdiffitertowrite = 0;
1719 countiterlastwritten = 0;
1720 }
1721
1722 keys[0] = " stop%98s %98s"; /* s=="now" or eg "MaxIter+" %lg"-number */
1723 /* works with and without space */
1724 keys[1] = " print %98s %98s"; /* s==keyword for WriteFile */
1725 keys[2] = " write %98s %128s %98s"; /* s1==keyword, s2==filename */
1726 keys[3] = " check%98s %98s";
1727 keys[4] = " maxTimeFractionForEigendecompostion %98s";
1728 ckeys = 5;
1729 strcpy(sin2, "tmpcmaes.dat");
1730
1732 {
1733 deltaprinttime = (double)time(NULL); /* forces printing */
1734 deltawritetime = (double)time(NULL);
1735 }
1736 while(fgets(s, sizeof(s), fp) != NULL)
1737 {
1738 if (s[0] == '#' || s[0] == '%') /* skip comments */
1739 continue;
1740 sin1[0] = sin2[0] = sin3[0] = sin4[0] = '\0';
1741 for (ikey=0; ikey < ckeys; ++ikey)
1742 {
1743 if((nb=sscanf(s, keys[ikey], sin1, sin2, sin3, sin4)) >= 1)
1744 {
1745 switch(ikey) {
1746 case 0 : /* "stop", reads "stop now" or eg. stopMaxIter */
1747 if (strncmp(sin1, "now", 3) == 0)
1748 t->flgStop = 1;
1749 else if (strncmp(sin1, "MaxFunEvals", 11) == 0) {
1750 if (sscanf(sin2, " %lg", &d) == 1)
1751 t->sp.stopMaxFunEvals = d;
1752 }
1753 else if (strncmp(sin1, "MaxIter", 4) == 0) {
1754 if (sscanf(sin2, " %lg", &d) == 1)
1755 t->sp.stopMaxIter = d;
1756 }
1757 else if (strncmp(sin1, "Fitness", 7) == 0) {
1758 if (sscanf(sin2, " %lg", &d) == 1)
1759 {
1760 t->sp.stStopFitness.flg = 1;
1761 t->sp.stStopFitness.val = d;
1762 }
1763 }
1764 else if (strncmp(sin1, "TolFunHist", 10) == 0) {
1765 if (sscanf(sin2, " %lg", &d) == 1)
1766 t->sp.stopTolFunHist = d;
1767 }
1768 else if (strncmp(sin1, "TolFun", 6) == 0) {
1769 if (sscanf(sin2, " %lg", &d) == 1)
1770 t->sp.stopTolFun = d;
1771 }
1772 else if (strncmp(sin1, "TolX", 4) == 0) {
1773 if (sscanf(sin2, " %lg", &d) == 1)
1774 t->sp.stopTolX = d;
1775 }
1776 else if (strncmp(sin1, "TolUpXFactor", 4) == 0) {
1777 if (sscanf(sin2, " %lg", &d) == 1)
1778 t->sp.stopTolUpXFactor = d;
1779 }
1780 break;
1781 case 1 : /* "print" */
1782 d = 1; /* default */
1783 if (sscanf(sin2, "%lg", &d) < 1 && deltaprinttimefirst < 1)
1784 d = 0; /* default at first time */
1785 if (deltaprinttime >= d && !flglockprint) {
1786 cmaes_WriteToFilePtr(t, sin1, stdout);
1787 flgprinted = 1;
1788 }
1789 if(d < 0)
1790 flglockprint += 2;
1791 break;
1792 case 2 : /* "write" */
1793 /* write header, before first generation */
1794 if (t->countevals < t->sp.lambda && t->flgresumedone == 0)
1795 cmaes_WriteToFileAW(t, sin1, sin2, "w"); /* overwrite */
1796 d = 0.9; /* default is one with smooth increment of gaps */
1797 if (sscanf(sin3, "%lg", &d) < 1 && deltawritetimefirst < 2)
1798 d = 0; /* default is zero for the first second */
1799 if(d < 0)
1800 flglockwrite += 2;
1801 if (!flglockwrite) {
1802 if (deltawritetime >= d) {
1803 cmaes_WriteToFile(t, sin1, sin2);
1804 flgwritten = 1;
1805 } else if (d < 1
1806 && t->gen-countiterlastwritten > maxdiffitertowrite) {
1807 cmaes_WriteToFile(t, sin1, sin2);
1808 flgwritten = 1;
1809 }
1810 }
1811 break;
1812 case 3 : /* check, checkeigen 1 or check eigen 1 */
1813 if (strncmp(sin1, "eigen", 5) == 0) {
1814 if (sscanf(sin2, " %lg", &d) == 1) {
1815 if (d > 0)
1816 t->flgCheckEigen = 1;
1817 else
1818 t->flgCheckEigen = 0;
1819 }
1820 else
1821 t->flgCheckEigen = 0;
1822 }
1823 break;
1824 case 4 : /* maxTimeFractionForEigendecompostion */
1825 if (sscanf(sin1, " %lg", &d) == 1)
1826 t->sp.updateCmode.maxtime = d;
1827 break;
1828 default :
1829 break;
1830 }
1831 break; /* for ikey */
1832 } /* if line contains keyword */
1833 } /* for each keyword */
1834 } /* while not EOF of signals.par */
1835 if (t->writetime == 0)
1836 t->firstwritetime = time(NULL);
1837 if (t->printtime == 0)
1838 t->firstprinttime = time(NULL);
1839
1840 if (flgprinted)
1841 t->printtime = time(NULL);
1842 if (flgwritten) {
1843 t->writetime = time(NULL);
1844 if (t->gen-countiterlastwritten > maxdiffitertowrite)
1845 ++maxdiffitertowrite; /* smooth prolongation of writing gaps/intervals */
1846 countiterlastwritten = (long int) t->gen;
1847 }
1848 --flglockprint;
1849 --flglockwrite;
1850 flglockprint = (flglockprint > 0) ? 1 : 0;
1851 flglockwrite = (flglockwrite > 0) ? 1 : 0;
1852} /* cmaes_ReadFromFilePtr */
1853
1854/* ========================================================= */
1855static int
1856Check_Eigen( int N, double **C, double *diag, double **Q)
1857/*
1858 exhaustive test of the output of the eigendecomposition
1859 needs O(n^3) operations
1860
1861 writes to error file
1862 returns number of detected inaccuracies
1863*/
1864{
1865 /* compute Q diag Q^T and Q Q^T to check */
1866 int i, j, k, res = 0;
1867 double cc, dd;
1868 static char s[324];
1869
1870 for (i=0; i < N; ++i)
1871 for (j=0; j < N; ++j) {
1872 for (cc=0.,dd=0., k=0; k < N; ++k) {
1873 cc += diag[k] * Q[i][k] * Q[j][k];
1874 dd += Q[i][k] * Q[j][k];
1875 }
1876 /* check here, is the normalization the right one? */
1877 if (fabs(cc - C[i>j?i:j][i>j?j:i])/sqrt(C[i][i]*C[j][j]) > 1e-10
1878 && fabs(cc - C[i>j?i:j][i>j?j:i]) > 3e-14) {
1879 sprintf(s, "%d %d: %.17e %.17e, %e",
1880 i, j, cc, C[i>j?i:j][i>j?j:i], cc-C[i>j?i:j][i>j?j:i]);
1881 ERRORMESSAGE("cmaes_t:Eigen(): imprecise result detected ",
1882 s, 0, 0);
1883 ++res;
1884 }
1885 if (fabs(dd - (i==j)) > 1e-10) {
1886 sprintf(s, "%d %d %.17e ", i, j, dd);
1887 ERRORMESSAGE("cmaes_t:Eigen(): imprecise result detected (Q not orthog.)",
1888 s, 0, 0);
1889 ++res;
1890 }
1891 }
1892 return res;
1893}
1894
1895/* --------------------------------------------------------- */
1896/* --------------------------------------------------------- */
1897void
1899{
1900 int i, N = t->sp.N;
1901
1903
1904 if(flgforce == 0) {
1905 if (t->flgEigensysIsUptodate == 1)
1906 return;
1907
1908 /* return on modulo generation number */
1909 if (t->sp.updateCmode.flgalways == 0 /* not implemented, always ==0 */
1911 )
1912 return;
1913
1914 /* return on time percentage */
1915 if (t->sp.updateCmode.maxtime < 1.00
1917 && t->eigenTimings.tictoctime > 0.0002)
1918 return;
1919 }
1921
1922 Eigen( N, t->C, t->rgD, t->B, t->rgdTmp);
1923
1925
1926 /* find largest and smallest eigenvalue, they are supposed to be sorted anyway */
1927 t->minEW = rgdouMin(t->rgD, N);
1928 t->maxEW = rgdouMax(t->rgD, N);
1929
1930 if (t->flgCheckEigen)
1931 /* needs O(n^3)! writes, in case, error message in error file */
1932 i = Check_Eigen( N, t->C, t->rgD, t->B);
1933
1934#if 0
1935 /* Limit Condition of C to dMaxSignifKond+1 */
1936 if (t->maxEW > t->minEW * t->dMaxSignifKond) {
1937 ERRORMESSAGE("Warning: Condition number of covariance matrix at upper limit.",
1938 " Consider a rescaling or redesign of the objective function. " ,"","");
1939 printf("\nWarning: Condition number of covariance matrix at upper limit\n");
1940 tmp = t->maxEW/t->dMaxSignifKond - t->minEW;
1941 tmp = t->maxEW/t->dMaxSignifKond;
1942 t->minEW += tmp;
1943 for (i=0;i<N;++i) {
1944 t->C[i][i] += tmp;
1945 t->rgD[i] += tmp;
1946 }
1947 } /* if */
1948 t->dLastMinEWgroesserNull = minEW;
1949#endif
1950
1951 for (i = 0; i < N; ++i)
1952 t->rgD[i] = sqrt(t->rgD[i]);
1953
1954 t->flgEigensysIsUptodate = 1;
1955 t->genOfEigensysUpdate = t->gen;
1956
1957 return;
1958
1959} /* cmaes_UpdateEigensystem() */
1960
1961
1962/* ========================================================= */
1963static void
1964Eigen( int N, double **C, double *diag, double **Q, double *rgtmp)
1965/*
1966 Calculating eigenvalues and vectors.
1967 Input:
1968 N: dimension.
1969 C: symmetric (1:N)xN-matrix, solely used to copy data to Q
1970 niter: number of maximal iterations for QL-Algorithm.
1971 rgtmp: N+1-dimensional vector for temporal use.
1972 Output:
1973 diag: N eigenvalues.
1974 Q: Columns are normalized eigenvectors.
1975 */
1976{
1977 int i, j;
1978
1979 if (rgtmp == NULL) /* was OK in former versions */
1980 FATAL("cmaes_t:Eigen(): input parameter double *rgtmp must be non-NULL", 0,0,0);
1981
1982 /* copy C to Q */
1983 if (C != Q) {
1984 for (i=0; i < N; ++i)
1985 for (j = 0; j <= i; ++j)
1986 Q[i][j] = Q[j][i] = C[i][j];
1987 }
1988
1989#if 0
1990 Householder( N, Q, diag, rgtmp);
1991 QLalgo( N, diag, Q, 30*N, rgtmp+1);
1992#else
1993 Householder2( N, Q, diag, rgtmp);
1994 QLalgo2( N, diag, rgtmp, Q);
1995#endif
1996
1997}
1998
1999
2000/* ========================================================= */
2001static void
2002QLalgo2 (int n, double *d, double *e, double **V) {
2003 /*
2004 -> n : Dimension.
2005 -> d : Diagonale of tridiagonal matrix.
2006 -> e[1..n-1] : off-diagonal, output from Householder
2007 -> V : matrix output von Householder
2008 <- d : eigenvalues
2009 <- e : garbage?
2010 <- V : basis of eigenvectors, according to d
2011
2012 Symmetric tridiagonal QL algorithm, iterative
2013 Computes the eigensystem from a tridiagonal matrix in roughtly 3N^3 operations
2014
2015 code adapted from Java JAMA package, function tql2.
2016 */
2017
2018 int i, k, l, m;
2019 double f = 0.0;
2020 double tst1 = 0.0;
2021 double eps = 2.22e-16; /* Math.pow(2.0,-52.0); == 2.22e-16 */
2022
2023 /* shift input e */
2024 for (i = 1; i < n; i++) {
2025 e[i-1] = e[i];
2026 }
2027 e[n-1] = 0.0; /* never changed again */
2028
2029 for (l = 0; l < n; l++) {
2030
2031 /* Find small subdiagonal element */
2032
2033 if (tst1 < fabs(d[l]) + fabs(e[l]))
2034 tst1 = fabs(d[l]) + fabs(e[l]);
2035 m = l;
2036 while (m < n) {
2037 if (fabs(e[m]) <= eps*tst1) {
2038 /* if (fabs(e[m]) + fabs(d[m]+d[m+1]) == fabs(d[m]+d[m+1])) { */
2039 break;
2040 }
2041 m++;
2042 }
2043
2044 /* If m == l, d[l] is an eigenvalue, */
2045 /* otherwise, iterate. */
2046
2047 if (m > l) { /* TODO: check the case m == n, should be rejected here!? */
2048 int iter = 0;
2049 do { /* while (fabs(e[l]) > eps*tst1); */
2050 double dl1, h;
2051 double g = d[l];
2052 double p = (d[l+1] - g) / (2.0 * e[l]);
2053 double r = myhypot(p, 1.);
2054
2055 iter = iter + 1; /* Could check iteration count here */
2056
2057 /* Compute implicit shift */
2058
2059 if (p < 0) {
2060 r = -r;
2061 }
2062 d[l] = e[l] / (p + r);
2063 d[l+1] = e[l] * (p + r);
2064 dl1 = d[l+1];
2065 h = g - d[l];
2066 for (i = l+2; i < n; i++) {
2067 d[i] -= h;
2068 }
2069 f = f + h;
2070
2071 /* Implicit QL transformation. */
2072
2073 p = d[m];
2074 {
2075 double c = 1.0;
2076 double c2 = c;
2077 double c3 = c;
2078 double el1 = e[l+1];
2079 double s = 0.0;
2080 double s2 = 0.0;
2081 for (i = m-1; i >= l; i--) {
2082 c3 = c2;
2083 c2 = c;
2084 s2 = s;
2085 g = c * e[i];
2086 h = c * p;
2087 r = myhypot(p, e[i]);
2088 e[i+1] = s * r;
2089 s = e[i] / r;
2090 c = p / r;
2091 p = c * d[i] - s * g;
2092 d[i+1] = h + s * (c * g + s * d[i]);
2093
2094 /* Accumulate transformation. */
2095
2096 for (k = 0; k < n; k++) {
2097 h = V[k][i+1];
2098 V[k][i+1] = s * V[k][i] + c * h;
2099 V[k][i] = c * V[k][i] - s * h;
2100 }
2101 }
2102 p = -s * s2 * c3 * el1 * e[l] / dl1;
2103 e[l] = s * p;
2104 d[l] = c * p;
2105 }
2106
2107 /* Check for convergence. */
2108
2109 } while (fabs(e[l]) > eps*tst1);
2110 }
2111 d[l] = d[l] + f;
2112 e[l] = 0.0;
2113 }
2114
2115 /* Sort eigenvalues and corresponding vectors. */
2116#if 1
2117 /* TODO: really needed here? So far not, but practical and only O(n^2) */
2118 {
2119 int j;
2120 double p;
2121 for (i = 0; i < n-1; i++) {
2122 k = i;
2123 p = d[i];
2124 for (j = i+1; j < n; j++) {
2125 if (d[j] < p) {
2126 k = j;
2127 p = d[j];
2128 }
2129 }
2130 if (k != i) {
2131 d[k] = d[i];
2132 d[i] = p;
2133 for (j = 0; j < n; j++) {
2134 p = V[j][i];
2135 V[j][i] = V[j][k];
2136 V[j][k] = p;
2137 }
2138 }
2139 }
2140 }
2141#endif
2142} /* QLalgo2 */
2143
2144
2145/* ========================================================= */
2146static void
2147Householder2(int n, double **V, double *d, double *e) {
2148 /*
2149 Householder transformation of a symmetric matrix V into tridiagonal form.
2150 -> n : dimension
2151 -> V : symmetric nxn-matrix
2152 <- V : orthogonal transformation matrix:
2153 tridiag matrix == V * V_in * V^t
2154 <- d : diagonal
2155 <- e[0..n-1] : off diagonal (elements 1..n-1)
2156
2157 code slightly adapted from the Java JAMA package, function private tred2()
2158
2159 */
2160
2161 int i,j,k;
2162
2163 for (j = 0; j < n; j++) {
2164 d[j] = V[n-1][j];
2165 }
2166
2167 /* Householder reduction to tridiagonal form */
2168
2169 for (i = n-1; i > 0; i--) {
2170
2171 /* Scale to avoid under/overflow */
2172
2173 double scale = 0.0;
2174 double h = 0.0;
2175 for (k = 0; k < i; k++) {
2176 scale = scale + fabs(d[k]);
2177 }
2178 if (scale == 0.0) {
2179 e[i] = d[i-1];
2180 for (j = 0; j < i; j++) {
2181 d[j] = V[i-1][j];
2182 V[i][j] = 0.0;
2183 V[j][i] = 0.0;
2184 }
2185 } else {
2186
2187 /* Generate Householder vector */
2188
2189 double f, g, hh;
2190
2191 for (k = 0; k < i; k++) {
2192 d[k] /= scale;
2193 h += d[k] * d[k];
2194 }
2195 f = d[i-1];
2196 g = sqrt(h);
2197 if (f > 0) {
2198 g = -g;
2199 }
2200 e[i] = scale * g;
2201 h = h - f * g;
2202 d[i-1] = f - g;
2203 for (j = 0; j < i; j++) {
2204 e[j] = 0.0;
2205 }
2206
2207 /* Apply similarity transformation to remaining columns */
2208
2209 for (j = 0; j < i; j++) {
2210 f = d[j];
2211 V[j][i] = f;
2212 g = e[j] + V[j][j] * f;
2213 for (k = j+1; k <= i-1; k++) {
2214 g += V[k][j] * d[k];
2215 e[k] += V[k][j] * f;
2216 }
2217 e[j] = g;
2218 }
2219 f = 0.0;
2220 for (j = 0; j < i; j++) {
2221 e[j] /= h;
2222 f += e[j] * d[j];
2223 }
2224 hh = f / (h + h);
2225 for (j = 0; j < i; j++) {
2226 e[j] -= hh * d[j];
2227 }
2228 for (j = 0; j < i; j++) {
2229 f = d[j];
2230 g = e[j];
2231 for (k = j; k <= i-1; k++) {
2232 V[k][j] -= (f * e[k] + g * d[k]);
2233 }
2234 d[j] = V[i-1][j];
2235 V[i][j] = 0.0;
2236 }
2237 }
2238 d[i] = h;
2239 }
2240
2241 /* Accumulate transformations */
2242
2243 for (i = 0; i < n-1; i++) {
2244 double h;
2245 V[n-1][i] = V[i][i];
2246 V[i][i] = 1.0;
2247 h = d[i+1];
2248 if (h != 0.0) {
2249 for (k = 0; k <= i; k++) {
2250 d[k] = V[k][i+1] / h;
2251 }
2252 for (j = 0; j <= i; j++) {
2253 double g = 0.0;
2254 for (k = 0; k <= i; k++) {
2255 g += V[k][i+1] * V[k][j];
2256 }
2257 for (k = 0; k <= i; k++) {
2258 V[k][j] -= g * d[k];
2259 }
2260 }
2261 }
2262 for (k = 0; k <= i; k++) {
2263 V[k][i+1] = 0.0;
2264 }
2265 }
2266 for (j = 0; j < n; j++) {
2267 d[j] = V[n-1][j];
2268 V[n-1][j] = 0.0;
2269 }
2270 V[n-1][n-1] = 1.0;
2271 e[0] = 0.0;
2272
2273} /* Housholder() */
2274
2275
2276#if 0
2277/* ========================================================= */
2278static void
2279WriteMaxErrorInfo(cmaes_t *t)
2280{
2281 int i,j, N=t->sp.N;
2282 char *s = (char *)new_void(200+30*(N+2), sizeof(char)); s[0] = '\0';
2283
2284 sprintf( s+strlen(s),"\nComplete Info\n");
2285 sprintf( s+strlen(s)," Gen %20.12g\n", t->gen);
2286 sprintf( s+strlen(s)," Dimension %d\n", N);
2287 sprintf( s+strlen(s)," sigma %e\n", t->sigma);
2288 sprintf( s+strlen(s)," lastminEW %e\n",
2290 sprintf( s+strlen(s)," maxKond %e\n\n", t->dMaxSignifKond);
2291 sprintf( s+strlen(s)," x-vector rgD Basis...\n");
2292 ERRORMESSAGE( s,0,0,0);
2293 s[0] = '\0';
2294 for (i = 0; i < N; ++i)
2295 {
2296 sprintf( s+strlen(s), " %20.12e", t->rgxmean[i]);
2297 sprintf( s+strlen(s), " %10.4e", t->rgD[i]);
2298 for (j = 0; j < N; ++j)
2299 sprintf( s+strlen(s), " %10.2e", t->B[i][j]);
2300 ERRORMESSAGE( s,0,0,0);
2301 s[0] = '\0';
2302 }
2303 ERRORMESSAGE( "\n",0,0,0);
2304 free( s);
2305} /* WriteMaxErrorInfo() */
2306#endif
2307
2308/* --------------------------------------------------------- */
2309/* --------------- Functions: cmaes_timings_t -------------- */
2310/* --------------------------------------------------------- */
2311/* cmaes_timings_t measures overall time and times between calls
2312 * of tic and toc. For small time spans (up to 1000 seconds)
2313 * CPU time via clock() is used. For large time spans the
2314 * fall-back to elapsed time from time() is used.
2315 * cmaes_timings_update() must be called often enough to prevent
2316 * the fallback. */
2317/* --------------------------------------------------------- */
2318void
2323void
2325 t->totaltime = 0;
2326 t->tictoctime = 0;
2327 t->lasttictoctime = 0;
2328 t->istic = 0;
2329 t->lastclock = clock();
2330 t->lasttime = time(NULL);
2331 t->lastdiff = 0;
2332 t->tictoczwischensumme = 0;
2333 t->isstarted = 1;
2334}
2335
2336double
2338/* returns time between last call of cmaes_timings_*() and now,
2339 * should better return totaltime or tictoctime?
2340 */
2341 double diffc, difft;
2342 clock_t lc = t->lastclock; /* measure CPU in 1e-6s */
2343 time_t lt = t->lasttime; /* measure time in s */
2344
2345 if (t->isstarted != 1)
2346 FATAL("cmaes_timings_started() must be called before using timings... functions",0,0,0);
2347
2348 t->lastclock = clock(); /* measures at most 2147 seconds, where 1s = 1e6 CLOCKS_PER_SEC */
2349 t->lasttime = time(NULL);
2350
2351 diffc = (double)(t->lastclock - lc) / CLOCKS_PER_SEC; /* is presumably in [-21??, 21??] */
2352 difft = difftime(t->lasttime, lt); /* is presumably an integer */
2353
2354 t->lastdiff = difft; /* on the "save" side */
2355
2356 /* use diffc clock measurement if appropriate */
2357 if (diffc > 0 && difft < 1000)
2358 t->lastdiff = diffc;
2359
2360 if (t->lastdiff < 0)
2361 FATAL("BUG in time measurement", 0, 0, 0);
2362
2363 t->totaltime += t->lastdiff;
2364 t->totaltotaltime += t->lastdiff;
2365 if (t->istic) {
2367 t->tictoctime += t->lastdiff;
2368 }
2369
2370 return t->lastdiff;
2371}
2372
2373void
2375 if (t->istic) { /* message not necessary ? */
2376 ERRORMESSAGE("Warning: cmaes_timings_tic called twice without toc",0,0,0);
2377 return;
2378 }
2380 t->istic = 1;
2381}
2382
2383double
2385 if (!t->istic) {
2386 ERRORMESSAGE("Warning: cmaes_timings_toc called without tic",0,0,0);
2387 return -1;
2388 }
2391 t->tictoczwischensumme = 0;
2392 t->istic = 0;
2393 return t->lasttictoctime;
2394}
2395
2396/* --------------------------------------------------------- */
2397/* ---------------- Functions: cmaes_random_t -------------- */
2398/* --------------------------------------------------------- */
2399/* --------------------------------------------------------- */
2400/* X_1 exakt : 0.79788456) */
2401/* chi_eins simuliert : 0.798xx (seed -3) */
2402/* +-0.001 */
2403/* --------------------------------------------------------- */
2404/*
2405 Gauss() liefert normalverteilte Zufallszahlen
2406 bei vorgegebenem seed.
2407*/
2408/* --------------------------------------------------------- */
2409/* --------------------------------------------------------- */
2410
2411long
2412cmaes_random_init( cmaes_random_t *t, long unsigned inseed)
2413{
2414 clock_t cloc = clock();
2415
2416 t->flgstored = 0;
2417 t->rgrand = (long *) new_void(32, sizeof(long));
2418 if (inseed < 1) {
2419 while ((long) (cloc - clock()) == 0)
2420 ; /* TODO: remove this for time critical applications? */
2421 inseed = (long unsigned)labs((long)(100*time(NULL)+clock()));
2422 }
2423 return cmaes_random_Start(t, inseed);
2424}
2425
2426void
2428{
2429 free( t->rgrand);
2430}
2431
2432/* --------------------------------------------------------- */
2433long cmaes_random_Start( cmaes_random_t *t, long unsigned inseed)
2434{
2435 long tmp;
2436 int i;
2437
2438 t->flgstored = 0;
2439 t->startseed = inseed; /* purely for bookkeeping */
2440 while (inseed > 2e9)
2441 inseed /= 2; /* prevent infinite loop on 32 bit system */
2442 if (inseed < 1)
2443 inseed = 1;
2444 t->aktseed = inseed;
2445 for (i = 39; i >= 0; --i)
2446 {
2447 tmp = t->aktseed/127773;
2448 t->aktseed = 16807 * (t->aktseed - tmp * 127773)
2449 - 2836 * tmp;
2450 if (t->aktseed < 0) t->aktseed += 2147483647;
2451 if (i < 32)
2452 t->rgrand[i] = t->aktseed;
2453 }
2454 t->aktrand = t->rgrand[0];
2455 return inseed;
2456}
2457
2458/* --------------------------------------------------------- */
2460{
2461 double x1, x2, rquad, fac;
2462
2463 if (t->flgstored)
2464 {
2465 t->flgstored = 0;
2466 return t->hold;
2467 }
2468 do
2469 {
2470 x1 = 2.0 * cmaes_random_Uniform(t) - 1.0;
2471 x2 = 2.0 * cmaes_random_Uniform(t) - 1.0;
2472 rquad = x1*x1 + x2*x2;
2473 } while(rquad >= 1 || rquad <= 0);
2474 fac = sqrt(-2.0*log(rquad)/rquad);
2475 t->flgstored = 1;
2476 t->hold = fac * x1;
2477 return fac * x2;
2478}
2479
2480/* --------------------------------------------------------- */
2482{
2483 long tmp;
2484
2485 tmp = t->aktseed/127773;
2486 t->aktseed = 16807 * (t->aktseed - tmp * 127773)
2487 - 2836 * tmp;
2488 if (t->aktseed < 0)
2489 t->aktseed += 2147483647;
2490 tmp = t->aktrand / 67108865;
2491 t->aktrand = t->rgrand[tmp];
2492 t->rgrand[tmp] = t->aktseed;
2493 return (double)(t->aktrand)/(2.147483647e9);
2494}
2495
2496/* --------------------------------------------------------- */
2497static void
2498printErrorStrings(FILE *fd, char const *s1, char const *s2,
2499 char const *s3, char const *s4)
2500{
2501 if (s1 != NULL)
2502 fprintf(fd, "%s", s1);
2503 if (s2 != NULL)
2504 fprintf(fd, " %s", s2);
2505 if (s3 != NULL)
2506 fprintf(fd, " %s", s3);
2507 if (s4 != NULL)
2508 fprintf(fd, " %s", s4);
2509}
2510
2511/* --------------------------------------------------------- */
2512/* -------------- Functions: cmaes_readpara_t -------------- */
2513/* --------------------------------------------------------- */
2514void
2516 int dim,
2517 const double * inxstart,
2518 const double * inrgsigma,
2519 int inseed,
2520 int lambda,
2521 const char * filename)
2522{
2523 int i, N;
2524 /* TODO: make sure cmaes_readpara_init has not been called already */
2525 t->filename = NULL; /* set after successful Read */
2526 t->rgsformat = (const char **) new_void(55, sizeof(char *));
2527 t->rgpadr = (void **) new_void(55, sizeof(void *));
2528 t->rgskeyar = (const char **) new_void(11, sizeof(char *));
2529 t->rgp2adr = (double ***) new_void(11, sizeof(double **));
2530 t->weigkey = (char *)new_void(7, sizeof(char));
2531
2532 /* All scalars: */
2533 i = 0;
2534 t->rgsformat[i] = " N %d"; t->rgpadr[i++] = (void *) &t->N;
2535 t->rgsformat[i] = " seed %d"; t->rgpadr[i++] = (void *) &t->seed;
2536 t->rgsformat[i] = " stopMaxFunEvals %lg"; t->rgpadr[i++] = (void *) &t->stopMaxFunEvals;
2537 t->rgsformat[i] = " stopMaxIter %lg"; t->rgpadr[i++] = (void *) &t->stopMaxIter;
2538 t->rgsformat[i] = " stopFitness %lg"; t->rgpadr[i++]=(void *) &t->stStopFitness.val;
2539 t->rgsformat[i] = " stopTolFun %lg"; t->rgpadr[i++]=(void *) &t->stopTolFun;
2540 t->rgsformat[i] = " stopTolFunHist %lg"; t->rgpadr[i++]=(void *) &t->stopTolFunHist;
2541 t->rgsformat[i] = " stopTolX %lg"; t->rgpadr[i++]=(void *) &t->stopTolX;
2542 t->rgsformat[i] = " stopTolUpXFactor %lg"; t->rgpadr[i++]=(void *) &t->stopTolUpXFactor;
2543 t->rgsformat[i] = " lambda %d"; t->rgpadr[i++] = (void *) &t->lambda;
2544 t->rgsformat[i] = " mu %d"; t->rgpadr[i++] = (void *) &t->mu;
2545 t->rgsformat[i] = " weights %5s"; t->rgpadr[i++] = (void *) t->weigkey;
2546 t->rgsformat[i] = " fac*cs %lg";t->rgpadr[i++] = (void *) &t->cs;
2547 t->rgsformat[i] = " fac*damps %lg"; t->rgpadr[i++] = (void *) &t->damps;
2548 t->rgsformat[i] = " ccumcov %lg"; t->rgpadr[i++] = (void *) &t->ccumcov;
2549 t->rgsformat[i] = " mucov %lg"; t->rgpadr[i++] = (void *) &t->mucov;
2550 t->rgsformat[i] = " fac*ccov %lg"; t->rgpadr[i++]=(void *) &t->ccov;
2551 t->rgsformat[i] = " diagonalCovarianceMatrix %lg"; t->rgpadr[i++]=(void *) &t->diagonalCov;
2552 t->rgsformat[i] = " updatecov %lg"; t->rgpadr[i++]=(void *) &t->updateCmode.modulo;
2553 t->rgsformat[i] = " maxTimeFractionForEigendecompostion %lg"; t->rgpadr[i++]=(void *) &t->updateCmode.maxtime;
2554 t->rgsformat[i] = " resume %59s"; t->rgpadr[i++] = (void *) t->resumefile;
2555 t->rgsformat[i] = " fac*maxFunEvals %lg"; t->rgpadr[i++] = (void *) &t->facmaxeval;
2556 t->rgsformat[i] = " fac*updatecov %lg"; t->rgpadr[i++]=(void *) &t->facupdateCmode;
2557 t->n1para = i;
2558 t->n1outpara = i-2; /* disregard last parameters in WriteToFile() */
2559
2560 /* arrays */
2561 i = 0;
2562 t->rgskeyar[i] = " typicalX %d"; t->rgp2adr[i++] = &t->typicalX;
2563 t->rgskeyar[i] = " initialX %d"; t->rgp2adr[i++] = &t->xstart;
2564 t->rgskeyar[i] = " initialStandardDeviations %d"; t->rgp2adr[i++] = &t->rgInitialStds;
2565 t->rgskeyar[i] = " diffMinChange %d"; t->rgp2adr[i++] = &t->rgDiffMinChange;
2566 t->n2para = i;
2567
2568 t->N = dim;
2569 t->seed = (unsigned) inseed;
2570 t->xstart = NULL;
2571 t->typicalX = NULL;
2572 t->typicalXcase = 0;
2573 t->rgInitialStds = NULL;
2574 t->rgDiffMinChange = NULL;
2575 t->stopMaxFunEvals = -1;
2576 t->stopMaxIter = -1;
2577 t->facmaxeval = 1;
2578 t->stStopFitness.flg = -1;
2579 t->stopTolFun = 1e-12;
2580 t->stopTolFunHist = 1e-13;
2581 t->stopTolX = 0; /* 1e-11*insigma would also be reasonable */
2582 t->stopTolUpXFactor = 1e3;
2583
2584 t->lambda = lambda;
2585 t->mu = -1;
2586 t->mucov = -1;
2587 t->weights = NULL;
2588 strcpy(t->weigkey, "log");
2589
2590 t->cs = -1;
2591 t->ccumcov = -1;
2592 t->damps = -1;
2593 t->ccov = -1;
2594
2595 t->diagonalCov = 0; /* default is 0, but this might change in future, see below */
2596
2597 t->updateCmode.modulo = -1;
2598 t->updateCmode.maxtime = -1;
2599 t->updateCmode.flgalways = 0;
2600 t->facupdateCmode = 1;
2601 strcpy(t->resumefile, "_no_");
2602
2603 /* filename == NULL invokes default in cmaes_readpara_Read... */
2604 if (!isNoneStr(filename) && (!filename || strcmp(filename, "writeonly") != 0))
2605 cmaes_readpara_ReadFromFile(t, filename);
2606
2607 if (t->N <= 0)
2608 t->N = dim;
2609
2610 N = t->N;
2611 if (N == 0)
2612 FATAL("cmaes_readpara_t(): problem dimension N undefined.\n",
2613 " (no default value available).",0,0);
2614 if (t->xstart == NULL && inxstart == NULL && t->typicalX == NULL) {
2615 ERRORMESSAGE("Error: initialX undefined. typicalX = 0.5...0.5 used.","","","");
2616 printf("\nError: initialX undefined. typicalX = 0.5...0.5 used.\n");
2617 }
2618 if (t->rgInitialStds == NULL && inrgsigma == NULL) {
2619 /* FATAL("initialStandardDeviations undefined","","",""); */
2620 ERRORMESSAGE("Error: initialStandardDeviations undefined. 0.3...0.3 used.","","","");
2621 printf("\nError: initialStandardDeviations undefined. 0.3...0.3 used.\n");
2622 }
2623
2624 if (t->xstart == NULL) {
2625 t->xstart = new_double(N);
2626
2627 /* put inxstart into xstart */
2628 if (inxstart != NULL) {
2629 for (i=0; i<N; ++i)
2630 t->xstart[i] = inxstart[i];
2631 }
2632 /* otherwise use typicalX or default */
2633 else {
2634 t->typicalXcase = 1;
2635 for (i=0; i<N; ++i)
2636 t->xstart[i] = (t->typicalX == NULL) ? 0.5 : t->typicalX[i];
2637 }
2638 } /* xstart == NULL */
2639
2640 if (t->rgInitialStds == NULL) {
2641 t->rgInitialStds = new_double(N);
2642 for (i=0; i<N; ++i)
2643 t->rgInitialStds[i] = (inrgsigma == NULL) ? 0.3 : inrgsigma[i];
2644 }
2645
2646 t->flgsupplemented = 0;
2647
2648} /* cmaes_readpara_init */
2649
2650/* --------------------------------------------------------- */
2651/* --------------------------------------------------------- */
2653{
2654 if (t->filename != NULL)
2655 free( t->filename);
2656 if (t->xstart != NULL) /* not really necessary */
2657 free( t->xstart);
2658 if (t->typicalX != NULL)
2659 free( t->typicalX);
2660 if (t->rgInitialStds != NULL)
2661 free( t->rgInitialStds);
2662 if (t->rgDiffMinChange != NULL)
2663 free( t->rgDiffMinChange);
2664 if (t->weights != NULL)
2665 free( t->weights);
2666
2667 free((void*)t->rgsformat);
2668 free(t->rgpadr);
2669 free((void*)t->rgskeyar);
2670 free(t->rgp2adr);
2671 free(t->weigkey);
2672}
2673
2674/* --------------------------------------------------------- */
2675/* --------------------------------------------------------- */
2676void
2678{
2679 char s[1000];
2680 const char *ss = "cmaes_initials.par";
2681 int ipara, i;
2682 int size;
2683 FILE *fp;
2684 if (filename == NULL) {
2685 filename = ss;
2686 }
2687 t->filename = NULL; /* nothing read so far */
2688 fp = fopen( filename, "r");
2689 if (fp == NULL) {
2690 ERRORMESSAGE("cmaes_ReadFromFile(): could not open '", filename, "'",0);
2691 return;
2692 }
2693 for (ipara=0; ipara < t->n1para; ++ipara)
2694 {
2695 rewind(fp);
2696 while(fgets(s, sizeof(s), fp) != NULL)
2697 { /* skip comments */
2698 if (s[0] == '#' || s[0] == '%')
2699 continue;
2700 if(sscanf(s, t->rgsformat[ipara], t->rgpadr[ipara]) == 1) {
2701 if (strncmp(t->rgsformat[ipara], " stopFitness ", 13) == 0)
2702 t->stStopFitness.flg = 1;
2703 break;
2704 }
2705 }
2706 } /* for */
2707 if (t->N <= 0)
2708 FATAL("cmaes_readpara_ReadFromFile(): No valid dimension N",0,0,0);
2709 for (ipara=0; ipara < t->n2para; ++ipara)
2710 {
2711 rewind(fp);
2712 while(fgets(s, sizeof(s), fp) != NULL) /* read one line */
2713 { /* skip comments */
2714 if (s[0] == '#' || s[0] == '%')
2715 continue;
2716 if(sscanf(s, t->rgskeyar[ipara], &size) == 1) { /* size==number of values to be read */
2717 if (size > 0) {
2718 *t->rgp2adr[ipara] = new_double(t->N);
2719 for (i=0;i<size&&i<t->N;++i) /* start reading next line */
2720 if (fscanf(fp, " %lf", &(*t->rgp2adr[ipara])[i]) != 1)
2721 break;
2722 if (i<size && i < t->N) {
2723 ERRORMESSAGE("cmaes_readpara_ReadFromFile ", filename, ": ",0);
2724 FATAL( "'", t->rgskeyar[ipara],
2725 "' not enough values found.\n",
2726 " Remove all comments between numbers.");
2727 }
2728 for (; i < t->N; ++i) /* recycle */
2729 (*t->rgp2adr[ipara])[i] = (*t->rgp2adr[ipara])[i%size];
2730 }
2731 }
2732 }
2733 } /* for */
2734 fclose(fp);
2735 assign_string(&(t->filename), filename); /* t->filename must be freed */
2736 return;
2737} /* cmaes_readpara_ReadFromFile() */
2738
2739/* --------------------------------------------------------- */
2740/* --------------------------------------------------------- */
2741void
2743{
2744 int ipara, i;
2745 size_t len;
2746 time_t ti = time(NULL);
2747 FILE *fp = fopen( filenamedest, "a");
2748 if(fp == NULL) {
2749 ERRORMESSAGE("cmaes_WriteToFile(): could not open '",
2750 filenamedest, "'",0);
2751 return;
2752 }
2753 fprintf(fp, "\n# Read from %s at %s\n", t->filename ? t->filename : "",
2754 asctime(localtime(&ti))); /* == ctime() */
2755 for (ipara=0; ipara < 1; ++ipara) {
2756 fprintf(fp, t->rgsformat[ipara], *(int *)t->rgpadr[ipara]);
2757 fprintf(fp, "\n");
2758 }
2759 for (ipara=0; ipara < t->n2para; ++ipara) {
2760 if(*t->rgp2adr[ipara] == NULL)
2761 continue;
2762 fprintf(fp, t->rgskeyar[ipara], t->N);
2763 fprintf(fp, "\n");
2764 for (i=0; i<t->N; ++i)
2765 fprintf(fp, "%7.3g%c", (*t->rgp2adr[ipara])[i], (i%5==4)?'\n':' ');
2766 fprintf(fp, "\n");
2767 }
2768 for (ipara=1; ipara < t->n1outpara; ++ipara) {
2769 if (strncmp(t->rgsformat[ipara], " stopFitness ", 13) == 0)
2770 if(t->stStopFitness.flg == 0) {
2771 fprintf(fp, " stopFitness\n");
2772 continue;
2773 }
2774 len = strlen(t->rgsformat[ipara]);
2775 if (t->rgsformat[ipara][len-1] == 'd') /* read integer */
2776 fprintf(fp, t->rgsformat[ipara], *(int *)t->rgpadr[ipara]);
2777 else if (t->rgsformat[ipara][len-1] == 's') /* read string */
2778 fprintf(fp, t->rgsformat[ipara], (char *)t->rgpadr[ipara]);
2779 else {
2780 if (strncmp(" fac*", t->rgsformat[ipara], 5) == 0) {
2781 fprintf(fp, " ");
2782 fprintf(fp, t->rgsformat[ipara]+5, *(double *)t->rgpadr[ipara]);
2783 } else
2784 fprintf(fp, t->rgsformat[ipara], *(double *)t->rgpadr[ipara]);
2785 }
2786 fprintf(fp, "\n");
2787 } /* for */
2788 fprintf(fp, "\n");
2789 fclose(fp);
2790} /* cmaes_readpara_WriteToFile() */
2791
2792/* --------------------------------------------------------- */
2793/* --------------------------------------------------------- */
2794void
2796/* Called (only) once to finally set parameters. The settings
2797 * typically depend on the current parameter values itself,
2798 * where 0 or -1 may indicate to set them to a certain default
2799 * value. For this reason calling `SupplementDefaults` twice
2800 * might lead to unexpected results.
2801*/
2802{
2803 double t1, t2;
2804 int N = t->N;
2805 clock_t cloc = clock();
2806
2807 if (t->flgsupplemented)
2808 FATAL("cmaes_readpara_SupplementDefaults() cannot be called twice.",0,0,0);
2809 if (t->seed < 1) {
2810 while ((int) (cloc - clock()) == 0)
2811 ; /* TODO: remove this for time critical applications!? */
2812 t->seed = (unsigned int)labs((long)(100*time(NULL)+clock()));
2813 }
2814
2815 if (t->stStopFitness.flg == -1)
2816 t->stStopFitness.flg = 0;
2817
2818 if (t->lambda < 2)
2819 t->lambda = 4+(int)(3*log((double)N));
2820 if (t->mu == -1) {
2821 t->mu = t->lambda/2;
2823 }
2824 if (t->weights == NULL)
2826
2827 if (t->cs > 0) /* factor was read */
2828 t->cs *= (t->mueff + 2.) / (N + t->mueff + 3.);
2829 if (t->cs <= 0 || t->cs >= 1)
2830 t->cs = (t->mueff + 2.) / (N + t->mueff + 3.);
2831
2832 if (t->ccumcov <= 0 || t->ccumcov > 1)
2833 t->ccumcov = 4. / (N + 4);
2834
2835 if (t->mucov < 1) {
2836 t->mucov = t->mueff;
2837 }
2838 t1 = 2. / ((N+1.4142)*(N+1.4142));
2839 t2 = (2.*t->mueff-1.) / ((N+2.)*(N+2.)+t->mueff);
2840 t2 = (t2 > 1) ? 1 : t2;
2841 t2 = (1./t->mucov) * t1 + (1.-1./t->mucov) * t2;
2842 if (t->ccov >= 0) /* ccov holds the read factor */
2843 t->ccov *= t2;
2844 if (t->ccov < 0 || t->ccov > 1) /* set default in case */
2845 t->ccov = t2;
2846
2847 if (t->diagonalCov == -1)
2848 t->diagonalCov = 2 + 100. * N / sqrt((double)t->lambda);
2849
2850 if (t->stopMaxFunEvals == -1) /* may depend on ccov in near future */
2851 t->stopMaxFunEvals = t->facmaxeval*900*(N+3)*(N+3);
2852 else
2853 t->stopMaxFunEvals *= t->facmaxeval;
2854
2855 if (t->stopMaxIter == -1)
2856 t->stopMaxIter = ceil((double)(t->stopMaxFunEvals / t->lambda));
2857
2858 if (t->damps < 0)
2859 t->damps = 1; /* otherwise a factor was read */
2860 t->damps = t->damps
2861 * (1 + 2*douMax(0., sqrt((t->mueff-1.)/(N+1.)) - 1)) /* basic factor */
2862 * douMax(0.3, 1. - /* modify for short runs */
2863 (double)N / (1e-6+douMin(t->stopMaxIter, t->stopMaxFunEvals/t->lambda)))
2864 + t->cs; /* minor increment */
2865
2866 if (t->updateCmode.modulo < 0)
2867 t->updateCmode.modulo = 1./t->ccov/(double)(N)/10.;
2869 if (t->updateCmode.maxtime < 0)
2870 t->updateCmode.maxtime = 0.20; /* maximal 20% of CPU-time */
2871
2872 t->flgsupplemented = 1;
2873
2874} /* cmaes_readpara_SupplementDefaults() */
2875
2876
2877/* --------------------------------------------------------- */
2878/* --------------------------------------------------------- */
2879void
2881{
2882 double s1, s2;
2883 int i;
2884
2885 if(t->weights != NULL)
2886 free( t->weights);
2887 t->weights = new_double(t->mu);
2888 if (strcmp(mode, "lin") == 0)
2889 for (i=0; i<t->mu; ++i)
2890 t->weights[i] = t->mu - i;
2891 else if (strncmp(mode, "equal", 3) == 0)
2892 for (i=0; i<t->mu; ++i)
2893 t->weights[i] = 1;
2894 else if (strcmp(mode, "log") == 0)
2895 for (i=0; i<t->mu; ++i)
2896 t->weights[i] = log(t->mu+1.)-log(i+1.);
2897 else
2898 for (i=0; i<t->mu; ++i)
2899 t->weights[i] = log(t->mu+1.)-log(i+1.);
2900
2901 /* normalize weights vector and set mueff */
2902 for (i=0, s1=0, s2=0; i<t->mu; ++i) {
2903 s1 += t->weights[i];
2904 s2 += t->weights[i]*t->weights[i];
2905 }
2906 t->mueff = s1*s1/s2;
2907 for (i=0; i<t->mu; ++i)
2908 t->weights[i] /= s1;
2909
2910 if(t->mu < 1 || t->mu > t->lambda ||
2911 (t->mu==t->lambda && t->weights[0]==t->weights[t->mu-1]))
2912 FATAL("cmaes_readpara_SetWeights(): invalid setting of mu or lambda",0,0,0);
2913
2914} /* cmaes_readpara_SetWeights() */
2915
2916/* --------------------------------------------------------- */
2917/* --------------------------------------------------------- */
2918static int
2919isNoneStr(const char * filename)
2920{
2921 if (filename && (strcmp(filename, "no") == 0
2922 || strcmp(filename, "non") == 0
2923 || strcmp(filename, "none") == 0))
2924 return 1;
2925
2926 return 0;
2927}
2928
2929/* --------------------------------------------------------- */
2930/* --------------------------------------------------------- */
2931static double
2932douSquare(double d)
2933{
2934 return d*d;
2935}
2936static double
2937douMax( double i, double j)
2938{
2939 return i > j ? i : j;
2940}
2941static double
2942douMin( double i, double j)
2943{
2944 return i < j ? i : j;
2945}
2946static double
2947rgdouMax( const double *rgd, int len)
2948{
2949 int i;
2950 double max = rgd[0];
2951 for (i = 1; i < len; ++i)
2952 max = (max < rgd[i]) ? rgd[i] : max;
2953 return max;
2954}
2955
2956static double
2957rgdouMin( const double *rgd, int len)
2958{
2959 int i;
2960 double min = rgd[0];
2961 for (i = 1; i < len; ++i)
2962 min = (min > rgd[i]) ? rgd[i] : min;
2963 return min;
2964}
2965
2966static int
2967MaxIdx( const double *rgd, int len)
2968{
2969 int i, res;
2970 for(i=1, res=0; i<len; ++i)
2971 if(rgd[i] > rgd[res])
2972 res = i;
2973 return res;
2974}
2975static int
2976MinIdx( const double *rgd, int len)
2977{
2978 int i, res;
2979 for(i=1, res=0; i<len; ++i)
2980 if(rgd[i] < rgd[res])
2981 res = i;
2982 return res;
2983}
2984
2985static double
2986myhypot(double a, double b)
2987/* sqrt(a^2 + b^2) numerically stable. */
2988{
2989 double r = 0;
2990 if (fabs(a) > fabs(b)) {
2991 r = b/a;
2992 r = fabs(a)*sqrt(1+r*r);
2993 } else if (b != 0) {
2994 r = a/b;
2995 r = fabs(b)*sqrt(1+r*r);
2996 }
2997 return r;
2998}
2999
3000static int SignOfDiff(const void *d1, const void * d2)
3001{
3002 return *((double *) d1) > *((double *) d2) ? 1 : -1;
3003}
3004
3005#if 1
3006/* dirty index sort */
3007static void Sorted_index(const double *rgFunVal, int *iindex, int n)
3008{
3009 int i, j;
3010 for (i=1, iindex[0]=0; i<n; ++i) {
3011 for (j=i; j>0; --j) {
3012 if (rgFunVal[iindex[j-1]] < rgFunVal[i])
3013 break;
3014 iindex[j] = iindex[j-1]; /* shift up */
3015 }
3016 iindex[j] = i; /* insert i */
3017 }
3018}
3019#endif
3020
3021static void * new_void(int n, size_t size)
3022{
3023 static char s[70];
3024 void *p = calloc((unsigned) n, size);
3025 if (p == NULL) {
3026 sprintf(s, "new_void(): calloc(%ld,%ld) failed",(long)n,(long)size);
3027 FATAL(s,0,0,0);
3028 }
3029 return p;
3030}
3031
3032double *
3034{
3035 return new_double(n);
3036}
3037
3038static double * new_double(int n)
3039{
3040 static char s[170];
3041 double *p = (double *) calloc((unsigned) n, sizeof(double));
3042 if (p == NULL) {
3043 sprintf(s, "new_double(): calloc(%ld,%ld) failed",
3044 (long)n,(long)sizeof(double));
3045 FATAL(s,0,0,0);
3046 }
3047 return p;
3048}
3049
3050static char * new_string(const char *ins)
3051{
3052 static char s[170];
3053 unsigned i;
3054 char *p;
3055 unsigned len = (unsigned) ((ins != NULL) ? strlen(ins) : 0);
3056
3057 if (len > 1000) {
3058 FATAL("new_string(): input string length was larger then 1000 ",
3059 "(possibly due to uninitialized char *filename)",0,0);
3060 }
3061
3062 p = (char *) calloc( len + 1, sizeof(char));
3063 if (p == NULL) {
3064 sprintf(s, "new_string(): calloc(%ld,%ld) failed",
3065 (long)len,(long)sizeof(char));
3066 FATAL(s,0,0,0);
3067 }
3068 for (i = 0; i < len; ++i)
3069 p[i] = ins[i];
3070 return p;
3071}
3072static void assign_string(char ** pdests, const char *ins)
3073{
3074 if (*pdests)
3075 free(*pdests);
3076 if (ins == NULL)
3077 *pdests = NULL;
3078 else
3079 *pdests = new_string(ins);
3080}
3081
3082/* --------------------------------------------------------- */
3083/* --------------------------------------------------------- */
3084
3085/* ========================================================= */
3086void
3087cmaes_FATAL(char const *s1, char const *s2, char const *s3,
3088 char const *s4)
3089{
3090 time_t t = time(NULL);
3091 ERRORMESSAGE( s1, s2, s3, s4);
3092 ERRORMESSAGE("*** Exiting cmaes_t ***",0,0,0);
3093 printf("\n -- %s ", asctime(localtime(&t)));
3094 printErrorStrings(stdout, s1, s2, s3, s4);
3095 printf("\n");
3096 printf(" *** CMA-ES ABORTED, see errcmaes.err *** \n");
3097 fflush(stdout);
3098 exit(1);
3099}
3100
3101/* ========================================================= */
3102static void
3103FATAL(char const *s1, char const *s2, char const *s3,
3104 char const *s4)
3105{
3106 cmaes_FATAL(s1, s2, s3, s4);
3107}
3108
3109/* ========================================================= */
3110static void ERRORMESSAGE( char const *s1, char const *s2,
3111 char const *s3, char const *s4)
3112{
3113#if 1
3114 /* static char szBuf[700]; desirable but needs additional input argument
3115 sprintf(szBuf, "%f:%f", gen, gen*lambda);
3116 */
3117 FILE *fp = fopen( "errcmaes.err", "a");
3118 if (fp == NULL) {
3119 printf("\nFATAL ERROR: ");
3120 printErrorStrings(stdout, s1, s2, s3, s4);
3121 printf("\n");
3122 printf("cmaes_t could not open file 'errcmaes.err'.");
3123 printf("\n *** CMA-ES ABORTED *** ");
3124 fflush(stdout);
3125 exit(1);
3126 } else {
3127 time_t t = time(NULL);
3128 fprintf(fp, "\n -- %s ", asctime(localtime(&t)));
3129 printErrorStrings(fp, s1, s2, s3, s4);
3130 fprintf(fp, "\n");
3131 fclose (fp);
3132 }
3133#endif
3134}
size_t size
Definition chdk_pipe.c:143
const double * cmaes_Optimize(cmaes_t *, double(*pFun)(double const *, int dim), long iterations)
Definition cmaes.c:771
double * cmaes_UpdateDistribution(cmaes_t *t, const double *rgFunVal)
Definition cmaes.c:810
void cmaes_timings_start(cmaes_timings_t *timing)
Definition cmaes.c:2324
double *const * cmaes_ReSampleSingle(cmaes_t *t, int iindex)
Definition cmaes.c:698
static char * new_string(const char *)
Definition cmaes.c:3050
static int Check_Eigen(int N, double **C, double *diag, double **Q)
Definition cmaes.c:1856
char * cmaes_SayHello(cmaes_t *t)
Definition cmaes.c:263
static int MinIdx(const double *rgd, int len)
Definition cmaes.c:2976
void cmaes_resume_distribution(cmaes_t *t, char *filename)
Definition cmaes.c:438
void cmaes_ReadSignals(cmaes_t *t, char const *filename)
Definition cmaes.c:1684
void cmaes_readpara_ReadFromFile(cmaes_readpara_t *, const char *szFileName)
Definition cmaes.c:2677
static int SignOfDiff(const void *d1, const void *d2)
Definition cmaes.c:3000
static void Adapt_C2(cmaes_t *t, int hsig)
Definition cmaes.c:976
static void Sorted_index(const double *rgFunVal, int *index, int n)
Definition cmaes.c:3007
static int MaxIdx(const double *rgd, int len)
Definition cmaes.c:2967
static void Householder2(int n, double **V, double *d, double *e)
Definition cmaes.c:2147
void cmaes_readpara_init(cmaes_readpara_t *, int dim, const double *xstart, const double *sigma, int seed, int lambda, const char *filename)
Definition cmaes.c:2515
long cmaes_random_Start(cmaes_random_t *, long unsigned seed)
Definition cmaes.c:2433
double const * cmaes_SetMean(cmaes_t *, const double *xmean)
Definition cmaes.c:601
static void ERRORMESSAGE(char const *sz1, char const *s2, char const *s3, char const *s4)
Definition cmaes.c:3110
void cmaes_readpara_WriteToFile(cmaes_readpara_t *, const char *filenamedest)
Definition cmaes.c:2742
double * cmaes_PerturbSolutionInto(cmaes_t *t, double *xout, double const *xin, double eps)
Definition cmaes.c:747
void cmaes_readpara_SupplementDefaults(cmaes_readpara_t *)
Definition cmaes.c:2795
double * cmaes_NewDouble(int n)
Definition cmaes.c:3033
double * cmaes_SampleSingleInto(cmaes_t *t, double *rgx)
Definition cmaes.c:725
static double myhypot(double a, double b)
Definition cmaes.c:2986
void cmaes_readpara_SetWeights(cmaes_readpara_t *, const char *mode)
Definition cmaes.c:2880
void cmaes_WriteToFile(cmaes_t *, const char *key, const char *name)
Definition cmaes.c:1035
void cmaes_readpara_exit(cmaes_readpara_t *)
Definition cmaes.c:2652
void cmaes_timings_tic(cmaes_timings_t *timing)
Definition cmaes.c:2374
static double douSquare(double)
Definition cmaes.c:2932
static void * new_void(int n, size_t size)
Definition cmaes.c:3021
void cmaes_ReadFromFilePtr(cmaes_t *, FILE *fp)
Definition cmaes.c:1699
double *const * cmaes_SamplePopulation(cmaes_t *t)
Definition cmaes.c:625
static double douMax(double d1, double d2)
Definition cmaes.c:2937
static void assign_string(char **, const char *)
Definition cmaes.c:3072
double * cmaes_GetNew(cmaes_t *t, char const *s)
Definition cmaes.c:1502
double cmaes_random_Uniform(cmaes_random_t *)
Definition cmaes.c:2481
double cmaes_timings_update(cmaes_timings_t *timing)
Definition cmaes.c:2337
double * cmaes_GetInto(cmaes_t *t, char const *s, double *res)
Definition cmaes.c:1489
static void FATAL(char const *sz1, char const *s2, char const *s3, char const *s4)
Definition cmaes.c:3103
double cmaes_Get(cmaes_t *t, char const *s)
Definition cmaes.c:1426
static void printErrorStrings(FILE *fd, char const *s1, char const *s2, char const *s3, char const *s4)
Definition cmaes.c:2498
double * cmaes_init_final(cmaes_t *t)
Definition cmaes.c:291
double * cmaes_init(cmaes_t *t, int dimension, double *inxstart, double *inrgstddev, long int inseed, int lambda, const char *input_parameter_filename)
Definition cmaes.c:416
static int isNoneStr(const char *filename)
Definition cmaes.c:2919
static double douMin(double d1, double d2)
Definition cmaes.c:2942
void cmaes_WriteToFilePtr(cmaes_t *, const char *key, FILE *fp)
Definition cmaes.c:1071
static const char * c_cmaes_version
Definition cmaes.c:245
void cmaes_FATAL(char const *s1, char const *s2, char const *s3, char const *s4)
Definition cmaes.c:3087
double cmaes_timings_toc(cmaes_timings_t *timing)
Definition cmaes.c:2384
static double rgdouMin(const double *rgd, int len)
Definition cmaes.c:2957
double const * cmaes_ReSampleSingle_old(cmaes_t *t, double *rgx)
Definition cmaes.c:676
void cmaes_WriteToFileAW(cmaes_t *t, const char *key, const char *name, const char *append)
Definition cmaes.c:1042
static void Eigen(int N, double **C, double *diag, double **Q, double *rgtmp)
Definition cmaes.c:1964
void cmaes_init_para(cmaes_t *t, int dimension, double *inxstart, double *inrgstddev, long int inseed, int lambda, const char *input_parameter_filename)
Definition cmaes.c:277
static char * getTimeStr(void)
Definition cmaes.c:252
static void QLalgo2(int n, double *d, double *e, double **V)
Definition cmaes.c:2002
static void TestMinStdDevs(cmaes_t *)
Definition cmaes.c:1019
void cmaes_random_exit(cmaes_random_t *)
Definition cmaes.c:2427
double cmaes_random_Gauss(cmaes_random_t *)
Definition cmaes.c:2459
void cmaes_UpdateEigensystem(cmaes_t *t, int flgforce)
Definition cmaes.c:1898
static double * new_double(int n)
Definition cmaes.c:3038
void cmaes_exit(cmaes_t *t)
Definition cmaes.c:565
const char * cmaes_TestForTermination(cmaes_t *t)
Definition cmaes.c:1548
long cmaes_random_init(cmaes_random_t *, long unsigned seed)
Definition cmaes.c:2412
const double * cmaes_GetPtr(cmaes_t *t, char const *s)
Definition cmaes.c:1509
static double rgdouMax(const double *rgd, int len)
Definition cmaes.c:2947
void cmaes_timings_init(cmaes_timings_t *timing)
Definition cmaes.c:2319
static bool stop(uint32_t session)
End only the matching session, even if its stop packet cannot be sent.
static bool range(size_t total, size_t offset, size_t length)
Check a byte span without forming a potentially overflowing end offset.
Definition exifite.c:133
int fd
Definition serial.c:39
long int startseed
Definition cmaes.h:43
long int * rgrand
Definition cmaes.h:46
short flgstored
Definition cmaes.h:49
double hold
Definition cmaes.h:50
long int aktseed
Definition cmaes.h:44
long int aktrand
Definition cmaes.h:45
double damps
Definition cmaes.h:109
double mueff
Definition cmaes.h:107
const char ** rgskeyar
Definition cmaes.h:123
double stopTolFun
Definition cmaes.h:99
double * weights
Definition cmaes.h:108
struct cmaes_readpara_t::@246072133035303104351372072164363242153236050306 stStopFitness
short flgsupplemented
Definition cmaes.h:83
double val
Definition cmaes.h:98
double facupdateCmode
Definition cmaes.h:115
double *** rgp2adr
Definition cmaes.h:124
double diagonalCov
Definition cmaes.h:113
char * filename
Definition cmaes.h:82
struct cmaes_readpara_t::@107217224144210100141365135165274176060022201164 updateCmode
double * typicalX
Definition cmaes.h:89
const char ** rgsformat
Definition cmaes.h:121
void ** rgpadr
Definition cmaes.h:122
double stopTolX
Definition cmaes.h:101
char * weigkey
Definition cmaes.h:119
double facmaxeval
Definition cmaes.h:96
double ccov
Definition cmaes.h:112
double stopMaxIter
Definition cmaes.h:97
double maxtime
Definition cmaes.h:114
unsigned int seed
Definition cmaes.h:87
double stopMaxFunEvals
Definition cmaes.h:95
double * rgInitialStds
Definition cmaes.h:91
double mucov
Definition cmaes.h:107
double ccumcov
Definition cmaes.h:111
double * xstart
Definition cmaes.h:88
int typicalXcase
Definition cmaes.h:90
double modulo
Definition cmaes.h:114
double * rgDiffMinChange
Definition cmaes.h:92
char resumefile[99]
Definition cmaes.h:120
double stopTolFunHist
Definition cmaes.h:100
double stopTolUpXFactor
Definition cmaes.h:102
double * rgxbestever
Definition cmaes.h:142
double countevals
Definition cmaes.h:165
char sOutString[330]
Definition cmaes.h:173
short flgIniphase
Definition cmaes.h:147
double ** rgrgx
Definition cmaes.h:143
short flgCheckEigen
Definition cmaes.h:176
double * rgxmean
Definition cmaes.h:141
double ** B
Definition cmaes.h:152
const char * version
Definition cmaes.h:134
double minEW
Definition cmaes.h:171
double chiN
Definition cmaes.h:150
double ** C
Definition cmaes.h:151
double * rgpc
Definition cmaes.h:155
short flgStop
Definition cmaes.h:148
cmaes_random_t rand
Definition cmaes.h:137
double genOfEigensysUpdate
Definition cmaes.h:177
double * rgdTmp
Definition cmaes.h:160
short flgEigensysIsUptodate
Definition cmaes.h:175
double * rgFuncValue
Definition cmaes.h:161
double gen
Definition cmaes.h:164
double * rgBDz
Definition cmaes.h:159
double maxEW
Definition cmaes.h:170
double * rgD
Definition cmaes.h:153
double * rgout
Definition cmaes.h:158
time_t firstprinttime
Definition cmaes.h:188
double dMaxSignifKond
Definition cmaes.h:180
double dLastMinEWgroesserNull
Definition cmaes.h:181
time_t firstwritetime
Definition cmaes.h:187
double maxdiagC
Definition cmaes.h:168
double * rgps
Definition cmaes.h:156
time_t writetime
Definition cmaes.h:186
double * rgxold
Definition cmaes.h:157
cmaes_readpara_t sp
Definition cmaes.h:136
short flgresumedone
Definition cmaes.h:183
cmaes_timings_t eigenTimings
Definition cmaes.h:178
double * arFuncValueHist
Definition cmaes.h:145
double sigma
Definition cmaes.h:139
int * index
Definition cmaes.h:144
double state
Definition cmaes.h:166
time_t printtime
Definition cmaes.h:185
double mindiagC
Definition cmaes.h:169
double * publicFitness
Definition cmaes.h:162
short istic
Definition cmaes.h:69
double tictoczwischensumme
Definition cmaes.h:73
double totaltotaltime
Definition cmaes.h:60
double lasttictoctime
Definition cmaes.h:62
clock_t lastclock
Definition cmaes.h:65
double totaltime
Definition cmaes.h:59
double lastdiff
Definition cmaes.h:72
short isstarted
Definition cmaes.h:70
double tictoctime
Definition cmaes.h:61
time_t lasttime
Definition cmaes.h:66