SDDSlib
Loading...
Searching...
No Matches
minc.c
Go to the documentation of this file.
1/**
2 * @file minc.c
3 * @brief Finds the minimum of a multi-parameter function with parameter constraints.
4 *
5 * This file contains the implementation of the minc() function, which searches for the
6 * minimum of a multi-parameter function while respecting constraints on the parameter ranges.
7 *
8 * @copyright
9 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
10 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
11 *
12 * @license
13 * This file is distributed under the terms of the Software License Agreement
14 * found in the file LICENSE included with this distribution.
15 *
16 * @author M. Borland, C. Saunders, R. Soliday
17 */
18
19#include "mdb.h"
20
21/**
22 * @brief Finds the minimum of a multi-parameter function with constraints.
23 *
24 * The minc() function searches for the minimum value of a user-defined multi-parameter
25 * function, allowing constraints on the range of each parameter. It iteratively adjusts
26 * the parameters within specified limits to find the function's minimum value.
27 *
28 * @param fn Pointer to the function to be minimized.
29 * @param x Array of starting values for the parameters.
30 * @param dx Array of step sizes for each parameter.
31 * @param dx_lim Array of step size limits for each parameter.
32 * @param xlo Array of lower bounds for the parameters.
33 * @param xhi Array of upper bounds for the parameters.
34 * @param np Number of parameters.
35 * @param ns_max Maximum number of steps to take before increasing the step size.
36 * @param p_flag If >= 0, prints information during the minimization process.
37 *
38 * @return The minimum value of the function.
39 */
40double minc(fn, x, dx, dx_lim, xlo, xhi, np, ns_max, p_flag) double (*fn)(); /* pointer to fn to be minimize */
41double *x; /* array of starting values of parameters */
42double *dx; /* array of step sizes */
43double *dx_lim; /* array of step size limits */
44double *xlo; /* array of lower limits */
45double *xhi; /* array of upper limits */
46long np; /* number of parameters */
47long ns_max; /* number of steps to take before increasing dx */
48long p_flag; /* if >= 0, information is printed during minimization */
49{
50 register long i, n_steps, pc;
51 register double f0, f1, _dx;
52 long flag, *constraint, at_upper, at_lower;
53
54 pc = 0;
55 constraint = tmalloc(sizeof(long) * np);
56 for (i = 0; i < np; i++)
57 constraint[i] = xlo[i] != xhi[i];
58
59 f0 = (*fn)(x);
60
61 do {
62 for (i = flag = 0; i < np; i++) {
63 if (fabs(_dx = dx[i]) < dx_lim[i]) {
64 flag++;
65 continue;
66 }
67 x[i] += _dx;
68 if (constraint[i]) {
69 if (x[i] < xlo[i]) {
70 x[i] = xlo[i] + 2 * (dx[i] = fabs(_dx) / 2);
71 continue;
72 }
73 if (x[i] > xhi[i]) {
74 x[i] = xhi[i] + 2 * (dx[i] = -fabs(_dx) / 2);
75 continue;
76 }
77 }
78 f1 = (*fn)(x);
79 n_steps = 0;
80 if (f1 > f0) {
81 dx[i] = _dx = -_dx;
82 x[i] += 2 * _dx;
83 if (constraint[i]) {
84 if (x[i] < xlo[i]) {
85 x[i] = xlo[i] + 2 * (dx[i] = fabs(_dx) / 2);
86 continue;
87 }
88 if (x[i] > xhi[i]) {
89 x[i] = xhi[i] + 2 * (dx[i] = -fabs(_dx) / 2);
90 continue;
91 }
92 }
93 f1 = (*fn)(x);
94 }
95 while (f1 < f0) {
96 if (n_steps++ == ns_max) {
97 n_steps = 0;
98 dx[i] = _dx = 2 * _dx;
99 }
100 f0 = f1;
101 x[i] += _dx;
102 if (constraint[i]) {
103 if (x[i] < xlo[i]) {
104 x[i] = xlo[i] + 2 * (dx[i] = fabs(_dx));
105 break;
106 }
107 if (x[i] > xhi[i]) {
108 x[i] = xhi[i] + 2 * (dx[i] = -fabs(_dx));
109 break;
110 }
111 }
112 f1 = (*fn)(x);
113 }
114 dx[i] /= 2;
115 x[i] -= _dx;
116 }
117 if (pc++ == p_flag) {
118 printf("%.16le\n", f0);
119 for (i = 0; i < np; i++)
120 printf("%.16le\t%.16le\n", x[i], dx[i]);
121 pc = 0;
122 }
123 } while (flag != np);
124 return (f0);
125}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
double minc(double *fn, double *x, double *dx, double *dx_lim, double *xlo, double *xhi, long np, long ns_max, long p_flag)
Finds the minimum of a multi-parameter function with constraints.
Definition minc.c:40