$search
00001 /* 00002 * MyObject.cpp 00003 * 00004 * Created on: Apr 27, 2011 00005 * Author: sdries 00006 */ 00007 00008 #include "wire/storage/SemanticObject.h" 00009 #include "wire/storage/KnowledgeDatabase.h" 00010 00011 #include "wire/core/Evidence.h" 00012 #include "wire/core/ClassModel.h" 00013 #include "wire/core/Property.h" 00014 #include "wire/logic/Assignment.h" 00015 #include "wire/logic/Hypothesis.h" 00016 00017 #include <problib/conversions.h> 00018 00019 using namespace std; 00020 00021 namespace mhf { 00022 00023 int SemanticObject::N_SEMANTICOBJECT = 0; 00024 00025 SemanticObject::SemanticObject(long ID) : ID_(ID), expected_class_("") { 00026 ++N_SEMANTICOBJECT; 00027 } 00028 00029 SemanticObject::SemanticObject(const SemanticObject& orig) 00030 : PropertySet(orig), ID_(orig.ID_), expected_class_(orig.expected_class_) { 00031 ++N_SEMANTICOBJECT; 00032 } 00033 00034 SemanticObject::~SemanticObject() { 00035 --N_SEMANTICOBJECT; 00036 } 00037 00038 void SemanticObject::init(const Evidence& z) { 00039 update(z); 00040 } 00041 00042 double SemanticObject::getLikelihood(const PropertySet& ev) const { 00043 00044 //propagate(ev.getTimeStamp()); 00045 00046 double likelihood = 1; 00047 00048 vector<Attribute> need_to_deduce; 00049 00050 const map<Attribute, Property*>& ev_props = ev.getPropertyMap(); 00051 for(map<Attribute, Property*>::const_iterator it = ev_props.begin(); it != ev_props.end(); ++it) { 00052 const Attribute& attribute = it->first; 00053 const Property* ev_prop = it->second; 00054 00055 const Property* this_prop = getProperty(attribute); 00056 00057 if (this_prop) { 00058 likelihood *= this_prop->getLikelihood(ev_prop->getValue()); 00059 } else { 00060 need_to_deduce.push_back(attribute); 00061 } 00062 } 00063 00064 vector<Property> deduced_props = KnowledgeDatabase::getInstance().inferProperties(*this, need_to_deduce); 00065 00066 for(vector<Property>::iterator it_prop = deduced_props.begin(); it_prop != deduced_props.end(); ++it_prop) { 00067 const Property* ev_prop = ev.getProperty(it_prop->getAttribute()); 00068 assert(ev_prop); 00069 likelihood *= it_prop->getLikelihood(ev_prop->getValue()); 00070 } 00071 00072 // cout << " Likelihood existing = " << likelihood << endl; 00073 00074 return likelihood; 00075 } 00076 00077 void SemanticObject::update(const Evidence& ev) { 00078 00079 propagate(ev.getTimestamp()); 00080 00081 // first update class property 00082 00083 Attribute class_att = AttributeConv::attribute("class_label"); 00084 const Property* ev_class = ev.getProperty(class_att); 00085 00086 if (ev_class) { 00087 Property* my_class = getProperty(class_att); 00088 00089 if (my_class) { 00090 my_class->update(ev_class->getValue(), ev.getTimestamp()); 00091 } else { 00092 const IStateEstimator* prototype = getExpectedObjectModel().getEstimator(class_att); 00093 if (prototype) { 00094 Property* new_prop = new Property(class_att, *prototype); 00095 new_prop->update(ev_class->getValue(), ev.getTimestamp()); 00096 addProperty(new_prop); 00097 } else { 00098 printf("Could not find prototype estimator for attribute '%s'\n", AttributeConv::attribute_str(class_att).c_str()); 00099 } 00100 } 00101 } 00102 00103 bool class_changed = false; 00104 const ClassModel& class_model = getExpectedObjectModel(); 00105 00106 if (expected_class_ != class_model.getModelName()) { 00107 expected_class_ = class_model.getModelName(); 00108 class_changed = true; 00109 } 00110 00111 // then update rest 00112 00113 for(map<Attribute, Property*>::const_iterator it = ev.getPropertyMap().begin(); it != ev.getPropertyMap().end(); ++it) { 00114 00115 const Attribute& attribute = it->first; 00116 const Property* ev_prop = it->second; 00117 00118 if (attribute != class_att) { 00119 Property* my_prop = getProperty(attribute); 00120 00121 if (my_prop && !class_changed) { 00122 my_prop->update(ev_prop->getValue(), ev.getTimestamp()); 00123 //cout << "Updating " << AttributeConv::attribute_str(attribute) << " with " << ev_prop->getValue().toString() << endl; 00124 //cout << "Result: " << my_prop->toString() << endl; 00125 } else { 00126 const IStateEstimator* prototype = getExpectedObjectModel().getEstimator(attribute); 00127 if (prototype) { 00128 Property* new_prop = new Property(attribute, *prototype); 00129 new_prop->update(ev_prop->getValue(), ev.getTimestamp()); 00130 addProperty(new_prop); 00131 } else { 00132 printf("Could not find prototype estimator for attribute '%s'\n", AttributeConv::attribute_str(it->first).c_str()); 00133 } 00134 } 00135 } 00136 } 00137 00138 } 00139 00140 SemanticObject* SemanticObject::clone() const { 00141 return new SemanticObject(*this); 00142 } 00143 00144 const ClassModel& SemanticObject::getExpectedObjectModel() const { 00145 const Property* class_prop = getProperty("class_label"); 00146 00147 string class_name; 00148 00149 if (class_prop) { 00150 class_prop->getValue().getExpectedValue(class_name); 00151 } else { 00152 class_name = "object"; 00153 } 00154 00155 return *KnowledgeDatabase::getInstance().getClassModel(class_name); 00156 } 00157 00158 void SemanticObject::addPotentialAssignment(const Evidence& ev, double probability) { 00159 Assignment* assignment = new Assignment(Assignment::EXISTING, &ev, this, probability); 00160 00161 for(set<Hypothesis*>::iterator it_hyp = parent_hypotheses_.begin(); it_hyp != parent_hypotheses_.end(); ++it_hyp) { 00162 Hypothesis& hyp = **it_hyp; 00163 hyp.addPotentialAssignment(assignment); 00164 } 00165 00166 // TODO: check if object now has potential assignments between hypotheses of different clusters. If so, those trees need to merge. 00167 } 00168 00169 ObjectID SemanticObject::getID() const { 00170 return ID_; 00171 } 00172 00173 void SemanticObject::addToHypothesis(Hypothesis* hyp) { 00174 parent_hypotheses_.insert(hyp); 00175 } 00176 00177 void SemanticObject::removeFromHypothesis(Hypothesis* hyp) { 00178 parent_hypotheses_.erase(hyp); 00179 } 00180 00181 unsigned int SemanticObject::getNumParentHypotheses() const { 00182 return parent_hypotheses_.size(); 00183 } 00184 00185 }