random_path.cpp
Go to the documentation of this file.
1 
19 
20 namespace pose_prediction_ism {
21 RandomPath::RandomPath(std::string database_filename):
22  PredictorWithScore::PredictorWithScore(database_filename, "random_path", Random)
23 {
24 }
25 
27 {
28 }
29 
30  AttributedPointCloud RandomPath::predictUnfoundPoses(ISM::PosePtr &reference_pose_ptr, std::string pattern_name, double percentage_of_records_for_prediction)
31 {
32  ROS_INFO_STREAM("Pose prediction is run for scene " << pattern_name << " with " << percentage_of_records_for_prediction << " times the demonstration trajectory lengths.");
33  paths_with_scores_map_.clear();
34  slots_map_.clear();
35  specifiers_size_map_.clear();
37  IsmObjects x;
38  x.push_back(IsmObject (pattern_name, "MOCK"));
39  createPathsWithScoresMap(pattern_name, x);
41  createAttributedPointCloud(reference_pose_ptr, percentage_of_records_for_prediction);
43 }
44 
46  IsmObjects predecessors)
47 {
48  IsmObjectSet objects_in_pattern = getObjectTypesAndIdsBelongingToPattern(type);
49  for (IsmObject object : objects_in_pattern)
50  {
51  if (!isFoundObject(object))
52  {
53  predecessors.push_back(object);
54  if (isReferenceObject(object))
55  createPathsWithScoresMap(object.first, predecessors);
56  else
57  {
58  std::pair<PathsWithScoresMap::iterator,bool>ret;
59  double newScore = calculatePathScore(predecessors);
60  PathWithScore newPathWithScore(predecessors,newScore);
62  p.push_back(newPathWithScore);
63  ret = paths_with_scores_map_.insert (std::pair<IsmObject, PathsWithScores>(object, p));
64  if (ret.second)
65  {
66  ROS_ASSERT_MSG(paths_with_scores_map_[object].size() == 1,
67  "New path was not inserted");
68  specifiers_size_map_[object] = votes_.at(type)
69  .at(object.first)
70  .at(object.second).size();
71  }
72  else
73  {
74  unsigned int oldLength = paths_with_scores_map_[object].size();
75  paths_with_scores_map_[object].push_back(newPathWithScore);
76  ROS_ASSERT_MSG(paths_with_scores_map_[object].size() == 1 + oldLength,
77  "New path was not inserted");
78  }
79  }
80  predecessors.pop_back();
81  }
82  else
83  ROS_DEBUG_STREAM("Object " << "(" << object.first << "," << object.second << ")" << "was already found");
84  }
85 }
86 
87  void RandomPath::createAttributedPointCloud(ISM::PosePtr reference_pose_ptr, double percentage_of_records_for_prediction)
88 {
90  for (PathsWithScoresMap::iterator paths_it = paths_with_scores_map_.begin();
91  paths_it != paths_with_scores_map_.end(); ++paths_it)
92  {
93  const unsigned int SIZE = specifiers_size_map_[paths_it->first];
94  unsigned int threshold = round(percentage_of_records_for_prediction * SIZE);
95  IsmObject object = paths_it->first;
96  PathsWithScores paths_with_scores = paths_it->second;
97 
98  bool has_choice_for_path = paths_with_scores.size() > 1;
99  Slots percentage_slots;
100  if (has_choice_for_path)
101  percentage_slots = slots_map_[object];
102  for (unsigned int i = 0; i < threshold ; ++i)
103  {
104  IsmObjects predecessors;
105  if (has_choice_for_path)
106  {
107  double slot = rand() / double(RAND_MAX);
108  Slots::iterator percentage_slots_it = percentage_slots.begin();
109  PathsWithScores::iterator current_path_it = paths_with_scores.begin();
110  ROS_ASSERT(percentage_slots.size() == paths_with_scores.size());
111  while (slot > (*percentage_slots_it))
112  {
113  percentage_slots_it++;
114  current_path_it++;
115  ROS_ASSERT(percentage_slots_it != percentage_slots.end());
116  }
117  predecessors = current_path_it->first;
118  }
119  else
120  predecessors = paths_with_scores.begin()->first;
121  ISM::PosePtr object_pose_ptr = predictPose(reference_pose_ptr,predecessors);
122  addPointToPointCloud(object_pose_ptr, object.first, object.second);
123  }
124  }
125 }
126 
128 {
130  ROS_ASSERT(slots_map_.empty());
131  for (PathsWithScoresMap::iterator paths_it = paths_with_scores_map_.begin();
132  paths_it != paths_with_scores_map_.end(); ++paths_it)
133  {
134  IsmObject object = paths_it->first;
135  PathsWithScores paths_with_scores = paths_it->second;
136  ROS_ASSERT(paths_with_scores.size() > 0);
137  if (paths_with_scores.size() > 1)
138  {
139  double sum = 0.0;
140  for (PathsWithScores::iterator current_path_it = paths_with_scores.begin();
141  current_path_it != paths_with_scores.end();
142  ++current_path_it)
143  sum += current_path_it->second;
144  double current_sum = 0.0;
145  Slots percentage_slots;
146  for (PathsWithScores::iterator current_path_it = paths_with_scores.begin();
147  current_path_it != paths_with_scores.end();
148  ++current_path_it)
149  {
150  double score = current_path_it->second;
151  double percentage_score = score / sum;
152  current_sum += percentage_score;
153  percentage_slots.push_back(current_sum);
154  }
155  ROS_ASSERT(percentage_slots.size() == paths_with_scores.size());
156  if (percentage_slots[paths_with_scores.size() - 1] != 1.0)
157  percentage_slots[paths_with_scores.size() - 1] = 1.0;
158  slots_map_[object] = percentage_slots;
159  }
160  else
161  ROS_DEBUG("No random election necessary: There is only one path");
162  }
163 }
165 {
166  double score = 1 / ( (double) path.size() );
167  ROS_ASSERT_MSG(score < 1,
168  "There must be more than one predecessor, otherwise all objects are in the reference");
169  return score;
170 }
171 }
ISM::PatternToObjectToVoteMap votes_
void addPointToPointCloud(ISM::PosePtr pose_to_add, std::string type, std::string identifier)
RecognizerPredictionISM::addPointToPointCloud Adds an ISM Pose with object type and identifier as att...
AttributedPointCloud attributed_point_cloud_
std::set< IsmObject > IsmObjectSet
void createAttributedPointCloud(ISM::PosePtr reference_pose_ptr, double percentage_of_records_for_prediction)
Definition: random_path.cpp:87
std::pair< IsmObjects, double > PathWithScore
ISM::PosePtr predictPose(ISM::PosePtr reference_pose_ptr, IsmObjects path_to_object)
#define ROS_ASSERT_MSG(cond,...)
AttributedPointCloud predictUnfoundPoses(ISM::PosePtr &reference_pose, std::string pattern_name, double percentage_of_records_for_prediction)
predictUnfoundPoses Processes the following steps for each object in the objectpattern set...
Definition: random_path.cpp:30
#define ROS_DEBUG_STREAM(args)
std::pair< std::string, std::string > IsmObject
bool isReferenceObject(IsmObject object)
std::vector< PathWithScore > PathsWithScores
Definition: random_path.h:35
double calculatePathScore(IsmObjects path)
#define ROS_INFO_STREAM(args)
RandomPath(std::string database_filename)
Definition: random_path.cpp:21
PathsWithScoresMap paths_with_scores_map_
Definition: random_path.h:39
std::vector< IsmObject > IsmObjects
#define ROS_ASSERT(cond)
std::vector< double > Slots
Definition: random_path.h:33
void createPathsWithScoresMap(std::string type, IsmObjects predecessors)
Definition: random_path.cpp:45
bool isFoundObject(IsmObject object)
IsmObjectSet getObjectTypesAndIdsBelongingToPattern(std::string type)
asr_msgs::AsrAttributedPointCloud AttributedPointCloud
Definition: typedef.h:37
#define ROS_DEBUG(...)


asr_lib_pose_prediction_ism
Author(s): Heizmann Heinrich, Heller Florian, Meißner Pascal, Stöckle Patrick
autogenerated on Thu Jan 9 2020 07:19:56