Repository navigation
Expand file tree
/
Copy pathGradient.h
More file actions
114 lines (88 loc) · 2.6 KB
/
Copy pathGradient.h
File metadata and controls
114 lines (88 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef GRADIENT_H
#define GRADIENT_H
#include <vector>
#include <functional>
class Gradient{
public:
/**
* @brief Construct a new Gradient object
*/
Gradient();
/**
* @brief Destroy the Gradient object
*/
~Gradient();
/**
* @brief Function to add a pointer to the object function.
* @param objFn
* @return void
* @note The object function is the function that is to be optimized.
*/
void setObjectFn(std::function<double(std::vector<double>*)> objFn);
/**
* @brief Function to set the start point.
* @param startPoint
* @return void
* @note The start point is the point from which the gradient descent algorithm starts.
*/
void setStartPoint(const std::vector<double> startPoint);
/**
* @brief Function to set the step size.
* @param stepSize
* @return void
* @note The step size is the size of the step taken in the direction of the gradient.
*/
void setStepSize(double stepSize);
/**
* @brief Set the Max Iterations object
* @param maxIterations
* @return void
* @note The max iterations is the maximum number of iterations the gradient descent algorithm will run for.
*/
void setMaxIterations(int maxIterations);
/**
* @brief Function to set the gradient threshold.
* @param gradientThreshold
* @return void
* @note The gradient threshold is the minimum value of the gradient at which the gradient descent algorithm will stop.
*/
void setGradientThreshold(double gradientThreshold);
/**
* @brief Function to perform the optimization.
* @param funcLoc
* @param funcVal
* @return bool
* @note The function returns true if the optimization was successful and false otherwise.
*/
bool Optimize(std::vector<double> *funcLoc, double *funcVal);
private:
/**
* @brief Function to compute the gradient in the specified dimension.
* @param dim
* @return double
*
*/
double computeGradient(int dim);
/**
* @brief Function to compute the gradient vector.
* @return std::vector<double>
* @note The gradient vector is the vector of gradients in each dimension.
*/
std::vector<double> computeGradientVector();
/**
* @brief Function to compute the gradient magnitude.
* @param gradientVector
* @return double
*/
double computeGradientMagnitude(std::vector<double> gradientVector);
private:
int m_nDimensions;
double m_stepSize;
int m_maxIterations;
double m_gradientStepSize;
double m_gradientThreshold;
std::vector<double> m_startPoint;
std::vector<double> m_currentPoint;
std::function<double(std::vector<double>*)> m_objectFunction;
};
#endif