G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsGradientDescent.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <gsCore/gsLinearAlgebra.h>
17 #include <gsIO/gsOptionList.h>
20 #define Eigen gsEigen
21 #include "gdcpp.h"
22 #undef Eigen
23 //#include "lsqcpp.h"
24 
25 namespace gismo
26 {
27 
28 template<typename T>
29 struct 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 
67 template<typename T = real_t,
68  typename StepSize=gdc::BarzilaiBorwein<T>,
69  typename Callback=gdc::NoCallback<T>,
70  typename FiniteDifferences=gdc::CentralDifferences<T> >
71 class gsGradientDescent : public gsOptimizer<T>
72 {
73  using Base = gsOptimizer<T>;
74 
75  typedef typename gdc::GradientDescent<T, gsGradientDescentObjective<T>, StepSize, Callback, FiniteDifferences>::Result Result;
76 
77 public:
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 
89 public:
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 
102 protected:
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 
122 public:
123 
124  void solve(const gsMatrix<T> &initialGuess)
125  {
126  this->getOptions();
127  this->minimize(initialGuess);
128  }
129 
130 protected:
131  using Base::m_op;
132  using Base::m_numIterations;
133  using Base::m_finalObjective;
134  using Base::m_curDesign;
135  using Base::m_options;
136  using Base::m_verbose;
137  using Base::m_maxIterations;
138 
139  using Base::defaultOptions;
140  using Base::getOptions;
141 
142  Result m_result;
143 
144 protected:
145  T m_minGradientLength;
146  T m_minStepLength;
147 
148  gdc::GradientDescent<T, gsGradientDescentObjective<T>, StepSize, Callback, FiniteDifferences> m_solver;
149 
150 };
151 
152 
153 // using gsGradientDescent = gdc::GradientDescent<T, Objective, StepSize, Callback, FiniteDifferences>;
154 
155 } //namespace gismo
Class defining an optimization problem.
Definition: gsOptProblem.h:24
Class defining an optimizer.
Definition: gsOptimizer.h:27
gsOptionList m_options
Options.
Definition: gsOptimizer.h:108
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.
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.
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
This class describes the gradient descent method.
Definition: gsGradientDescent.h:71