KnowledgeDatabase.cpp
Go to the documentation of this file.
2 
3 #include "wire/core/Property.h"
4 #include "wire/core/ClassModel.h"
5 #include "wire/core/Evidence.h"
7 
8 #include "problib/conversions.h"
9 
10 using namespace std;
11 
12 namespace mhf {
13 
14 KnowledgeDatabase* KnowledgeDatabase::instance_ = 0;
15 
16 KnowledgeDatabase& KnowledgeDatabase::getInstance() {
17  if (instance_) {
18  return *instance_;
19  }
20  instance_ = new KnowledgeDatabase();
21  return *instance_;
22 }
23 
24 KnowledgeDatabase::KnowledgeDatabase() : prior_new_(0), prior_existing_(0), prior_clutter_(0) {
25 
26 }
27 
29  for(map<string, ClassModel*>::iterator it_model = class_models_.begin(); it_model != class_models_.end(); ++it_model) {
30  delete it_model->second;
31  }
32 }
33 
34 void KnowledgeDatabase::addClassModel(const string& class_name, ClassModel* model) {
35  class_models_[class_name] = model;
36 }
37 
38 const PropertySet& KnowledgeDatabase::getNewPDFs(const std::string& class_name) const {
39  return getClassModel(class_name)->getNewPDFs();
40 }
41 
42 const PropertySet& KnowledgeDatabase::getClutterPDFs(const std::string& class_name) const {
43  return getClassModel(class_name)->getClutterPDFs();
44 }
45 
46 const IStateEstimator* KnowledgeDatabase::getEstimator(const std::string& class_name, const Attribute& attribute) const {
47  return getClassModel(class_name)->getEstimator(attribute);
48 }
49 
51  return class_pmf_;
52 }
53 
54 void KnowledgeDatabase::setPriorNew(double prior_new) {
55  prior_new_ = prior_new;
56 }
57 
58 void KnowledgeDatabase::setPriorExisting(double prior_existing) {
59  prior_existing_ = prior_existing;
60 }
61 
62 void KnowledgeDatabase::setPriorClutter(double prior_clutter) {
63  prior_clutter_ = prior_clutter;
64 }
65 
67  return prior_new_;
68 }
69 
71  return prior_existing_;
72 }
73 
75  return prior_clutter_;
76 }
77 
78 const std::map<std::string, ClassModel*>& KnowledgeDatabase::getClassModels() const {
79  return class_models_;
80 }
81 
82 const ClassModel* KnowledgeDatabase::getClassModel(const std::string& class_name) const {
83  map<string, ClassModel*>::const_iterator it_model = class_models_.find(class_name);
84  if (it_model != class_models_.end()) {
85  return it_model->second;
86  }
87 
88  it_model = class_models_.find("object");
89  assert(it_model != class_models_.end());
90 
91  return it_model->second;
92 }
93 
95 
96  const Property* class_prop = z.getProperty("class_label");
97 
98  double likelihood = 0;
99  double total_prob = 0;
100 
101  if (class_prop) {
102  // we have information about the class distribution
103  const pbl::PMF* class_pmf = pbl::PDFtoPMF(class_prop->getValue());
104 
105  vector<double> class_probs;
106  class_pmf->getProbabilities(class_probs);
107 
108  vector<string> class_names;
109  class_pmf->getValues(class_names);
110 
111  likelihood = 0;
112  for(unsigned int i = 0; i < class_probs.size(); ++i) {
113  const ClassModel* class_model = getClassModel(class_names[i]);
114 
115  if (class_model) {
116  likelihood += class_probs[i] * class_model->getNewPDFs().getLikelihood(z);
117  total_prob += class_probs[i];
118  }
119  }
120  }
121 
122  const ClassModel* default_model = getClassModel("object");
123  assert(default_model != 0);
124 
125  likelihood += (1 - total_prob) * default_model->getNewPDFs().getLikelihood(z);
126 
127  double p_new = getPriorNew() * likelihood;
128 
129  //cout << "p_new = " << getPriorNew() << " * " << likelihood << " = " << p_new << endl;
130 
131  return p_new;
132 }
133 
135 
136  const Property* class_prop = z.getProperty("class_label");
137 
138  double likelihood = 0;
139  double total_prob = 0;
140 
141  if (class_prop) {
142  // we have information about the class distribution
143  const pbl::PMF* class_pmf = pbl::PDFtoPMF(class_prop->getValue());
144 
145  vector<double> class_probs;
146  class_pmf->getProbabilities(class_probs);
147 
148  vector<string> class_names;
149  class_pmf->getValues(class_names);
150 
151  likelihood = 0;
152  for(unsigned int i = 0; i < class_probs.size(); ++i) {
153  const ClassModel* class_model = getClassModel(class_names[i]);
154 
155  if (class_model) {
156  likelihood += class_probs[i] * class_model->getClutterPDFs().getLikelihood(z);
157  total_prob += class_probs[i];
158  }
159  }
160  }
161 
162  const ClassModel* default_model = getClassModel("object");
163  assert(default_model != 0);
164 
165  likelihood += (1 - total_prob) * default_model->getClutterPDFs().getLikelihood(z);
166 
167  double p_clutter = getPriorClutter() * likelihood;
168 
169  //cout << "p_new = " << getPriorNew() << " * " << likelihood << " = " << p_new << endl;
170 
171  return p_clutter;
172 }
173 
175  // calculate prior (prior probability that target generates a detection)
176  double prior = getPriorExisting();
177 
178  // calculate likelihood (likelihood that measurements originates from the target)
179  double likelihood = obj.getLikelihood(z);
180 
181  //cout << "p_existing = " << prior << " * " << likelihood << " = " << prior * likelihood << endl;
182 
183  return prior * likelihood;
184 }
185 
186 vector<Property> KnowledgeDatabase::inferProperties(const PropertySet& prop_set, vector<Attribute> attribs) const {
187  const Property* class_prop = prop_set.getProperty("class_label");
188 
189  const ClassModel* most_prob_class_model = 0;
190  if (class_prop) {
191  string most_prob_class;
192  class_prop->getValue().getExpectedValue(most_prob_class);
193  most_prob_class_model = getClassModel(most_prob_class);
194  } else {
195  most_prob_class_model = getClassModel("object");
196  }
197 
198  vector<Property> inferred_props;
199  for(vector<Attribute>::iterator it_att = attribs.begin(); it_att != attribs.end(); ++it_att) {
200  const Property* prop = most_prob_class_model->getNewPDFs().getProperty(*it_att);
201  assert(prop);
202  inferred_props.push_back(*prop);
203  }
204 
205  return inferred_props;
206 }
207 
208 }
const ClassModel * getClassModel(const std::string &class_name) const
void setPriorExisting(double prior_existing)
const Property * getProperty(const Attribute &attribute) const
Definition: PropertySet.cpp:75
void setPriorClutter(double prior_clutter)
const std::map< std::string, ClassModel * > & getClassModels() const
const IStateEstimator * getEstimator(const Attribute &attribute) const
Definition: ClassModel.cpp:48
void setPriorNew(double prior_new)
virtual bool getExpectedValue(std::string &v) const
void addClassModel(const std::string &class_name, ClassModel *model)
double getPriorClutter() const
const PropertySet & getNewPDFs(const std::string &class_name) const
const PropertySet & getClutterPDFs() const
Definition: ClassModel.cpp:60
Base class for all state estimators used by the world model.
const PMF * PDFtoPMF(const PDF &pdf)
const IStateEstimator * getEstimator(const std::string &class_name, const Attribute &attribute) const
iterator(field< oT > &in_M, const bool at_end=false)
const PropertySet & getNewPDFs() const
Definition: ClassModel.cpp:56
The class Evidence represents a set of properties (PropertySet) that all originate from one physical ...
Definition: Evidence.h:61
double getProbabilityExisting(const Evidence &z, const SemanticObject &obj)
int Attribute
Definition: datatypes.h:49
const_iterator(const field< oT > &in_M, const bool at_end=false)
void getValues(std::vector< std::string > &values) const
double getLikelihood(const PropertySet &ev) const
Contains knowledge about a specific object class on where to expect new objects of that class (new) a...
Definition: ClassModel.h:57
virtual double getLikelihood(const PropertySet &P) const
double getProbabilityClutter(const Evidence &z)
std::vector< Property > inferProperties(const PropertySet &prop_set, std::vector< Attribute >) const
const PropertySet & getClutterPDFs(const std::string &class_name) const
void getProbabilities(std::vector< double > &probabilities) const
std::map< std::string, ClassModel * > class_models_
const pbl::PDF & getValue() const
Definition: Property.cpp:48
double getPriorExisting() const
double getProbabilityNew(const Evidence &z)
const pbl::PMF & getClassDistribution() const
Definition: ClassModel.h:44


wire_core
Author(s): Sjoerd van den Dries, Jos Elfring
autogenerated on Fri Apr 16 2021 02:32:27