HeuristicalTreeGenerator.cpp
Go to the documentation of this file.
1 
19 #include <ros/ros.h>
20 
21 namespace SceneModel {
22 
25  {
26  }
27 
29  {
30  }
31 
33  {
34  deleteEmptyObjectSets(pObjectSets);
35 
36  // This is a helper datastructure for building the tree. We do agglomerative clustering,
37  // which means we expand clusters until we have a tree of them. We're building bottom up,
38  // thats because we have to store the tree as a list of clusters during building.
39  std::vector<boost::shared_ptr<TreeNode> > clusters;
40 
41  // Initialize the helper data structure.
42  BOOST_FOREACH(boost::shared_ptr<ISM::ObjectSet> set, pObjectSets.mObjectSets)
43  clusters.push_back(boost::shared_ptr<TreeNode>(new TreeNode(set)));
44 
45  // Use the heuristics and agglomerative clustering to create the tree.
46  // Search the best cluster of trajectories.
47  // Remove it from the trajectory list.
48  // Build the next layer of the tree using the cluster.
49  while(clusters.size() > 1)
50  {
51  // Evaluate the heuristics for the remaining trajectories.
52  evaluateHeuristics(clusters);
53 
54  // Get the best cluster from the best scoring heuristic.
55  boost::shared_ptr<TreeNode> cluster = mHeuristics[0]->getBestCluster();
56 
57  // Remove the object set used by the cluster's root from our 'clusters' helper data structure.
58  for(unsigned int i = 0; i < clusters.size(); i++)
59  {
60  if(clusters[i]->mObjectSet->mIdentifier == cluster->mObjectSet->mIdentifier)
61  {
62  clusters.erase(clusters.begin() + i);
63  i--;
64  }
65  }
66 
67  // Remove the object sets used by the child nodes of the cluster's root from our 'clusters' helper data structure.
68  BOOST_FOREACH(boost::shared_ptr<TreeNode> child, cluster->mChildren)
69  {
70  for(unsigned int i = 0; i < clusters.size(); i++)
71  {
72  if(clusters[i]->mObjectSet->mIdentifier == child->mObjectSet->mIdentifier)
73  {
74  clusters.erase(clusters.begin() + i);
75  i--;
76  }
77  }
78  }
79 
80  // Add the cluster to the list of candidates.
81  clusters.push_back(cluster);
82  }
83 
84  // There's only one node left. This will be the root node of the tree.
85  pRoot = clusters[0];
86  }
87 
90  std::string pType)
91  {
92  deleteEmptyObjectSets(pTrajectories);
93 
94  // This is a helper datastructure for building the tree. It will contain all unassigned nodes.
95  std::vector<boost::shared_ptr<TreeNode> > clusters;
96 
97  // Initialize the helper data structure.
98  BOOST_FOREACH(boost::shared_ptr<ISM::ObjectSet> set, pTrajectories.mObjectSets)
99  clusters.push_back(boost::shared_ptr<TreeNode>(new TreeNode(set)));
100 
101  // Add the object with the given type as root node.
102  for(std::vector<boost::shared_ptr<TreeNode> >::iterator it = clusters.begin() ; it != clusters.end(); ++it)
103  {
104  boost::shared_ptr<ISM::ObjectSet> objectSet = (*it)->mObjectSet;
105 
106  // Check, if we found our object set.
107  if(objectSet->objects[0]->type.compare(pType) == 0)
108  {
109  // Initialize the root node with the found object set.
110  pRoot.reset(new TreeNode(objectSet));
111 
112  // Delete the object set from the list.
113  clusters.erase(it);
114 
115  // Stop the loop.
116  break;
117  }
118  }
119 
120  // While there are other nodes in the helper data structure, continue searching for the best pairs.
121  // Add the best pair found to the tree.
122  while(clusters.size() > 0)
123  {
124  // This is a list of all nodes currentry assigned to the tree.
125  std::vector<boost::shared_ptr<TreeNode> > assignedNodes;
126 
127  // Iterate over the tree and fill the list of assigned nodes.
128  std::queue<boost::shared_ptr<TreeNode> > nodesToVisit;
129  nodesToVisit.push(pRoot);
130 
131  while(nodesToVisit.size() > 0)
132  {
133  boost::shared_ptr<TreeNode> node = nodesToVisit.front();
134  nodesToVisit.pop();
135 
136  BOOST_FOREACH(boost::shared_ptr<TreeNode> child, node->mChildren)
137  nodesToVisit.push(child);
138 
139  assignedNodes.push_back(node);
140  }
141 
142  // Pick an arbitrary and not yet assigned node and remove it from the list of unassigned nodes.
143  boost::shared_ptr<TreeNode> nodeToAssign = *clusters.begin();
144  clusters.erase(clusters.begin());
145 
146  // Evaluate the heuristics for the remaining trajectories.
147  evaluateHeuristics(assignedNodes, nodeToAssign);
148 
149  // Get the best cluster from the best scoring heuristic.
150  boost::shared_ptr<TreeNode> best = mHeuristics[0]->getBestParentNode();
151 
152  // Found a parent node or no good candidate was given by the heuristic.
153  if(best)
154  {
155  // Add our current unassigned node to the parent node determined by the heuristic.
156  std::queue<boost::shared_ptr<TreeNode> > nodesToVisitAgain;
157  nodesToVisitAgain.push(pRoot);
158 
159  while(nodesToVisitAgain.size() > 0)
160  {
161  boost::shared_ptr<TreeNode> node = nodesToVisitAgain.front();
162  nodesToVisitAgain.pop();
163 
164  if(node == best)
165  {
166  node->addChild(nodeToAssign);
167  break;
168  }
169 
170  BOOST_FOREACH(boost::shared_ptr<TreeNode> child, node->mChildren)
171  nodesToVisitAgain.push(child);
172  }
173  } else {
174  pRoot->addChild(nodeToAssign);
175  ROS_INFO("Scene tree: No matching candidate found. Appended to root node.");
176  }
177  }
178  }
179 
181  {
182  mHeuristics.push_back(pHeuristic);
183  }
184 
186  {
187  // Evaluate every single trajectory based on the given object sets. Sort them by score.
188  BOOST_FOREACH(boost::shared_ptr<AbstractHeuristic> heuristic, mHeuristics)
189  {
190  // Reset the heuristic.
191  heuristic->reset();
192 
193  // Apply the heuristic.
194  heuristic->apply(pClusters);
195  }
196 
197  // Sort the heuristics based on their score.
198  std::sort(mHeuristics.begin(), mHeuristics.end());
199  }
200 
203  {
204  // Evaluate every single trajectory based on the given object sets. Sort them by score.
205  BOOST_FOREACH(boost::shared_ptr<AbstractHeuristic> heuristic, mHeuristics)
206  {
207  // Reset the heuristic.
208  heuristic->reset();
209 
210  // Apply the heuristic.
211  heuristic->apply(pClusters, pChild);
212  }
213 
214  // Sort the heuristics based on their score.
215  std::sort(mHeuristics.begin(), mHeuristics.end());
216  }
217 
219  {
220  // Iterate over the list of trajectories and delete empty object sets.
221  for(unsigned int i = 0; i < pObjectSets.mObjectSets.size(); i++)
222  {
223  if (pObjectSets.mObjectSets[i]->objects.size() == 0 )
224  {
225  pObjectSets.mObjectSets.erase(pObjectSets.mObjectSets.begin() + i);
226  i--;
227  }
228  }
229  }
230 
231 }
void buildTree(ObjectSetList pObjectSets, boost::shared_ptr< TreeNode > &pRoot)
std::vector< boost::shared_ptr< ISM::ObjectSet > > mObjectSets
Definition: ObjectSetList.h:67
#define ROS_INFO(...)
std::vector< boost::shared_ptr< AbstractHeuristic > > mHeuristics
void deleteEmptyObjectSets(ObjectSetList &pObjectSets)
void addHeuristic(boost::shared_ptr< AbstractHeuristic > pHeuristic)
void evaluateHeuristics(std::vector< boost::shared_ptr< TreeNode > > pClusters)


asr_relation_graph_generator
Author(s): Meißner Pascal
autogenerated on Fri Nov 15 2019 03:39:19