G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsGradientDescent.h
Go to the documentation of this file.
1
14#pragma once
15
17#include <gsIO/gsOptionList.h>
20#define Eigen gsEigen
21#include "gdcpp.h"
22#undef Eigen
23//#include "lsqcpp.h"
24
25namespace gismo
26{
27
28template<typename T>
29struct gsGradientDescentObjective
30{
31 typedef gsEigen::Matrix<T, gsEigen::Dynamic, 1> Vector;
32 // typedef gsEigen::Matrix<T, gsEigen::Dynamic, gsEigen::Dynamic> Matrix;
33
34 gsGradientDescentObjective(gsOptProblem<T>* objective)
35 :
36 obj(objective)
37 { }
38
39 gsGradientDescentObjective()
40 :
41 obj(nullptr)
42 { }
43
44
45 T operator()(const Vector & vx, Vector & vfgrad) const
46 {
47 vfgrad.resize(obj->numDesignVars());
48 gsAsConstVector<T> xvec(vx.data(),vx.size());
49 gsAsVector<T> gvec(vfgrad.data(),vfgrad.size());
50 obj->gradObj_into(xvec,gvec);
51 return obj->evalObj(xvec);
52 }
53
54 gsOptProblem<T> * obj;
55};
56
67template<typename T = real_t,
68 typename StepSize=gdc::BarzilaiBorwein<T>,
69 typename Callback=gdc::NoCallback<T>,
70 typename FiniteDifferences=gdc::CentralDifferences<T> >
72{
73 using Base = gsOptimizer<T>;
74
75 typedef typename gdc::GradientDescent<T, gsGradientDescentObjective<T>, StepSize, Callback, FiniteDifferences>::Result Result;
76
77public:
79 :
80 Base(problem),
81 m_solver()
82 {
83 this->defaultOptions();
84 gsGradientDescentObjective<T> obj(m_op);
85 m_solver.setObjective(obj);
86 }
87
88
89public:
90 // const gsMatrix<T> & lambda() const { return m_lambda; }
91
92 void minimize(const gsMatrix<T> &initialGuess)
93 {
94 m_result = m_solver.minimize(initialGuess);
95 m_numIterations = m_result.iterations;
96 m_finalObjective = m_result.fval;
97 m_curDesign = m_result.xval;
98 }
99
100 Result result() { return m_result; };
101
102protected:
103 void defaultOptions()
104 {
105 Base::defaultOptions();
106 m_options.addReal("MinGradientLength","Minimal gradient length",1e-9);
107 m_options.addReal("MinStepLength","Minimal step length",1e-9);
108 }
109
110 void getOptions()
111 {
112 Base::getOptions();
113 m_minGradientLength = m_options.getReal("MinGradientLength");
114 m_minStepLength = m_options.getReal("MinStepLength");
115
116 m_solver.setMaxIterations(m_maxIterations);
117 m_solver.setMinGradientLength(m_minGradientLength);
118 m_solver.setMinStepLength(m_minStepLength);
119 m_solver.setVerbosity(m_verbose);
120 }
121
122public:
123
124 using Base::solve;
125 void solve(const gsMatrix<T> &initialGuess)
126 {
127 this->getOptions();
128 this->minimize(initialGuess);
129 }
130
131protected:
132 using Base::m_op;
133 using Base::m_numIterations;
134 using Base::m_finalObjective;
135 using Base::m_curDesign;
136 using Base::m_options;
137 using Base::m_verbose;
138 using Base::m_maxIterations;
139
140 using Base::defaultOptions;
141 using Base::getOptions;
142
143 Result m_result;
144
145protected:
146 T m_minGradientLength;
147 T m_minStepLength;
148
149 gdc::GradientDescent<T, gsGradientDescentObjective<T>, StepSize, Callback, FiniteDifferences> m_solver;
150
151};
152
153
154// using gsGradientDescent = gdc::GradientDescent<T, Objective, StepSize, Callback, FiniteDifferences>;
155
156} //namespace gismo
This class describes the gradient descent method.
Definition gsGradientDescent.h:72
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
gsMatrix< T > m_curDesign
Current design variables (and starting point )
Definition gsOptimizer.h:105
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Class defining an optimization problem.
Definition gsOptProblem.h:25
Class defining an optimizer.
Definition gsOptimizer.h:28
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
gsMatrix< T > m_curDesign
Current design variables (and starting point )
Definition gsOptimizer.h:105
Real getReal(const std::string &label) const
Reads value for option label from options.
Definition gsOptionList.cpp:44
void addReal(const std::string &label, const std::string &desc, const Real &value)
Adds a option named label, with description desc and value value.
Definition gsOptionList.cpp:211
This is the main header file that collects wrappers of Eigen for linear algebra.
Provides declaration of an optimization problem.
This file is part of the G+Smo library.
Provides a list of labeled parameters/options that can be set and accessed easily.
The G+Smo namespace, containing all definitions for the library.