MBSim

Project Page
lemke_algorithm.h
00001 /* Copyright (C) 2004-2012 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  *
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: martin.o.foerg@googlemail.com
00018  */
00019 
00020 #ifndef NUMERICS_LEMKE_ALGORITHM_H_
00021 #define NUMERICS_LEMKE_ALGORITHM_H_
00022 
00023 #include <fmatvec/fmatvec.h>
00024 #include <fmatvec/atom.h>
00025 
00026 namespace MBSim {
00027 
00028   class LemkeAlgorithm : public fmatvec::Atom {
00029     public:
00030       LemkeAlgorithm(const bool & DEBUGLEVEL_ = 0) :
00031           M(), q(), steps(0), DEBUGLEVEL(DEBUGLEVEL_), info(-1) {
00032       }
00033 
00034       LemkeAlgorithm(const fmatvec::SqrMat & M_, const fmatvec::Vec & q_, const bool & DEBUGLEVEL_ = 0) :
00035           M(M_), q(q_), steps(0), DEBUGLEVEL(DEBUGLEVEL_), info(-1) {
00036         assert(M_.rows() == q.size());
00037         assert(M_.cols() == q.size());
00038       }
00039 
00040       LemkeAlgorithm(const fmatvec::SymMat & M_, const fmatvec::Vec & q_, const bool & DEBUGLEVEL_ = 0) :
00041           DEBUGLEVEL(DEBUGLEVEL_) {
00042         setSystem(M_, q_);
00043       }
00044 
00045       /* GETTER / SETTER */
00049       int getInfo() {
00050         return info;
00051       }
00052 
00056       int getSteps(void) {
00057         return steps;
00058       }
00059 
00063       void setSystem(const fmatvec::SqrMat & M_, const fmatvec::Vec & q_) {
00064         assert(M_.rows() == q_.size());
00065         assert(M_.cols() == q_.size());
00066         M.resize() = M_;
00067         q.resize() = q_;
00068       }
00069 
00073       void setSystem(const fmatvec::SymMat & M_, const fmatvec::Vec & q_) {
00074         M = fmatvec::SqrMat(M_.size(), fmatvec::NONINIT);
00075         for (int i = 0; i < M.size(); i++)
00076           for (int j = 0; j < M.size(); j++)
00077             M(i, j) = M_(i, j);
00078         q = q_;
00079       }
00080       /***************************************************/
00081 
00085       fmatvec::Vec solve(unsigned int maxloops = 0);
00086 
00087       virtual ~LemkeAlgorithm() {
00088       }
00089 
00090     protected:
00091       int findLexicographicMinimum(const fmatvec::Mat &A, const int & pivotColIndex);
00092       bool LexicographicPositive(const fmatvec::Vec & v);
00093       void GaussJordanEliminationStep(fmatvec::Mat &A, int pivotRowIndex, int pivotColumnIndex, const std::vector<size_t> & basis);
00094       bool greaterZero(const fmatvec::Vec & vector);
00095       bool validBasis(const std::vector<size_t> & basis);
00096 
00097       fmatvec::SqrMat M;
00098       fmatvec::Vec q;
00099 
00103       unsigned int steps;
00104 
00108       bool DEBUGLEVEL;
00109 
00116       int info;
00117   };
00118 
00119 } /* namespace MBSim */
00120 #endif /* NUMERICS_LEMKE_ALGORITHM_H_ */