DART  6.6.2
Solver.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2018, 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 
66  double mTolerance;
67 
70  std::size_t mNumMaxIterations;
71 
74  std::size_t mIterationsPerPrint;
75 
77  std::ostream* mOutStream;
78 
81 
84  std::string mResultFile;
85 
86  Properties(std::shared_ptr<Problem> _problem = nullptr,
87  double _tolerance = 1e-9,
88  std::size_t _numMaxIterations = 500,
89  std::size_t _iterationsPerPrint = 0,
90  std::ostream* _ostream = &std::cout,
91  bool _printFinalResult = false,
92  const std::string& _resultFile = "");
93  };
94 
96  explicit Solver(const Properties& _properties = Properties());
97 
99  explicit Solver(std::shared_ptr<Problem> _problem);
100 
102  virtual ~Solver() = default;
103 
105  virtual bool solve() = 0;
106 
108  virtual std::string getType() const = 0;
109 
111  virtual std::shared_ptr<Solver> clone() const = 0;
112 
114  void setProperties(const Properties& _properties);
115 
117  const Properties& getSolverProperties() const;
118 
120  void copy(const Solver& _otherSolver);
121 
123  Solver& operator=(const Solver& _otherSolver);
124 
126  virtual void setProblem(std::shared_ptr<Problem> _newProblem);
127 
129  std::shared_ptr<Problem> getProblem() const;
130 
133  virtual void setTolerance(double _newTolerance);
134 
137  double getTolerance() const;
138 
140  virtual void setNumMaxIterations(std::size_t _newMax);
141 
143  std::size_t getNumMaxIterations() const;
144 
147  virtual void setIterationsPerPrint(std::size_t _newRatio);
148 
151  std::size_t getIterationsPerPrint() const;
152 
154  virtual void setOutStream(std::ostream* _os);
155 
157  std::ostream* getOutStream() const;
158 
160  virtual void setPrintFinalResult(bool _print);
161 
163  bool getPrintFinalResult() const;
164 
167  virtual void setResultFileName(const std::string& _resultFile);
168 
171  const std::string& getResultFileName() const;
172 
173 protected:
174 
176 
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: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:175
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:77
std::string mResultFile
Publish the results of the optimization to a file.
Definition: Solver.hpp:84
std::size_t mIterationsPerPrint
How many iterations between printing the Solver's progress to the terminal.
Definition: Solver.hpp:74
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:80
std::size_t mNumMaxIterations
The maximum number of iterations that the solver should use.
Definition: Solver.hpp:70
double mTolerance
The maximum step size allowed for the Problem to be considered converged.
Definition: Solver.hpp:66
std::shared_ptr< Problem > mProblem
Nonlinear optimization Problem to be solved.
Definition: Solver.hpp:63