Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
DownhillSimplexMinimization.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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 /**
38 * 0: no limit
39 */
40 unsigned int fMaxIterations{0};
41
42 /**
43 */
44 FLOAT_TYPE fNoImprovementThreshold{static_cast<FLOAT_TYPE> (10e-6)};
45
46 nonvirtual String ToString () const;
47 };
48
49 /**
50 * Results of minimization
51 */
52 template <typename FLOAT_TYPE>
53 struct Results {
54 Sequence<FLOAT_TYPE> fOptimizedParameters;
55 FLOAT_TYPE fScore{};
56 unsigned int fIterationCount{};
57
58 nonvirtual String ToString () const;
59 };
60
61 /**
62 * \brief Downhill Simplex Minimization, AKA Nelder-Mead algorithm, to compute minimization
63 *
64 * Take an argument function with argument 'initialValues' and try many values to find where the targetFunction is least (ie minimize it).
65 *
66 * If the function is naturally periodic - you can add checks in the minimization function for the desired target range and produce LARGE answers there
67 * so they wont appear as minima of the 'targetFunction'.
68 *
69 * Reference: https://en.wikipedia.org/wiki/Nelder%E2%80%93Mead_method
70 * Reference: https://github.com/fchollet/nelder-mead/blob/master/nelder_mead.py
71 * Reference: http://www.aip.de/groups/soe/local/numres/bookcpdf/c10-4.pdf
72 * Reference: https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/20398/versions/1/previews/fminsearch2.m/index.html
73 *
74 * \note The size of the Sequence<> in initialValues will be the same as the size of the Sequence passed to 'function2Minimize', which
75 * in turn will the the size of the Sequence<> in the Results.
76 *
77 * \par Example Usage
78 * \code
79 * TargetFunction<double> f = [](const Sequence<double>& x) {
80 * double d = x[0];
81 * if (d < 0 or d >= numbers::pi) { // avoid falling off ends of ranges - periodic function
82 * return 100.0;
83 * }
84 * return -std::cos (d);
85 * };
86 * Results<double> result = Run (f, {.1});
87 * EXPECT_TRUE (Math::NearlyEquals (result.fOptimizedParameters[0], 0.0, 1e-10));
88 * \endcode
89 */
90 template <typename FLOAT_TYPE>
91 Results<FLOAT_TYPE> Run (const TargetFunction<FLOAT_TYPE>& function2Minimize, const Sequence<FLOAT_TYPE>& initialValues,
92 const Options<FLOAT_TYPE>& options = Options<FLOAT_TYPE>{});
93
94}
95
96/*
97 ********************************************************************************
98 ***************************** Implementation Details ***************************
99 ********************************************************************************
100 */
101#include "DownhillSimplexMinimization.inl"
102
103#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.