trainer.cpp
Go to the documentation of this file.
1 
18 //Global includes
19 #include <iostream>
20 #include <fstream>
21 #include <string>
22 #include <set>
23 #include <vector>
24 #include <cmath>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/filesystem/path.hpp>
27 #include <boost/algorithm/string/split.hpp>
28 #include <boost/algorithm/string/classification.hpp>
29 #include <sstream>
30 
31 //Pkg includes
32 #include <ros/ros.h>
33 #include <visualization_msgs/Marker.h>
34 #include <visualization_msgs/MarkerArray.h>
35 #include <tf/transform_datatypes.h>
36 #include <tf/transform_listener.h>
37 
38 #include <asr_msgs/AsrObject.h>
39 
40 //ISM includes
41 #include <ISM/heuristic_trainer/Trainer.hpp>
42 #include <ISM/heuristic_trainer/DataCollector.hpp>
43 #include <ISM/heuristic_trainer/ManuallyDefPseudoHeuristic.hpp>
44 #include <ISM/utility/Util.hpp>
45 
46 //Local includes
47 #include "ism_helper.hpp"
48 
49 #define LINEWIDTH 0.008
50 
51 bool changes;
52 
59 class Trainer {
60  typedef boost::filesystem::path path;
61 public:
62 
66  Trainer() :
67  mNh("~") {
68 
69  std::string dbfilename;
70  bool useUserDefCluster;
71  bool usePredefinedRefs;
72  path preDefRefListFile;
73  path clusterListFile;
74  bool dropOldModelTables;
75 
76  //Extract cli parameters of ros node for being used in this program.
77  getNodeParameters(dbfilename, useUserDefCluster, clusterListFile, dropOldModelTables,
78  usePredefinedRefs, preDefRefListFile);
79 
80  ISM::DataCollector::setCollect(true);
81  if (dbfilename != "")
82  {
83  ISM::TableHelperPtr table_helper = ISM::TableHelperPtr(new ISM::TableHelper(dbfilename));
84  if(!table_helper->recordDataExists())
85  {
86  ISM::printRed("The database \"" + dbfilename + "\" doesn't contain any recordings!\n");
87  exit(0);
88  }
89  mTrainer = boost::shared_ptr<ISM::Trainer>(new ISM::Trainer(dbfilename, dropOldModelTables));
90  }
91  else
92  {
93  throw std::runtime_error("No db specified");
94  }
95 
96  if(useUserDefCluster)
97  {
98  std::ifstream manualList(clusterListFile.string());
99  std::string line;
100  std::vector<std::pair<std::vector<ISM::ManuallyDefPseudoHeuristic::ClusterObject>,
101  uint16_t>> cluster;
102  while(std::getline(manualList, line))
103  {
104  std::vector<ISM::ManuallyDefPseudoHeuristic::ClusterObject> clusterObjects;
105  std::vector<std::string> objAndId;
106  boost::split(objAndId, line, boost::is_any_of(";"));
107  uint16_t clusterId = boost::lexical_cast<uint16_t>(objAndId[1]);
108  std::vector<std::string> objSs;
109  boost::split(objSs, objAndId[0], boost::is_any_of(","));
110  for(std::string& objS : objSs)
111  {
112  uint16_t subId;
113  bool isSub = true;
114  try
115  {
116  subId = boost::lexical_cast<int>(objS);
117  }
118  catch(boost::bad_lexical_cast& e)
119  {
120  isSub = false;
121  }
122  if(isSub)
123  {
124  clusterObjects.push_back(ISM::ManuallyDefPseudoHeuristic::ClusterObject(subId));
125  } else
126  {
127  clusterObjects.push_back(ISM::ManuallyDefPseudoHeuristic::ClusterObject(objS));
128  }
129  }
130  cluster.push_back(std::make_pair(clusterObjects, clusterId));
131  }
132  mTrainer->setClusterForManualDefHeuristic(cluster);
133  }
134 
135  if(usePredefinedRefs)
136  {
137  std::string line;
138  std::ifstream preDefRefS (preDefRefListFile.string());
139  std::map<std::string, std::string> preDefRefList;
140  while(std::getline(preDefRefS, line))
141  {
142  std::vector<std::string> patternType;
143  boost::split(patternType, line, boost::is_any_of(","));
144  preDefRefList[patternType[0]] = patternType[1];
145  }
146  mTrainer->setPredefinedRefs(preDefRefList);
147  }
148 
149 
150  mTrainer->staticBreakRatio = mStaticBreakRatio;
151  mTrainer->togetherRatio = mTogetherRatio;
152  mTrainer->maxAngleDeviation = mMaxAngleDeviation;
153 
154  mTrainer->setUseClustering(mUseClustering);
155 
156  }
157 
163  void getNodeParameters(std::string& pDbfilename, bool& useUserDefCluster, path& clusterListFile,
164  bool& dropOldModelTables, bool& usePredefinedRefs, path& preDefRefListFile) {
165 
166  if (!mNh.getParam("dbfilename", pDbfilename)) {
167  pDbfilename = "";
168  }
169  ROS_INFO_STREAM("dbfilename: " << pDbfilename);
170 
171  if (!mNh.getParam("baseFrame", mBaseFrame)) {
172  mBaseFrame = "/map";
173  }
174  ROS_INFO_STREAM("baseFrame: " << mBaseFrame);
175 
176  if (!mNh.getParam("useClustering", mUseClustering)) {
177  mUseClustering = true;
178  }
179  ROS_INFO_STREAM("useClustering: " << mUseClustering);
180 
181  if (!mNh.getParam("staticBreakRatio", mStaticBreakRatio)) {
182  mStaticBreakRatio = 0.01;
183  }
184  ROS_INFO_STREAM("staticBreakRatio: " << mStaticBreakRatio);
185 
186  if (!mNh.getParam("togetherRatio", mTogetherRatio)) {
187  mTogetherRatio = 0.90;
188  }
189  ROS_INFO_STREAM("togetherRatio: " << mTogetherRatio);
190 
191  if (!mNh.getParam("maxAngleDeviation", mMaxAngleDeviation)) {
192  mMaxAngleDeviation = 45;
193  }
194  ROS_INFO_STREAM("maxAngleDeviation: " << mMaxAngleDeviation);
195 
196  if (!mNh.getParam("useUserDefCluster", useUserDefCluster)) {
197  useUserDefCluster = false;
198  }
199  ROS_INFO_STREAM("useUserDefCluster: " << useUserDefCluster);
200  if(useUserDefCluster)
201  {
202  std::string temp;
203  if (!mNh.getParam("manualHeuristicListFile", temp)) {
204  temp = "";
205  } else
206  {
207  clusterListFile = temp;
208  }
209  if(!boost::filesystem::is_regular_file(clusterListFile))
210  {
211  std::stringstream ss;
212  ss << clusterListFile << " does not exist or is not a file";
213  ROS_ERROR_STREAM(ss.str());
214  throw std::runtime_error(ss.str());
215  }
216  ROS_INFO_STREAM("clusterListFile: " << clusterListFile);
217  }
218 
219  std::string temp;
220  if (mNh.getParam("preDefRefListFile", temp))
221  {
222  preDefRefListFile = temp;
223  if(boost::filesystem::is_regular_file(preDefRefListFile))
224  {
225  usePredefinedRefs = true;
226  ROS_INFO_STREAM("preDefRefListFile" << preDefRefListFile);
227  }
228  }
229  else
230  ROS_INFO_STREAM("preDefRefListFile: No file.");
231 
232  if (!mNh.getParam("dropOldModelTables", dropOldModelTables)) {
233  dropOldModelTables = false;
234  }
235  ROS_INFO_STREAM("dropOldModelTables: " << dropOldModelTables);
236 
237  }
238 
242  void train() {
243  this->mTrainer->trainPattern();
244  }
245 
246 private:
247 
248  //See launch file documentation
249  std::string mBaseFrame;
254 
255  //Core functionality
257 
258  //Ros Node Stuff
260 
261 };
262 
263 int main(int argc, char **argv) {
264 
265  //Usual ros node stuff
266  ros::init(argc, argv, "trainer");
267 
268  Trainer* tr = new Trainer();
269  tr->train();
270 
271  ISM::printGreen("Training is done!\n\n");
272 
273  delete tr;
274 };
void train()
Definition: trainer.cpp:242
std::string mBaseFrame
Definition: trainer.cpp:249
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
Definition: trainer.cpp:263
bool changes
Definition: trainer.cpp:51
double mTogetherRatio
Definition: trainer.cpp:252
double mStaticBreakRatio
Definition: trainer.cpp:251
boost::filesystem::path path
Definition: trainer.cpp:60
boost::shared_ptr< ISM::Trainer > mTrainer
Definition: trainer.cpp:256
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
void getNodeParameters(std::string &pDbfilename, bool &useUserDefCluster, path &clusterListFile, bool &dropOldModelTables, bool &usePredefinedRefs, path &preDefRefListFile)
Definition: trainer.cpp:163
double mMaxAngleDeviation
Definition: trainer.cpp:253
ros::NodeHandle mNh
Definition: trainer.cpp:259
Trainer()
Definition: trainer.cpp:66
#define ROS_ERROR_STREAM(args)
bool mUseClustering
Definition: trainer.cpp:250


asr_ism
Author(s): Borella Jocelyn, Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Thu Jan 9 2020 07:20:58