AGX Dynamics  2.37.3.2
MixedLCP.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/Referenced.h>
20 #include <agx/SparseMatrix.h>
21 #include <agx/SparseInverse.h>
22 #include <agx/IndexSet.h>
23 #include <agx/SparseTypes.h>
24 #include <agx/Solver.h>
25 
26 #include <agxSabre/agxSabre.h>
27 
28 #if defined(_MSC_VER)
29 #pragma warning( push )
30 #pragma warning( disable : 4275 ) // C4275: non dll-interface class 'x' used as base for dll-interface class 'y'
31 #endif
32 
33 
34 namespace agx
35 {
36  class StabilityReport;
37  class SparseInverse;
39 
41 
50  {
51  public:
52  void setNumEquations(size_t numEquations);
53 
55  void reportNumNlMixedCpIterations(UInt numIterations);
56  void reportMurtyIterations(UInt numIterations);
57  void reportKellerMakeFeasibleIterations(UInt numIterations);
58  void reportKellerSearchIterations(UInt numIterations);
59  void reportNumFreeEquationsAtEnd(const agx::IndexSet& indexSet);
60 
61  void addStats(const McpAlgorithmStats& other);
62 
63  void clear(size_t numEquations);
64 
65  void setEnableGatherStatistics(bool enable);
66  bool getEnableGatherStatistics() const;
67 
68 
69 
70 
71  const std::vector<int>& timesAdded() const;
72  const std::vector<int>& timesRemoved() const;
74  const std::vector<UInt>& numMurtyIterations() const;
75  const std::vector<UInt>& numKellerMakeFeasibleIterations() const;
76  const std::vector<UInt>& numKellerSearchIterations() const;
77  const std::vector<UInt>& numFreeEquationsAtEnd() const;
78 
79  private:
84  std::vector<int> m_timesAdded;
85 
90  std::vector<int> m_timesRemoved;
91 
92  UInt m_numNlMixedCpIterations = 0;
93 
94  std::vector<UInt> m_numMurtyIterations;
95  std::vector<UInt> m_numKellerMakeFeasibleIterations;
96  std::vector<UInt> m_numKellerSearchIterations;
97 
98  std::vector<UInt> m_numFreeEquationsAtEnd;
99 
100  bool m_gatherStatistics = false;
101  };
102 
108  {
109  public:
115  {
116  public:
118  {
119  public:
126  SolveReport( agx::UInt numIterations, agx::Real residual, agx::Bool successful );
127 
131  agx::UInt getNumIterations() const;
132 
136  agx::Real getResidual() const;
137 
141  agx::Bool isSuccessful() const;
142 
143  private:
144  agx::UInt m_numIterations;
145  agx::Real m_residual;
146  agx::Bool m_successful;
147  };
148 
149  class AGXPHYSICS_EXPORT Exception : public std::exception
150  {
151  public:
152  Exception() : std::exception() {}
153  };
154 
158  struct Args
159  {
163  agx::RealValarray& z0,
164  const agx::SparseRangeReal& bounds,
165  agx::IndexSet& indexSet,
166  const agx::RealValarray& signs,
167  agx::SparseInverse& sInv,
168  agx::Bool warm = false,
169  agx::StabilityReport* stabilityReport = nullptr );
170 
171  ~Args();
172 
186 
187  private:
188  Args& operator = ( const Args& ) = delete;
189  };
190 
191  public:
200  static agx::Real getComplementarityError( const agx::IndexSet& indexSet, const agx::SparseRangeReal& bounds, const agx::RealValarray& z, const agx::RealValarray& w );
201 
209  static agx::Bool isFeasible( const SparseRangeReal& bounds, const RealValarray& z, const Real threshold );
210 
221  static agx::Bool saveAsHdf5( const agx::String& filename, const SparseMatrix& m, RealValarray& z, RealValarray& q, RealValarray& signs, const agx::SparseRangeReal& bounds );
222 
223  public:
227  McpAlgorithm( agx::Real globalTolerance, agx::UInt maxNumIterations, bool gatherStatistics );
228 
232  agx::Real getGlobalTolerance() const;
233 
237  void setGlobalTolerance( agx::Real globalTolerance );
238 
242  agx::UInt getMaxNumIterations() const;
243 
248  void setMaxNumIterations( agx::UInt maxNumIterations );
249 
255  void resizeStats(size_t numEquations);
256 
262  void clearStats(size_t numEquations);
263 
267  const McpAlgorithmStats& getStats() const;
268 
272  McpAlgorithmStats& getStats();
273 
279 
280 
281  protected:
291  virtual agx::UInt switchIndices( const agx::RealValarray& z, const agx::RealValarray& w, const agx::SparseRangeReal& bounds, agx::IndexSet& indexSet, agx::Real tolerance = agx::Real( 1E-8 ) ) const;
292 
303  void solvePp( agx::SparseMatrix& m, agx::SparseInverse& sparseInverse, const RealValarray& z0, const agx::SparseRangeReal& bounds, const IndexSet& indexSet, const RealValarray& signs, RealValarray& z ) const;
304 
305 
316  void solvePp( agx::SparseMatrix& m, const agx::SparseRangeReal& bounds, const IndexSet& indexSet, const RealValarray& signs,
317  const RealValarray& q, RealValarray& z, RealValarray& workspace );
318 
327  void getSlack( agx::SparseMatrix& m, agx::RealValarray& z, const agx::RealValarray& q, const agx::RealValarray& signs, agx::RealValarray& slack ) const;
328 
336  void linearSolve( agx::SparseMatrix& m, agx::RealValarray& z, const agx::RealValarray& q, const agx::RealValarray& signs ) const;
337 
341  void solveInitial( agx::NlMixedCp::McpAlgorithm::Args& args ) const;
342 
346  void forceToBound( RealValarray& v, const agx::SparseRangeReal& bounds, const agx::IndexSet& indexSet ) const;
347 
351  void debugCheckSolution( const RealValarray& z, const SparseRangeReal& /*bounds*/ ) const;
352 
353  protected:
356 
358  };
359 
361 
362  public:
366  static NlMixedCpRef create( agx::Solver::NlMcpConfig config, bool gatherStatistics );
367 
368  public:
369  NlMixedCp( agx::NlMixedCp::McpAlgorithm* mcpAlgorithm );
370 
374  agx::SparseMatrix& getMatrix();
375 
379  const agx::SparseMatrix& getMatrix() const;
380 
384  agx::RealValarray& getQ();
385 
389  const agx::RealValarray& getQ() const;
390 
394  agx::RealValarray& getSolution();
395 
399  const agx::RealValarray& getSolution() const;
400 
404  agx::SparseRangeReal& getSparseBounds();
405 
409  const agx::SparseRangeReal& getSparseBounds() const;
410 
414  agx::RealValarray& getSigns();
415 
419  const agx::RealValarray& getSigns() const;
420 
421 
422  const agx::NlMixedCp::McpAlgorithm* getAlgorithm() const;
423 
424 
428  virtual void setSize( agx::UInt numEquations );
429 
434  virtual agx::UInt solve( agx::Bool warm = false,
435  agx::StabilityReport* stabilityReport = nullptr,
436  agx::INlSolveDataH5* nlSolveDataH5 = nullptr );
437 
438  protected:
439  virtual ~NlMixedCp();
440 
441  protected:
442  friend class DirectSolverData;
443  Solver::NlMcpConfig m_config;
455  };
456 
461  {
462  public:
463 
469  SparseRangeReal& bounds, RealValarray& z, const RealValarray& signs, int max_iterations=-1) ;
470 
471  ~MixedLCPOld();
472 
474  void solve(bool warm = false);
475 
477  int getIterations() const;
478 
479  Real getLastResidual() const;
480 
482  bool isSuccess() const;
483 
484  SparseRangeReal & bounds() ;
485  const SparseRangeReal & bounds() const ;
486  RealValarray & z();
487  RealValarray & q();
488  const RealValarray & z() const;
489  const RealValarray & q() const;
490 
491  std::ostream & diag(std::ostream &os, const agx::String & msg = "" ) const ;
492 
493  private:
494  class MixedLCPRep * m_rep; // hide the representation.
495  };
496 }
497 
498 #if defined(_MSC_VER)
499 #pragma warning( pop ) // restoring: warning( disable : 4127 )
500 #endif
agx::RealValarray m_z0
Definition: MixedLCP.h:448
Class that stores rows and columns of the matrix.
Definition: SparseMatrix.h:283
void reportKellerSearchIterations(UInt numIterations)
agx::RealValarray m_signs
Definition: MixedLCP.h:450
Storage location for solver statistics.
Definition: MixedLCP.h:49
agx::RealValarray m_q
Definition: MixedLCP.h:446
agx::ref_ptr< McpAlgorithm > McpAlgorithmRef
Definition: MixedLCP.h:360
bool Bool
Definition: Integer.h:48
const std::vector< UInt > & numFreeEquationsAtEnd() const
const agx::SparseRangeReal & bounds
Definition: MixedLCP.h:179
void addStats(const McpAlgorithmStats &other)
agx::SparseMatrix m_matrix
Definition: MixedLCP.h:445
McpAlgorithmStats m_stats
Definition: MixedLCP.h:357
Class that implements a method to solve linear- or nonlinear complementary problems.
Definition: MixedLCP.h:107
const std::vector< UInt > & numKellerSearchIterations() const
const std::vector< UInt > & numMurtyIterations() const
STL namespace.
#define AGXPHYSICS_EXPORT
const std::vector< int > & timesAdded() const
agx::ColumnStorage * columnStorage
Definition: MixedLCP.h:184
agx::SparseInverse m_inv
Definition: MixedLCP.h:452
agx::UInt m_maxNumOuterIterations
Definition: MixedLCP.h:453
void reportAddedAndRemovedEquations(const agxSabre::UInt32Vector &add, const agxSabre::UInt32Vector &del)
void reportNumNlMixedCpIterations(UInt numIterations)
Base class providing referencing counted objects.
Definition: Referenced.h:119
Essentially a sparse array of bounds.
Definition: SparseTypes.h:73
uint32_t UInt
Definition: Integer.h:34
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
void reportNumFreeEquationsAtEnd(const agx::IndexSet &indexSet)
agx::IndexSet m_indexSet
Definition: MixedLCP.h:451
std::valarray< agx::Real > RealValarray
For this module, vectors are defined using valarray.
VectorPOD< UInt32 > UInt32Vector
Definition of a mixed LCP problem.
Definition: MixedLCP.h:460
bool getEnableGatherStatistics() const
agx::RealValarray m_z
Definition: MixedLCP.h:447
void clear(size_t numEquations)
const agx::RealValarray & signs
Definition: MixedLCP.h:181
void setEnableGatherStatistics(bool enable)
void reportKellerMakeFeasibleIterations(UInt numIterations)
Structure for all the arguments passed around.
Definition: MixedLCP.h:158
agx::SparseRangeReal m_sparseBounds
Definition: MixedLCP.h:449
void reportMurtyIterations(UInt numIterations)
Solver::NlMcpConfig m_config
Definition: MixedLCP.h:443
const std::vector< UInt > & numKellerMakeFeasibleIterations() const
UInt numNlMixedCpIterations() const
agx::NlMixedCp::McpAlgorithmRef m_mcp
Definition: MixedLCP.h:444
agx::SparseInverse & inv
Definition: MixedLCP.h:185
void setNumEquations(size_t numEquations)
double Real
Definition: Real.h:42
Interface class for algorithm to solve general mixed complementarity problem.
Definition: MixedLCP.h:114
agx::SparseMatrix holds a matrix representation which contains the data.
Definition: SparseMatrix.h:85
agx::StabilityReport * stabilityReport
Definition: MixedLCP.h:183
const std::vector< int > & timesRemoved() const
agx::ref_ptr< class NlMixedCp > NlMixedCpRef
Definition: MixedLCP.h:38
agx::Real m_outerGlobalTolerance
Definition: MixedLCP.h:454