23 const std::vector<std::string>& pObjectTypes,
26 mEvaluator(pEvaluator), mTopologyCreator(pTopologyCreator), mExamplesList(pExamplesList), mObjectTypes(pObjectTypes), mHistoryIndex(0), mPrintHelper(
'#')
32 throw std::runtime_error(
"Please specify parameter optimization_history_output when starting this node.");
36 throw std::runtime_error(
"Please specify parameter optimization_history_file_path when starting this node.");
40 throw std::runtime_error(
"Please specify parameter revisit_topologies when starting this node.");
59 if (!nextNeighbour->isTreeValid())
makeTree(nextNeighbour);
62 nextNeighbour->mUsedInOptimization =
true;
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;
83 selectedNeighbours.push_back(neighbour);
115 if (!
mTopologyCreator)
throw std::runtime_error(
"In TopologyManager::getFullyMeshedTopology(): TopologyCreator not initialized.");
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++)
139 return starTopologies;
144 if (!
mTopologyCreator)
throw std::runtime_error(
"In TopologyManager::getRandomTopology(): TopologyCreator not initialized.");
151 return randomTopology;
159 SceneModel::TopologyTreeTrainer tttrainer(pTopology->mRelations);
162 tttrainer.loadTrajectoriesAndBuildTree();
167 ROS_INFO(
"Topology tree trainer successfully generated tree.");
168 std::cout <<
"------------- TREE:" << std::endl;
169 tree->printTreeToConsole(0);
170 std::cout <<
"---------------------" << std::endl;
172 pTopology->setTree(tree);
178 topology.second->mUsedInOptimization =
false;
187 std::pair<unsigned int, unsigned int> bestCostIndices;
188 double overallBestCost = std::numeric_limits<double>::max();
191 for (
unsigned int i = 0; i <
mHistory.size(); i++)
193 std::vector<std::pair<boost::shared_ptr<SceneModel::Topology>,
bool>>
step =
mHistory[i];
195 for (
unsigned int j = 0; j < step.size(); j++)
197 if (step[j].first->isCostValid() && step[j].first->getCost() < overallBestCost)
199 overallBestCost = step[j].first->getCost();
200 bestCostIndices.first = i;
201 bestCostIndices.second = j;
209 std::vector<std::vector<std::pair<ISM::TopologyPtr, unsigned int>>> history;
215 std::vector<std::pair<ISM::TopologyPtr, unsigned int>> ismstep;
218 ISM::TopologyPtr ismtop = topologyAdapter->psmToIsm(psmtop.first);
221 ismtop->evaluationResult.falsePositives += ismtop->evaluationResult.falseNegatives;
222 unsigned int selected;
223 if (psmtop.second) selected = 1;
225 ismstep.push_back(std::pair<ISM::TopologyPtr, unsigned int>(ismtop, selected));
227 history.push_back(ismstep);
231 history[bestCostIndices.first][bestCostIndices.second].second = 4;
234 mSVGHelper->processHistory(history, globalCostFunction, sceneId);
239 std::stringstream documentation;
240 std::string title =
" step 000: ";
241 std::string thirddivider =
"";
242 for (
unsigned int i = 0; i < title.length(); i++) thirddivider +=
"=";
243 std::stringstream divider;
244 divider << thirddivider << thirddivider << thirddivider << std::endl;
247 documentation << divider.str() <<
"History of optimization run " << pRunNumber <<
":" << std::endl << divider.str();
249 unsigned int numberOfVisitedTopologies = 0;
250 for (
unsigned int i = 0; i <
mHistory.size(); i++)
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;
258 std::vector<std::pair<boost::shared_ptr<SceneModel::Topology>,
bool>>
step =
mHistory[i];
260 for (
unsigned int j = 0; j < step.size(); j++)
262 std::pair<boost::shared_ptr<SceneModel::Topology>,
bool> topologyPair = step[j];
264 documentation << topology->mIdentifier <<
": ";
265 numberOfVisitedTopologies++;
266 if (topology->isEvaluated())
268 documentation << topology->getFalsePositives() <<
" false positives, " << topology->getFalseNegatives() <<
" false negatives, " << topology->getAverageRecognitionRuntime() <<
"s average recognition runtime.";
269 if (topology->isCostValid())
271 documentation <<
" cost: " << topology->getCost();
272 if (topologyPair.second) documentation <<
"[selected]";
273 if (i == bestCostIndices.first && j == bestCostIndices.second) documentation <<
"(best)";
275 else documentation <<
" <cost not calculated> ";
277 else documentation <<
" <not evaluated> ";
278 documentation << std::endl;
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();
290 std::cout << documentation.str();
294 std::string
filename =
mHistoryFilePath +
"history_of_run_" + boost::lexical_cast<std::string>(pRunNumber) +
".txt";
299 file << documentation.str();
302 else throw std::runtime_error(
"Could not open file " + filename);
304 else throw std::runtime_error(
"Invalid history output type " +
mHistoryOutput);
ISM::SVGHelperPtr mSVGHelper
std::vector< boost::shared_ptr< ISM::ObjectSet > > mExamplesList
std::string mHistoryFilePath
boost::shared_ptr< AbstractTopologyEvaluator > mEvaluator
boost::shared_ptr< SceneModel::Topology > getRandomTopology()
virtual void setReferenceInstance(boost::shared_ptr< SceneModel::Topology > instance)
virtual bool hasNextNeighbour()
void makeTree(boost::shared_ptr< SceneModel::Topology > pTopology)
std::vector< boost::shared_ptr< SceneModel::Topology > > mNeighbours
std::vector< std::string > mObjectTypes
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.
std::string mHistoryOutput
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 mHistoryIndex
bool getParam(const std::string &key, std::string &s) const
void printHistory(unsigned int pRunNumber)