Unit.h

Go to the documentation of this file.
00001 
00024 #ifndef _UNIT_H
00025 #define _UNIT_H
00026 
00027 #include "../../FeatureContainer/FeatureContainer.h"
00028 #include "../../util/splaytree.h"
00029 
00030 class Unit;
00031 
00032 #if NO_OPTIMIZATIONS
00033 # undef OPTIMIZATIONS_SPLAYTREE
00034 # undef OPTIMIZATIONS_CACHE_SIMILARITY
00035 # undef OPTIMIZATIONS_SPLIT_MERGE
00036 
00037 # define OPTIMIZATIONS_SPLAYTREE 0
00038 # define OPTIMIZATIONS_CACHE_SIMILARITY 0
00039 # define OPTIMIZATIONS_SPLIT_MERGE 0
00040 #endif
00041 
00045 template <class comparator>
00046 class unittree : public splaytree<comparator>
00047 { public: unittree() : splaytree<comparator>(NULL) {} };
00048 
00050 typedef map<Unit*, unsigned long> edge_map;
00051 
00053 
00055 #if OPTIMIZATIONS_SPLAYTREE
00056 typedef unittree<Unit*> unit_list;
00057 #else
00058 typedef list<Unit*> unit_list;
00059 #endif
00060 
00061 
00065 class Unit
00066 {
00067         private:
00069 
00071                 // this is ugly, ugly, ugly
00072                 unit_list const*const all_units;
00073 
00074                 static list<unsigned long> used_meta_ids;       // remember the meta ids which have already been used (just to speed things up a bit)
00075                 unsigned long meta_id;
00076 
00077                 featurevector position;
00078                 edge_map edge;
00079                                                                 // the hash map keeps track of all edges and their age
00080                 double error_short;                             // accumulated short-term error
00081                 double error_long;                              // accumulated long-term error
00082                 double error_insert;                            // the inherited error at the time of insertion of this node
00083                 double insertion_threshold;
00084                 double youth;
00085                 double radius;
00086 
00087                 mutable bool traversing;
00088                 mutable double helper_local_similarity;         // this is a cache for performance reasons
00089 
00090                 void insertEdge(Unit* u);
00091                 void eraseEdge(Unit* u);
00092 
00093                 static unsigned long getNextMetaId();           // serach for the next unused meta id
00094 
00095                 void movePositionTowards(const featurevector *sample, bool isWinner);
00096                 void setMetaId(unsigned long id);
00097                 void updateMetaId();
00098                 bool isConnectedHelper(Unit* u) const;          // used by isConnected
00099                 bool isConnected(Unit* u) const;                // this should be used to check if a unit is connected to another one
00100                 void updateRadius();
00102 
00103         public:
00105                 Unit(unit_list const*const units, const featurevector pos);
00107                 Unit(unit_list const*const units, const featurevector pos, unsigned long id);
00109                 virtual ~Unit();
00110 
00112                 Unit *splitUnit();
00113 
00115                 double getShortTermError() const;
00117                 void setShortTermError(double error);
00119                 double getLongTermError() const;
00121                 void setLongTermError(double error);
00122                 //void decreaseErrors();                                // decreases both the short and the long term errors according to their time constants (called for the winning unit)
00124                 void updateErrors(const featurevector *sample);
00125 
00127                 double getInsertionError() const;
00129                 void setInsertionError(double error);
00130 
00132                 double getInsertionThreshold() const;
00134                 void setInsertionThreshold(double threshold);
00136                 void updateInsertionThreshold();
00137 
00139                 double getYouth() const;
00141                 void setYouth(double youth);
00143                 void decreaseYouth();                           // decreases the youth (called for the winning unit)
00144 
00146                 void decreaseInsertionThreshold();              // decreases the insertion threshold (called for the winning neuron)
00147 
00149                 const featurevector* getPosition() const;
00151                 double getDistance(const featurevector *sample) const; // returns the distance to the given sample, normlized to [0; 1]
00153                 void moveTowards(const featurevector *sample);
00154 
00156                 double getQualityLearning() const;              // compute the quality measure for learning at this unit (B^L)
00158                 double getQualityInsertion() const;             // compute the quality measure for insertion (B^I)
00160                 double getLearningRate(bool isWinner) const;    // compute the learning rate (called for the winning neuron and its neighbors)
00161 
00163                 void calculateLocalSimilarity() const;          // compute the local similarity of input weights (the average difference of this unit's position to allits neighbors)
00164                                                                                                                 // this has to be called whenever a node (or one of its neighbors is moved) !
00165 
00167                 double getLocalSimilarity() const;              // return the pre-computed local similarity of input weights (the average difference of this unit's position to all its neighbors)
00168 
00170                 static size_t getNumMetaIds();
00172                 static unsigned long getMaxMetaId();
00174                 static void clearMetaIds();             // clear the list - only used for unserialization
00176                 static void reserveMetaId(unsigned long meta_id);       // reserve this meta id
00178                 static void releaseMetaId(unsigned long meta_id); // release a meta id so that it can be used again
00180                 unsigned long getMetaId() const;
00181 
00183                 void ageEdge(Unit* u);                          // increments the age of a single adge
00185                 void ageEdges();                                // increments the age of all connected edges by one (and also for the edges to this node, i.e. symmetrically)
00187                 void addEdge(Unit* u);
00189                 void removeEdge(Unit* u);
00191                 void setEdgeAge(Unit* u, unsigned long age);            // sets the edge age (symmetrically), only used for unserialize in GNG
00193                 edge_map::size_type getEdgeCount() const;       // returns the number of edges, i.e. the number of neighbors
00195                 edge_map* getEdges();                   // return the edge map
00196 
00198                 string serialize() const;
00200                 size_t unserialize(const featurevector& features, const string data);
00201 
00203                 bool operator < (Unit *u);
00205                 bool operator > (Unit *u) { return u < this; }
00207                 bool operator != (Unit *u) { return (this < u) || (this > u); }
00209                 bool operator == (Unit *u) { return !(this != u); }
00210 };
00211 
00212 #endif /* _UNIT_H */

Generated on Mon Jun 5 10:20:42 2006 for Intelligence.kdevelop by  doxygen 1.4.6