openGPMP
Open Source Mathematics Package
function.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
34 #ifndef FUNCTION_HPP
35 #define FUNCTION_HPP
36 
37 #include <cmath>
38 #include <functional>
39 #include <stdexcept>
40 #include <vector>
41 
42 namespace gpmp {
43 
44 namespace optim {
45 
49 class Func {
50  public:
58  std::vector<double>
59  generate_random_point(const std::vector<double> &lower_bounds,
60  const std::vector<double> &upper_bounds) const;
61 
68  std::vector<double> generate_fibonacci_sequence(size_t length) const;
69 
77  std::vector<double> vector_addition(const std::vector<double> &a,
78  const std::vector<double> &b) const;
79 
87  std::vector<double> vector_subtraction(const std::vector<double> &a,
88  const std::vector<double> &b) const;
89 
97  std::vector<double>
98  vector_scalar_multiply(double scalar, const std::vector<double> &vec) const;
99 
108  double calculate_midpoint(double a, double b, double fraction) const;
109 
110  // Golden-section search methods for univariate functions
111 
122  double golden_section_search(const std::function<double(double)> &func,
123  double a,
124  double b,
125  double tol);
126 
127  // Interpolation methods for univariate functions
128 
139  double
140  linear_interpolation(double x, double x0, double x1, double y0, double y1);
141 
154  double cubic_interpolation(double x,
155  double x0,
156  double x1,
157  double y0,
158  double y1,
159  double y0_prime,
160  double y1_prime);
161 
162  // Golden-section search methods for multivariate functions
163 
174  std::vector<double> golden_section_search_multivariate(
175  const std::function<double(const std::vector<double> &)> &func,
176  const std::vector<double> &lower_bounds,
177  const std::vector<double> &upper_bounds,
178  double tol);
179 
189  double random_search(
190  const std::function<double(const std::vector<double> &)> &func,
191  const std::vector<double> &lower_bounds,
192  const std::vector<double> &upper_bounds,
193  size_t max_iterations);
194 
202  std::vector<double> fit_linear(const std::vector<double> &x,
203  const std::vector<double> &y);
204 
214  double fibonacci_search(
215  const std::function<double(const std::vector<double> &)> &func,
216  const std::vector<double> &lower_bounds,
217  const std::vector<double> &upper_bounds,
218  size_t max_iterations);
219 
229  double ternary_search(
230  const std::function<double(const std::vector<double> &)> &func,
231  const std::vector<double> &lower_bounds,
232  const std::vector<double> &upper_bounds,
233  size_t max_iterations) const;
234 
244  std::vector<double> bisection_method(
245  const std::function<double(const std::vector<double> &)> &func,
246  double lower_bound,
247  double upper_bound,
248  size_t max_iterations);
249 
259  std::vector<double> newton_method(
260  const std::function<double(const std::vector<double> &)> &func,
261  const std::function<double(const std::vector<double> &)> &derivative,
262  double initial_guess,
263  size_t max_iterations);
264 
275  std::vector<double>
276  regula_falsi(const std::function<double(const std::vector<double> &)> &func,
277  double lower_bound,
278  double upper_bound,
279  size_t max_iterations);
280 
288  std::vector<double> cubic_fit(const std::vector<double> &x,
289  const std::vector<double> &y);
290 
291  // Nelder–Mead method
292 
303  std::vector<double>
304  nelder_mead(const std::function<double(const std::vector<double> &)> &func,
305  std::vector<double> initial_point,
306  double tolerance,
307  size_t max_iterations);
308 
316  std::vector<double>
317  calculate_centroid(const std::vector<std::vector<double>> &simplex,
318  size_t exclude_index);
319 
328  std::vector<double> reflect(const std::vector<double> &point,
329  const std::vector<double> &centroid,
330  double reflection_coefficient);
331 
338  double calculate_range(const std::vector<double> &values);
339 
347  bool is_valid_interval(double a, double b);
348 
349  private:
350  // Helper methods for golden section search
351 
364  double
365  golden_section_search_minimize(const std::function<double(double)> &func,
366  double a,
367  double b,
368  double tol,
369  double x1,
370  double x2);
371 
372  // Helper methods for multivariate golden section search
373 
387  const std::function<double(const std::vector<double> &)> &func,
388  const std::vector<double> &lower_bounds,
389  const std::vector<double> &upper_bounds,
390  double tol,
391  const std::vector<double> &x1,
392  const std::vector<double> &x2);
393 };
394 
395 } // namespace optim
396 
397 } // namespace gpmp
398 
399 #endif // FUNCTION_HPP
A class containing various utility functions and optimization methods.
Definition: function.hpp:49
double cubic_interpolation(double x, double x0, double x1, double y0, double y1, double y0_prime, double y1_prime)
Interpolates a univariate function using cubic interpolation.
Definition: function.cpp:147
double fibonacci_search(const std::function< double(const std::vector< double > &)> &func, const std::vector< double > &lower_bounds, const std::vector< double > &upper_bounds, size_t max_iterations)
Performs Fibonacci search for function optimization.
Definition: function.cpp:289
std::vector< double > regula_falsi(const std::function< double(const std::vector< double > &)> &func, double lower_bound, double upper_bound, size_t max_iterations)
Performs Regula Falsi (False Position) method for function optimization.
Definition: function.cpp:414
std::vector< double > generate_random_point(const std::vector< double > &lower_bounds, const std::vector< double > &upper_bounds) const
Generates a random point within specified bounds.
Definition: function.cpp:37
double random_search(const std::function< double(const std::vector< double > &)> &func, const std::vector< double > &lower_bounds, const std::vector< double > &upper_bounds, size_t max_iterations)
Performs random search for function optimization.
Definition: function.cpp:233
std::vector< double > bisection_method(const std::function< double(const std::vector< double > &)> &func, double lower_bound, double upper_bound, size_t max_iterations)
Performs the bisection method for function optimization.
Definition: function.cpp:362
std::vector< double > vector_subtraction(const std::vector< double > &a, const std::vector< double > &b) const
Performs vector subtraction.
Definition: function.cpp:90
std::vector< double > fit_linear(const std::vector< double > &x, const std::vector< double > &y)
Fits a linear function to given data points.
Definition: function.cpp:264
std::vector< double > reflect(const std::vector< double > &point, const std::vector< double > &centroid, double reflection_coefficient)
Reflects a point with respect to a centroid.
Definition: function.cpp:628
double ternary_search(const std::function< double(const std::vector< double > &)> &func, const std::vector< double > &lower_bounds, const std::vector< double > &upper_bounds, size_t max_iterations) const
Performs ternary search for function optimization.
Definition: function.cpp:331
std::vector< double > cubic_fit(const std::vector< double > &x, const std::vector< double > &y)
Fits a cubic function to given data points.
Definition: function.cpp:449
std::vector< double > golden_section_search_minimize_multivariate(const std::function< double(const std::vector< double > &)> &func, const std::vector< double > &lower_bounds, const std::vector< double > &upper_bounds, double tol, const std::vector< double > &x1, const std::vector< double > &x2)
Finds the minimum of a multivariate function using Golden-section search (Internal helper function)
Definition: function.cpp:650
std::vector< double > newton_method(const std::function< double(const std::vector< double > &)> &func, const std::function< double(const std::vector< double > &)> &derivative, double initial_guess, size_t max_iterations)
Performs Newton's method for function optimization.
Definition: function.cpp:393
double calculate_midpoint(double a, double b, double fraction) const
Calculates the midpoint between two values.
Definition: function.cpp:116
std::vector< double > nelder_mead(const std::function< double(const std::vector< double > &)> &func, std::vector< double > initial_point, double tolerance, size_t max_iterations)
Finds the minimum of a multivariate function using the Nelder–Mead method.
Definition: function.cpp:519
double calculate_range(const std::vector< double > &values)
Calculates the range of values.
Definition: function.cpp:642
bool is_valid_interval(double a, double b)
Checks if a given interval is valid (lower bound < upper bound)
Definition: function.cpp:690
double golden_section_search(const std::function< double(double)> &func, double a, double b, double tol)
Finds the minimum of a univariate function using Golden-section search.
Definition: function.cpp:122
double linear_interpolation(double x, double x0, double x1, double y0, double y1)
Interpolates a univariate function using linear interpolation.
Definition: function.cpp:139
std::vector< double > vector_scalar_multiply(double scalar, const std::vector< double > &vec) const
Performs vector scalar multiplication.
Definition: function.cpp:105
std::vector< double > calculate_centroid(const std::vector< std::vector< double >> &simplex, size_t exclude_index)
Calculates the centroid of a simplex excluding a specific point.
Definition: function.cpp:606
double golden_section_search_minimize(const std::function< double(double)> &func, double a, double b, double tol, double x1, double x2)
Finds the minimum of a univariate function using Golden-section search (Internal helper function)
Definition: function.cpp:167
std::vector< double > vector_addition(const std::vector< double > &a, const std::vector< double > &b) const
Performs vector addition.
Definition: function.cpp:74
std::vector< double > generate_fibonacci_sequence(size_t length) const
Generates a Fibonacci sequence up to a specified length.
Definition: function.cpp:57
std::vector< double > golden_section_search_multivariate(const std::function< double(const std::vector< double > &)> &func, const std::vector< double > &lower_bounds, const std::vector< double > &upper_bounds, double tol)
Finds the minimum of a multivariate function using Golden-section search.
Definition: function.cpp:200
The source C++ openGPMP namespace.