DART  6.10.1
Solver.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_SOLVER_HPP_
34 #define DART_OPTIMIZER_SOLVER_HPP_
35 
36 #include <iostream>
37 #include <memory>
38 
39 #include <Eigen/Dense>
40 
41 namespace dart {
42 namespace optimizer {
43 
44 class Problem;
45 
52 class Solver
53 {
54 public:
59  struct Properties
60  {
62  std::shared_ptr<Problem> mProblem;
63 
67  double mTolerance;
68 
71  std::size_t mNumMaxIterations;
72 
75  std::size_t mIterationsPerPrint;
76 
78  std::ostream* mOutStream;
79 
82 
85  std::string mResultFile;
86 
87  Properties(
88  std::shared_ptr<Problem> _problem = nullptr,
89  double _tolerance = 1e-9,
90  std::size_t _numMaxIterations = 500,
91  std::size_t _iterationsPerPrint = 0,
92  std::ostream* _ostream = &std::cout,
93  bool _printFinalResult = false,
94  const std::string& _resultFile = "");
95  };
96 
98  explicit Solver(const Properties& _properties = Properties());
99 
101  explicit Solver(std::shared_ptr<Problem> _problem);
102 
104  virtual ~Solver() = default;
105 
107  virtual bool solve() = 0;
108 
110  virtual std::string getType() const = 0;
111 
113  virtual std::shared_ptr<Solver> clone() const = 0;
114 
116  void setProperties(const Properties& _properties);
117 
119  const Properties& getSolverProperties() const;
120 
122  void copy(const Solver& _otherSolver);
123 
125  Solver& operator=(const Solver& _otherSolver);
126 
128  virtual void setProblem(std::shared_ptr<Problem> _newProblem);
129 
131  std::shared_ptr<Problem> getProblem() const;
132 
135  virtual void setTolerance(double _newTolerance);
136 
139  double getTolerance() const;
140 
142  virtual void setNumMaxIterations(std::size_t _newMax);
143 
145  std::size_t getNumMaxIterations() const;
146 
149  virtual void setIterationsPerPrint(std::size_t _newRatio);
150 
153  std::size_t getIterationsPerPrint() const;
154 
156  virtual void setOutStream(std::ostream* _os);
157 
159  std::ostream* getOutStream() const;
160 
162  virtual void setPrintFinalResult(bool _print);
163 
165  bool getPrintFinalResult() const;
166 
169  virtual void setResultFileName(const std::string& _resultFile);
170 
173  const std::string& getResultFileName() const;
174 
175 protected:
177 };
178 
179 } // namespace optimizer
180 } // namespace dart
181 
182 #endif // DART_OPTIMIZER_SOLVER_HPP_
Abstract class that provides a common interface for different Solvers.
Definition: Solver.hpp:53
virtual void setPrintFinalResult(bool _print)
Set to true if the final result should be printed to the terminal.
Definition: Solver.cpp:165
std::shared_ptr< Problem > getProblem() const
Get nonlinear optimization problem.
Definition: Solver.cpp:111
virtual void setOutStream(std::ostream *_os)
Set the output stream that prints the Solver's progress.
Definition: Solver.cpp:147
virtual void setProblem(std::shared_ptr< Problem > _newProblem)
Set the nonlinear optimization problem.
Definition: Solver.cpp:105
std::size_t getIterationsPerPrint() const
Get the number of iterations that should pass between printing progress to the terminal.
Definition: Solver.cpp:159
const std::string & getResultFileName() const
Get the name of the file that results should be printed to.
Definition: Solver.cpp:183
std::size_t getNumMaxIterations() const
Get the maximum number of iterations that the Solver should use.
Definition: Solver.cpp:135
void copy(const Solver &_otherSolver)
Copy the generic Properties of another Solver.
Definition: Solver.cpp:89
Solver & operator=(const Solver &_otherSolver)
Copy the generic Properties of another Solver.
Definition: Solver.cpp:98
std::ostream * getOutStream() const
Get the output stream that prints the Solver's progress.
Definition: Solver.cpp:153
Properties mProperties
Definition: Solver.hpp:176
bool getPrintFinalResult() const
Returns true if the final result should be printed to the terminal.
Definition: Solver.cpp:171
virtual ~Solver()=default
Destructor.
double getTolerance() const
Get the maximum step size allowed for the Problem to be considered converged.
Definition: Solver.cpp:123
virtual void setIterationsPerPrint(std::size_t _newRatio)
Set the number of iterations that should pass between printing progress to the terminal.
Definition: Solver.cpp:141
virtual bool solve()=0
Solve optimization problem.
virtual void setNumMaxIterations(std::size_t _newMax)
Set the maximum number of iterations that the Solver should use.
Definition: Solver.cpp:129
virtual void setTolerance(double _newTolerance)
Set the maximum step size allowed for the Problem to be considered converged.
Definition: Solver.cpp:117
void setProperties(const Properties &_properties)
Set the generic Properties of this Solver.
Definition: Solver.cpp:72
virtual void setResultFileName(const std::string &_resultFile)
Set the name of the file that results should be printed to.
Definition: Solver.cpp:177
const Properties & getSolverProperties() const
Get the generic Properties of this Solver.
Definition: Solver.cpp:83
Solver(const Properties &_properties=Properties())
Default Constructor.
Definition: Solver.cpp:60
virtual std::string getType() const =0
Get the type (implementation) of this Solver.
virtual std::shared_ptr< Solver > clone() const =0
Create an identical clone of this Solver.
Definition: BulletCollisionDetector.cpp:65
The Solver::Properties class contains Solver parameters that are common to all Solver types.
Definition: Solver.hpp:60
std::ostream * mOutStream
Stream for printing the Solver's progress. Default is std::cout.
Definition: Solver.hpp:78
std::string mResultFile
Publish the results of the optimization to a file.
Definition: Solver.hpp:85
std::size_t mIterationsPerPrint
How many iterations between printing the Solver's progress to the terminal.
Definition: Solver.hpp:75
Properties(std::shared_ptr< Problem > _problem=nullptr, double _tolerance=1e-9, std::size_t _numMaxIterations=500, std::size_t _iterationsPerPrint=0, std::ostream *_ostream=&std::cout, bool _printFinalResult=false, const std::string &_resultFile="")
Definition: Solver.cpp:40
bool mPrintFinalResult
Set to true if the final result should be printed to the terminal.
Definition: Solver.hpp:81
std::size_t mNumMaxIterations
The maximum number of iterations that the solver should use.
Definition: Solver.hpp:71
double mTolerance
The relative tolerance on the optimization parameters.
Definition: Solver.hpp:67
std::shared_ptr< Problem > mProblem
Nonlinear optimization Problem to be solved.
Definition: Solver.hpp:62