MBSim

Project Page
linear_complementarity_problem.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_LINEAR_COMPLEMENTARITY_PROBLEM_H_
00021 #define NUMERICS_LINEAR_COMPLEMENTARITY_PROBLEM_H_
00022 
00023 #include <fmatvec/fmatvec.h>
00024 
00025 #include <mbsim/numerics/nonlinear_algebra/multi_dimensional_newton_method.h>
00026 #include <mbsim/numerics/nonlinear_algebra/multi_dimensional_fixpoint_solver.h>
00027 #include <mbsim/numerics/linear_complementarity_problem/lemke_algorithm.h>
00028 
00029 #include <mbsim/numerics/functions/lcp_reformulation_functions.h>
00030 
00031 namespace MBSim {
00032 
00033   enum LCPSolvingStrategy {
00034     Standard,      //trying to solve the LCP in a standard way (first some steps of Lemke algorithm, then trying iterative schemes in reformulated system, then trying Lemke again as fallback with all steps)
00035     ReformulatedStandard,  //trying to use the iterative schemes at first (as fallback use Lemke)
00036     ReformulatedNewtonOnly,
00037     ReformulatedFixpointOnly,
00038     LemkeOnly
00039     //only use LemkeAgorithm
00040   };
00041 
00042   enum JacobianType {
00043     Numerical,  //use only a numerical Jacobian Matrix
00044     LCPSpecial
00045     //use a analytical Jacobian Matrix almost everywhere and choose a derivation at the kink (=LinearComplementarityJacobianFunction) //TODO: find a better name
00046   };
00047 
00048   enum CriteriaType {
00049     Global,
00050     Local
00051   };
00052   std::ostream& operator <<(std::ostream & o, const LCPSolvingStrategy &strategy);
00053 
00054   std::ostream& operator <<(std::ostream & o, const JacobianType &jacobianType);
00055 
00056 
00062   class LinearComplementarityProblem {
00063     public:
00072       LinearComplementarityProblem(const fmatvec::SymMat & M_, const fmatvec::Vec & q_, const LCPSolvingStrategy & strategy_ = Standard, const JacobianType & jacobianType_ = LCPSpecial, const unsigned int & DEBUGLEVEL = 0);
00073 
00074       virtual ~LinearComplementarityProblem();
00075 
00076       /*GETTER / SETTER*/
00077       void setSystem(const fmatvec::SymMat & M_, const fmatvec::Vec & q_);
00078       void setStrategy(const LCPSolvingStrategy & strategy_) {
00079         strategy = strategy_;
00080         oldStrategy = strategy_;
00081       }
00082       void setJacobianType(const JacobianType & jacobianType_) {
00083         jacobianType = jacobianType_;
00084       }
00085       void setNewtonCriteriaFunction(CriteriaFunction * criteriaFunction_) {
00086         newtonSolver->setCriteriaFunction(criteriaFunction_);
00087       }
00088       void setFixpointCriteriaFunction(CriteriaFunction * criteriaFunction_) {
00089         fixpointSolver->setCriteriaFunction(criteriaFunction_);
00090       }
00091       void setDebugLevel(const unsigned int & DEBUGLEVEL_) {
00092         DEBUGLEVEL = DEBUGLEVEL_;
00093       }
00094       /****************/
00095 
00096       fmatvec::Vec solve(const fmatvec::Vec & initialSolution = fmatvec::Vec(0, fmatvec::NONINIT));
00097 
00101       static double computeMediumEigVal(const fmatvec::SqrMat & M);
00102 
00106       static fmatvec::Vec createInitialSolution(const fmatvec::SymMat & M, const fmatvec::Vec & q, double mediumEigVal = 0);
00107 
00111       static fmatvec::Vec createInitialSolution(const fmatvec::SqrMat & M, const fmatvec::Vec & q, double mediumEigVal = 0);
00112 
00113     protected:
00117       void useNewton(fmatvec::Vec & solution, bool & solved);
00118 
00122       void useFixpoint(fmatvec::Vec & solution, bool & solved);
00123 
00127       fmatvec::SqrMat M;
00128 
00132       fmatvec::Vec q;
00133 
00137       LCPSolvingStrategy strategy;
00138 
00142       LCPSolvingStrategy oldStrategy;
00143 
00147       double mediumEigenValue;
00148 
00152       JacobianType jacobianType;
00153 
00157       LemkeAlgorithm lemkeSolver;
00158 
00162       MultiDimensionalNewtonMethod * newtonSolver;
00163 
00167       LCPNewtonReformulationFunction * newtonFunction;
00168 
00172       NewtonJacobianFunction * jacobianFunction;
00173 
00177       CriteriaFunction * criteriaNewton;
00178 
00182       MultiDimensionalFixpointSolver * fixpointSolver;
00183 
00187       LCPFixpointReformulationFunction * fixpointFunction;
00188 
00192       CriteriaFunction * criteriaFixedpoint;
00193 
00197       unsigned int DEBUGLEVEL;
00198   };
00199 }
00200 
00201 #endif /*__LINEAR_COMPLEMENTARITY_PROBLEM_H_*/