MBSim

Project Page
lemke_algorithm.h
1 /* Copyright (C) 2004-2012 MBSim Development Team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact: martin.o.foerg@googlemail.com
18  */
19 
20 #ifndef NUMERICS_LEMKE_ALGORITHM_H_
21 #define NUMERICS_LEMKE_ALGORITHM_H_
22 
23 #include <fmatvec.h>
24 
25 namespace MBSim {
26 
28  public:
29  LemkeAlgorithm(const bool & DEBUGLEVEL_ = 0): DEBUGLEVEL(DEBUGLEVEL_) {
30  }
31 
32  LemkeAlgorithm(const fmatvec::SqrMat & M_, const fmatvec::Vec & q_, const bool & DEBUGLEVEL_ = 0) :
33  M(M_), q(q_), DEBUGLEVEL(DEBUGLEVEL_) {
34  assert(M_.rows() == q.size());
35  assert(M_.cols() == q.size());
36  }
37 
38  LemkeAlgorithm(const fmatvec::SymMat & M_, const fmatvec::Vec & q_, const bool & DEBUGLEVEL_ = 0) :
39  DEBUGLEVEL(DEBUGLEVEL_) {
40  setSystem(M_, q_);
41  }
42 
43  /* GETTER / SETTER */
47  int getInfo() {
48  return info;
49  }
50 
54  int getSteps(void) {
55  return steps;
56  }
57 
61  void setSystem(const fmatvec::SqrMat & M_, const fmatvec::Vec & q_) {
62  assert(M_.rows() == q_.size());
63  assert(M_.cols() == q_.size());
64  M = M_;
65  q = q_;
66  }
67 
71  void setSystem(const fmatvec::SymMat & M_, const fmatvec::Vec & q_) {
72  M = fmatvec::SqrMat(M_.size(), M_.size(), fmatvec::NONINIT);
73  for(int i = 0; i < M.size(); i++)
74  for(int j = 0; j < M.size(); j++)
75  M(i, j) = M_(i, j);
76  q = q_;
77  }
78  /***************************************************/
79 
83  fmatvec::Vec solve(unsigned int maxloops = 0);
84 
85  virtual ~LemkeAlgorithm() {
86  }
87 
88  protected:
89  int findLexicographicMinimum(const fmatvec::Mat &A, const int & pivotColIndex);
90  bool LexicographicPositive(const fmatvec::Vec & v);
91  void GaussJordanEliminationStep(fmatvec::Mat &A, int pivotRowIndex, int pivotColumnIndex, const std::vector<size_t> & basis);
92  bool greaterZero(const fmatvec::Vec & vector);
93  bool validBasis(const std::vector<size_t> & basis);
94 
95  fmatvec::SqrMat M;
96  fmatvec::Vec q;
97 
101  unsigned int steps;
102 
107 
114  int info;
115  };
116 
117 } /* namespace MBSim */
118 #endif /* NUMERICS_LEMKE_ALGORITHM_H_ */