Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
DownhillSimplexMinimization.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Math_Optimization_DownhillSimplexMinimization_h_
5#define _Stroika_Foundation_Math_Optimization_DownhillSimplexMinimization_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <optional>
10
12#include "Stroika/Foundation/Containers/Sequence.h"
14
15/**
16 * \file
17 *
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 */
20
21namespace Stroika::Foundation::Math::Optimization::DownhillSimplexMinimization {
22
23 using Characters::String;
24 using Containers::Sequence;
25
26 /*
27 * This is the type of function to be minimized.
28 */
29 template <typename FLOAT_TYPE>
30 using TargetFunction = function<FLOAT_TYPE (const Sequence<FLOAT_TYPE>&)>;
31
32 /**
33 * Options to control minimization
34 */
35 template <typename FLOAT_TYPE>
36 struct Options {
37 optional<unsigned int> fMaxIterations;
38 optional<FLOAT_TYPE> fNoImprovementThreshold;
39
40 nonvirtual String ToString () const;
41 };
42
43 /**
44 * Results of minimization
45 */
46 template <typename FLOAT_TYPE>
47 struct Results {
48 Sequence<FLOAT_TYPE> fOptimizedParameters;
49 FLOAT_TYPE fScore{};
50 unsigned int fIterationCount{};
51
52 nonvirtual String ToString () const;
53 };
54
55 /**
56 * \brief Downhill Simplex Minimization, AKA Nelder-Mead algorithm, to compute minimization
57 *
58 * Take an argument function with argument 'initialValues' and try many values to find where the targetFunction is least (ie minimize it).
59 *
60 * If the function is naturally periodic - you can add checks in the minimization function for the desired target range and produce LARGE answers there
61 * so they wont appear as minima of the 'targetFunction'.
62 *
63 * Reference: https://en.wikipedia.org/wiki/Nelder%E2%80%93Mead_method
64 * Reference: https://github.com/fchollet/nelder-mead/blob/master/nelder_mead.py
65 * Reference: http://www.aip.de/groups/soe/local/numres/bookcpdf/c10-4.pdf
66 * Reference: https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/20398/versions/1/previews/fminsearch2.m/index.html
67 *
68 * \note The size of the Sequence<> in initialValues will be the same as the size of the Sequence passed to 'function2Minimize', which
69 * in turn will the the size of the Sequence<> in the Results.
70 *
71 * \par Example Usage
72 * \code
73 * TargetFunction<double> f = [](const Sequence<double>& x) {
74 * double d = x[0];
75 * if (d < 0 or d >= numbers::pi) { // avoid falling off ends of ranges - periodic function
76 * return 100.0;
77 * }
78 * return -std::cos (d);
79 * };
80 * Results<double> result = Run (f, {.1});
81 * EXPECT_TRUE (Math::NearlyEquals (result.fOptimizedParameters[0], 0.0, 1e-10));
82 * \endcode
83 */
84 template <typename FLOAT_TYPE>
85 Results<FLOAT_TYPE> Run (const TargetFunction<FLOAT_TYPE>& function2Minimize, const Sequence<FLOAT_TYPE>& initialValues,
86 const Options<FLOAT_TYPE>& options = Options<FLOAT_TYPE>{});
87
88}
89
90/*
91 ********************************************************************************
92 ***************************** Implementation Details ***************************
93 ********************************************************************************
94 */
95#include "DownhillSimplexMinimization.inl"
96
97#endif /*_Stroika_Foundation_Math_Optimization_DownhillSimplexMinimization_h_*/
Results< FLOAT_TYPE > Run(const TargetFunction< FLOAT_TYPE > &function2Minimize, const Sequence< FLOAT_TYPE > &initialValues, const Options< FLOAT_TYPE > &options=Options< FLOAT_TYPE >{})
Downhill Simplex Minimization, AKA Nelder-Mead algorithm, to compute minimization.
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187