TestSetGenerator.cpp
Go to the documentation of this file.
1 
18 #include "TestSetGenerator.hpp"
19 
20 #include <ctime>
21 #include <algorithm>
22 #include <math.h>
23 #include <fstream>
24 #include <sys/wait.h>
25 #include <thread>
26 #include <sys/time.h>
27 #include <string>
28 #include <chrono>
29 #include <random>
30 
31 #include <boost/filesystem.hpp>
32 #include <boost/filesystem/path.hpp>
33 #include <boost/thread.hpp>
34 #include <boost/functional/hash.hpp>
35 #include <boost/random.hpp>
36 #include <boost/random/variate_generator.hpp>
37 #include <boost/random/uniform_real.hpp>
38 #include <boost/random/mersenne_twister.hpp>
39 #include <boost/random/normal_distribution.hpp>
40 
41 #include "utility/Util.hpp"
43 #include "ImplicitShapeModel.hpp"
44 
45 namespace ISM {
46 
47  std::pair<std::vector<ObjectSetPtr>, std::vector<ObjectSetPtr>> TestSetGenerator::generateTestSets(
48  const std::string &patternName, const TracksPtr& tracks, const IsmPtr ism, unsigned int testSetCount)
49  {
50  LogHelper::logMessage("Generating " + std::to_string(testSetCount) + " test sets for pattern " + patternName + ":");
51 
52  std::vector<ObjectSetPtr> validTestSets;
53  std::vector<ObjectSetPtr> invalidTestSets;
54 
55  mObjectSetValidator->setISM(ism);
56 
57  std::random_device rd;
58  ENG eng;
59  eng.seed(rd());
60  DIST dist(0,RAND_MAX);
61  GEN gen(eng,dist);
62 
64 
65  for (unsigned int i = 0; i < testSetCount; ++i)
66  {
67  ObjectSetPtr randomObjectSet = generateRandomObjectSetFromTracks(tracks, patternName, gen);
68 
69  if (mObjectSetValidator->isSetValid(randomObjectSet, patternName).first)
70  {
71  validTestSets.push_back(randomObjectSet);
72  }
73  else
74  {
75  invalidTestSets.push_back(randomObjectSet);
76  }
77  LogHelper::displayProgress(((double) i) / testSetCount);
78  }
79 
81 
82  LogHelper::logMessage("\nThe test sets for pattern " + patternName
83  + " contain " + std::to_string(validTestSets.size())
84  + " valid and " + std::to_string(invalidTestSets.size())
85  + " invalid object sets.\n");
86 
87  return std::make_pair(validTestSets, invalidTestSets);
88  }
89 
90 
92  const std::string& pattern, GEN &gen)
93  {
94  ObjectSetPtr randomObjectSet(new ObjectSet());
95  ObjectPtr referenceObject;
96 
97  unsigned int tries = 0;
98  const unsigned int maxTries = (allTracks->tracks.size() + allTracks->tracks[0]->objects.size()) * 100;
99  while(!referenceObject && tries < maxTries)
100  {
101  unsigned randomRefTrack = gen() % allTracks->tracks.size();
102  unsigned randomRefPos = gen() % allTracks->tracks[randomRefTrack]->objects.size();
103  referenceObject = allTracks->tracks[randomRefTrack]->objects[randomRefPos];
104  ++tries;
105  }
106  if(!referenceObject)
107  {
108  std::cerr << "TestSetGenerator::generateTestSetsIterative couldnt find referenceObject. Increase maxTries?" << std::endl;
109  throw std::runtime_error("TestSetGenerator::generateTestSetsIterative couldnt find referenceObject. Increase maxTries?");
110  }
111 
112  randomObjectSet->insert(referenceObject);
113 
114  for (std::vector<TrackPtr>::iterator track = allTracks->tracks.begin(); track != allTracks->tracks.end(); ++track)
115  {
116  if ((*track)->type == referenceObject->type && (*track)->observedId == referenceObject->observedId)
117  {
118  continue;
119  }
120 
121  const ObjectRelations& allRelations = this->mAllObjectRelationsPerPattern.at(pattern);
122  for (const std::pair<unsigned int, ObjectRelationPtr>& relation : allRelations)
123  {
124  if ( (relation.second->getVotesFromAForReferencePoseB().size() > 0
125  || relation.second->getVotesFromBForReferencePoseA().size() > 0)
126  && relation.second->containsObject(referenceObject->type, referenceObject->observedId) == true
127  && relation.second->containsObject((*track)->type, (*track)->observedId) == true)
128  {
129  PosePtr randomRelativePose;
130 
131  //if objects dont occur together size = 0
132  if (relation.second->getVotesFromBForReferencePoseA().size() > 0 &&
133  relation.second->getObjectTypeA() == referenceObject->type &&
134  relation.second->getObjectIdA() == referenceObject->observedId)
135  {
136  unsigned randomTimeStep = gen() % relation.second->getVotesFromBForReferencePoseA().size();
137  VoteSpecifierPtr randomVote = relation.second->getVotesFromBForReferencePoseA()[randomTimeStep];
138  PointPtr randomRelativePoint = GeometryHelper::getSourcePoint(referenceObject->pose, randomVote->refToObjectQuat, randomVote->radius);
139  randomRelativePose = GeometryHelper::getSourcePose(referenceObject->pose, randomRelativePoint, randomVote->refToObjectPoseQuat);
140  }
141  else if(relation.second->getVotesFromAForReferencePoseB().size() > 0)
142  {
143  unsigned randomTimeStep = gen() % relation.second->getVotesFromAForReferencePoseB().size();
144  VoteSpecifierPtr randomVote = relation.second->getVotesFromAForReferencePoseB()[randomTimeStep];
145  PointPtr randomRelativePoint = GeometryHelper::getSourcePoint(referenceObject->pose, randomVote->refToObjectQuat, randomVote->radius);
146  randomRelativePose = GeometryHelper::getSourcePose(referenceObject->pose, randomRelativePoint, randomVote->refToObjectPoseQuat);
147  }
148  ObjectPtr fittingObject(new Object((*track)->type, randomRelativePose, (*track)->observedId));
149  randomObjectSet->insert(fittingObject);
150  break;
151  }
152  }
153  }
154 
155  return randomObjectSet;
156  }
157 }
const std::map< std::string, ISM::ObjectRelations > mAllObjectRelationsPerPattern
boost::shared_ptr< Point > PointPtr
Definition: Point.hpp:45
static void displayProgress(double progress)
Definition: LogHelper.cpp:147
std::string patternName
static void logMessage(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
Definition: LogHelper.cpp:96
std::map< unsigned int, ISM::ObjectRelationPtr, std::less< unsigned > > ObjectRelations
boost::uniform_int DIST
boost::shared_ptr< ImplicitShapeModel > IsmPtr
boost::shared_ptr< VoteSpecifier > VoteSpecifierPtr
boost::shared_ptr< ObjectSet > ObjectSetPtr
Definition: ObjectSet.hpp:53
boost::shared_ptr< Tracks > TracksPtr
Definition: Tracks.hpp:42
boost::shared_ptr< Pose > PosePtr
Definition: Pose.hpp:79
ISM::ObjectSetValidatorPtr mObjectSetValidator
boost::mt19937 ENG
static PointPtr getSourcePoint(const PosePtr &refPose, const QuaternionPtr &refToObjectQuat, double radius)
static PosePtr getSourcePose(const PosePtr &reference, const PointPtr &sourcePoint, const QuaternionPtr &refToObjectPoseQuat)
this namespace contains all generally usable classes.
ObjectSetPtr generateRandomObjectSetFromTracks(const ISM::TracksPtr &allTracks, const std::string &pattern, GEN &gen)
std::pair< std::vector< ObjectSetPtr >, std::vector< ObjectSetPtr > > generateTestSets(const std::string &patternName, const TracksPtr &tracks, const IsmPtr ism, unsigned int testSetCount)
boost::shared_ptr< Object > ObjectPtr
Definition: Object.hpp:82
boost::variate_generator< ENG, DIST > GEN


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41