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 
00025 namespace MBSim {
00026 
00027   class LemkeAlgorithm {
00028     public:
00029       LemkeAlgorithm(const bool & DEBUGLEVEL_ = 0) :
00030           M(), q(), steps(0), DEBUGLEVEL(DEBUGLEVEL_), info(-1) {
00031       }
00032 
00033       LemkeAlgorithm(const fmatvec::SqrMat & M_, const fmatvec::Vec & q_, const bool & DEBUGLEVEL_ = 0) :
00034           M(M_), q(q_), steps(0), DEBUGLEVEL(DEBUGLEVEL_), info(-1) {
00035         assert(M_.rows() == q.size());
00036         assert(M_.cols() == q.size());
00037       }
00038 
00039       LemkeAlgorithm(const fmatvec::SymMat & M_, const fmatvec::Vec & q_, const bool & DEBUGLEVEL_ = 0) :
00040           DEBUGLEVEL(DEBUGLEVEL_) {
00041         setSystem(M_, q_);
00042       }
00043 
00044       /* GETTER / SETTER */
00048       int getInfo() {
00049         return info;
00050       }
00051 
00055       int getSteps(void) {
00056         return steps;
00057       }
00058 
00062       void setSystem(const fmatvec::SqrMat & M_, const fmatvec::Vec & q_) {
00063         assert(M_.rows() == q_.size());
00064         assert(M_.cols() == q_.size());
00065         M.resize() = M_;
00066         q.resize() = q_;
00067       }
00068 
00072       void setSystem(const fmatvec::SymMat & M_, const fmatvec::Vec & q_) {
00073         M = fmatvec::SqrMat(M_.size(), fmatvec::NONINIT);
00074         for (int i = 0; i < M.size(); i++)
00075           for (int j = 0; j < M.size(); j++)
00076             M(i, j) = M_(i, j);
00077         q = q_;
00078       }
00079       /***************************************************/
00080 
00084       fmatvec::Vec solve(unsigned int maxloops = 0);
00085 
00086       virtual ~LemkeAlgorithm() {
00087       }
00088 
00089     protected:
00090       int findLexicographicMinimum(const fmatvec::Mat &A, const int & pivotColIndex);
00091       bool LexicographicPositive(const fmatvec::Vec & v);
00092       void GaussJordanEliminationStep(fmatvec::Mat &A, int pivotRowIndex, int pivotColumnIndex, const std::vector<size_t> & basis);
00093       bool greaterZero(const fmatvec::Vec & vector);
00094       bool validBasis(const std::vector<size_t> & basis);
00095 
00096       fmatvec::SqrMat M;
00097       fmatvec::Vec q;
00098 
00102       unsigned int steps;
00103 
00107       bool DEBUGLEVEL;
00108 
00115       int info;
00116   };
00117 
00118 } /* namespace MBSim */
00119 #endif /* NUMERICS_LEMKE_ALGORITHM_H_ */