DART  6.7.3
Solver.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_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:
55 
60  struct Properties
61  {
63  std::shared_ptr<Problem> mProblem;
64 
68  double mTolerance;
69 
72  std::size_t mNumMaxIterations;
73 
76  std::size_t mIterationsPerPrint;
77 
79  std::ostream* mOutStream;
80 
83 
86  std::string mResultFile;
87 
88  Properties(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:
176 
178 
179 };
180 
181 } // namespace optimizer
182 } // namespace dart
183 
184 #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:167
std::shared_ptr< Problem > getProblem() const
Get nonlinear optimization problem.
Definition: Solver.cpp:113
virtual void setOutStream(std::ostream *_os)
Set the output stream that prints the Solver's progress.
Definition: Solver.cpp:149
virtual void setProblem(std::shared_ptr< Problem > _newProblem)
Set the nonlinear optimization problem.
Definition: Solver.cpp:107
std::size_t getIterationsPerPrint() const
Get the number of iterations that should pass between printing progress to the terminal.
Definition: Solver.cpp:161
const std::string & getResultFileName() const
Get the name of the file that results should be printed to.
Definition: Solver.cpp:185
std::size_t getNumMaxIterations() const
Get the maximum number of iterations that the Solver should use.
Definition: Solver.cpp:137
void copy(const Solver &_otherSolver)
Copy the generic Properties of another Solver.
Definition: Solver.cpp:91
Solver & operator=(const Solver &_otherSolver)
Copy the generic Properties of another Solver.
Definition: Solver.cpp:100
std::ostream * getOutStream() const
Get the output stream that prints the Solver's progress.
Definition: Solver.cpp:155
Properties mProperties
Definition: Solver.hpp:177
bool getPrintFinalResult() const
Returns true if the final result should be printed to the terminal.
Definition: Solver.cpp:173
virtual ~Solver()=default
Destructor.
double getTolerance() const
Get the maximum step size allowed for the Problem to be considered converged.
Definition: Solver.cpp:125
virtual void setIterationsPerPrint(std::size_t _newRatio)
Set the number of iterations that should pass between printing progress to the terminal.
Definition: Solver.cpp:143
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:131
virtual void setTolerance(double _newTolerance)
Set the maximum step size allowed for the Problem to be considered converged.
Definition: Solver.cpp:119
void setProperties(const Properties &_properties)
Set the generic Properties of this Solver.
Definition: Solver.cpp:74
virtual void setResultFileName(const std::string &_resultFile)
Set the name of the file that results should be printed to.
Definition: Solver.cpp:179
const Properties & getSolverProperties() const
Get the generic Properties of this Solver.
Definition: Solver.cpp:85
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:63
The Solver::Properties class contains Solver parameters that are common to all Solver types.
Definition: Solver.hpp:61
std::ostream * mOutStream
Stream for printing the Solver's progress. Default is std::cout.
Definition: Solver.hpp:79
std::string mResultFile
Publish the results of the optimization to a file.
Definition: Solver.hpp:86
std::size_t mIterationsPerPrint
How many iterations between printing the Solver's progress to the terminal.
Definition: Solver.hpp:76
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:82
std::size_t mNumMaxIterations
The maximum number of iterations that the solver should use.
Definition: Solver.hpp:72
double mTolerance
The relative tolerance on the optimization parameters.
Definition: Solver.hpp:68
std::shared_ptr< Problem > mProblem
Nonlinear optimization Problem to be solved.
Definition: Solver.hpp:63