AGX Dynamics  2.35.0.1
MixedLCP.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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;
38  class ColumnStorage;
39 
41 
50  {
51  public:
52  void setNumEquations(size_t numEquations);
53 
55  void reportNumNlMixedCpIterations(UInt numIterations);
56  void reportMurtyIterations(UInt numIterations);
58  void reportKellerSearchIterations(UInt numIterations);
60 
61  void addStats(const McpAlgorithmStats& other);
62 
63  void clear(size_t numEquations);
64 
65  void setEnableGatherStatistics(bool enable);
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 
132 
137 
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 
233 
237  void setGlobalTolerance( agx::Real globalTolerance );
238 
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 
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 
328 
337 
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:
370 
375 
379  const agx::SparseMatrix& getMatrix() const;
380 
385 
389  const agx::RealValarray& getQ() const;
390 
395 
400 
405 
410 
415 
419  const agx::RealValarray& getSigns() const;
420 
421 
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 
472 
474  void solve(bool warm = false);
475 
477  int getIterations() const;
478 
480 
482  bool isSuccess() const;
483 
485  const SparseRangeReal & bounds() const ;
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
#define AGXPHYSICS_EXPORT
Class that stores rows and columns of the matrix.
Definition: SparseMatrix.h:284
Storage location for solver statistics.
Definition: MixedLCP.h:50
const std::vector< UInt > & numMurtyIterations() const
const std::vector< UInt > & numKellerSearchIterations() const
void reportKellerSearchIterations(UInt numIterations)
const std::vector< int > & timesAdded() const
void setNumEquations(size_t numEquations)
void clear(size_t numEquations)
bool getEnableGatherStatistics() const
void reportKellerMakeFeasibleIterations(UInt numIterations)
void addStats(const McpAlgorithmStats &other)
void reportNumNlMixedCpIterations(UInt numIterations)
void reportNumFreeEquationsAtEnd(const agx::IndexSet &indexSet)
const std::vector< UInt > & numFreeEquationsAtEnd() const
void setEnableGatherStatistics(bool enable)
const std::vector< UInt > & numKellerMakeFeasibleIterations() const
const std::vector< int > & timesRemoved() const
void reportMurtyIterations(UInt numIterations)
UInt numNlMixedCpIterations() const
void reportAddedAndRemovedEquations(const agxSabre::UInt32Vector &add, const agxSabre::UInt32Vector &del)
Definition of a mixed LCP problem.
Definition: MixedLCP.h:461
const RealValarray & q() const
RealValarray & z()
void solve(bool warm=false)
Solve the problem .
int getIterations() const
Returns the number of iterations.
const SparseRangeReal & bounds() const
SparseRangeReal & bounds()
bool isSuccess() const
Returns true if the solver computed a correct answer, false * otherwise.
RealValarray & q()
MixedLCPOld(SparseMatrix &M, RealValarray &q, SparseRangeReal &bounds, RealValarray &z, const RealValarray &signs, int max_iterations=-1)
Construction copies references to the internal objects.
const RealValarray & z() const
Real getLastResidual() const
std::ostream & diag(std::ostream &os, const agx::String &msg="") const
SolveReport(agx::UInt numIterations, agx::Real residual, agx::Bool successful)
Construct given solve result.
Interface class for algorithm to solve general mixed complementarity problem.
Definition: MixedLCP.h:115
void debugCheckSolution(const RealValarray &z, const SparseRangeReal &) const
Asserts if z has invalid values.
void resizeStats(size_t numEquations)
Ensure that the solver statistics can report data for the given number of equations.
McpAlgorithm(agx::Real globalTolerance, agx::UInt maxNumIterations, bool gatherStatistics)
Construct given minimum tolerance and maximum number of iterations.
McpAlgorithmStats m_stats
Definition: MixedLCP.h:357
void clearStats(size_t numEquations)
Ensure that the solver statistics can report data for the given number of equations.
const McpAlgorithmStats & getStats() const
void solvePp(agx::SparseMatrix &m, const agx::SparseRangeReal &bounds, const IndexSet &indexSet, const RealValarray &signs, const RealValarray &q, RealValarray &z, RealValarray &workspace)
Solve principle problem given sparse matrix, initial solution, bounds and current index set.
void linearSolve(agx::SparseMatrix &m, agx::RealValarray &z, const agx::RealValarray &q, const agx::RealValarray &signs) const
Solves: M z = -q for z.
agx::UInt getMaxNumIterations() const
void setGlobalTolerance(agx::Real globalTolerance)
Assign the tolerance (accuracy) of the solution.
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
Switch indices in the index set given current solution and slack.
void getSlack(agx::SparseMatrix &m, agx::RealValarray &z, const agx::RealValarray &q, const agx::RealValarray &signs, agx::RealValarray &slack) const
Computes slack w = M z + q.
static agx::Bool isFeasible(const SparseRangeReal &bounds, const RealValarray &z, const Real threshold)
Checks if the solution z is feasible, i.e., within bounds.
void setMaxNumIterations(agx::UInt maxNumIterations)
Assign the maximum number of iterations this algorithm may perform to reach the given tolerance of th...
static agx::Bool saveAsHdf5(const agx::String &filename, const SparseMatrix &m, RealValarray &z, RealValarray &q, RealValarray &signs, const agx::SparseRangeReal &bounds)
Save matrix, solution, right hand side, sign convention and bounds to a unique problem in filename.
static agx::Real getComplementarityError(const agx::IndexSet &indexSet, const agx::SparseRangeReal &bounds, const agx::RealValarray &z, const agx::RealValarray &w)
Computes the complementarity error of a current solution.
void solveInitial(agx::NlMixedCp::McpAlgorithm::Args &args) const
Performs linear solve and stores the solution into args.z0 AND args.z.
virtual agx::NlMixedCp::McpAlgorithm::SolveReport solve(agx::NlMixedCp::McpAlgorithm::Args &args)=0
Solve given current state of the index set, matrix, q, bounds etc.
void forceToBound(RealValarray &v, const agx::SparseRangeReal &bounds, const agx::IndexSet &indexSet) const
Write the elements v[i] for which i is at a bound.
void solvePp(agx::SparseMatrix &m, agx::SparseInverse &sparseInverse, const RealValarray &z0, const agx::SparseRangeReal &bounds, const IndexSet &indexSet, const RealValarray &signs, RealValarray &z) const
Solve principle problem given sparse matrix, initial solution, bounds and current index set.
agx::Real getGlobalTolerance() const
McpAlgorithmStats & getStats()
Class that implements a method to solve linear- or nonlinear complementary problems.
Definition: MixedLCP.h:108
agx::IndexSet m_indexSet
Definition: MixedLCP.h:451
agx::RealValarray m_z0
Definition: MixedLCP.h:448
agx::RealValarray & getQ()
agx::SparseMatrix m_matrix
Definition: MixedLCP.h:445
agx::NlMixedCp::McpAlgorithmRef m_mcp
Definition: MixedLCP.h:444
agx::SparseInverse m_inv
Definition: MixedLCP.h:452
agx::RealValarray m_z
Definition: MixedLCP.h:447
virtual agx::UInt solve(agx::Bool warm=false, agx::StabilityReport *stabilityReport=nullptr, agx::INlSolveDataH5 *nlSolveDataH5=nullptr)
Solves the system, returns the total number of iterations made in the inner (mcp) loop.
virtual ~NlMixedCp()
agx::SparseRangeReal m_sparseBounds
Definition: MixedLCP.h:449
agx::SparseMatrix & getMatrix()
const agx::RealValarray & getQ() const
const agx::RealValarray & getSigns() const
NlMixedCp(agx::NlMixedCp::McpAlgorithm *mcpAlgorithm)
const agx::SparseRangeReal & getSparseBounds() const
const agx::RealValarray & getSolution() const
agx::ref_ptr< McpAlgorithm > McpAlgorithmRef
Definition: MixedLCP.h:360
const agx::SparseMatrix & getMatrix() const
Solver::NlMcpConfig m_config
Definition: MixedLCP.h:443
const agx::NlMixedCp::McpAlgorithm * getAlgorithm() const
agx::UInt m_maxNumOuterIterations
Definition: MixedLCP.h:453
agx::RealValarray & getSolution()
agx::RealValarray m_signs
Definition: MixedLCP.h:450
agx::Real m_outerGlobalTolerance
Definition: MixedLCP.h:454
virtual void setSize(agx::UInt numEquations)
Set the size of the system.
agx::RealValarray m_q
Definition: MixedLCP.h:446
agx::SparseRangeReal & getSparseBounds()
agx::RealValarray & getSigns()
static NlMixedCpRef create(agx::Solver::NlMcpConfig config, bool gatherStatistics)
Creates default solver assuming symmetric-, sparse- and lower triangle matrix.
Base class providing referencing counted objects.
Definition: Referenced.h:114
agx::SparseMatrix holds a matrix representation which contains the data.
Definition: SparseMatrix.h:86
Essentially a sparse array of bounds.
Definition: SparseTypes.h:74
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agx::ref_ptr< class NlMixedCp > NlMixedCpRef
Definition: MixedLCP.h:38
bool Bool
Definition: Integer.h:48
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
std::valarray< agx::Real > RealValarray
For this module, vectors are defined using valarray.
VectorPOD< UInt32 > UInt32Vector
Structure for all the arguments passed around.
Definition: MixedLCP.h:159
const agx::RealValarray & signs
Definition: MixedLCP.h:181
agx::ColumnStorage * columnStorage
Definition: MixedLCP.h:184
const agx::SparseRangeReal & bounds
Definition: MixedLCP.h:179
agx::SparseInverse & inv
Definition: MixedLCP.h:185
Args(agx::SparseMatrix &m, agx::RealValarray &q, agx::RealValarray &z, agx::RealValarray &z0, const agx::SparseRangeReal &bounds, agx::IndexSet &indexSet, const agx::RealValarray &signs, agx::SparseInverse &sInv, agx::Bool warm=false, agx::StabilityReport *stabilityReport=nullptr)
agx::StabilityReport * stabilityReport
Definition: MixedLCP.h:183