AGX Dynamics  2.35.0.1
MLCP.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 #ifndef AGX_MLCP_H
18 #define AGX_MLCP_H
19 
20 #ifdef _MSC_VER
21 # pragma warning(push)
22 # pragma warning( disable: 4512 ) // Disable warning about assignment operator could not be generated due to reference members.
23 #endif
24 
25 
26 #include <agx/agxPhysics_export.h>
27 #include <agx/agx_valarray_types.h>
28 #include <agx/SparseMatrix.h>
29 #include <agx/agx_vector_types.h>
30 #include <agxSabre/DenseTypes.h>
31 
32 
33 
34 namespace agx
35 {
37  class SparseRangeReal;
38 
43  template< typename T >
45  {
46  public:
48  RowAllocator() : m_elements( nullptr ), m_size( 0 ), m_counter( 0 ), m_currentElement( 0 ) {}
49 
51 
57  void allocate( size_t size, int val )
58  {
59  allocate( size );
60  if ( m_elements )
61  memset( m_elements, val, m_size * sizeof( T ) );
62  }
63 
68  void allocate( size_t size )
69  {
70  bool mustDeallocate = size > m_size;
71  if ( mustDeallocate )
72  deallocate();
73  m_size = size;
74  m_currentElement = 0;
75  m_counter = 0;
76 
77  if ( m_size == 0 || !mustDeallocate ) return;
78 
79  agxAssert( m_elements == nullptr );
80 
81  m_elements = new T[ m_size ];
82  }
83 
84  void allocate()
85  {
86  allocate( m_counter, 0 );
87  }
88 
92  void deallocate()
93  {
94  if ( m_elements ) {
95  delete[] m_elements;
96  m_elements = nullptr;
97  }
98  m_size = 0;
99  m_currentElement = 0;
100  }
101 
102  inline T* get( size_t numElements )
103  {
104  agxAssert( m_elements );
105  T* elementsStart = ( m_elements + m_currentElement );
106  m_currentElement += numElements;
107  agxAssert( m_currentElement <= m_size );
108  return elementsStart;
109  }
110 
111  inline size_t& count() { return m_counter; }
112 
113  private:
114  T* m_elements;
115  size_t m_size;
116  size_t m_counter;
117  size_t m_currentElement;
118  };
119 
121 
122 
123 
126  class IdxSet
127  {
128  public:
129  enum IdxType { EQUALITY = -1, FREE = 0, LOWER = 2, UPPER = 4, IGNORE_INDEX = 8 };
130 
131  IdxSet() : m_empty( true ) {}
132 
140  void reset( const SparseRangeReal& bounds, size_t size );
141 
147  void update( const SparseRangeReal& bounds );
148 
152  inline bool empty() const { return m_empty; }
153 
159  void switchIndices( const RealValarray& z, const RealValarray& w, const SparseRangeReal& bounds );
160 
164  inline size_t getNumBoundedEquations() const { return m_atBounds.size(); }
165 
169  inline const UIntVector& getAtBoundsVector() const { return m_atBounds; }
170 
174  inline const UIntVector& getAtBoundsLocalVector() const { return m_atBoundsLocal; }
175 
176  inline IdxType& operator() ( size_t i )
177  {
178  agxAssert( i < m_ix.size() );
179  return (IdxType&)m_ix[ i ];
180  }
181 
182  inline IdxType operator() ( size_t i ) const
183  {
184  agxAssert( i < m_ix.size() );
185  return (IdxType)m_ix[ i ];
186  }
187 
188  private:
189  IntValarray m_ix;
190  bool m_empty;
191  UIntVector m_atBounds;
192  UIntVector m_atBoundsLocal;
193  };
195 
197  {
198  public:
199  MLCP( SparseMatrix& H, const RealValarray& q, RealValarray& z, const SparseRangeReal& bounds, const IntValarray& signs = IntValarray() );
200 
206  int solve( bool warm = false );
207 
208  size_t getIterations() const { return m_numIterations; }
209 
210  private:
214  void initialSolve();
215 
219  void solvePP();
220 
228  void solveSubproblem( const UIntVector& atBounds, const UIntVector& atBoundsLocal, const SparseRangeReal& bounds, RealValarray& u );
229 
230  void updateInverse( const UIntVector& atBounds, const SparseRangeReal& bounds );
231 
232  void buildSubmatrix( const UIntVector& atBounds, const UIntVector& atBoundsLocal, const SparseRangeReal& bounds, agxSabre::DenseMatrix<Real>& subH ) const;
233 
237  void allocateAndMapUnitSolveSolution( const SparseRangeReal& bounds, UInt row, const RealValarray& solution );
238 
242  void convertSigns( RealValarray& z, const IntValarray& signs ) const;
243 
247  void computeSlack();
248 
255  Real computeResidual() const;
256 
257  private:
258  SparseMatrix& m_H;
259  const RealValarray& m_q;
260  RealValarray& m_z;
261  const SparseRangeReal& m_bounds;
262  const IntValarray& m_signs;
263  IdxSet m_idxSet;
264  RealValarray m_w;
265  UIntRealPtrTable m_boundSolution;
266  RowAllocator<Real> m_boundSolutionAllocator;
267  RealValarray m_unitSolveSolution;
268  RealValarray m_z0;
269  RealValarray m_zBest;
270  size_t m_numIterations;
271  };
272 }
273 
274 #ifdef _MSC_VER
275 # pragma warning(pop)
276 #endif
277 
278 #endif
#define AGXPHYSICS_EXPORT
#define m_size
Definition: agx/Vector.h:429
Inheritance with partial specialization due to bug with ref_ptr containers.
size_t getIterations() const
Definition: MLCP.h:208
int solve(bool warm=false)
Solves this mixed LCP.
MLCP(SparseMatrix &H, const RealValarray &q, RealValarray &z, const SparseRangeReal &bounds, const IntValarray &signs=IntValarray())
Class to allocate an array of elements where some other structure works on pointer.
Definition: MLCP.h:45
void allocate(size_t size)
Allocate buffer of given size.
Definition: MLCP.h:68
void allocate()
Definition: MLCP.h:84
void allocate(size_t size, int val)
Allocate a buffer of given size with initial value val.
Definition: MLCP.h:57
void deallocate()
Deallocates the buffer and resets internal parameters.
Definition: MLCP.h:92
T * get(size_t numElements)
Definition: MLCP.h:102
size_t & count()
Definition: MLCP.h:111
RowAllocator()
Constructor.
Definition: MLCP.h:48
agx::SparseMatrix holds a matrix representation which contains the data.
Definition: SparseMatrix.h:86
Essentially a sparse array of bounds.
Definition: SparseTypes.h:74
Vector containing 'raw' data.
Definition: agx/Vector.h:246
#define agxAssert(expr)
Definition: debug.h:143
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::valarray< agx::Int > IntValarray
VectorPOD< UInt > UIntVector
uint32_t UInt
Definition: Integer.h:34
HashTable< UInt, Real * > UIntRealPtrTable
Definition: MLCP.h:36
double Real
Definition: Real.h:42
std::valarray< agx::Real > RealValarray
For this module, vectors are defined using valarray.