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
00072 unit_list const*const all_units;
00073
00074 static list<unsigned long> used_meta_ids;
00075 unsigned long meta_id;
00076
00077 featurevector position;
00078 edge_map edge;
00079
00080 double error_short;
00081 double error_long;
00082 double error_insert;
00083 double insertion_threshold;
00084 double youth;
00085 double radius;
00086
00087 mutable bool traversing;
00088 mutable double helper_local_similarity;
00089
00090 void insertEdge(Unit* u);
00091 void eraseEdge(Unit* u);
00092
00093 static unsigned long getNextMetaId();
00094
00095 void movePositionTowards(const featurevector *sample, bool isWinner);
00096 void setMetaId(unsigned long id);
00097 void updateMetaId();
00098 bool isConnectedHelper(Unit* u) const;
00099 bool isConnected(Unit* u) const;
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
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();
00144
00146 void decreaseInsertionThreshold();
00147
00149 const featurevector* getPosition() const;
00151 double getDistance(const featurevector *sample) const;
00153 void moveTowards(const featurevector *sample);
00154
00156 double getQualityLearning() const;
00158 double getQualityInsertion() const;
00160 double getLearningRate(bool isWinner) const;
00161
00163 void calculateLocalSimilarity() const;
00164
00165
00167 double getLocalSimilarity() const;
00168
00170 static size_t getNumMetaIds();
00172 static unsigned long getMaxMetaId();
00174 static void clearMetaIds();
00176 static void reserveMetaId(unsigned long meta_id);
00178 static void releaseMetaId(unsigned long meta_id);
00180 unsigned long getMetaId() const;
00181
00183 void ageEdge(Unit* u);
00185 void ageEdges();
00187 void addEdge(Unit* u);
00189 void removeEdge(Unit* u);
00191 void setEdgeAge(Unit* u, unsigned long age);
00193 edge_map::size_type getEdgeCount() const;
00195 edge_map* getEdges();
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