TopologyManager.cpp
Go to the documentation of this file.
1 
19 
21 
23  const std::vector<std::string>& pObjectTypes,
26  mEvaluator(pEvaluator), mTopologyCreator(pTopologyCreator), mExamplesList(pExamplesList), mObjectTypes(pObjectTypes), mHistoryIndex(0), mPrintHelper('#')
27 {
28  ros::NodeHandle nodeHandle("~");
29 
30  // Try to get output target for the optimization history; from "none", "screen", "file".
31  if(!nodeHandle.getParam("optimization_history_output", mHistoryOutput))
32  throw std::runtime_error("Please specify parameter optimization_history_output when starting this node.");
33 
34  // Try to get the history file path.
35  if(!nodeHandle.getParam("optimization_history_file_path", mHistoryFilePath))
36  throw std::runtime_error("Please specify parameter optimization_history_file_path when starting this node.");
37 
38  // Try to get whether to revisit .
39  if(!nodeHandle.getParam("revisit_topologies", mRevisitTopologies))
40  throw std::runtime_error("Please specify parameter revisit_topologies when starting this node.");
41 
42  if (mHistoryOutput == "svg")
43  mSVGHelper.reset(new ISM::SVGHelper(mHistoryFilePath));
44 }
45 
47 {
48  if (mHistoryOutput == "svg")
49  mSVGHelper->writeResult();
50 }
51 
53 {
54  // pop neighbour:
56  mNeighbours.erase(mNeighbours.begin());
57 
58  // initialize neighbour:
59  if (!nextNeighbour->isTreeValid()) makeTree(nextNeighbour); // make sure the neighbour has a tree associated with it, which will be used by evaluator
60  mEvaluator->evaluate(nextNeighbour); // if nextNeighbour has already been evaluated, the Evaluator returns without rerunning the evaluation
61 
62  nextNeighbour->mUsedInOptimization = true; // topology has been used in optimization
63 
64  mHistory[mHistoryIndex].push_back(std::pair<boost::shared_ptr<SceneModel::Topology>, bool>(nextNeighbour, false)); // add neighbour to history
65 
66  return nextNeighbour;
67 }
68 
70 {
71  return !(mNeighbours.empty());
72 }
73 
75 {
76  if (!mTopologyCreator) throw std::runtime_error("In TopologyManager::setReferenceInstance(): TopologyCreator not initialized.");
77  std::vector<boost::shared_ptr<SceneModel::Topology>> allNeighbours = mTopologyCreator->generateNeighbours(instance);
78  std::vector<boost::shared_ptr<SceneModel::Topology>> selectedNeighbours;
79  for (boost::shared_ptr<SceneModel::Topology> neighbour: allNeighbours)
80  {
81  if (!mSeenTopologies[neighbour->mIdentifier]) // no pointer assigned
82  {
83  selectedNeighbours.push_back(neighbour);
84  mSeenTopologies[neighbour->mIdentifier] = neighbour;
85  }
86  else if (mRevisitTopologies || !mSeenTopologies[neighbour->mIdentifier]->mUsedInOptimization) // if topologies get revisited or if topology in list of seen topologies, but not yet used in optimization
87  {
88  selectedNeighbours.push_back(mSeenTopologies[neighbour->mIdentifier]);
89  }
90  }
91 
92  // add first step to history:
93  if (mHistory.empty())
94  {
95  mHistory.push_back(std::vector<std::pair<boost::shared_ptr<SceneModel::Topology>, bool>>());
96  mHistoryIndex = 0;
97  // add starting topology to history and mark it as selected:
98  mHistory[mHistoryIndex].push_back(std::pair<boost::shared_ptr<SceneModel::Topology>, bool>(instance, true));
99  }
100 
101  // mark selected topology in last step:
102  for (unsigned int i = 0; i < mHistory[mHistoryIndex].size(); i++)
103  if (mHistory[mHistoryIndex][i].first->mIdentifier == instance->mIdentifier)
104  mHistory[mHistoryIndex][i].second = true;
105 
106  // add a new step to history:
107  mHistoryIndex++;
108  mHistory.push_back(std::vector<std::pair<boost::shared_ptr<SceneModel::Topology>, bool>>());
109 
110  mNeighbours = selectedNeighbours;
111 }
112 
114 {
115  if (!mTopologyCreator) throw std::runtime_error("In TopologyManager::getFullyMeshedTopology(): TopologyCreator not initialized.");
116  boost::shared_ptr<SceneModel::Topology> fullyMeshed = mTopologyCreator->generateFullyMeshedTopology();
117  if (mSeenTopologies[fullyMeshed->mIdentifier])
118  return mSeenTopologies[fullyMeshed->mIdentifier]; // only set up tree once
119  makeTree(fullyMeshed);
120  mSeenTopologies[fullyMeshed->mIdentifier] = fullyMeshed;
121 
122  return fullyMeshed;
123 }
124 
125 std::vector<boost::shared_ptr<SceneModel::Topology>> TopologyManager::getStarTopologies()
126 {
127  if (!mTopologyCreator) throw std::runtime_error("In TopologyManager::getStarTopologies(): TopologyCreator not initialized.");
128  std::vector<boost::shared_ptr<SceneModel::Topology>> starTopologies = mTopologyCreator->generateStarTopologies();
129  for (unsigned int i = 0; i < starTopologies.size(); i++)
130  {
131  boost::shared_ptr<SceneModel::Topology> star = starTopologies[i];
132  if (!mSeenTopologies[star->mIdentifier])
133  {
134  makeTree(star);
135  mSeenTopologies[star->mIdentifier] = star;
136  }
137  else starTopologies[i] = mSeenTopologies[star->mIdentifier]; // only set up tree once
138  }
139  return starTopologies;
140 }
141 
143 {
144  if (!mTopologyCreator) throw std::runtime_error("In TopologyManager::getRandomTopology(): TopologyCreator not initialized.");
145  boost::shared_ptr<SceneModel::Topology> randomTopology = mTopologyCreator->generateRandomTopology();
146  if (mSeenTopologies[randomTopology->mIdentifier])
147  return mSeenTopologies[randomTopology->mIdentifier]; // only set up tree once
148  makeTree(randomTopology);
149  mSeenTopologies[randomTopology->mIdentifier] = randomTopology;
150 
151  return randomTopology;
152 }
153 
155 {
156 
157  mPrintHelper.printAsHeader("Generating tree from topology " + pTopology->mIdentifier);
158 
159  SceneModel::TopologyTreeTrainer tttrainer(pTopology->mRelations);
160  tttrainer.addSceneGraphMessages(mExamplesList);
161 
162  tttrainer.loadTrajectoriesAndBuildTree();
163 
164  boost::shared_ptr<SceneModel::TreeNode> tree = tttrainer.getTree();
165 
166  // Print tree.
167  ROS_INFO("Topology tree trainer successfully generated tree.");
168  std::cout << "------------- TREE:" << std::endl;
169  tree->printTreeToConsole(0);
170  std::cout << "---------------------" << std::endl;
171 
172  pTopology->setTree(tree);
173 }
174 
176 {
177  for (std::pair<std::string, boost::shared_ptr<SceneModel::Topology>> topology: mSeenTopologies)
178  topology.second->mUsedInOptimization = false;
179  mHistory.clear(); // reset history
180  mHistoryIndex = 0;
181 }
182 
183 void TopologyManager::printHistory(unsigned int pRunNumber)
184 {
185  if (mHistoryOutput != "none")
186  {
187  std::pair<unsigned int, unsigned int> bestCostIndices;
188  double overallBestCost = std::numeric_limits<double>::max();
189 
190  // find and mark best topology:
191  for (unsigned int i = 0; i < mHistory.size(); i++)
192  {
193  std::vector<std::pair<boost::shared_ptr<SceneModel::Topology>, bool>> step = mHistory[i];
194 
195  for (unsigned int j = 0; j < step.size(); j++)
196  {
197  if (step[j].first->isCostValid() && step[j].first->getCost() < overallBestCost) // because of first check, this doesn't throw if there has not been set a cost.
198  {
199  overallBestCost = step[j].first->getCost();
200  bestCostIndices.first = i;
201  bestCostIndices.second = j;
202  }
203  }
204  }
205 
206  if (mHistoryOutput == "svg")
207  {
208  // Adapt history to ISM requirements:
209  std::vector<std::vector<std::pair<ISM::TopologyPtr, unsigned int>>> history;
210  std::string sceneId = mExamplesList[0]->mIdentifier; // Assuming all scene graphs are examples for the same scene.
211  boost::shared_ptr<TopologyAdapter> topologyAdapter(new TopologyAdapter(mObjectTypes, sceneId));
212 
213  for (std::vector<std::pair<boost::shared_ptr<SceneModel::Topology>, bool>> psmstep: mHistory)
214  {
215  std::vector<std::pair<ISM::TopologyPtr, unsigned int>> ismstep;
216  for (std::pair<boost::shared_ptr<SceneModel::Topology>, bool> psmtop: psmstep)
217  {
218  ISM::TopologyPtr ismtop = topologyAdapter->psmToIsm(psmtop.first);
219  // ISM has no false negatives, so SVGHelper doesn't normally output them.
220  // Here, they are added to the false positives, so the output of them instead becomes the output of the number of recognition failures.
221  ismtop->evaluationResult.falsePositives += ismtop->evaluationResult.falseNegatives;
222  unsigned int selected;
223  if (psmtop.second) selected = 1;
224  else selected = 0;
225  ismstep.push_back(std::pair<ISM::TopologyPtr, unsigned int>(ismtop, selected));
226  }
227  history.push_back(ismstep);
228  }
229 
230  // mark best topology:
231  history[bestCostIndices.first][bestCostIndices.second].second = 4;
232 
233  ISM::CostFunctionPtr<ISM::TopologyPtr> globalCostFunction(new TopologyAdapter::CostFunction());
234  mSVGHelper->processHistory(history, globalCostFunction, sceneId);
235  // result gets written after all runs are completed.
236  }
237  else
238  {
239  std::stringstream documentation;
240  std::string title = " step 000: ";
241  std::string thirddivider = ""; // a third of a divider
242  for (unsigned int i = 0; i < title.length(); i++) thirddivider += "=";
243  std::stringstream divider;
244  divider << thirddivider << thirddivider << thirddivider << std::endl;
245 
246  // Print History:
247  documentation << divider.str() << "History of optimization run " << pRunNumber << ":" << std::endl << divider.str();
248 
249  unsigned int numberOfVisitedTopologies = 0;
250  for (unsigned int i = 0; i < mHistory.size(); i++)
251  {
252  std::string padding = "";
253  if (i < 10) padding += "0";
254  if (i < 100) padding += "0";
255  std::string title = " step " + padding + boost::lexical_cast<std::string>(i) + ": ";
256  documentation << thirddivider << title << thirddivider << std::endl;
257 
258  std::vector<std::pair<boost::shared_ptr<SceneModel::Topology>, bool>> step = mHistory[i];
259 
260  for (unsigned int j = 0; j < step.size(); j++)
261  {
262  std::pair<boost::shared_ptr<SceneModel::Topology>, bool> topologyPair = step[j];
263  boost::shared_ptr<SceneModel::Topology> topology = topologyPair.first;
264  documentation << topology->mIdentifier << ": ";
265  numberOfVisitedTopologies++;
266  if (topology->isEvaluated())
267  {
268  documentation << topology->getFalsePositives() << " false positives, " << topology->getFalseNegatives() << " false negatives, " << topology->getAverageRecognitionRuntime() << "s average recognition runtime.";
269  if (topology->isCostValid())
270  {
271  documentation << " cost: " << topology->getCost();
272  if (topologyPair.second) documentation << "[selected]";
273  if (i == bestCostIndices.first && j == bestCostIndices.second) documentation << "(best)";
274  }
275  else documentation << " <cost not calculated> ";
276  }
277  else documentation << " <not evaluated> ";
278  documentation << std::endl;
279  }
280  }
281  documentation << divider.str();
282  documentation << "Visited " << numberOfVisitedTopologies << " topologies in " << mHistory.size() << " steps." << std::endl;
283  documentation << "Best topology overall: " << std::endl;
284  documentation << mHistory[bestCostIndices.first][bestCostIndices.second].first->mIdentifier << ": ";
285  documentation << " cost: " << overallBestCost << std::endl << divider.str();
286 
287  if (mHistoryOutput == "screen") // print to console:
288  {
289  mPrintHelper.printAsHeader("History:");
290  std::cout << documentation.str();
291  }
292  else if (mHistoryOutput == "txt")
293  {
294  std::string filename = mHistoryFilePath + "history_of_run_" + boost::lexical_cast<std::string>(pRunNumber) + ".txt";
295  std::ofstream file;
296  file.open(filename);
297  if (file.is_open())
298  {
299  file << documentation.str();
300  file.close();
301  }
302  else throw std::runtime_error("Could not open file " + filename);
303  }
304  else throw std::runtime_error("Invalid history output type " + mHistoryOutput);
305  }
306  }
307  // If mHistoryOutput == none: do nothing.
308 }
309 
310 }
311 
std::vector< boost::shared_ptr< ISM::ObjectSet > > mExamplesList
filename
boost::shared_ptr< AbstractTopologyEvaluator > mEvaluator
boost::shared_ptr< SceneModel::Topology > getRandomTopology()
virtual void setReferenceInstance(boost::shared_ptr< SceneModel::Topology > instance)
void makeTree(boost::shared_ptr< SceneModel::Topology > pTopology)
std::vector< boost::shared_ptr< SceneModel::Topology > > mNeighbours
std::map< std::string, boost::shared_ptr< SceneModel::Topology > > mSeenTopologies
std::vector< boost::shared_ptr< SceneModel::Topology > > getStarTopologies()
boost::shared_ptr< SceneModel::AbstractTopologyCreator > mTopologyCreator
Generator creating the topologies.
#define ROS_INFO(...)
boost::shared_ptr< SceneModel::Topology > getFullyMeshedTopology()
TopologyManager(std::vector< boost::shared_ptr< ISM::ObjectSet >> pExamplesList, const std::vector< std::string > &pObjectTypes, boost::shared_ptr< SceneModel::AbstractTopologyCreator > pTopologyCreator, boost::shared_ptr< AbstractTopologyEvaluator > pEvaluator)
std::vector< std::vector< std::pair< boost::shared_ptr< SceneModel::Topology >, bool > > > mHistory
virtual boost::shared_ptr< SceneModel::Topology > getNextNeighbour()
unsigned int step
bool getParam(const std::string &key, std::string &s) const


asr_psm
Author(s): Braun Kai, Gehrung Joachim, Heizmann Heinrich, Meißner Pascal
autogenerated on Fri Nov 15 2019 04:00:08