DART  6.10.1
GradientDescentSolver.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2021, 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  static const std::string Type;
53 
55  {
57  double mStepSize;
58 
65  std::size_t mMaxAttempts;
66 
69  std::size_t mPerturbationStep;
70 
81 
84 
89 
94  Eigen::VectorXd mEqConstraintWeights;
95 
100  Eigen::VectorXd mIneqConstraintWeights;
101 
103  double _stepMultiplier = 0.1,
104  std::size_t _maxAttempts = 1,
105  std::size_t _perturbationStep = 0,
106  double _maxPerturbationFactor = 1.0,
107  double _maxRandomizationStep = 1e10,
108  double _defaultConstraintWeight = 1.0,
109  Eigen::VectorXd _eqConstraintWeights = Eigen::VectorXd(),
110  Eigen::VectorXd _ineqConstraintWeights = Eigen::VectorXd());
111  };
112 
114  {
115  Properties(
116  const Solver::Properties& _solverProperties = Solver::Properties(),
117  const UniqueProperties& _descentProperties = UniqueProperties());
118  };
119 
121  explicit GradientDescentSolver(const Properties& _properties = Properties());
122 
124  explicit GradientDescentSolver(std::shared_ptr<Problem> _problem);
125 
127  virtual ~GradientDescentSolver();
128 
129  // Documentation inherited
130  bool solve() override;
131 
133  Eigen::VectorXd getLastConfiguration() const;
134 
135  // Documentation inherited
136  std::string getType() const override;
137 
138  // Documentation inherited
139  std::shared_ptr<Solver> clone() const override;
140 
142  void setProperties(const Properties& _properties);
143 
145  void setProperties(const UniqueProperties& _properties);
146 
149 
151  void copy(const GradientDescentSolver& _other);
152 
155 
157  void setStepSize(double _newMultiplier);
158 
160  double getStepSize() const;
161 
166  void setMaxAttempts(std::size_t _maxAttempts);
167 
169  std::size_t getMaxAttempts() const;
170 
173  void setPerturbationStep(std::size_t _step);
174 
176  std::size_t getPerturbationStep() const;
177 
179  void setMaxPerturbationFactor(double _factor);
180 
182  double getMaxPerturbationFactor() const;
183 
185  void setDefaultConstraintWeight(double _newDefault);
186 
188  double getDefaultConstraintWeight() const;
189 
191  Eigen::VectorXd& getEqConstraintWeights();
192 
194  const Eigen::VectorXd& getEqConstraintWeights() const;
195 
197  Eigen::VectorXd& getIneqConstraintWeights();
198 
200  const Eigen::VectorXd& getIneqConstraintWeights() const;
201 
203  void randomizeConfiguration(Eigen::VectorXd& _x);
204 
206  void clampToBoundary(Eigen::VectorXd& _x);
207 
209  std::size_t getLastNumIterations() const;
210 
211 protected:
214 
216  std::size_t mLastNumIterations;
217 
219  std::random_device mRD;
220 
222  std::mt19937 mMT;
223 
225  std::uniform_real_distribution<double> mDistribution;
226 
228  Eigen::VectorXd mEqConstraintCostCache;
229 
231  Eigen::VectorXd mIneqConstraintCostCache;
232 
234  Eigen::VectorXd mLastConfig;
235 };
236 
237 } // namespace optimizer
238 } // namespace dart
239 
240 #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:422
virtual ~GradientDescentSolver()
Destructor.
Definition: GradientDescentSolver.cpp:101
void setStepSize(double _newMultiplier)
Set the multiplier for the step size.
Definition: GradientDescentSolver.cpp:350
std::size_t getPerturbationStep() const
Get UniqueProperties::mPerturbationStep.
Definition: GradientDescentSolver.cpp:380
double getStepSize() const
Get the multiplier for the step size.
Definition: GradientDescentSolver.cpp:356
UniqueProperties mGradientP
GradientDescentSolver properties.
Definition: GradientDescentSolver.hpp:213
std::random_device mRD
Randomization device.
Definition: GradientDescentSolver.hpp:219
double getMaxPerturbationFactor() const
Get UniqueProperties::mPerturbationFactor.
Definition: GradientDescentSolver.cpp:392
void setPerturbationStep(std::size_t _step)
Set the number of steps that will be taken before applying a randomized perturbation.
Definition: GradientDescentSolver.cpp:374
static const std::string Type
Definition: GradientDescentSolver.hpp:52
Eigen::VectorXd mEqConstraintCostCache
Cache to track the costs of equality constraints.
Definition: GradientDescentSolver.hpp:228
GradientDescentSolver(const Properties &_properties=Properties())
Default constructor.
Definition: GradientDescentSolver.cpp:78
std::size_t getLastNumIterations() const
Get the number of iterations used in the last attempt to solve the problem.
Definition: GradientDescentSolver.cpp:485
GradientDescentSolver & operator=(const GradientDescentSolver &_other)
Copy the Properties of another GradientDescentSolver.
Definition: GradientDescentSolver.cpp:342
Eigen::VectorXd getLastConfiguration() const
Get the last configuration that was used by the Solver.
Definition: GradientDescentSolver.cpp:289
Eigen::VectorXd mIneqConstraintCostCache
Cache to track the costs of inequality constraints.
Definition: GradientDescentSolver.hpp:231
std::uniform_real_distribution< double > mDistribution
Distribution.
Definition: GradientDescentSolver.hpp:225
void randomizeConfiguration(Eigen::VectorXd &_x)
Randomize the configuration based on this Solver's settings.
Definition: GradientDescentSolver.cpp:434
void setProperties(const Properties &_properties)
Set the Properties of this GradientDescentSolver.
Definition: GradientDescentSolver.cpp:308
void clampToBoundary(Eigen::VectorXd &_x)
Clamp the configuration to the limits of the Problem.
Definition: GradientDescentSolver.cpp:458
Eigen::VectorXd mLastConfig
The last config reached by this Solver.
Definition: GradientDescentSolver.hpp:234
void setMaxPerturbationFactor(double _factor)
Set UniqueProperties::mPerturbationFactor.
Definition: GradientDescentSolver.cpp:386
std::size_t mLastNumIterations
The last number of iterations performed by this Solver.
Definition: GradientDescentSolver.hpp:216
Properties getGradientDescentProperties() const
Get the Properties of this GradientDescentSolver.
Definition: GradientDescentSolver.cpp:327
void copy(const GradientDescentSolver &_other)
Copy the Properties of another GradientDescentSolver.
Definition: GradientDescentSolver.cpp:333
std::mt19937 mMT
Mersenne twister method.
Definition: GradientDescentSolver.hpp:222
void setDefaultConstraintWeight(double _newDefault)
Set UniqueProperties::mDefaultConstraintWeight.
Definition: GradientDescentSolver.cpp:398
Eigen::VectorXd & getEqConstraintWeights()
Set UniqueProperties::mEqConstraintWeights.
Definition: GradientDescentSolver.cpp:410
std::size_t getMaxAttempts() const
Get the maximum number of solving attempts.
Definition: GradientDescentSolver.cpp:368
void setMaxAttempts(std::size_t _maxAttempts)
Set the maximum number of solving attempts before quitting.
Definition: GradientDescentSolver.cpp:362
bool solve() override
Solve optimization problem.
Definition: GradientDescentSolver.cpp:107
std::string getType() const override
Get the type (implementation) of this Solver.
Definition: GradientDescentSolver.cpp:295
std::shared_ptr< Solver > clone() const override
Create an identical clone of this Solver.
Definition: GradientDescentSolver.cpp:301
double getDefaultConstraintWeight() const
Get UniqueProperties::mDefaultConstraintWeight.
Definition: GradientDescentSolver.cpp:404
Abstract class that provides a common interface for different Solvers.
Definition: Solver.hpp:53
Definition: BulletCollisionDetector.cpp:65
Definition: GradientDescentSolver.hpp:114
Properties(const Solver::Properties &_solverProperties=Solver::Properties(), const UniqueProperties &_descentProperties=UniqueProperties())
Definition: GradientDescentSolver.cpp:69
Definition: GradientDescentSolver.hpp:55
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:65
double mStepSize
Value of the fixed step size.
Definition: GradientDescentSolver.hpp:57
Eigen::VectorXd mEqConstraintWeights
Vector of weights that should be applied to the equality constraints.
Definition: GradientDescentSolver.hpp:94
double mDefaultConstraintWeight
This is the weight that will be applied to any constraints that do not have a corresponding weight sp...
Definition: GradientDescentSolver.hpp:88
double mMaxRandomizationStep
The largest permittable change in value when randomizing a configuration.
Definition: GradientDescentSolver.hpp:83
std::size_t mPerturbationStep
The number of steps between random perturbations being applied to the configuration.
Definition: GradientDescentSolver.hpp:69
double mMaxPerturbationFactor
The random perturbation works as follows: A random point in the domain of the Problem is selected,...
Definition: GradientDescentSolver.hpp:80
Eigen::VectorXd mIneqConstraintWeights
Vector of weights that should be applied to the inequality constraints.
Definition: GradientDescentSolver.hpp:100
The Solver::Properties class contains Solver parameters that are common to all Solver types.
Definition: Solver.hpp:60