Structural deformable models
ExpMap.cpp
Go to the documentation of this file.
1 #include "ExpMap.h"
2 #include "DMatrixLinAlg.h"
3 
5  if(&rhs == this) return *this;
6  EMDRect::operator=(rhs);
8  clear();
9  add(rhs.m_Distributions);
10  return *this;
11 }
12 
14 {
15  return (EMDistribution*) new ExpectationMap(*this);
16 }
17 
19 {
20  m_Representative = model;
21 }
22 
24 {
25  if(!ed) return;
26  dword wid = ed->m_Creator.m_WinnerID;
27  if(wid == Winner::WID_EMPTY) {
28  if(!m_Distributions.empty()) wid = m_Distributions.rbegin()->first+1;
30  }
31  add(ed,wid);
32 }
33 
35 {
36  if(!ed) return;
37  erase(wid);
38  m_Distributions[wid] = ed;
40 }
41 
42 void ExpectationMap::add(const EDistributions &distlist)
43 {
44  for(EDistributions::const_iterator dl=distlist.begin();
45  dl != distlist.end(); dl++)
46  {
47  add(dl->second->copy());
48  }
49 }
50 
52 {
53  EDistributions::iterator ed = m_Distributions.find(wid);
54  if(ed == m_Distributions.end()) return false;
55  if(ed->second) delete ed->second;
56  m_Distributions.erase(ed);
57  return true;
58 }
59 
61 {
62  return m_Distributions.find(wid) != m_Distributions.end();
63 }
64 
66 {
67  EDistributions::iterator ed = m_Distributions.find(wid);
68  if(ed == m_Distributions.end()) return NULL;
69  else return ed->second;
70 }
71 
73 {
74  EDistributions::const_iterator ed = m_Distributions.find(wid);
75  if(ed == m_Distributions.end()) return NULL;
76  else return ed->second;
77 }
78 
80 {
81  const EMDistribution* ed = getEDist(wid);
82  if(!ed) return 0;
83  else return ed->getShootCount();
84 }
85 
87 {
88  if(m_SortDist.empty() || m_Integral==0.0) {
89  if(!m_Distributions.empty()) {
90  ((ExpectationMap*)this)->updateIntegral();
91  if(m_SortDist.empty() || m_Integral==0.0)
92  return getIdentityPropTF();
93  } else return getIdentityPropTF();
94  }
95  float rint = frand(m_Integral);
96  std::map<float, EMDistribution*>::const_iterator
97  ed = m_SortDist.lower_bound(rint);
98  if(ed == m_SortDist.end()) ed = m_SortDist.begin();
99  ed->second->m_ShootCount++;
100  PropVec prop = ed->second->getPropVec();
101  setPropDir(prop, mapAngle2PI(getPropDir(prop),
102  getPropDir(m_LB)));
103  return prop.clamp(m_LB,m_UB);
104  //return m_Distributions.begin()->getPropVec(); // is this fair?
105 }
106 
107 float ExpectationMap::ratePropVec(const PropVec& prop, Winner* winner) const
108 {
109  if(m_Distributions.empty()) return 0;
110  PropVec pv(prop);
112  if(EMDRect::ratePropVec(pv) == 0.0) {
113  return 0; //clamped?
114  }
115 
116  float rate = 0.f;
117  if(winner) {
118  winner->clearRatings();
119  for(EDistributions::const_iterator dl=m_Distributions.begin();
120  dl != m_Distributions.end(); dl++)
121  {
122  float tr = dl->second->ratePropVec(prop);
123  if(dl->second->m_Creator.m_WinnerID) {
124  const std::string& creatorname = dl->second->m_Creator.m_StructName;
125  dword creatorid = dl->second->m_Creator.m_WinnerID;
126  winner->rateBy(creatorname, creatorid, tr);
127  } else if(winner->m_BestRating < tr) winner->m_BestRating = tr;
128  }
129  rate = winner->m_BestRating;
130  } else {
131  for(EDistributions::const_iterator dl=m_Distributions.begin();
132  dl != m_Distributions.end(); dl++)
133  {
134  float tr = dl->second->ratePropVec(prop);
135  if(rate<tr) rate = tr;
136  }
137  }
138  return rate;
139 }
140 
141 void ExpectationMap::clear(bool oldonly)
142 {
143  EDistributions::iterator dl=m_Distributions.begin();
144  while(dl != m_Distributions.end())
145  {
146  EDistributions::iterator cd = dl++;
147  if(cd->second)
148  {
149  if(!oldonly || cd->second->hasFlags(EMDistribution::ED_OLD)) {
150  delete cd->second;
151  if(oldonly) m_Distributions.erase(cd);
152  }
153  } else if(oldonly) m_Distributions.erase(cd);
154  }
155  if(!oldonly) m_Distributions.clear();
156  if(oldonly) updateIntegral();
157 }
158 
160  for(EDistributions::iterator dl=m_Distributions.begin();
161  dl != m_Distributions.end(); dl++)
162  {
163  if(dl->second) dl->second->setFlags(EMDistribution::ED_OLD);
164  }
165 }
166 
168 {
169  m_Integral = 0.f;
170  m_SortDist.clear();
171  for(EDistributions::const_iterator dl=m_Distributions.begin();
172  dl != m_Distributions.end(); dl++)
173  {
174  if(dl->second) {
175  if(dl->second->m_Integral > 0.00001) {
176  m_Integral += dl->second->m_Integral;
177  m_SortDist[m_Integral] = dl->second;
178  }
179  }
180  }
181  return m_Integral;
182 }
183 
185 {
186  Model *model = new Model(m_Representative);
187  PropVec v = getPropVec();
188  model->adaptProperties(v);
189  return model;
190 }
191 
192 #define MIN_SCALE 0.0001
194 {
195  float la = mapAngle2PI(getPropDir(lb));
196  float ua = mapAngle2PI(getPropDir(ub), la);
197  if(ua-la < 0.00001) ua += 2*M_PI;
198  setPropDir(lb, la); setPropDir(ub, ua);
199  PropVec cub(ub); ub.clampLB(lb); lb.clampUB(cub); //conditional swap
200  float ls = getPropScale(lb);
201  if(ls < MIN_SCALE) setPropScale(lb, MIN_SCALE);
202 }
203 
204 //----------------------------------------------------------------------------
206 {
207  PropVec pv;
208  for(unsigned int i=0; i<PropVec::size(); i++)
209  pv[i] = frand(m_UB[i]-m_LB[i])+m_LB[i];
210  return pv;
211 }
212 
213 float EMDRect::ratePropVec(const PropVec& prop, Winner* winner) const
214 {
215  PropVec clampvec(prop);
216  clampvec.clamp(m_LB, m_UB);
217  if((clampvec-prop).norm2() < 0.0001) return 1;
218  else return 0;
219 }
220 
221 //----------------------------------------------------------------------------
223 {
224  PropVec pv;
225  for(unsigned int i=0; i<PropVec::size(); i++)
226  pv[i] = fgauss01()*m_Stdev[i] + m_Avg[i];
227  return pv;
228 }
229 
230 float EMDGauss::ratePropVec(const PropVec& prop, Winner* winner) const
231 {
232  return gauss2(((prop-m_Avg)/=m_StdevRate).norm2(),1)*M_SQRT2PI;
233 }
234 
236 {
237  m_Stdev = stdev;
238  for(dword i = 0; i<stdev.size(); i++) {
239  if(stdev[i]>0.0001) m_StdevRate[i] = stdev[i];
240  else m_StdevRate[i] = std::numeric_limits<float>::max();
241  }
242 }
243 
244 //----------------------------------------------------------------------------
246 {
247  PropVec propPV = m_InputEMD->getPropVec();
248  hgPropTF(m_TFMat, propPV);
249  propPV *= m_Stdev;
250  propPV += m_Mean;
251  PropVec pv(m_Origin);
252  fwdPropTF(pv, propPV);
253  return pv;
254 }
255 
256 float EMDXformer::ratePropVec(const PropVec& prop, Winner* winner) const
257 {
258 //inverse transform
259  PropVec tfprop(getPropTF(m_Origin, prop));
260  tfprop -= m_Mean;
261  setPropDir(tfprop, mapAnglePI(getPropDir(tfprop)));
262  tfprop /= m_Stdev;
263  hgPropTF(m_ITFMat, tfprop);
264  return m_InputEMD->ratePropVec(tfprop);
265 }
266 
268 {
269  if(mat.sizeX() == mat.sizeY() &&
270  (mat.sizeX() == PropVec::size() ||
271  mat.sizeX() == PropVec::size()+1)) {
272  m_TFMat = mat;
273  m_ITFMat = dmutil::inverse(m_TFMat);
274  } else {
275  m_TFMat.clear();
276  m_ITFMat.clear();
277  }
278 }
279 
280 //----------------------------------------------------------------------------
282 {
283  m_Model = m;
284  m_WinnerID = m ? m->getID() : Winner::WID_EMPTY;
285  m_StructName = m ? m->getName() : "";
286 }
287 
289 {
290  m_BestRating = 0.0f;
291  m_BestRatings.clear();
292  m_Ratings.clear();
293 }
294 
295 float Winner::rateBy(const std::string& creatorname, dword creatorid, float rating)
296 {
297  m_Ratings[creatorname][creatorid] = rating;
298  dword& brid = m_BestRatings[creatorname];
299  if(!brid || m_Ratings[creatorname][brid] < rating) {
300  brid = creatorid;
301  if(m_BestRating < rating) m_BestRating = rating;
302  }
303  return m_BestRating;
304 }
305 
306 const std::pair<dword,float> Winner::getBest(const std::string& sname) const
307 {
308  const std::map<dword,float>& rl = getRatingList(sname);
309  dword bid = getBestRatingID(sname);
310  return *rl.find(bid);
311 }
312 
313 float Winner::getConnection(const std::string& sname, dword wid) const
314 {
315  if(!hasConnection(sname)) return -1.f;
316  const std::map<dword,float>& rl = getRatingList(sname);
317  std::map<dword,float>::const_iterator rt = rl.find(wid);
318  if(rt != rl.end()) return rt->second;
319  else return -1.f;
320 }
321 
323 {
324  m_BestRating = 0.f;
325  m_BestRatings.clear();
326  for(Ratings::const_iterator refstruct = m_Ratings.begin();
327  refstruct != m_Ratings.end(); refstruct++)
328  {
329  dword& brid = m_BestRatings[refstruct->first];
330  float bestrating = 0.f; //m_Ratings[refstruct->first][brid];
331  for(std::map<dword,float>::const_iterator wr= refstruct->second.begin();
332  wr != refstruct->second.end(); wr++)
333  { // for each rating wr
334  if(wr->second > bestrating) {
335  bestrating = wr->second;
336  brid = wr->first;
337  }
338  }
339  if(m_BestRating < bestrating) m_BestRating = bestrating;
340  }
341  return m_BestRating;
342 }
float m_Integral
Definition: ExpMap.h:67
#define NULL
Definition: simpletypes.h:9
void clearRatings()
Definition: ExpMap.cpp:288
DMatrix< T > inverse(const DMatrix< T > &dmat)
Definition: DMatrixLinAlg.h:67
Model m_Representative
Definition: ExpMap.h:175
#define frand(max)
Definition: mathutil.h:48
ExpectationMap & operator=(const ExpectationMap &rhs)
Definition: ExpMap.cpp:4
float ratePropVec(const PropVec &prop, Winner *winner=NULL) const
Definition: ExpMap.cpp:230
void clear(bool oldonly=false)
Definition: ExpMap.cpp:141
EDistributions m_Distributions
Definition: ExpMap.h:176
Model * generateInstance() const
Definition: ExpMap.cpp:184
EMDistribution * getEDist(dword wid)
Definition: ExpMap.cpp:65
float gauss2(float x2, float stdev)
Definition: mathutil.h:71
const std::pair< dword, float > getBest(const std::string &sname) const
Definition: ExpMap.cpp:306
static void correctLBUB(PropVec &lb, PropVec &ub)
Definition: ExpMap.cpp:193
dword getShootCount() const
Definition: ExpMap.h:62
PropTF getIdentityPropTF()
Definition: PropVec.h:81
VT & clamp(const VT &lb, const VT &ub)
Definition: VVector.h:253
float getPropScale(const PropVec &prop)
Definition: PropVec.h:19
float ratePropVec(const PropVec &prop, Winner *winner=NULL) const
Definition: ExpMap.cpp:107
virtual EMDistribution * copy() const
Definition: ExpMap.cpp:13
PropVec m_UB
Definition: ExpMap.h:90
void add(EMDistribution *ed)
Definition: ExpMap.cpp:23
PropTF getPropTF(const PropVec &from, const PropVec &to)
Definition: PropVec.h:37
float mapAngle2PI(float a)
Definition: mathutil.h:97
PropVec getPropVec() const
Definition: ExpMap.cpp:205
float ratePropVec(const PropVec &prop, Winner *winner=NULL) const
Definition: ExpMap.cpp:256
void setStdev(const PropVec &stdev)
Definition: ExpMap.cpp:235
PropVec & hgPropTF(const DMatrixf &tfmat, PropVec &prop)
Definition: ExpMap.h:182
void setModel(Model *m)
Definition: ExpMap.cpp:281
PropVec m_LB
Definition: ExpMap.h:90
std::map< float, EMDistribution * > m_SortDist
Definition: ExpMap.h:177
PropVec & setPropDir(PropVec &prop, float dir)
Definition: PropVec.h:28
bool erase(dword wid)
Definition: ExpMap.cpp:51
VT & clampUB(const VT &ub)
Definition: VVector.h:247
float m_BestRating
Definition: ExpMap.h:39
float getPropDir(const PropVec &prop)
Definition: PropVec.h:25
bool hasDist(dword wid) const
Definition: ExpMap.cpp:60
float getConnection(const std::string &sname, dword wid) const
Definition: ExpMap.cpp:313
dword sizeX() const
Definition: DMatrix.h:42
#define M_PI
Definition: mathutil.h:9
Definition: ExpMap.h:10
Definition: Model.h:33
ExpectationMap()
Definition: ExpMap.h:146
dword getID() const
Definition: Model.h:83
PropVec getPropVec() const
Definition: ExpMap.cpp:222
float updateBestRating()
Definition: ExpMap.cpp:322
unsigned long dword
Definition: simpletypes.h:6
float updateIntegral()
Definition: ExpMap.cpp:167
void adaptProperties(const PropVec &prop)
Definition: Model.cpp:1249
PropVec & fwdPropTF(PropVec &from, const PropTF &tf)
Definition: PropVec.h:51
VT & clampLB(const VT &lb)
Definition: VVector.h:241
float rateBy(const std::string &creatorname, dword creatorid, float rating)
Definition: ExpMap.cpp:295
float fgauss01()
Definition: mathutil.h:53
virtual PropVec getPropVec() const
Definition: ExpMap.cpp:86
void markAllOld()
Definition: ExpMap.cpp:159
#define MIN_SCALE
Definition: ExpMap.cpp:192
dword m_WinnerID
Definition: ExpMap.h:37
#define M_SQRT2PI
Definition: mathutil.h:18
DMatrix< T > stdev(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:106
dword sizeY() const
Definition: DMatrix.h:43
std::map< dword, EMDistribution * > EDistributions
Definition: ExpMap.h:144
float mapAnglePI(float a)
Definition: mathutil.h:107
const std::string & getName() const
Definition: Model.h:81
Winner m_Creator
Definition: ExpMap.h:68
PropVec & setPropScale(PropVec &prop, float pscale)
Definition: PropVec.h:22
void setTFMat(const DMatrixf &mat)
Definition: ExpMap.cpp:267
PropVec getPropVec() const
Definition: ExpMap.cpp:245
static unsigned int size()
Definition: VVector.h:41
void setRepresentative(const Model &model)
Definition: ExpMap.cpp:18
float ratePropVec(const PropVec &prop, Winner *winner=NULL) const
Definition: ExpMap.cpp:213