Structural deformable models
Searcher.h
Go to the documentation of this file.
1 #ifndef _SEARCHER_H_
2 #define _SEARCHER_H_
3 #include <iostream>
4 #include <vector>
5 #include <list>
6 #include <map>
7 #include <functional>
8 #include <algorithm>
9 #include "ExpMap.h"
10 #include "Model.h"
11 
12 class ParseFile;
13 
16 class Searcher {
17  typedef std::list<Model*> MBin;
18  typedef const std::list<Model*> CMBin;
19 public:
20  Searcher();
21  Searcher(const Searcher& rhs);
22  ~Searcher();
23 
24  Searcher& operator=(const Searcher& rhs);
25  bool step(float dt);
26  void evolve();
27  void startSearch(bool dostart=true);
28  bool searchDone() const { return m_Mode == MODE_DONE; }
29  template<class Iter> inline unsigned int getWinners( Iter iter ) const;
30  std::map<dword,Winner>& getWinList() { return m_Winners; }
31  const std::map<dword,Winner>& getWinList() const { return m_Winners; }
32  float getBestQOF() const;
33  float getLeastQOF() const;
34  float relativeQOF(float qof) const;
35  const Winner* getWinner(dword id) const;
36  void setExpectationMap(const ExpectationMap &em);
38  const ExpectationMap& getExpectationMap() const { return m_EMap; }
39  void clear();
40  void draw() const;
41  bool triggerTest(int mx, int my, int what);
42  operator bool() const { return (bool)m_EMap; }
43  const Model& getRepresentative() const { return m_Representative; }
44  void reattachSensors();
45  float getGeneration(const Model* model) const;
46  void setShapeWeight(float shapeweight) { m_ShapeWeight = shapeweight; }
47  float getShapeWeight() const { return m_ShapeWeight; }
48 protected:
49  std::map<dword,Winner>& updateWinList();
50  inline int getBindex(int bx, int by, bool &culled=*((bool*)NULL)) const;
51  inline int getBindex(const PropVec &prop,bool& culled=*((bool*)NULL))const;
52  inline int getBindex(const Model &model, bool &culled=*((bool*)NULL))const;
53  //getQualityRange - min,max or avg,stdev?
54  void getQualityRange(float &lqof, float &hqof, float shapeweight=-1) const;
55  void findWinners();
56  bool buildBins(float bindist=0.f);
57  int buildClusters();
58  void selectWinners();
59  void cleanFlags();
60  std::list<Model*>::iterator& remove(std::list<Model*>::iterator &mi);
61  int add(Model* m);
62  Model& mutate(Model& model, float rate=1) const;
63  int distribute(int n=-1, float qth=0, bool count=false);
64 protected:
69  std::list<Model*> m_Population;
70  int m_NPop;
71  std::vector< MBin > m_Bins;
73  float m_BinDist;
74  std::list< MBin > m_Clusters;
76  MBin* m_CurCluster;
77  float m_Time;
81  float m_EvolveT;
82  float m_MutateRate;
83  std::map<dword,Winner> m_Winners;
86 };
87 
88 //-----------------------------------------------------------------------------
89 template<class Iter>
90 inline unsigned int Searcher::getWinners( Iter iter) const
91 {
92  unsigned int nwin=0;
93  for(std::list<Model*>::const_iterator wi=m_Population.begin();
94  wi != m_Population.end(); wi++)
95  {
96  if((*wi)->isWinner()) {
97  *iter++ = *wi;
98  nwin++;
99  }
100  }
101  return nwin;
102 }
103 
104 inline int Searcher::getBindex(int bx, int by, bool &culled) const
105 {
106  bool tb;
107  bool &cf = &culled != NULL ? culled : tb; // redirect to temporary bool?
108  cf = false;
109  if(bx<0) { bx=0; cf = true; }
110  else if(bx>=m_BinsX) { bx=m_BinsX-1; cf = true; }
111  if(by<0) { by=0; cf = true; }
112  else if(by>=m_BinsY) { by=m_BinsY-1; cf = true; }
113  return by*m_BinsX+bx;
114 }
115 
116 inline int Searcher::getBindex(const PropVec &prop, bool &culled) const
117 {
118  return getBindex((int)prop[0], (int)prop[1], culled);
119 }
120 
121 inline int Searcher::getBindex(const Model &model, bool &culled) const
122 {
123  //PropVec prop=model.getProperties();
124  PropVec prop(0.);
125  setPropPos(prop, model.getCenter());
126  return getBindex(prop, culled);
127 }
128 
129 inline float Searcher::getBestQOF() const
130 {
131  return m_Population.empty()? 1.f
132  :(*m_Population.begin())->getQualityOfFit();
133 }
134 
135 inline float Searcher::getLeastQOF() const
136 {
137  return m_Population.empty()? 0.f
138  :(*m_Population.rbegin())->getQualityOfFit();
139 }
140 
141 inline float Searcher::relativeQOF(float qof) const
142 {
143  const float mq = getBestQOF(), lq = getLeastQOF();
144  float range = mq-lq; if(range <= 0.f) range = 1.f;
145  return (qof-lq)/range;
146 }
147 
148 //----------------------------------------------------------------------------
150 public:
151  SearcherParams();
152  std::ostream& write(std::ostream& os, bool showcomment=false) const;
153  bool read(ParseFile& is);
154  friend std::ostream& operator<<(std::ostream& os,
155  const SearcherParams& rhs)
156  { return rhs.write(os); }
157  friend std::istream& operator>>(std::istream& is, SearcherParams& rhs)
158  { ParseFile pf(is); rhs.read(pf); return is; }
160 public:
165  float m_ClusterTH;
170  float m_RelBinDist;
171 
173  float m_MutateRate;
174  float m_MutateHL;
176  float m_ScaleStd;
178  float m_MutatePDir;
179  float m_MutatePPos;
184 
186 
187  float m_RatingTH;
188  float m_PCTH;
190  int m_PDist;
192 };
193 
195 
196 #endif
dword m_MutateSpawns
number of spawns when mutating a shape
Definition: Searcher.h:177
float m_RelBinDist
distance of bins relative to representative radius
Definition: Searcher.h:170
float m_MutatePropRate
rate of adaption of proportion
Definition: Searcher.h:183
ExpectationMap & getExpectationMap()
Definition: Searcher.h:37
dword m_Mode
Definition: Searcher.h:80
float m_ClusterMeltDist
maximum Hausdorff distance in mm for melting two shapes
Definition: Searcher.h:169
float m_ShapeWeight
influence of shape deformation on QOF
Definition: Searcher.h:185
void startSearch(bool dostart=true)
Definition: Searcher.cpp:718
#define NULL
Definition: simpletypes.h:9
std::vector< MBin > m_Bins
Definition: Searcher.h:71
float getGeneration(const Model *model) const
Definition: Searcher.cpp:303
bool buildBins(float bindist=0.f)
Definition: Searcher.cpp:361
void setExpectationMap(const ExpectationMap &em)
Definition: Searcher.cpp:346
dword m_NextWinID
Definition: Searcher.h:84
std::map< dword, Winner > & getWinList()
Definition: Searcher.h:30
float m_MutatePProp
probability of mutation of proportion
Definition: Searcher.h:182
Point m_PosMax
Definition: Searcher.h:78
bool triggerTest(int mx, int my, int what)
Definition: Searcher.cpp:728
void setShapeWeight(float shapeweight)
Definition: Searcher.h:46
PropVec m_MaxModel
Definition: Searcher.h:67
const Model & getRepresentative() const
Definition: Searcher.h:43
float m_PCTH
threshold for principle components
Definition: Searcher.h:188
PropVec m_MinModel
Definition: Searcher.h:67
float m_BinDist
Definition: Searcher.h:73
void getQualityRange(float &lqof, float &hqof, float shapeweight=-1) const
Definition: Searcher.cpp:649
const ExpectationMap & getExpectationMap() const
Definition: Searcher.h:38
float m_MutateRate
initial mutation rate
Definition: Searcher.h:173
Point m_PosRange
Definition: Searcher.h:78
float m_ScaleStd
scale stdev of link
Definition: Searcher.h:176
bool read(ParseFile &is)
Definition: Searcher.cpp:924
int m_BinsX
Definition: Searcher.h:72
float m_ShapeWeight
Definition: Searcher.h:85
float m_ClusterDScale
maximum relative difference in scale to cluster representative
Definition: Searcher.h:167
float m_ClusterTH
least quality of cluster relative to the best
Definition: Searcher.h:165
const std::map< dword, Winner > & getWinList() const
Definition: Searcher.h:31
dword m_NSpawns
number of new spawned shapes at each cycle
Definition: Searcher.h:161
float m_NSpawnsTHRed
threshold reduction factor for new spawns
Definition: Searcher.h:162
static SearcherParams global
Definition: Searcher.h:159
void evolve()
Definition: Searcher.cpp:163
void reattachSensors()
Definition: Searcher.cpp:616
float m_Time
Definition: Searcher.h:77
PropVec m_StdWinner
Definition: Searcher.h:66
int m_PDist
type of probability distr. to represent structural relationships
Definition: Searcher.h:190
dword m_NClusters
number of clusters (winners) determined at each cycle
Definition: Searcher.h:164
const Point getCenter() const
Definition: Model.cpp:852
int m_MaxShoot
maximum number of spawns from an exp map
Definition: Searcher.h:191
std::ostream & write(std::ostream &os, bool showcomment=false) const
Definition: Searcher.cpp:832
~Searcher()
Definition: Searcher.cpp:90
friend std::ostream & operator<<(std::ostream &os, const SearcherParams &rhs)
Definition: Searcher.h:154
float getBestQOF() const
Definition: Searcher.h:129
float relativeQOF(float qof) const
Definition: Searcher.h:141
bool step(float dt)
Definition: Searcher.cpp:288
PropVec & setPropPos(PropVec &prop, const Point2D &p)
Definition: PropVec.h:16
void clear()
Definition: Searcher.cpp:126
float m_MutateTHRed
threshold reduction factor for mutated spawns
Definition: Searcher.h:175
std::map< dword, Winner > m_Winners
Definition: Searcher.h:83
void draw() const
Definition: Searcher.cpp:687
int getBindex(int bx, int by, bool &culled=*((bool *) NULL)) const
Definition: Searcher.h:104
void findWinners()
int distribute(int n=-1, float qth=0, bool count=false)
Definition: Searcher.cpp:627
const Winner * getWinner(dword id) const
Definition: Searcher.cpp:668
float getShapeWeight() const
Definition: Searcher.h:47
Definition: ExpMap.h:10
float m_MutateHL
half-life of mutation rate in seconds
Definition: Searcher.h:174
int m_ParaSolver
method for solving ODE, 0-gauss, 1-midpnt
Definition: Searcher.h:189
Definition: Model.h:33
enum Searcher::@5 SMode
float m_EvolveCycle
time in seconds for one evolution cycle
Definition: Searcher.h:172
Searcher()
Definition: Searcher.cpp:76
SearcherParams & getSearchPara()
Definition: Searcher.h:194
std::list< Model * > m_Population
Definition: Searcher.h:69
Point m_PosMin
Definition: Searcher.h:78
unsigned long dword
Definition: simpletypes.h:6
friend std::istream & operator>>(std::istream &is, SearcherParams &rhs)
Definition: Searcher.h:157
MBin * m_CurCluster
Definition: Searcher.h:76
void selectWinners()
Definition: Searcher.cpp:513
Model m_Representative
Definition: Searcher.h:65
int m_NClusters
Definition: Searcher.h:75
int buildClusters()
Definition: Searcher.cpp:419
ExpectationMap m_EMap
Definition: Searcher.h:68
int m_BinsY
Definition: Searcher.h:72
float getLeastQOF() const
Definition: Searcher.h:135
std::list< MBin > m_Clusters
Definition: Searcher.h:74
int add(Model *m)
Definition: Searcher.cpp:143
float m_EvolveT
Definition: Searcher.h:81
int m_NPop
Definition: Searcher.h:70
float m_RatingTH
least allowed rating for a winner
Definition: Searcher.h:187
unsigned int getWinners(Iter iter) const
Definition: Searcher.h:90
bool searchDone() const
Definition: Searcher.h:28
void cleanFlags()
Definition: Searcher.cpp:546
float m_ClusterDDir
maximum difference in direction in $$
Definition: Searcher.h:168
float m_ClusterDPos
maximum distance of shapes in a cluster (relative to shape radius)
Definition: Searcher.h:166
Searcher & operator=(const Searcher &rhs)
Definition: Searcher.cpp:95
dword m_MaxPop
maximum population count
Definition: Searcher.h:163
Definition: Point.h:16
PropVec m_AvgWinner
Definition: Searcher.h:66
float m_MutateRate
Definition: Searcher.h:82
Model & mutate(Model &model, float rate=1) const
Definition: Searcher.cpp:307
float m_MutateNoiseRate
distortion ratio for node positions
Definition: Searcher.h:181
float m_MutatePDir
probability of mutation of orientation
Definition: Searcher.h:178
std::map< dword, Winner > & updateWinList()
Definition: Searcher.cpp:675
float m_MutatePNoise
probability of mutation of node positions
Definition: Searcher.h:180
float m_MutatePPos
probability of mutation of position
Definition: Searcher.h:179