DART  6.7.3
GradientDescentSolver.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2019, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_OPTIMIZER_GRADIENTDESCENTSOLVER_HPP_
34 #define DART_OPTIMIZER_GRADIENTDESCENTSOLVER_HPP_
35 
36 #include <random>
37 
39 
40 namespace dart {
41 namespace optimizer {
42 
50 {
51 public:
52 
53  static const std::string Type;
54 
56  {
58  double mStepSize;
59 
66  std::size_t mMaxAttempts;
67 
70  std::size_t mPerturbationStep;
71 
82 
85 
90 
95  Eigen::VectorXd mEqConstraintWeights;
96 
101  Eigen::VectorXd mIneqConstraintWeights;
102 
104  double _stepMultiplier = 0.1,
105  std::size_t _maxAttempts = 1,
106  std::size_t _perturbationStep = 0,
107  double _maxPerturbationFactor = 1.0,
108  double _maxRandomizationStep = 1e10,
109  double _defaultConstraintWeight = 1.0,
110  Eigen::VectorXd _eqConstraintWeights = Eigen::VectorXd(),
111  Eigen::VectorXd _ineqConstraintWeights = Eigen::VectorXd() );
112  };
113 
115  {
116  Properties(
117  const Solver::Properties& _solverProperties = Solver::Properties(),
118  const UniqueProperties& _descentProperties = UniqueProperties() );
119  };
120 
122  explicit GradientDescentSolver(const Properties& _properties = Properties());
123 
125  explicit GradientDescentSolver(std::shared_ptr<Problem> _problem);
126 
128  virtual ~GradientDescentSolver();
129 
130  // Documentation inherited
131  bool solve() override;
132 
134  Eigen::VectorXd getLastConfiguration() const;
135 
136  // Documentation inherited
137  std::string getType() const override;
138 
139  // Documentation inherited
140  std::shared_ptr<Solver> clone() const override;
141 
143  void setProperties(const Properties& _properties);
144 
146  void setProperties(const UniqueProperties& _properties);
147 
150 
152  void copy(const GradientDescentSolver& _other);
153 
156 
158  void setStepSize(double _newMultiplier);
159 
161  double getStepSize() const;
162 
167  void setMaxAttempts(std::size_t _maxAttempts);
168 
170  std::size_t getMaxAttempts() const;
171 
174  void setPerturbationStep(std::size_t _step);
175 
177  std::size_t getPerturbationStep() const;
178 
180  void setMaxPerturbationFactor(double _factor);
181 
183  double getMaxPerturbationFactor() const;
184 
186  void setDefaultConstraintWeight(double _newDefault);
187 
189  double getDefaultConstraintWeight() const;
190 
192  Eigen::VectorXd& getEqConstraintWeights();
193 
195  const Eigen::VectorXd& getEqConstraintWeights() const;
196 
198  Eigen::VectorXd& getIneqConstraintWeights();
199 
201  const Eigen::VectorXd& getIneqConstraintWeights() const;
202 
204  void randomizeConfiguration(Eigen::VectorXd& _x);
205 
207  void clampToBoundary(Eigen::VectorXd& _x);
208 
210  std::size_t getLastNumIterations() const;
211 
212 protected:
213 
216 
218  std::size_t mLastNumIterations;
219 
221  std::random_device mRD;
222 
224  std::mt19937 mMT;
225 
227  std::uniform_real_distribution<double> mDistribution;
228 
230  Eigen::VectorXd mEqConstraintCostCache;
231 
233  Eigen::VectorXd mIneqConstraintCostCache;
234 
236  Eigen::VectorXd mLastConfig;
237 };
238 
239 } // namespace optimizer
240 } // namespace dart
241 
242 #endif // DART_OPTIMIZER_GRADIENTDESCENTSOLVER_HPP_
DefaultSolver is a Solver extension which is native to DART (rather than relying on third-party libra...
Definition: GradientDescentSolver.hpp:50
Eigen::VectorXd & getIneqConstraintWeights()
Set UniqueProperties::mIneqConstraintWeights.
Definition: GradientDescentSolver.cpp:418
virtual ~GradientDescentSolver()
Destructor.
Definition: GradientDescentSolver.cpp:100
void setStepSize(double _newMultiplier)
Set the multiplier for the step size.
Definition: GradientDescentSolver.cpp:346
std::size_t getPerturbationStep() const
Get UniqueProperties::mPerturbationStep.
Definition: GradientDescentSolver.cpp:376
double getStepSize() const
Get the multiplier for the step size.
Definition: GradientDescentSolver.cpp:352
UniqueProperties mGradientP
GradientDescentSolver properties.
Definition: GradientDescentSolver.hpp:215
std::random_device mRD
Randomization device.
Definition: GradientDescentSolver.hpp:221
double getMaxPerturbationFactor() const
Get UniqueProperties::mPerturbationFactor.
Definition: GradientDescentSolver.cpp:388
void setPerturbationStep(std::size_t _step)
Set the number of steps that will be taken before applying a randomized perturbation.
Definition: GradientDescentSolver.cpp:370
static const std::string Type
Definition: GradientDescentSolver.hpp:53
Eigen::VectorXd mEqConstraintCostCache
Cache to track the costs of equality constraints.
Definition: GradientDescentSolver.hpp:230
GradientDescentSolver(const Properties &_properties=Properties())
Default constructor.
Definition: GradientDescentSolver.cpp:79
std::size_t getLastNumIterations() const
Get the number of iterations used in the last attempt to solve the problem.
Definition: GradientDescentSolver.cpp:479
GradientDescentSolver & operator=(const GradientDescentSolver &_other)
Copy the Properties of another GradientDescentSolver.
Definition: GradientDescentSolver.cpp:338
Eigen::VectorXd getLastConfiguration() const
Get the last configuration that was used by the Solver.
Definition: GradientDescentSolver.cpp:285
Eigen::VectorXd mIneqConstraintCostCache
Cache to track the costs of inequality constraints.
Definition: GradientDescentSolver.hpp:233
std::uniform_real_distribution< double > mDistribution
Distribution.
Definition: GradientDescentSolver.hpp:227
void randomizeConfiguration(Eigen::VectorXd &_x)
Randomize the configuration based on this Solver's settings.
Definition: GradientDescentSolver.cpp:430
void setProperties(const Properties &_properties)
Set the Properties of this GradientDescentSolver.
Definition: GradientDescentSolver.cpp:304
void clampToBoundary(Eigen::VectorXd &_x)
Clamp the configuration to the limits of the Problem.
Definition: GradientDescentSolver.cpp:454
Eigen::VectorXd mLastConfig
The last config reached by this Solver.
Definition: GradientDescentSolver.hpp:236
void setMaxPerturbationFactor(double _factor)
Set UniqueProperties::mPerturbationFactor.
Definition: GradientDescentSolver.cpp:382
std::size_t mLastNumIterations
The last number of iterations performed by this Solver.
Definition: GradientDescentSolver.hpp:218
Properties getGradientDescentProperties() const
Get the Properties of this GradientDescentSolver.
Definition: GradientDescentSolver.cpp:323
void copy(const GradientDescentSolver &_other)
Copy the Properties of another GradientDescentSolver.
Definition: GradientDescentSolver.cpp:329
std::mt19937 mMT
Mersenne twister method.
Definition: GradientDescentSolver.hpp:224
void setDefaultConstraintWeight(double _newDefault)
Set UniqueProperties::mDefaultConstraintWeight.
Definition: GradientDescentSolver.cpp:394
Eigen::VectorXd & getEqConstraintWeights()
Set UniqueProperties::mEqConstraintWeights.
Definition: GradientDescentSolver.cpp:406
std::size_t getMaxAttempts() const
Get the maximum number of solving attempts.
Definition: GradientDescentSolver.cpp:364
void setMaxAttempts(std::size_t _maxAttempts)
Set the maximum number of solving attempts before quitting.
Definition: GradientDescentSolver.cpp:358
bool solve() override
Solve optimization problem.
Definition: GradientDescentSolver.cpp:106
std::string getType() const override
Get the type (implementation) of this Solver.
Definition: GradientDescentSolver.cpp:291
std::shared_ptr< Solver > clone() const override
Create an identical clone of this Solver.
Definition: GradientDescentSolver.cpp:297
double getDefaultConstraintWeight() const
Get UniqueProperties::mDefaultConstraintWeight.
Definition: GradientDescentSolver.cpp:400
Abstract class that provides a common interface for different Solvers.
Definition: Solver.hpp:53
Definition: BulletCollisionDetector.cpp:63
Definition: GradientDescentSolver.hpp:115
Properties(const Solver::Properties &_solverProperties=Solver::Properties(), const UniqueProperties &_descentProperties=UniqueProperties())
Definition: GradientDescentSolver.cpp:69
Definition: GradientDescentSolver.hpp:56
UniqueProperties(double _stepMultiplier=0.1, std::size_t _maxAttempts=1, std::size_t _perturbationStep=0, double _maxPerturbationFactor=1.0, double _maxRandomizationStep=1e10, double _defaultConstraintWeight=1.0, Eigen::VectorXd _eqConstraintWeights=Eigen::VectorXd(), Eigen::VectorXd _ineqConstraintWeights=Eigen::VectorXd())
Definition: GradientDescentSolver.cpp:47
std::size_t mMaxAttempts
Number of attempts to make before quitting.
Definition: GradientDescentSolver.hpp:66
double mStepSize
Value of the fixed step size.
Definition: GradientDescentSolver.hpp:58
Eigen::VectorXd mEqConstraintWeights
Vector of weights that should be applied to the equality constraints.
Definition: GradientDescentSolver.hpp:95
double mDefaultConstraintWeight
This is the weight that will be applied to any constraints that do not have a corresponding weight sp...
Definition: GradientDescentSolver.hpp:89
double mMaxRandomizationStep
The largest permittable change in value when randomizing a configuration.
Definition: GradientDescentSolver.hpp:84
std::size_t mPerturbationStep
The number of steps between random perturbations being applied to the configuration.
Definition: GradientDescentSolver.hpp:70
double mMaxPerturbationFactor
The random perturbation works as follows: A random point in the domain of the Problem is selected,...
Definition: GradientDescentSolver.hpp:81
Eigen::VectorXd mIneqConstraintWeights
Vector of weights that should be applied to the inequality constraints.
Definition: GradientDescentSolver.hpp:101
The Solver::Properties class contains Solver parameters that are common to all Solver types.
Definition: Solver.hpp:61