MBSim

Project Page
criteria_functions.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 NUMERICSCRITERIAFUNCTIONS_H_
00021 #define NUMERICSCRITERIAFUNCTIONS_H_
00022 
00023 #include <fmatvec/fmatvec.h>
00024 #include <mbsim/functions/function.h>
00025 
00026 #include <map>
00027 
00028 namespace MBSim {
00029 
00033   class CriteriaFunction : public Function<int(fmatvec::Vec)> {
00034 
00035     public:
00039       CriteriaFunction();
00040 
00044       virtual ~CriteriaFunction();
00045 
00046       /* GETTER / SETTER*/
00047       void setFunction(Function<fmatvec::Vec(fmatvec::Vec)> *function_) {
00048         function = function_;
00049       }
00050       /*****************/
00051 
00061       virtual int operator ()(const fmatvec::Vec & vector) = 0;
00062 
00066       virtual void clear() = 0;
00067 
00071       virtual bool isBetter(const fmatvec::Vec & vector, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT)) = 0;
00072 
00073     protected:
00077       Function<fmatvec::Vec(fmatvec::Vec)> *function;
00078 
00079   };
00080 
00084   class GlobalCriteriaFunction : public CriteriaFunction {
00085 
00086     public:
00090       GlobalCriteriaFunction(const double & tolerance_ = 1e-10);
00091 
00095       virtual ~GlobalCriteriaFunction();
00096 
00097       /* INHERITED INTERFACE */
00098       virtual int operator ()(const fmatvec::Vec & vector);
00099       virtual bool isBetter(const fmatvec::Vec & vector, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT));
00100       virtual void clear();
00101       /*END - INHERITED INTERFACE*/
00102 
00103       const std::vector<double> & getResults() {
00104         return criteriaResults;
00105       }
00106 
00107       void setTolerance(double tol) {
00108         tolerance = tol;
00109       }
00110 
00111     protected:
00112       /*INHERITED INTERFACE*/
00113       virtual double computeResults(const fmatvec::Vec & x, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT)) = 0;
00114       /********************/
00115 
00119       double tolerance;
00120 
00124       std::vector<double> criteriaResults;
00125   };
00126 
00130   class LocalCriteriaFunction : public CriteriaFunction {
00131 
00132     public:
00136       LocalCriteriaFunction(const std::map<fmatvec::Index, double> & tolerances_);
00137 
00141       virtual ~LocalCriteriaFunction();
00142 
00143       /* INHERITED INTERFACE */
00144       virtual int operator ()(const fmatvec::Vec & vector);
00145       virtual bool isBetter(const fmatvec::Vec & vector, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT));
00146       virtual void clear();
00147       /*END - INHERITED INTERFACE*/
00148 
00149       virtual void setTolerances(const std::map<fmatvec::Index, double> & tolerances_) {
00150         tolerances = tolerances_;
00151       }
00152 
00153     protected:
00154       virtual std::vector<double> computeResults(const fmatvec::Vec & x, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT)) = 0;
00155 
00156       /*
00157        * \brief saves the tolerance for a specified index sets
00158        */
00159       std::map<fmatvec::Index, double> tolerances;
00160 
00164       std::vector<std::vector<double> > criteriaResults;
00165   };
00166 
00170   class GlobalResidualCriteriaFunction : public GlobalCriteriaFunction {
00171 
00172     public:
00176       GlobalResidualCriteriaFunction(const double & tolerance_ = 1e-10);
00177 
00181       virtual ~GlobalResidualCriteriaFunction();
00182 
00183     protected:
00184       /* INHERITED INTERFACE */
00185       virtual double computeResults(const fmatvec::Vec & x, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT));
00186       /*END - INHERITED INTERFACE*/
00187   };
00188 
00192   class LocalResidualCriteriaFunction : public LocalCriteriaFunction {
00193 
00194     public:
00198       LocalResidualCriteriaFunction(const std::map<fmatvec::Index, double> & tolerances_);
00199 
00203       virtual ~LocalResidualCriteriaFunction();
00204 
00205     protected:
00206 
00207       virtual std::vector<double> computeResults(const fmatvec::Vec & x, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT));
00208   };
00209 
00213   class GlobalShiftCriteriaFunction : public GlobalCriteriaFunction {
00214     public:
00218       GlobalShiftCriteriaFunction(const double & tolerance_ = 1e-10);
00219 
00223       virtual ~GlobalShiftCriteriaFunction();
00224 
00225       virtual fmatvec::Vec getLastPoint() {
00226         return lastPoint;
00227       }
00228 
00229     protected:
00230       /* INHERITED INTERFACE */
00231       virtual double computeResults(const fmatvec::Vec & x, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT));
00232       /*END - INHERITED INTERFACE*/
00233 
00237       fmatvec::Vec lastPoint;
00238 
00239   };
00240 
00244   class LocalShiftCriteriaFunction : public LocalCriteriaFunction {
00245 
00246     public:
00250       LocalShiftCriteriaFunction(const std::map<fmatvec::Index, double> & tolerances_);
00251 
00255       virtual ~LocalShiftCriteriaFunction();
00256 
00257       virtual fmatvec::Vec getLastPoint() {
00258         return lastPoint;
00259       }
00260 
00261     protected:
00262       virtual std::vector<double> computeResults(const fmatvec::Vec & x, const fmatvec::Vec & fVal = fmatvec::Vec(0,fmatvec::NONINIT));
00263 
00267       fmatvec::Vec lastPoint;
00268   };
00269 
00270   inline CriteriaFunction::~CriteriaFunction(){}
00271   inline GlobalCriteriaFunction::~GlobalCriteriaFunction(){}
00272   inline LocalCriteriaFunction::~LocalCriteriaFunction(){}
00273   inline GlobalResidualCriteriaFunction::~GlobalResidualCriteriaFunction() {}
00274   inline GlobalShiftCriteriaFunction::~GlobalShiftCriteriaFunction() {}
00275   inline LocalResidualCriteriaFunction::~LocalResidualCriteriaFunction() {}
00276   inline LocalShiftCriteriaFunction::~LocalShiftCriteriaFunction() {}
00277 }
00278 #endif //NUMERICSCRITERIAFUNCTIONS_H_