So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
MecheInterface.hpp
1 /*
2  * This file is part of So-bogus, a C++ sparse block matrix library and
3  * Second Order Cone solver.
4  *
5  * Copyright 2013 Gilles Daviet <gdaviet@gmail.com>
6  *
7  * So-bogus is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11 
12  * So-bogus is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with So-bogus. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <iosfwd>
22 
23 #include "../Core/Utils/Signal.hpp"
24 #include "../Core/Utils/Timer.hpp"
25 #include "../Core/Utils/CppTools.hpp"
26 
27 #ifndef BOGUS_MECHE_INTERFACE_HPP
28 #define BOGUS_MECHE_INTERFACE_HPP
29 
30 namespace bogus
31 {
32 
33 template< unsigned Dimension > struct PrimalFrictionProblem ;
34 template< unsigned Dimension > struct DualFrictionProblem ;
35 
37 {
38 public:
39 
40  enum Algorithm {
41  GaussSeidel = 0,
43  MatrixFreeGaussSeidel,
44  ADMM,
45  DualAMA
46  };
47 
48  struct Options {
49  int maxThreads ;
50 
51  unsigned maxIters ;
52  unsigned cadouxIters ;
53 
54  double tolerance ;
56 
57  Algorithm algorithm ;
58 
59  // Solver-specific options
60 
61  double gsRegularization ;
62  bool gsColoring ;
63 
64  double admmProjStepSize ;
65  double admmFpStepSize ;
66 
67  Options() ;
68  };
69 
72 
74 
77  void fromPrimal (
78  unsigned int NObj,
79  const unsigned int * ndof,
80  const double *const * MassMat,
81  const double * f_in,
82  unsigned int n_in,
83  const double * mu_in,
84  const double * E_in,
85  const double * w_in,
86  const int * const ObjA,
87  const int * const ObjB,
88  const double *const HA[],
89  const double *const HB[]
90  );
91 
93  double solve(double * r,
94  double * v,
95  bool staticProblem ,
96  double problemRegularization,
97  const Options & options
98  ) ;
99 
101  double solve(double * r,
102  double * v,
103  int maxThreads = 0,
104  double tol = 0.,
105  unsigned maxIters = 0,
106  bool staticProblem = false,
107  double regularization = 0.,
108  bool useInfinityNorm = false,
109  bool useProjectedGradient = false,
110  unsigned cadouxIters = 0
111  );
112 
114  void computeDual( double regularization ) ;
115 
117  void reset() ;
118 
119  unsigned nDegreesOfFreedom() const ;
120  unsigned nContacts() const ;
121 
123  void setOutStream( std::ostream *out ) ;
124 
126  Signal< unsigned, double, double > &callback() { return m_callback ; }
127 
129 
130  bool dumpToFile( const char* fileName, const double *r0 = BOGUS_NULL_PTR(const double) ) const ;
132 
136  bool fromFile( const char* fileName, double* &r0 ) ;
137 
138  // solvers Callback
139  void ackCurrentResidual( unsigned GSIter, double err ) ;
140 
141  // Accessors
142 
143  const PrimalFrictionProblem<3u> & primal() const { return *m_primal ; }
144  const DualFrictionProblem<3u> & dual() const { return *m_dual ; }
145 
146  PrimalFrictionProblem<3u> & primal() { return *m_primal ; }
147  DualFrictionProblem<3u> & dual() { return *m_dual ; }
148 
149  double *f (){ return m_f ; }
150  double *w (){ return m_w ; }
151  double *mu(){ return m_mu ; }
152 
154  double lastSolveTime() const { return m_lastSolveTime ; }
155 
156 protected:
157 
158  void destroy() ;
159 
160  PrimalFrictionProblem<3u> * m_primal ;
161  DualFrictionProblem<3u> * m_dual ;
162 
163  double m_lastSolveTime ;
164 
166  Timer m_timer ;
167 
168 private:
169  // Non copyable, non assignable
171  MecheFrictionProblem &operator=( const MecheFrictionProblem & ) ;
172 
173  // Used to store data when loading problem from file
174  double *m_f ;
175  double *m_w ;
176  double *m_mu ;
177 
178  std::ostream *m_out ;
179 } ;
180 
181 }
182 
183 #endif
unsigned cadouxIters
If staticProblem is false and cadouxIters is greater than zero, use the Cadoux algorithm to solve the...
Definition: MecheInterface.hpp:52
void computeDual(double regularization)
Computes the dual from the primal.
Definition: MecheInterface.cpp:189
double gsRegularization
Solver algorithm;.
Definition: MecheInterface.hpp:61
void reset()
Cleams up the problem, then allocates a new PrimalFrictionProblem and make m_primal point to it...
Definition: MecheInterface.cpp:91
bool dumpToFile(const char *fileName, const double *r0=(static_cast< const double * >(0))) const
Dumps the current primal() to fileName.
Definition: MecheInterface.cpp:415
bool fromFile(const char *fileName, double *&r0)
Loads the primal from a previously saved problem file.
Definition: MecheInterface.cpp:435
unsigned maxIters
Max number of iterations. 0 means GS&#39;s default.
Definition: MecheInterface.hpp:51
ADMM (Alternating Direction Method of Multipliers ) iterative solver.
Definition: ADMM.hpp:44
bool gsColoring
Use coloring for parallel GS; slower but deterministic.
Definition: MecheInterface.hpp:62
void setOutStream(std::ostream *out)
Sets the standard output stream ( out can be NULL to remove all output )
Definition: MecheInterface.cpp:409
Projected Gradient iterative solver.
Definition: ProjectedGradient.hpp:41
Simple timer class. Starts when constructed.
Definition: Timer.hpp:24
Dual AMA iterative solver (Alternating Minimization Algorithm on dual formuation of quadratic optimiz...
Definition: ADMM.hpp:191
double solve(double *r, double *v, bool staticProblem, double problemRegularization, const Options &options)
Solves the friction problem.
Definition: MecheInterface.cpp:247
Projected Gauss-Seidel iterative solver.
Definition: GaussSeidel.hpp:43
Definition: MecheInterface.hpp:48
bool useInfinityNorm
Whether to use the infinity norm to evaluate the residual of the friction problem,.
Definition: MecheInterface.hpp:55
double lastSolveTime() const
Time spent in last solver call. In seconds.
Definition: MecheInterface.hpp:154
void fromPrimal(unsigned int NObj, const unsigned int *ndof, const double *const *MassMat, const double *f_in, unsigned int n_in, const double *mu_in, const double *E_in, const double *w_in, const int *const ObjA, const int *const ObjB, const double *const HA[], const double *const HB[])
Allocates and sets up the primal friction problem.
Definition: MecheInterface.cpp:99
int maxThreads
Maximum number of threads that the GS will use.
Definition: MecheInterface.hpp:49
double tolerance
Solver tolerance.
Definition: MecheInterface.hpp:54
Signal< unsigned, double, double > & callback()
Signal&lt; interationNumber, error, elapsedTime &gt; that will be triggered every few iterations.
Definition: MecheInterface.hpp:126
Definition: MecheInterface.hpp:36