SemanticObject.cpp
Go to the documentation of this file.
1 /*
2  * MyObject.cpp
3  *
4  * Created on: Apr 27, 2011
5  * Author: sdries
6  */
7 
10 
11 #include "wire/core/Evidence.h"
12 #include "wire/core/ClassModel.h"
13 #include "wire/core/Property.h"
14 #include "wire/logic/Assignment.h"
15 #include "wire/logic/Hypothesis.h"
16 
17 #include <problib/conversions.h>
18 
19 using namespace std;
20 
21 namespace mhf {
22 
23 int SemanticObject::N_SEMANTICOBJECT = 0;
24 
25 SemanticObject::SemanticObject(long ID) : ID_(ID), expected_class_("") {
27 }
28 
30  : PropertySet(orig), ID_(orig.ID_), expected_class_(orig.expected_class_) {
32 }
33 
36 }
37 
39  update(z);
40 }
41 
42 double SemanticObject::getLikelihood(const PropertySet& ev) const {
43 
44  //propagate(ev.getTimeStamp());
45 
46  double likelihood = 1;
47 
48  vector<Attribute> need_to_deduce;
49 
50  const map<Attribute, Property*>& ev_props = ev.getPropertyMap();
51  for(map<Attribute, Property*>::const_iterator it = ev_props.begin(); it != ev_props.end(); ++it) {
52  const Attribute& attribute = it->first;
53  const Property* ev_prop = it->second;
54 
55  const Property* this_prop = getProperty(attribute);
56 
57  if (this_prop) {
58  likelihood *= this_prop->getLikelihood(ev_prop->getValue());
59  } else {
60  need_to_deduce.push_back(attribute);
61  }
62  }
63 
64  vector<Property> deduced_props = KnowledgeDatabase::getInstance().inferProperties(*this, need_to_deduce);
65 
66  for(vector<Property>::iterator it_prop = deduced_props.begin(); it_prop != deduced_props.end(); ++it_prop) {
67  const Property* ev_prop = ev.getProperty(it_prop->getAttribute());
68  assert(ev_prop);
69  likelihood *= it_prop->getLikelihood(ev_prop->getValue());
70  }
71 
72  // cout << " Likelihood existing = " << likelihood << endl;
73 
74  return likelihood;
75 }
76 
78 
79  propagate(ev.getTimestamp());
80 
81  // first update class property
82 
83  Attribute class_att = AttributeConv::attribute("class_label");
84  const Property* ev_class = ev.getProperty(class_att);
85 
86  if (ev_class) {
87  Property* my_class = getProperty(class_att);
88 
89  if (my_class) {
90  my_class->update(ev_class->getValue(), ev.getTimestamp());
91  } else {
92  const IStateEstimator* prototype = getExpectedObjectModel().getEstimator(class_att);
93  if (prototype) {
94  Property* new_prop = new Property(class_att, *prototype);
95  new_prop->update(ev_class->getValue(), ev.getTimestamp());
96  addProperty(new_prop);
97  } else {
98  printf("Could not find prototype estimator for attribute '%s'\n", AttributeConv::attribute_str(class_att).c_str());
99  }
100  }
101  }
102 
103  bool class_changed = false;
104  const ClassModel& class_model = getExpectedObjectModel();
105 
106  if (expected_class_ != class_model.getModelName()) {
107  expected_class_ = class_model.getModelName();
108  class_changed = true;
109  }
110 
111  // then update rest
112 
113  for(map<Attribute, Property*>::const_iterator it = ev.getPropertyMap().begin(); it != ev.getPropertyMap().end(); ++it) {
114 
115  const Attribute& attribute = it->first;
116  const Property* ev_prop = it->second;
117 
118  if (attribute != class_att) {
119  Property* my_prop = getProperty(attribute);
120 
121  if (my_prop && !class_changed) {
122  my_prop->update(ev_prop->getValue(), ev.getTimestamp());
123  //cout << "Updating " << AttributeConv::attribute_str(attribute) << " with " << ev_prop->getValue().toString() << endl;
124  //cout << "Result: " << my_prop->toString() << endl;
125  } else {
126  const IStateEstimator* prototype = getExpectedObjectModel().getEstimator(attribute);
127  if (prototype) {
128  Property* new_prop = new Property(attribute, *prototype);
129  new_prop->update(ev_prop->getValue(), ev.getTimestamp());
130  addProperty(new_prop);
131  } else {
132  printf("Could not find prototype estimator for attribute '%s'\n", AttributeConv::attribute_str(it->first).c_str());
133  }
134  }
135  }
136  }
137 
138 }
139 
141  return new SemanticObject(*this);
142 }
143 
145  const Property* class_prop = getProperty("class_label");
146 
147  string class_name;
148 
149  if (class_prop) {
150  class_prop->getValue().getExpectedValue(class_name);
151  } else {
152  class_name = "object";
153  }
154 
155  return *KnowledgeDatabase::getInstance().getClassModel(class_name);
156 }
157 
158 void SemanticObject::addPotentialAssignment(const Evidence& ev, double probability) {
159  Assignment* assignment = new Assignment(Assignment::EXISTING, &ev, this, probability);
160 
161  for(set<Hypothesis*>::iterator it_hyp = parent_hypotheses_.begin(); it_hyp != parent_hypotheses_.end(); ++it_hyp) {
162  Hypothesis& hyp = **it_hyp;
163  hyp.addPotentialAssignment(assignment);
164  }
165 
166  // TODO: check if object now has potential assignments between hypotheses of different clusters. If so, those trees need to merge.
167 }
168 
170  return ID_;
171 }
172 
174  parent_hypotheses_.insert(hyp);
175 }
176 
178  parent_hypotheses_.erase(hyp);
179 }
180 
182  return parent_hypotheses_.size();
183 }
184 
185 }
SemanticObject * clone() const
const ClassModel * getClassModel(const std::string &class_name) const
const Property * getProperty(const Attribute &attribute) const
Definition: PropertySet.cpp:75
Time getTimestamp() const
static Attribute attribute(const std::string &attribute_str)
attribute
Definition: datatypes.cpp:16
std::set< Hypothesis * > parent_hypotheses_
void update(const Evidence &z)
const IStateEstimator * getEstimator(const Attribute &attribute) const
Definition: ClassModel.cpp:48
void update(const pbl::PDF &z, const Time &time)
Definition: Property.cpp:52
virtual bool getExpectedValue(std::string &v) const
const std::map< Attribute, Property * > & getPropertyMap() const
virtual double getLikelihood(const pbl::PDF &pdf) const
Definition: Property.cpp:68
static int N_SEMANTICOBJECT
void addProperty(const Attribute &attribute, const pbl::PDF &value)
Definition: PropertySet.cpp:41
Base class for all state estimators used by the world model.
static KnowledgeDatabase & getInstance()
static std::string attribute_str(const Attribute &attribute)
attribute_str
Definition: datatypes.cpp:27
iterator(field< oT > &in_M, const bool at_end=false)
long ObjectID
Definition: datatypes.h:46
The class Evidence represents a set of properties (PropertySet) that all originate from one physical ...
Definition: Evidence.h:61
unsigned int getNumParentHypotheses() const
void init(const Evidence &z)
int Attribute
Definition: datatypes.h:49
std::string expected_class_
const std::string & getModelName() const
Definition: ClassModel.cpp:44
const_iterator(const field< oT > &in_M, const bool at_end=false)
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
void removeFromHypothesis(Hypothesis *hyp)
std::vector< Property > inferProperties(const PropertySet &prop_set, std::vector< Attribute >) const
const pbl::PDF & getValue() const
Definition: Property.cpp:48
void propagate(const Time &time)
Propagates the internal state to Time time.
Definition: PropertySet.cpp:95
ObjectID getID() const
void addPotentialAssignment(Assignment *assignment)
Definition: Hypothesis.cpp:125
const ClassModel & getExpectedObjectModel() const
void addPotentialAssignment(const Evidence &ev, double probability)
void addToHypothesis(Hypothesis *hyp)
Definition: ClassModel.h:44


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