Hypothesis.cpp
Go to the documentation of this file.
1 /*
2  * Hypothesis.cpp
3  *
4  * Created on: March, 2011
5  * Author: Jos Elfring, Sjoerd van den Dries
6  * Affiliation: Eindhoven University of Technology
7  */
8
10
12 #include "wire/logic/Assignment.h"
16
17 using namespace std;
18
19 namespace mhf {
20
21 /* ****************************************************************************** */
22 /* * CONSTRUCTOR / DESTRUCTOR * */
23 /* ****************************************************************************** */
24
25 Hypothesis::Hypothesis(const double& timestamp, double probability) : probability_(probability), timestamp_(timestamp),
26  parent_(0), assignment_set_(0), assignment_matrix_(0), height_(0), is_active_leaf_(true) {
27 }
28
30  clear();
31  delete assignment_matrix_;
32 }
33
34 /* ****************************************************************************** */
35 /* * GETTERS * */
36 /* ****************************************************************************** */
37
38
40  return assignment_set_;
41 }
42
44  return best_leaf_;
45 }
46
47
48 list<Hypothesis*>& Hypothesis::getChildHypotheses() {
49  return children_;
50 }
51
52 int Hypothesis::getHeight() const {
53  return height_;
54 }
55
57  return objects_.size();
58 }
59
60 const list<SemanticObject*>& Hypothesis::getObjects() const {
61  return objects_;
62 }
63
64
66  return parent_;
67 }
68
70  return probability_;
71 }
72
73
74 double Hypothesis::getTimestamp() const {
75  return timestamp_;
76 }
77
79  return assignment_matrix_;
80 }
81
82 /* ****************************************************************************** */
83 /* * SETTERS * */
84 /* ****************************************************************************** */
85
86
88  if (assignment_set_) {
89  delete assignment_set_;
90  }
91  assignment_set_ = assignments;
92 }
93
94
96  is_active_leaf_ = false;
97 }
98
99 void Hypothesis::setProbability(double prob) {
100  probability_ = prob;
101 }
102
103 /* ****************************************************************************** */
104 /* * HYPOTHESIS MODIFIERS * */
105 /* ****************************************************************************** */
106
107
109  h->parent_ = this;
110  this->is_active_leaf_ = false;
111  children_.push_back(h);
112 }
113
115  objects_.push_back(obj);
117 }
118
119
121  delete assignment_set_;
122  assignment_set_ = 0;
123 }
124
126  if (!assignment_matrix_) {
128  }
130 }
131
133
134  list<const Assignment*> all_assignments;
135  assignment_set_->getAllAssignments(all_assignments);
136
137  // apply cases without target
138  for(list<const Assignment*>::iterator it_ass = all_assignments.begin(); it_ass != all_assignments.end();) {
139  const Assignment* ass = *it_ass;
140
141  if (ass->getType() == Assignment::CLUTTER) {
142  // remove assignment from list
143  it_ass = all_assignments.erase(it_ass);
144  } else if (ass->getType() == Assignment::NEW) {
145  SemanticObject* new_obj = ass->getNewObject();
147
148  // remove assignment from list
149  it_ass = all_assignments.erase(it_ass);
150  } else {
151  ++it_ass;
152  }
153  }
154
155  // apply cases with target
156  const list<SemanticObject*>& hyp_parent_objs = parent_->getObjects();
157  for (list<SemanticObject*>::const_iterator it_obj = hyp_parent_objs.begin(); it_obj != hyp_parent_objs.end(); ++it_obj) {
158  SemanticObject* obj = *it_obj;
159
160  const Assignment* update_ass = 0;
161
162  for(list<const Assignment*>::iterator it_ass = all_assignments.begin(); it_ass != all_assignments.end();) {
163  const Assignment* ass = *it_ass;
164
165  if (obj == ass->getTarget()) {
166  update_ass = ass;
167  it_ass = all_assignments.erase(it_ass);
168  } else {
169  ++it_ass;
170  }
171  }
172
173  if (update_ass) {
174  SemanticObject* updated_obj = update_ass->getUpdatedObject();
176  } else {
178  }
179  }
180
181  assert(all_assignments.empty());
183 }
184
185 /* ****************************************************************************** */
186 /* * TREE UPDATE METHODS * */
187 /* ****************************************************************************** */
188
190  if (is_active_leaf_) {
191  return probability_;
192  }
193
194  probability_ = 0;
195
196  for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) {
197  probability_ += (*it)->calculateBranchProbabilities();
198  }
199  return probability_;
200 }
201
202
204  height_ = 0;
205  for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) {
206  height_ = max(height_, (*it)->calculateHeigth() + 1);
207  }
208  return height_;
209 }
210
211
213  if (is_active_leaf_) {
214  return this;
215  }
216  best_leaf_ = 0;
217  for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) {
218  Hypothesis* child_best_leaf_ = (*it)->determineBestLeaf();
219  if (best_leaf_ == 0 || (child_best_leaf_ != 0 && child_best_leaf_->getProbability() > best_leaf_->getProbability())) {
220  best_leaf_ = child_best_leaf_;
221  }
222  }
223  return best_leaf_;
224
225 }
226
227
228 void Hypothesis::findActiveLeafs(list<Hypothesis*>& active_leafs) {
229  if (is_active_leaf_) {
230  active_leafs.push_back(this);
231  return;
232  }
233
234  for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) {
235  (*it)->findActiveLeafs(active_leafs);
236  }
237
238 }
239
240 /* ****************************************************************************** */
241 /* * TREE CLEAR / DELETE METHODS * */
242 /* ****************************************************************************** */
243
244
246  // remove this hypothesis from the hypothesis list of all objects contained in this hypothesis
247  for (list<SemanticObject*>::iterator it_obj = objects_.begin(); it_obj != objects_.end(); ++it_obj) {
248  SemanticObject* obj = *it_obj;
249  obj->removeFromHypothesis(this);
250  if (obj->getNumParentHypotheses() == 0) {
252  delete obj;
253  }
254  }
255
256  objects_.clear();
257
258  is_active_leaf_ = false;
259
260  // remove any remaining assignments
261  if (assignment_set_) {
262  delete assignment_set_;
263  }
264 }
265
266
268  if (!is_active_leaf_) {
269  clear();
270  for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) {
271  (*it)->clearInactive();
272  }
273  }
274 }
275
276
278  // delete all child hypotheses
279  for (list<Hypothesis*>::iterator it = children_.begin(); it != children_.end(); ++it) {
280  (*it)->deleteChildren();
281  delete (*it);
282  }
283 }
284
285
287  for (list<Hypothesis*>::iterator it_child = children_.begin(); it_child != children_.end();) {
288  Hypothesis* new_child = (*it_child)->deleteSinglePaths();
289  if (new_child != *it_child) {
290  it_child = children_.erase(it_child);
291  children_.insert(it_child, new_child);
292  } else {
293  ++it_child;
294  }
295  }
296
297  if (children_.size() == 1) {
298  Hypothesis* hyp = *children_.begin();
299  hyp->parent_ = this->parent_;
300  delete this;
301  return hyp;
302  }
303
304  return this;
305 }
306
307 }
AssignmentMatrix * getAssignmentMatrix() const
Definition: Hypothesis.cpp:78
Hypothesis * determineBestLeaf()
Definition: Hypothesis.cpp:212
bool is_active_leaf_
Definition: Hypothesis.h:179
static ObjectStorage & getInstance()
AssignmentMatrix * assignment_matrix_
Definition: Hypothesis.h:173
const Hypothesis * getBestLeaf() const
Definition: Hypothesis.cpp:43
void addPotentialAssignment(const Assignment &ass)
double getTimestamp() const
Definition: Hypothesis.cpp:74
void clearAssignmentSet()
Definition: Hypothesis.cpp:120
double getProbability() const
Definition: Hypothesis.cpp:69
void setAssignments(AssignmentSet *assignments)
Definition: Hypothesis.cpp:87
double timestamp_
Definition: Hypothesis.h:163
SemanticObject * getUpdatedObject() const
Definition: Assignment.cpp:52
virtual ~Hypothesis()
Definition: Hypothesis.cpp:29
void getAllAssignments(std::list< const Assignment * > &assignments) const
iterator(field< oT > &in_M, const bool at_end=false)
Hypothesis * parent_
Definition: Hypothesis.h:167
std::list< Hypothesis * > children_
Definition: Hypothesis.h:169
unsigned int getNumParentHypotheses() const
int getHeight() const
Definition: Hypothesis.cpp:52
AssignmentType getType() const
Definition: Assignment.cpp:24
Definition: Hypothesis.cpp:114
Hypothesis * best_leaf_
Definition: Hypothesis.h:175
const_iterator(const field< oT > &in_M, const bool at_end=false)
const SemanticObject * getTarget() const
Definition: Assignment.cpp:32
void applyAssignments()
Definition: Hypothesis.cpp:132
const AssignmentSet * getAssignments() const
Definition: Hypothesis.cpp:39
arma_warn_unused eT max() const
void removeFromHypothesis(Hypothesis *hyp)
double calculateBranchProbabilities()
Definition: Hypothesis.cpp:189
int getNumObjects() const
Definition: Hypothesis.cpp:56
void deleteChildren()
Definition: Hypothesis.cpp:277
AssignmentSet * assignment_set_
Definition: Hypothesis.h:171
SemanticObject * getNewObject() const
Definition: Assignment.cpp:41
void findActiveLeafs(std::list< Hypothesis * > &active_leafs)
Definition: Hypothesis.cpp:228
double probability_
Definition: Hypothesis.h:161
Hypothesis * deleteSinglePaths()
Definition: Hypothesis.cpp:286
void setProbability(double prob)
Definition: Hypothesis.cpp:99
Definition: Hypothesis.cpp:125
std::list< Hypothesis * > & getChildHypotheses()
Definition: Hypothesis.cpp:48
const std::list< SemanticObject * > & getObjects() const
Definition: Hypothesis.cpp:60
Definition: Hypothesis.cpp:108
const Hypothesis * getParent() const
Definition: Hypothesis.cpp:65
void removeObject(SemanticObject &obj)
void clearInactive()
Definition: Hypothesis.cpp:267