1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/* multimin/test_funcs.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000 Fabrice Rossi
*
* 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 <config.h>
#include <math.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_multimin.h>
#include "test_funcs.h"
gsl_multimin_function_fdf simpleabs =
{&simpleabs_f,
&simpleabs_df,
&simpleabs_fdf,
2, 0};
gsl_multimin_function simpleabs_fmin =
{&simpleabs_f,
2, 0};
void simpleabs_initpt (gsl_vector * x)
{
gsl_vector_set (x, 0, 1.0);
gsl_vector_set (x, 1, 2.0);
}
void simpleabs_initpt1 (gsl_vector * x)
{
gsl_vector_set (x, 0, 1.0);
gsl_vector_set (x, 1, 1.0);
}
double simpleabs_f (const gsl_vector * x, void *params)
{
double u = gsl_vector_get(x,0);
double v = gsl_vector_get(x,1);
double a = u - 1;
double b = v - 2;
fcount++;
return fabs(a) + fabs(b);
}
void simpleabs_df (const gsl_vector * x, void *params, gsl_vector * df)
{
double u = gsl_vector_get(x,0);
double v = gsl_vector_get(x,1);
gcount++;
gsl_vector_set(df,0, GSL_SIGN(u-1));
gsl_vector_set(df,1, GSL_SIGN(v-2));
}
void simpleabs_fdf (const gsl_vector * x, void *params, double * f,
gsl_vector * df)
{
double u = gsl_vector_get(x,0);
double v = gsl_vector_get(x,1);
double a = u - 1;
double b = v - 2;
gcount++;
*f = fabs(a) + fabs(b);
gsl_vector_set(df,0, GSL_SIGN(u-1));
gsl_vector_set(df,1, GSL_SIGN(v-2));
}
gsl_multimin_function_fdf rosenbrock =
{&rosenbrock_f,
&rosenbrock_df,
&rosenbrock_fdf,
2, 0};
gsl_multimin_function rosenbrock_fmin =
{&rosenbrock_f,
2, 0};
void rosenbrock_initpt (gsl_vector * x)
{
gsl_vector_set (x, 0, -1.2);
gsl_vector_set (x, 1, 1.0);
}
void rosenbrock_initpt1 (gsl_vector * x)
{
gsl_vector_set (x, 0, 1.0);
gsl_vector_set (x, 1, 1.0);
}
double rosenbrock_f (const gsl_vector * x, void *params)
{
double u = gsl_vector_get(x,0);
double v = gsl_vector_get(x,1);
double a = u - 1;
double b = u * u - v;
fcount++;
return a * a + 10 * b * b;
}
void rosenbrock_df (const gsl_vector * x, void *params, gsl_vector * df)
{
double u = gsl_vector_get(x,0);
double v = gsl_vector_get(x,1);
double b = u * u - v;
gcount++;
gsl_vector_set(df,0,2 * (u - 1) + 40 * u * b);
gsl_vector_set(df,1,-20 * b);
}
void rosenbrock_fdf (const gsl_vector * x, void *params, double * f,
gsl_vector * df)
{
double u = gsl_vector_get(x,0);
double v = gsl_vector_get(x,1);
double a = u - 1;
double b = u * u - v;
gcount++;
*f = a * a + 10 * b * b;
gsl_vector_set(df,0,2 * (u - 1) + 40 * u * b);
gsl_vector_set(df,1,-20 * b);
}
gsl_multimin_function_fdf roth =
{&roth_f,
&roth_df,
&roth_fdf,
2, 0};
gsl_multimin_function roth_fmin =
{&roth_f,
2, 0};
void roth_initpt (gsl_vector * x)
{
gsl_vector_set (x, 0, 4.5);
gsl_vector_set (x, 1, 3.5);
}
double roth_f (const gsl_vector * x, void *params)
{
double u = gsl_vector_get(x,0);
double v = gsl_vector_get(x,1);
double a = -13.0 + u + ((5.0 - v)*v - 2.0)*v;
double b = -29.0 + u + ((v + 1.0)*v - 14.0)*v;
fcount++;
return a * a + b * b;
}
void roth_df (const gsl_vector * x, void *params, gsl_vector * df)
{
double u = gsl_vector_get(x,0);
double v = gsl_vector_get(x,1);
double a = -13.0 + u + ((5.0 - v)*v - 2.0)*v;
double b = -29.0 + u + ((v + 1.0)*v - 14.0)*v;
double c = -2 + v * (10 - 3 * v);
double d = -14 + v * (2 + 3 * v);
gcount++;
gsl_vector_set(df,0,2 * a + 2 * b);
gsl_vector_set(df,1,2 * a * c + 2 * b * d);
}
void roth_fdf (const gsl_vector * x, void *params, double * f,
gsl_vector * df)
{
*f = roth_f (x,params);
roth_df(x,params,df);
}
gsl_multimin_function_fdf wood =
{&wood_f,
&wood_df,
&wood_fdf,
4, 0};
gsl_multimin_function wood_fmin =
{&wood_f,
4, 0};
void
wood_initpt (gsl_vector * x)
{
gsl_vector_set (x, 0, -3.0);
gsl_vector_set (x, 1, -1.0);
gsl_vector_set (x, 2, -3.0);
gsl_vector_set (x, 3, -1.0);
}
double wood_f (const gsl_vector * x, void *params)
{
double u1 = gsl_vector_get(x,0);
double u2 = gsl_vector_get(x,1);
double u3 = gsl_vector_get(x,2);
double u4 = gsl_vector_get(x,3);
double t1 = u1 * u1 - u2;
double t2 = u3 * u3 - u4;
fcount++;
return 100 * t1 * t1 + (1 - u1) * (1 - u1)
+ 90 * t2 * t2 + (1 - u3) * (1 - u3)
+ 10.1 * ( (1 - u2) * (1 - u2) + (1 - u4) * (1 - u4) )
+ 19.8 * (1 - u2) * (1 - u4);
}
void wood_df (const gsl_vector * x, void *params, gsl_vector * df)
{
double u1 = gsl_vector_get(x,0);
double u2 = gsl_vector_get(x,1);
double u3 = gsl_vector_get(x,2);
double u4 = gsl_vector_get(x,3);
double t1 = u1 * u1 - u2;
double t2 = u3 * u3 - u4;
gcount++;
gsl_vector_set(df,0, 400 * u1 * t1 - 2 * (1 - u1) );
gsl_vector_set(df,1, -200 * t1 - 20.2 * (1 - u2) - 19.8 * (1 - u4) );
gsl_vector_set(df,2, 360 * u3 * t2 - 2 * (1 - u3) );
gsl_vector_set(df,3, -180 * t2 - 20.2 * (1 - u4) - 19.8 * (1 - u2) );
}
void wood_fdf (const gsl_vector * x, void *params, double * f, gsl_vector * df)
{
wood_df(x,params,df);
*f=wood_f(x,params);
}
gsl_multimin_function_fdf Nrosenbrock =
{&rosenbrock_f,
&Nrosenbrock_df,
&Nrosenbrock_fdf,
2, 0};
void Nrosenbrock_df (const gsl_vector * x, void *params, gsl_vector * df)
{
gsl_multimin_function F ;
F.f = rosenbrock_f;
F.params = params;
F.n = x->size;
gsl_multimin_diff (&F, x, df);
}
void Nrosenbrock_fdf (const gsl_vector * x, void *params, double * f,
gsl_vector * df)
{
*f = rosenbrock_f (x, params);
Nrosenbrock_df (x, params, df);
}
gsl_multimin_function_fdf Nroth =
{&roth_f,
&Nroth_df,
&Nroth_fdf,
2, 0};
void Nroth_df (const gsl_vector * x, void *params, gsl_vector * df)
{
gsl_multimin_function F ;
F.f = roth_f;
F.params = params;
F.n = x->size;
gsl_multimin_diff (&F, x, df);
}
void Nroth_fdf (const gsl_vector * x, void *params, double * f,
gsl_vector * df)
{
*f = roth_f (x, params);
Nroth_df (x, params, df);
}
gsl_multimin_function_fdf Nwood =
{&wood_f,
&Nwood_df,
&Nwood_fdf,
4, 0};
void Nwood_df (const gsl_vector * x, void *params, gsl_vector * df)
{
gsl_multimin_function F ;
F.f = wood_f;
F.params = params;
F.n = x->size;
gsl_multimin_diff (&F, x, df);
}
void Nwood_fdf (const gsl_vector * x, void *params, double * f,
gsl_vector * df)
{
*f = wood_f (x, params);
Nwood_df (x, params, df);
}
gsl_multimin_function spring_fmin = { &spring_f,
3, 0
};
void
spring_initpt (gsl_vector * x)
{
gsl_vector_set (x, 0, 1.0);
gsl_vector_set (x, 1, 0.0);
gsl_vector_set (x, 2, 7.0 * M_PI);
}
double
spring_f (const gsl_vector * x, void *params)
{
double x0 = gsl_vector_get (x, 0);
double x1 = gsl_vector_get (x, 1);
double x2 = gsl_vector_get (x, 2);
double theta = atan2 (x1, x0);
double r = sqrt (x0 * x0 + x1 * x1);
double z = x2;
while (z > M_PI)
z -= 2.0 * M_PI;
while (z < -M_PI)
z += 2.0 * M_PI;
{
double tmz = theta - z;
double rm1 = r - 1.0;
double ret = 0.1 * (expm1 (tmz * tmz + rm1 * rm1) + fabs (x2 / 10.0));
return ret;
}
}
|