openModeller  Version 1.4.0
environmental_distance.hh
Go to the documentation of this file.
00001 //
00002 // Generic environmental distance algorithm
00003 //
00004 // Description: Generic algorithm based on distances.
00005 //
00006 // Author:      Danilo J. S. Bellini <danilo.estagio@gmail.com>
00007 // Copyright:   See COPYING file that comes with this distribution
00008 // Date:        2006-09-18
00009 //
00010 
00011 #ifndef _DISTANCESH_
00012 #define _DISTANCESH_
00013 
00014 #include <openmodeller/om.hh>
00015 
00016 // Matrix burocracy
00017 #include "matrix.hh"
00018 #ifndef _NO_NAMESPACE
00019 using namespace std;
00020 using namespace math;
00021 #define STD std
00022 #else
00023 #define STD
00024 #endif
00025 #ifndef _NO_EXCEPTION
00026 #  define TRYBEGIN()    try {
00027 #  define CATCHERROR()  } catch (const STD::exception& e) { \
00028                      cerr << "Error: " << e.what() << endl; }
00029 #else
00030 #  define TRYBEGIN()
00031 #  define CATCHERROR()
00032 #endif
00033 typedef matrix<Scalar> Matrix; // Now we have the matrix free for use
00034 
00035 #define FIRST_DISTANCE_TYPE   1
00036 #define AMOUNT_DISTANCE_TYPES 4
00037 
00038 class EnvironmentalDistance : public AlgorithmImpl{
00039 
00040    public: // All methods here are inherited from AlgorithmImpl
00041       EnvironmentalDistance();  // Constructor, don't have init algorithm routines
00042       ~EnvironmentalDistance(); // Destructor
00043 
00044       int initialize();  // Called by oM to initialize the algorithm
00045       int done() const { return _done; } // Tell oM when the algorithm finished its work
00046       Scalar getValue(const Sample& x) const; // Returns the occurence probability
00047 
00048    private:
00049       // Common-use attributes
00050       bool _done;         // Flag to indicate when the work is finished;
00051       int _layer_count;     // Amount of layers used (dimension of environmental space)
00052       int _presence_count;  // Amount of presence points
00053       std::vector<Sample> _presence_points; // Have the presence points data
00054 
00055       // Parameters
00056       Scalar _par_dist;
00057       int _par_dist_type;
00058       int _par_point_qnt;
00059 
00060       bool _use_chisq; // Flag indicating when chi-square probabilities should be returned (used only for Mahalanobis distance!)
00061 
00062       // Algorithm-specific methods and attributes
00063       void _calc_covariance_matrix();
00064       inline Scalar _distance(const Sample& x, const Sample& y) const;
00065       bool _init_distance_type();
00066       Matrix * _cov_matrix;    // Covariance matrix
00067       Matrix * _cov_matrix_inv; // Inverse of covariance matrix
00068       Sample _average_point; // Average of all presence points
00069 
00070       // Alias for the distance types
00071       typedef enum{
00072          EuclideanDistance = FIRST_DISTANCE_TYPE,
00073          MahalanobisDistance,
00074          ManhattanDistance,
00075          ChebyshevDistance,
00076       } DistanceType;
00077 
00091       Scalar _poz(Scalar z) const;
00092 
00106       Scalar _pochisq(Scalar x, int df) const;
00107 
00108    protected:
00109       virtual void _getConfiguration(ConfigurationPtr&) const;
00110       virtual void _setConfiguration(const ConstConfigurationPtr&);
00111 };
00112 
00113 #endif