| 1 | #ifndef _Minimizers_h_ |
| 2 | #define _Minimizers_h_ |
| 3 | /* Minimizers.h |
| 4 | * |
| 5 | * Copyright (C) 1993-2019 David Weenink, 2015-2018 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "Data.h" |
| 22 | #include "Graphics.h" |
| 23 | |
| 24 | /*********** deferred class Minimizer **********************************/ |
| 25 | |
| 26 | Thing_define (Minimizer, Thing) { |
| 27 | integer numberOfParameters; |
| 28 | autoVEC p; /* the parameters */ |
| 29 | double minimum; /* current minimum */ |
| 30 | autoVEC history; /* previous minima */ |
| 31 | double tolerance; /* stopping criterion */ |
| 32 | Daata object; /* reference to the object that uses this Minimizer */ |
| 33 | integer numberOfFunctionCalls; /* the number of times 'func' has been called */ |
| 34 | bool success; /* indicates whether I'm done */ |
| 35 | //integer start; /* start iteration series */ |
| 36 | integer maximumNumberOfIterations; /* the current maximum number of iterations */ |
| 37 | integer iteration; /* the current number of iterations */ |
| 38 | void (*afterHook) (Minimizer me, Thing boss); /* to be called after each iteration */ |
| 39 | Thing afterBoss; |
| 40 | Graphics gmonitor; /* graphics to monitor the minimization process */ |
| 41 | |
| 42 | void v1_info () |
| 43 | override { } |
| 44 | |
| 45 | virtual void v_minimize () { } /* does the work */ |
| 46 | virtual void v_reset () { } |
| 47 | }; |
| 48 | |
| 49 | void Minimizer_init (Minimizer me, integer numberOfParameters, Daata object); |
| 50 | /* |
| 51 | Preconditions: |
| 52 | numberOfParameters > 0; |
| 53 | */ |
| 54 | |
| 55 | void Minimizer_reset (Minimizer me, constVEC const& guess); |
| 56 | /* reset the start values for the minimizer |
| 57 | * |
| 58 | * Post conditions: |
| 59 | * p [] = guess []; |
| 60 | * my minimum = 1e308; |
| 61 | * success = maximumNumberOfIterations = iteration = numberOfFunctionCalls = 0; |
| 62 | * reset (me); |
| 63 | */ |
| 64 | |
| 65 | void Minimizer_minimize (Minimizer me, integer maximumNumberOfIterations, double tolerance, int monitor); |
| 66 | /* Minimizes during maximally maximumNumberOfIterations. The gmonitor is initialized |
| 67 | * before minimization and cleared afterwards. |
| 68 | * Preconditions: |
| 69 | * maximumNumberOfIterations >= 0; |
| 70 | * tolerance > 0.0; |
| 71 | * Postconditions: |
| 72 | * if (reset) Minimizer_reset called with xopt as initial guess. |
| 73 | * after each function call: numberOfFunctionCalls++ |
| 74 | * after each iteration: iteration++ |
| 75 | */ |
| 76 | |
| 77 | void Minimizer_minimizeManyTimes (Minimizer me, integer maxIterationsPerTime, integer numberOfTimes, double tolerance); |
| 78 | |
| 79 | void Minimizer_drawHistory (Minimizer me, Graphics g, integer itmin, integer itmax, double minimum, double maximum, bool garnish); |
| 80 | |
| 81 | double Minimizer_getMinimum (Minimizer me); |
| 82 | |
| 83 | /********** deferred class LineMinimizer ************************************/ |
| 84 | |
| 85 | Thing_define (LineMinimizer, Minimizer) { |
| 86 | /* the function to be minimized */ |
| 87 | double (*func) (Daata object, VEC const& p); |
| 88 | double maxLineStep; // maximum step in line search direction |
| 89 | autoVEC direction; // search direction vector |
| 90 | autoVEC ptry; // point in search direction |
| 91 | |
| 92 | //virtual void v_linmin (double p [], double fp, double direction [], double *fret); |
| 93 | }; |
| 94 | |
| 95 | void LineMinimizer_init (LineMinimizer me, integer numberOfParameters, Daata object, double (*func) (Daata object, VEC const& p)); |
| 96 | |
| 97 | /****************** class SteepestDescentMinimizer**************************/ |
| 98 | |
| 99 | typedef struct structSteepestDescentMinimizer_parameters { |
| 100 | double eta, momentum; |
| 101 | } *SteepestDescentMinimizer_parameters; |
| 102 | |
| 103 | Thing_define (SteepestDescentMinimizer, Minimizer) { |
| 104 | double eta, momentum; |
| 105 | double (*func) (Daata object, VEC const& p); |
| 106 | void (*dfunc) (Daata object, VEC const& p, VEC const& dp); |
| 107 | /* calculates gradient at position p */ |
| 108 | |
| 109 | void v_minimize () |
| 110 | override; |
| 111 | }; |
| 112 | |
| 113 | autoSteepestDescentMinimizer SteepestDescentMinimizer_create (integer numberOfParameters, Daata object, double (*func) (Daata object, VEC const& p), void (*dfunc) (Daata object, VEC const& p, VEC const& dp)); |
| 114 | |
| 115 | |
| 116 | /********** class VDSmagtMinimizer ********************************/ |
| 117 | |
| 118 | typedef struct structVDSmagtMinimizer_parameters { |
| 119 | double lineSearchGradient; |
| 120 | integer lineSearchMaxNumOfIterations; |
| 121 | } *VDSmagtMinimizer_parameters; |
| 122 | |
| 123 | Thing_define (VDSmagtMinimizer, Minimizer) { |
| 124 | double (*func) (Daata object, VEC const& p); |
| 125 | void (*dfunc) (Daata object, VEC const& p, VEC const& dp); |
| 126 | autoVEC dp; |
| 127 | double lineSearchGradient; |
| 128 | integer lineSearchMaxNumOfIterations; |
| 129 | double gr0, gropt, df, alplim, alpha, dalpha, alphamin; |
| 130 | autoVEC pc; /* position of current point */ |
| 131 | autoVEC gc; /* gradient of current point */ |
| 132 | autoVEC g0; /* gradient at beginning of line search */ |
| 133 | autoVEC s; /* search direction for line search */ |
| 134 | autoVEC srst; /* search direction for first iteration after restart */ |
| 135 | autoVEC grst; /* gradient for first iteration after restart */ |
| 136 | double fc, grc, fch, gr2s, temp, grs, beta, gcg0; |
| 137 | double gamma, gamma_in, f0, gsq, gopt_sq; |
| 138 | integer lineSearch_iteration, flag, again, one_up, restart; |
| 139 | bool restart_flag; |
| 140 | |
| 141 | void v_minimize () |
| 142 | override; |
| 143 | void v_reset () |
| 144 | override; |
| 145 | }; |
| 146 | |
| 147 | autoVDSmagtMinimizer VDSmagtMinimizer_create (integer dimension, Daata object, double (*func) (Daata object, VEC const& p), void (*dfunc) (Daata object, VEC const& p, VEC const& dp)); |
| 148 | |
| 149 | #endif /* _Minimizer_h_ */ |