$search
00001 #include "wire/storage/KnowledgeDatabase.h" 00002 00003 #include "wire/core/Property.h" 00004 #include "wire/core/ClassModel.h" 00005 #include "wire/core/Evidence.h" 00006 #include "wire/storage/SemanticObject.h" 00007 00008 #include "problib/conversions.h" 00009 00010 using namespace std; 00011 00012 namespace mhf { 00013 00014 KnowledgeDatabase* KnowledgeDatabase::instance_ = 0; 00015 00016 KnowledgeDatabase& KnowledgeDatabase::getInstance() { 00017 if (instance_) { 00018 return *instance_; 00019 } 00020 instance_ = new KnowledgeDatabase(); 00021 return *instance_; 00022 } 00023 00024 KnowledgeDatabase::KnowledgeDatabase() : prior_new_(0), prior_existing_(0), prior_clutter_(0) { 00025 00026 } 00027 00028 KnowledgeDatabase::~KnowledgeDatabase() { 00029 for(map<string, ClassModel*>::iterator it_model = class_models_.begin(); it_model != class_models_.end(); ++it_model) { 00030 delete it_model->second; 00031 } 00032 } 00033 00034 void KnowledgeDatabase::addClassModel(const string& class_name, ClassModel* model) { 00035 class_models_[class_name] = model; 00036 } 00037 00038 const PropertySet& KnowledgeDatabase::getNewPDFs(const std::string& class_name) const { 00039 return getClassModel(class_name)->getNewPDFs(); 00040 } 00041 00042 const PropertySet& KnowledgeDatabase::getClutterPDFs(const std::string& class_name) const { 00043 return getClassModel(class_name)->getClutterPDFs(); 00044 } 00045 00046 const IStateEstimator* KnowledgeDatabase::getEstimator(const std::string& class_name, const Attribute& attribute) const { 00047 return getClassModel(class_name)->getEstimator(attribute); 00048 } 00049 00050 const pbl::PMF& KnowledgeDatabase::getClassDistribution() const { 00051 return class_pmf_; 00052 } 00053 00054 void KnowledgeDatabase::setPriorNew(double prior_new) { 00055 prior_new_ = prior_new; 00056 } 00057 00058 void KnowledgeDatabase::setPriorExisting(double prior_existing) { 00059 prior_existing_ = prior_existing; 00060 } 00061 00062 void KnowledgeDatabase::setPriorClutter(double prior_clutter) { 00063 prior_clutter_ = prior_clutter; 00064 } 00065 00066 double KnowledgeDatabase::getPriorNew() const { 00067 return prior_new_; 00068 } 00069 00070 double KnowledgeDatabase::getPriorExisting() const { 00071 return prior_existing_; 00072 } 00073 00074 double KnowledgeDatabase::getPriorClutter() const { 00075 return prior_clutter_; 00076 } 00077 00078 const std::map<std::string, ClassModel*>& KnowledgeDatabase::getClassModels() const { 00079 return class_models_; 00080 } 00081 00082 const ClassModel* KnowledgeDatabase::getClassModel(const std::string& class_name) const { 00083 map<string, ClassModel*>::const_iterator it_model = class_models_.find(class_name); 00084 if (it_model != class_models_.end()) { 00085 return it_model->second; 00086 } 00087 00088 it_model = class_models_.find("object"); 00089 assert(it_model != class_models_.end()); 00090 00091 return it_model->second; 00092 } 00093 00094 double KnowledgeDatabase::getProbabilityNew(const Evidence& z) { 00095 00096 const Property* class_prop = z.getProperty("class_label"); 00097 00098 double likelihood = 0; 00099 double total_prob = 0; 00100 00101 if (class_prop) { 00102 // we have information about the class distribution 00103 const pbl::PMF* class_pmf = pbl::PDFtoPMF(class_prop->getValue()); 00104 00105 vector<double> class_probs; 00106 class_pmf->getProbabilities(class_probs); 00107 00108 vector<string> class_names; 00109 class_pmf->getValues(class_names); 00110 00111 likelihood = 0; 00112 for(unsigned int i = 0; i < class_probs.size(); ++i) { 00113 const ClassModel* class_model = getClassModel(class_names[i]); 00114 00115 if (class_model) { 00116 likelihood += class_probs[i] * class_model->getNewPDFs().getLikelihood(z); 00117 total_prob += class_probs[i]; 00118 } 00119 } 00120 } 00121 00122 const ClassModel* default_model = getClassModel("object"); 00123 assert(default_model != 0); 00124 00125 likelihood += (1 - total_prob) * default_model->getNewPDFs().getLikelihood(z); 00126 00127 double p_new = getPriorNew() * likelihood; 00128 00129 //cout << "p_new = " << getPriorNew() << " * " << likelihood << " = " << p_new << endl; 00130 00131 return p_new; 00132 } 00133 00134 double KnowledgeDatabase::getProbabilityClutter(const Evidence& z) { 00135 00136 const Property* class_prop = z.getProperty("class_label"); 00137 00138 double likelihood = 0; 00139 double total_prob = 0; 00140 00141 if (class_prop) { 00142 // we have information about the class distribution 00143 const pbl::PMF* class_pmf = pbl::PDFtoPMF(class_prop->getValue()); 00144 00145 vector<double> class_probs; 00146 class_pmf->getProbabilities(class_probs); 00147 00148 vector<string> class_names; 00149 class_pmf->getValues(class_names); 00150 00151 likelihood = 0; 00152 for(unsigned int i = 0; i < class_probs.size(); ++i) { 00153 const ClassModel* class_model = getClassModel(class_names[i]); 00154 00155 if (class_model) { 00156 likelihood += class_probs[i] * class_model->getClutterPDFs().getLikelihood(z); 00157 total_prob += class_probs[i]; 00158 } 00159 } 00160 } 00161 00162 const ClassModel* default_model = getClassModel("object"); 00163 assert(default_model != 0); 00164 00165 likelihood += (1 - total_prob) * default_model->getClutterPDFs().getLikelihood(z); 00166 00167 double p_clutter = getPriorClutter() * likelihood; 00168 00169 //cout << "p_new = " << getPriorNew() << " * " << likelihood << " = " << p_new << endl; 00170 00171 return p_clutter; 00172 } 00173 00174 double KnowledgeDatabase::getProbabilityExisting(const Evidence& z, const SemanticObject& obj) { 00175 // calculate prior (prior probability that target generates a detection) 00176 double prior = getPriorExisting(); 00177 00178 // calculate likelihood (likelihood that measurements originates from the target) 00179 double likelihood = obj.getLikelihood(z); 00180 00181 //cout << "p_existing = " << prior << " * " << likelihood << " = " << prior * likelihood << endl; 00182 00183 return prior * likelihood; 00184 } 00185 00186 vector<Property> KnowledgeDatabase::inferProperties(const PropertySet& prop_set, vector<Attribute> attribs) const { 00187 const Property* class_prop = prop_set.getProperty("class_label"); 00188 00189 const ClassModel* most_prob_class_model = 0; 00190 if (class_prop) { 00191 string most_prob_class; 00192 class_prop->getValue().getExpectedValue(most_prob_class); 00193 most_prob_class_model = getClassModel(most_prob_class); 00194 } else { 00195 most_prob_class_model = getClassModel("object"); 00196 } 00197 00198 vector<Property> inferred_props; 00199 for(vector<Attribute>::iterator it_att = attribs.begin(); it_att != attribs.end(); ++it_att) { 00200 const Property* prop = most_prob_class_model->getNewPDFs().getProperty(*it_att); 00201 assert(prop); 00202 inferred_props.push_back(*prop); 00203 } 00204 00205 return inferred_props; 00206 } 00207 00208 }