combinatorialTrainerEvaluator.cpp
Go to the documentation of this file.
1 
18 #include <boost/filesystem/path.hpp>
19 #include <boost/date_time/posix_time/posix_time.hpp>
20 
24 #include "randomDemoRecorder.hpp"
25 
26 #include <sys/time.h>
27 #include <string>
28 #include <chrono>
29 #include <vector>
30 #include <string>
31 
32 #include <random>
33 #include <algorithm>
34 
35 using boost::posix_time::ptime;
36 using boost::posix_time::time_duration;
37 using boost::filesystem::path;
38 
39  std::vector<ISM::CombinatorialTrainerParameters> parameters;
40 
41  std::vector<unsigned> objectCounts = {
42  5, 10, 15
43  };
44 
45  std::vector<unsigned> posesPerObjectCount = {
46  10, 25, 50, 100, 200, 400
47  };
48 
50  {
52 
53  params.evaluator.evaluatorId = 0;
54  params.evaluator.countThreadsTester = 8;
55  params.evaluator.confidenceThreshold = 0.99;
57  params.topologyGenerator.swapRelations = true;
59 
60  params.treeValidator.treeValidatorId = 0;
61  params.treeValidator.maxTreeHeight = 3;
62 
63  params.optimizationStrategy.maxNeighbourCount = 30;
64  params.optimizationStrategy.randomWalkProbability = 0.0;
65  params.optimizationStrategy.randomRestartProbability = 0.0;
66 
67  params.evaluator.binSize = 0.1;
68 
69  params.costFunction.alpha = 5;
70  params.costFunction.beta = 1;
71 
72  params.voxelGrid.useVoxelGridFilter = false;
73  params.voxelGrid.posGridSize = 0.1;
74  params.voxelGrid.angleGridSize = 0.52;
75  params.voxelGrid.posErrTolerance = 0.1;
76  params.voxelGrid.recognitionBinSize = 0.1;
77  params.voxelGrid.tolearnaceDupMethod = ISM::PosOrientVoxelGrid::HandleErrorDuplicates::LEAVE_UNCHANGED;
78  params.voxelGrid.tolDupNumThreshold = 0;
79 
80  params.general.storeFullyMeshedISM = false;
81  params.general.storeStartTopologyISM = false;
82 
83  return params;
84  }
85 
86  void writeFile(const std::string & directoryPath, const std::string & filenName, std::ostringstream & content)
87  {
88  std::string filePath = directoryPath + "/" + filenName;
89  std::ofstream file;
90  std::ios_base::iostate exceptionMask = file.exceptions() | std::ios::failbit | std::ios::badbit;
91  file.exceptions(exceptionMask);
92  try
93  {
94  file.open(filePath);
95  file << content.str();
96  file.flush();
97  file.close();
98  }
99  catch (std::ios_base::failure& e)
100  {
101  std::cerr << e.what() << "\n";
102  }
103  }
104 
105  void runOptimization(ISM::CombinatorialTrainerParameters params, std::ostringstream & os)
106  {
107  ptime t1(boost::posix_time::microsec_clock::local_time());
108 
109  ISM::CombinatorialTrainerPtr combinatorialTrainer =
111  combinatorialTrainer->learn();
112 
113  ptime t2(boost::posix_time::microsec_clock::local_time());
114  time_duration td = t2 - t1;
115 
116  boost::filesystem::path path = params.general.dbfilename;
117 
118  long secs = td.total_seconds() % 60;
119  long mins = std::floor(td.total_seconds() / 60.);
120 
121  os << ", " << mins << "." << secs;
122  }
123 
124  void prepareSceneTests(const std::string & outputPath)
125  {
126  parameters.clear();
127 
129 
130  //Uncomment and specify path to the sqlite database that contains the scene that should be trained.
131  //params.general.dbfilename = "path_to_scene";
132 
133  params.general.outputDataPath = outputPath;
134  parameters.push_back(params);
135  }
136 
137  void initTestSets(const std::string & testSetFolderPath)
138  {
139  if(!boost::filesystem::exists(testSetFolderPath))
140  boost::filesystem::create_directories(testSetFolderPath);
141 
142  for (unsigned int i = 0; i < parameters.size(); ++i) {
144 
145  path dbPath(params.general.dbfilename);
146 
147  path validTestSetsPath(testSetFolderPath + "/validTestSets_" + dbPath.stem().string() + ".sqlite");
148  params.general.storeValidTestSetsTo = validTestSetsPath.string();
149 
150  path invalidTestSetsPath(testSetFolderPath + "/invalidTestSets_" + dbPath.stem().string() + ".sqlite");
151  params.general.storeInvalidTestSetsTo = invalidTestSetsPath.string();
152 
153  ISM::CombinatorialTrainerPtr combinatorialTrainer =
155 
156  parameters[i].general.loadValidTestSetsFrom = params.general.storeValidTestSetsTo;
157  parameters[i].general.loadInvalidTestSetsFrom = params.general.storeInvalidTestSetsTo;
158 
159  parameters[i].general.storeValidTestSetsTo = "";
160  parameters[i].general.storeInvalidTestSetsTo = "";
161  }
162  }
163 
164  void runSceneTests(std::ostringstream & os)
165  {
166  for (unsigned int i = 0; i < parameters.size(); ++i) {
168 
169  path p = path(params.general.dbfilename);
170  os << p.stem().string();
171 
172  //HillClimbing (strict)
174  runOptimization(params, os);
175 
176  //SimulatedAnnealing
179  params.optimizationAlgorithm.endTemperature = 0.00001;
181  runOptimization(params, os);
182 
183  //RecordHunt
187  runOptimization(params, os);
188 
189  os << std::endl;
190  }
191  }
192 
193  void testPerformance(const std::string & outputPath)
194  {
196 
197  std::string demoRecordingPath = outputPath + "/demoRecording.sqlite";
198 
199  params.general.dbfilename = demoRecordingPath;
200  params.general.outputDataPath = outputPath;
201 
202  RandomDemoRecorder randomDemoRecorder = *(new RandomDemoRecorder);
203 
204  ISM::TableHelperPtr tableHelper =
205  ISM::TableHelperPtr(new ISM::TableHelper(demoRecordingPath));
206 
207  std::ostringstream os;
208 
209  for (unsigned int i = 0; i < objectCounts.size(); ++i)
210  {
211  os << "Scene, Hill Climbing, Simulated Annealing, Record Hunt" << std::endl;
212 
213  for (unsigned int j = 0; j < posesPerObjectCount.size(); ++j)
214  {
215  tableHelper->dropRecordTables();
216  randomDemoRecorder.generateDemoRecording(demoRecordingPath, objectCounts[i],
217  posesPerObjectCount[j], false);
218 
219  os << objectCounts[i] << "-" << posesPerObjectCount[j];
220 
221  //HillClimbing (strict)
223  runOptimization(params, os);
224 
225  //SimulatedAnnealing
228  params.optimizationAlgorithm.endTemperature = 0.00001;
230  runOptimization(params, os);
231 
232  //RecordHunt
236  runOptimization(params, os);
237 
238  os << std::endl;
239  }
240 
241  os << std::endl;
242  }
243 
244  writeFile(outputPath, "runtimes.csv", os);
245  }
246 
247  void testScenes(const std::string & outputPath)
248  {
249  std::ostringstream os;
250  os << "Scene, Hill Climbing, Simulated Annealing, Record Hunt" << std::endl;
251 
252  prepareSceneTests(outputPath);
253  initTestSets(outputPath + "/TestSets");
254  runSceneTests(os);
255 
256  writeFile(outputPath, "runtimes.csv", os);
257  }
258 
259  int main()
260  {
261  //Uncomment and and path to the folder where the ouptu should be stored.
262 
263  //std::string outputPath = "output_path";
264  std::string performanceTestPath = outputPath + "/performanceTest";
265  std::string sceneTestPath = outputPath + "/sceneTest";
266 
267  if(!boost::filesystem::exists(outputPath))
268  boost::filesystem::create_directories(outputPath);
269 
270  if(!boost::filesystem::exists(performanceTestPath))
271  boost::filesystem::create_directories(performanceTestPath);
272 
273  if(!boost::filesystem::exists(sceneTestPath))
274  boost::filesystem::create_directories(sceneTestPath);
275 
276  //Test randomly generated scenes specified by the values in objectCounts and posesPerObjectCount.
277  testPerformance(performanceTestPath);
278 
279  //Test scenes specified in prepareSceneTests
280  testScenes(sceneTestPath);
281  }
282 
void runSceneTests(std::ostringstream &os)
void writeFile(const std::string &directoryPath, const std::string &filenName, std::ostringstream &content)
struct TopologyGeneratorParameters topologyGenerator
std::vector< unsigned > posesPerObjectCount
struct OptimizationAlgorithmParameters optimizationAlgorithm
struct ISM::CombinatorialTrainerParameters::@0 general
boost::shared_ptr< TableHelper > TableHelperPtr
void runOptimization(ISM::CombinatorialTrainerParameters params, std::ostringstream &os)
void generateDemoRecording(std::string dbfile, unsigned objects, unsigned timesteps, bool genRanOrientsAdditionally)
void initTestSets(const std::string &testSetFolderPath)
boost::shared_ptr< CombinatorialTrainer > CombinatorialTrainerPtr
ISM::CombinatorialTrainerParameters getDefaultParams()
void testScenes(const std::string &outputPath)
void prepareSceneTests(const std::string &outputPath)
std::vector< unsigned > objectCounts
std::vector< ISM::CombinatorialTrainerParameters > parameters
void testPerformance(const std::string &outputPath)


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:40