$search
00001 /* 00002 * AssignmentSet.h 00003 * 00004 * Created on: Jul 29, 2011 00005 * Author: sdries 00006 */ 00007 00008 #include "wire/logic/AssignmentSet.h" 00009 #include "wire/logic/Hypothesis.h" 00010 #include "wire/logic/Assignment.h" 00011 #include "wire/logic/AssignmentMatrix.h" 00012 00013 #include <iostream> 00014 #include <cassert> 00015 #include <stdio.h> 00016 #include <set> 00017 00018 using namespace std; 00019 00020 namespace mhf { 00021 00022 AssignmentSet::AssignmentSet(Hypothesis* hyp, AssignmentMatrix* assignment_matrix) : 00023 hyp_(hyp), assignment_matrix_(assignment_matrix), probability_(hyp_->getProbability()), 00024 evidence_assignments_(assignment_matrix_->getNumMeasurements()), n_blocked_(0) { 00025 00026 for(unsigned int i = 0; i < evidence_assignments_.size(); ++i) { 00027 evidence_assignments_[i] = 0; 00028 probability_ *= assignment_matrix_->getAssignment(i, 0).getProbability(); 00029 } 00030 00031 assert(probability_ > 0); 00032 } 00033 00034 AssignmentSet::AssignmentSet(const AssignmentSet& orig) : 00035 hyp_(orig.hyp_), assignment_matrix_(orig.assignment_matrix_), probability_(orig.probability_), 00036 evidence_assignments_(orig.evidence_assignments_), n_blocked_(orig.n_blocked_) { 00037 } 00038 00039 AssignmentSet::~AssignmentSet() { 00040 } 00041 00042 void AssignmentSet::expand(list<AssignmentSet*>& children) const { 00043 for(unsigned int i = n_blocked_; i < evidence_assignments_.size(); ++i) { 00044 if (evidence_assignments_[i] + 1 < assignment_matrix_->getNumAssignments(i)) { 00045 AssignmentSet* child = new AssignmentSet(*this); 00046 child->evidence_assignments_[i]++; 00047 child->n_blocked_ = i; 00048 child->probability_ *= assignment_matrix_->getAssignment(i, child->evidence_assignments_[i]).getProbability() 00049 / assignment_matrix_->getAssignment(i, this->evidence_assignments_[i]).getProbability(); 00050 00051 children.push_back(child); 00052 } 00053 } 00054 } 00055 00056 const Assignment& AssignmentSet::getMeasurementAssignment(unsigned int i_ev) const { 00057 return assignment_matrix_->getAssignment(i_ev, evidence_assignments_[i_ev]); 00058 } 00059 00060 void AssignmentSet::getAllAssignments(list<const Assignment*>& assignments) const { 00061 for(unsigned int i = 0; i < evidence_assignments_.size(); ++i) { 00062 assignments.push_back(&getMeasurementAssignment(i)); 00063 } 00064 } 00065 00066 double AssignmentSet::getProbability() const { 00067 return probability_; 00068 } 00069 00070 Hypothesis* AssignmentSet::getHypothesis() const { 00071 return hyp_; 00072 } 00073 00074 int AssignmentSet::getNumMeasurements() const { 00075 return evidence_assignments_.size(); 00076 } 00077 00078 bool AssignmentSet::isValid() const { 00079 return true; 00080 00081 /* 00082 set<const SemanticObject*> assigned_objects; 00083 00084 for(unsigned int i = 0; i < evidence_assignments_.size(); ++i) { 00085 const SemanticObject* target = assignment_matrix_->getAssignment(i, evidence_assignments_[i]).getTarget(); 00086 if (target) { 00087 if (assigned_objects.find(target) != assigned_objects.end()) { 00088 return false; 00089 } 00090 assigned_objects.insert(target); 00091 } 00092 } 00093 return true; 00094 */ 00095 } 00096 00097 void AssignmentSet::print() const { 00098 cout << endl << "===== P = " << getProbability() << " ====" << endl; 00099 00100 for(unsigned int i = 0; i < evidence_assignments_.size(); ++i) { 00101 for(unsigned int j = 0; j < assignment_matrix_->getNumAssignments(i); ++j) { 00102 double prob = assignment_matrix_->getAssignment(i, j).getProbability(); 00103 if (j == evidence_assignments_[i]) { 00104 std::cout << " (" << prob << ")"; 00105 } else { 00106 std::cout << " " << prob << " "; 00107 } 00108 } 00109 printf("\n"); 00110 } 00111 00112 for(unsigned int i = 0; i < evidence_assignments_.size(); ++i) { 00113 cout << assignment_matrix_->getAssignment(i, evidence_assignments_[i]).toString() << endl; 00114 } 00115 00116 isValid(); 00117 00118 printf("\n"); 00119 } 00120 00121 } 00122