1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/* gss.c
*
* Copyright (C) 2012 Tamas Nepusz
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <float.h>
#include <math.h>
#include <string.h>
#include "error.h"
#include "gss.h"
#include "platform.h"
/**
* \def PHI
*
* The golden ratio, i.e. 1+sqrt(5)/2
*/
#define PHI 1.618033988749895
/**
* \def RESPHI
*
* Constant defined as 2 - \c PHI
*/
#define RESPHI 0.3819660112501051
/**
* \const _defparam
*
* Default parameters for the GSS algorithm.
*/
static const gss_parameter_t _defparam = {
/* .epsilon = */ DBL_MIN,
/* .on_error = */ GSS_ERROR_STOP
};
/**
* Stores whether the last optimization run triggered a warning or not.
*/
static unsigned short int gss_i_warning_flag = 0;
void gss_parameter_init(gss_parameter_t *param) {
memcpy(param, &_defparam, sizeof(*param));
}
unsigned short int gss_get_warning_flag() {
return gss_i_warning_flag;
}
#define TERMINATE { \
if (_min) { \
*(_min) = min; \
} \
if (_fmin) { \
*(_fmin) = fmin; \
} \
}
#define EVALUATE(x, fx) { \
fx = proc_evaluate(instance, x); \
if (fmin > fx) { \
min = x; \
fmin = fx; \
} \
if (proc_progress) { \
retval = proc_progress(instance, x, fx, min, fmin, \
(a < b) ? a : b, (a < b) ? b : a, k); \
if (retval) { \
TERMINATE; \
return PLFIT_SUCCESS; \
} \
} \
}
int gss(double a, double b, double *_min, double *_fmin,
gss_evaluate_t proc_evaluate, gss_progress_t proc_progress,
void* instance, const gss_parameter_t *_param) {
double c, d, min;
double fa, fb, fc, fd, fmin;
int k = 0;
int retval;
unsigned short int successful = 1;
gss_parameter_t param = _param ? (*_param) : _defparam;
gss_i_warning_flag = 0;
if (a > b) {
c = a; a = b; b = c;
}
min = a;
fmin = proc_evaluate(instance, a);
c = a + RESPHI*(b-a);
EVALUATE(a, fa);
EVALUATE(b, fb);
EVALUATE(c, fc);
if (fc >= fa || fc >= fb) {
if (param.on_error == GSS_ERROR_STOP) {
return PLFIT_FAILURE;
} else {
gss_i_warning_flag = 1;
}
}
while (fabs(a-b) > param.epsilon) {
k++;
d = c + RESPHI*(b-c);
EVALUATE(d, fd);
if (fd >= fa || fd >= fb) {
if (param.on_error == GSS_ERROR_STOP) {
successful = 0;
break;
} else {
gss_i_warning_flag = 1;
}
}
if (fc <= fd) {
b = a; a = d;
} else {
a = c; c = d; fc = fd;
}
}
if (successful) {
c = (a+b) / 2.0;
k++;
EVALUATE(c, fc);
TERMINATE;
}
return successful ? PLFIT_SUCCESS : PLFIT_FAILURE;
}
|