$search
00001 /* 00002 * Hypothesis.cpp 00003 * 00004 * Created on: March, 2011 00005 * Author: Jos Elfring, Sjoerd van den Dries 00006 * Affiliation: Eindhoven University of Technology 00007 */ 00008 00009 #include "wire/logic/Hypothesis.h" 00010 00011 #include "wire/logic/AssignmentSet.h" 00012 #include "wire/logic/Assignment.h" 00013 #include "wire/logic/AssignmentMatrix.h" 00014 #include "wire/storage/SemanticObject.h" 00015 #include "wire/storage/ObjectStorage.h" 00016 00017 using namespace std; 00018 00019 namespace mhf { 00020 00021 /* ****************************************************************************** */ 00022 /* * CONSTRUCTOR / DESTRUCTOR * */ 00023 /* ****************************************************************************** */ 00024 00025 Hypothesis::Hypothesis(const double& timestamp, double probability) : probability_(probability), timestamp_(timestamp), 00026 parent_(0), assignment_set_(0), assignment_matrix_(0), height_(0), is_active_leaf_(true) { 00027 } 00028 00029 Hypothesis::~Hypothesis() { 00030 clear(); 00031 delete assignment_matrix_; 00032 } 00033 00034 /* ****************************************************************************** */ 00035 /* * GETTERS * */ 00036 /* ****************************************************************************** */ 00037 00038 00039 const AssignmentSet* Hypothesis::getAssignments() const { 00040 return assignment_set_; 00041 } 00042 00043 const Hypothesis* Hypothesis::getBestLeaf() const { 00044 return best_leaf_; 00045 } 00046 00047 00048 list<Hypothesis*>& Hypothesis::getChildHypotheses() { 00049 return children_; 00050 } 00051 00052 int Hypothesis::getHeight() const { 00053 return height_; 00054 } 00055 00056 int Hypothesis::getNumObjects() const { 00057 return objects_.size(); 00058 } 00059 00060 const list<SemanticObject*>& Hypothesis::getObjects() const { 00061 return objects_; 00062 } 00063 00064 00065 const Hypothesis* Hypothesis::getParent() const { 00066 return parent_; 00067 } 00068 00069 double Hypothesis::getProbability() const { 00070 return probability_; 00071 } 00072 00073 00074 double Hypothesis::getTimestamp() const { 00075 return timestamp_; 00076 } 00077 00078 AssignmentMatrix* Hypothesis::getAssignmentMatrix() const { 00079 return assignment_matrix_; 00080 } 00081 00082 /* ****************************************************************************** */ 00083 /* * SETTERS * */ 00084 /* ****************************************************************************** */ 00085 00086 00087 void Hypothesis::setAssignments(AssignmentSet* assignments) { 00088 if (assignment_set_) { 00089 delete assignment_set_; 00090 } 00091 assignment_set_ = assignments; 00092 } 00093 00094 00095 void Hypothesis::setInactive() { 00096 is_active_leaf_ = false; 00097 } 00098 00099 void Hypothesis::setProbability(double prob) { 00100 probability_ = prob; 00101 } 00102 00103 /* ****************************************************************************** */ 00104 /* * HYPOTHESIS MODIFIERS * */ 00105 /* ****************************************************************************** */ 00106 00107 00108 void Hypothesis::addChildHypothesis(Hypothesis* h) { 00109 h->parent_ = this; 00110 this->is_active_leaf_ = false; 00111 children_.push_back(h); 00112 } 00113 00114 void Hypothesis::addObject(SemanticObject* obj) { 00115 objects_.push_back(obj); 00116 obj->addToHypothesis(this); 00117 } 00118 00119 00120 void Hypothesis::clearAssignmentSet() { 00121 delete assignment_set_; 00122 assignment_set_ = 0; 00123 } 00124 00125 void Hypothesis::addPotentialAssignment(Assignment* assignment) { 00126 if (!assignment_matrix_) { 00127 assignment_matrix_ = new AssignmentMatrix(); 00128 } 00129 assignment_matrix_->addPotentialAssignment(*assignment); 00130 } 00131 00132 void Hypothesis::applyAssignments() { 00133 00134 list<const Assignment*> all_assignments; 00135 assignment_set_->getAllAssignments(all_assignments); 00136 00137 // apply cases without target 00138 for(list<const Assignment*>::iterator it_ass = all_assignments.begin(); it_ass != all_assignments.end();) { 00139 const Assignment* ass = *it_ass; 00140 00141 if (ass->getType() == Assignment::CLUTTER) { 00142 // remove assignment from list 00143 it_ass = all_assignments.erase(it_ass); 00144 } else if (ass->getType() == Assignment::NEW) { 00145 SemanticObject* new_obj = ass->getNewObject(); 00146 addObject(new_obj); 00147 00148 // remove assignment from list 00149 it_ass = all_assignments.erase(it_ass); 00150 } else { 00151 ++it_ass; 00152 } 00153 } 00154 00155 // apply cases with target 00156 const list<SemanticObject*>& hyp_parent_objs = parent_->getObjects(); 00157 for (list<SemanticObject*>::const_iterator it_obj = hyp_parent_objs.begin(); it_obj != hyp_parent_objs.end(); ++it_obj) { 00158 SemanticObject* obj = *it_obj; 00159 00160 const Assignment* update_ass = 0; 00161 00162 for(list<const Assignment*>::iterator it_ass = all_assignments.begin(); it_ass != all_assignments.end();) { 00163 const Assignment* ass = *it_ass; 00164 00165 if (obj == ass->getTarget()) { 00166 update_ass = ass; 00167 it_ass = all_assignments.erase(it_ass); 00168 } else { 00169 ++it_ass; 00170 } 00171 } 00172 00173 if (update_ass) { 00174 SemanticObject* updated_obj = update_ass->getUpdatedObject(); 00175 addObject(updated_obj); 00176 } else { 00177 addObject(obj); 00178 } 00179 } 00180 00181 assert(all_assignments.empty()); 00182 clearAssignmentSet(); 00183 } 00184 00185 /* ****************************************************************************** */ 00186 /* * TREE UPDATE METHODS * */ 00187 /* ****************************************************************************** */ 00188 00189 double Hypothesis::calculateBranchProbabilities() { 00190 if (is_active_leaf_) { 00191 return probability_; 00192 } 00193 00194 probability_ = 0; 00195 00196 for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) { 00197 probability_ += (*it)->calculateBranchProbabilities(); 00198 } 00199 return probability_; 00200 } 00201 00202 00203 int Hypothesis::calculateHeigth() { 00204 height_ = 0; 00205 for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) { 00206 height_ = max(height_, (*it)->calculateHeigth() + 1); 00207 } 00208 return height_; 00209 } 00210 00211 00212 Hypothesis* Hypothesis::determineBestLeaf() { 00213 if (is_active_leaf_) { 00214 return this; 00215 } 00216 best_leaf_ = 0; 00217 for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) { 00218 Hypothesis* child_best_leaf_ = (*it)->determineBestLeaf(); 00219 if (best_leaf_ == 0 || (child_best_leaf_ != 0 && child_best_leaf_->getProbability() > best_leaf_->getProbability())) { 00220 best_leaf_ = child_best_leaf_; 00221 } 00222 } 00223 return best_leaf_; 00224 00225 } 00226 00227 00228 void Hypothesis::findActiveLeafs(list<Hypothesis*>& active_leafs) { 00229 if (is_active_leaf_) { 00230 active_leafs.push_back(this); 00231 return; 00232 } 00233 00234 for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) { 00235 (*it)->findActiveLeafs(active_leafs); 00236 } 00237 00238 } 00239 00240 /* ****************************************************************************** */ 00241 /* * TREE CLEAR / DELETE METHODS * */ 00242 /* ****************************************************************************** */ 00243 00244 00245 void Hypothesis::clear() { 00246 // remove this hypothesis from the hypothesis list of all objects contained in this hypothesis 00247 for (list<SemanticObject*>::iterator it_obj = objects_.begin(); it_obj != objects_.end(); ++it_obj) { 00248 SemanticObject* obj = *it_obj; 00249 obj->removeFromHypothesis(this); 00250 if (obj->getNumParentHypotheses() == 0) { 00251 ObjectStorage::getInstance().removeObject(*obj); 00252 delete obj; 00253 } 00254 } 00255 00256 objects_.clear(); 00257 00258 is_active_leaf_ = false; 00259 00260 // remove any remaining assignments 00261 if (assignment_set_) { 00262 delete assignment_set_; 00263 } 00264 } 00265 00266 00267 void Hypothesis::clearInactive() { 00268 if (!is_active_leaf_) { 00269 clear(); 00270 for (list<Hypothesis*>::const_iterator it = children_.begin(); it != children_.end(); ++it) { 00271 (*it)->clearInactive(); 00272 } 00273 } 00274 } 00275 00276 00277 void Hypothesis::deleteChildren() { 00278 // delete all child hypotheses 00279 for (list<Hypothesis*>::iterator it = children_.begin(); it != children_.end(); ++it) { 00280 (*it)->deleteChildren(); 00281 delete (*it); 00282 } 00283 } 00284 00285 00286 Hypothesis* Hypothesis::deleteSinglePaths() { 00287 for (list<Hypothesis*>::iterator it_child = children_.begin(); it_child != children_.end();) { 00288 Hypothesis* new_child = (*it_child)->deleteSinglePaths(); 00289 if (new_child != *it_child) { 00290 it_child = children_.erase(it_child); 00291 children_.insert(it_child, new_child); 00292 } else { 00293 ++it_child; 00294 } 00295 } 00296 00297 if (children_.size() == 1) { 00298 Hypothesis* hyp = *children_.begin(); 00299 hyp->parent_ = this->parent_; 00300 delete this; 00301 return hyp; 00302 } 00303 00304 return this; 00305 } 00306 00307 }