OptimizationRunner.cpp
Go to the documentation of this file.
1 
18 #include "OptimizationRunner.hpp"
19 
21 #include "WeightedSum.hpp"
22 
23 #include "../combinatorial_optimization/HillClimbingAlogrithm.hpp"
24 
25 #include "../combinatorial_optimization/ExponentialCoolingSchedule.hpp"
26 #include "../combinatorial_optimization/SimulatedAnnealingAlgorithm.hpp"
27 
28 #include "../combinatorial_optimization/RecordHuntAlgorithm.hpp"
29 #include "../combinatorial_optimization/CostDeltaAcceptanceFunction.hpp"
30 
31 namespace ISM {
32 
33  std::pair<double, TreePtr> OptimizationRunner::runOptimization(const std::string& pattern,
34  TopologyPtr startTopology)
35  {
36  mStartTime = ptime(boost::posix_time::microsec_clock::local_time());
37  mCurrentPatternName = pattern;
38 
39  logOptimizationStart(pattern);
40 
41  std::vector<TopologyPtr> startTopologies;
42  if (startTopology)
43  {
44  startTopologies.push_back(startTopology);
45  }
46 
47  prepareOptimizationRun(startTopologies);
48  TopologyPtr selectedStartTopology = selectStartTopology(startTopologies);
49 
50 
51  if (!selectedStartTopology)
52  {
53  //There was no valid start topology, optimization failed
54  return std::make_pair(-1, TreePtr());
55  }
56 
57  TopologyPtr bestTopology = mOptimizationAlgorithm->optimize(selectedStartTopology);
58 
59  //Check if hill climbing is used for the optimization
61  {
62  //Check for random restart should be performed
64  {
66  TopologyPtr randomStartTopology = mTopologyManager->getRandomTopology();
67  TopologyPtr optimizationResult = mOptimizationAlgorithm->optimize(randomStartTopology);
68 
69  startTopologies.clear();
70  startTopologies.push_back(randomStartTopology);
71  mTopologyManager->addStartTopologiesToHistory(startTopologies);
72 
73  if (optimizationResult->cost < bestTopology->cost)
74  {
75  bestTopology = optimizationResult;
76  }
77  }
78  }
79 
80  logOptimizationFinish(bestTopology);
81  documentOptimzationRun(bestTopology);
82 
83  TreePtr tree = TreePtr(new Tree(mCurrentPatternName, bestTopology->objectRelations));
84 
85  mDocumentationHelper->storeTopology(bestTopology, mCurrentPatternName, "best_topology",
86  tree->getISM()->voteSpecifiersPerObject);
87 
88  return std::make_pair(mCostFunction->calculateCost(bestTopology), tree);
89  }
90 
91  void OptimizationRunner::prepareOptimizationRun(std::vector<TopologyPtr>& startTopologies)
92  {
94 
98 
99  if (startTopologies.empty())
100  {
102  {
103  TopologyPtr randomTopology;
104  while(true)
105  {
106  randomTopology = mTopologyManager->getRandomTopology();
107  if (mCostFunction->calculateCost(randomTopology) != std::numeric_limits<double>::infinity())
108  {
109  break;
110  }
111  }
112 
113  startTopologies.push_back(randomTopology);
114  }
115  else
116  {
117  //We have no specific topologies from wich we want to start the optimization,
118  //so we will use the star topologies as our initial topologies.
119  startTopologies = mStarTopologies;
120  }
121  }
122  else
123  {
124  for (unsigned int i = 0; i < startTopologies.size(); ++i)
125  {
126  mTopologyManager->evaluateTopology(startTopologies[i], "start_topology_" + std::to_string(i));
127  }
128  }
129 
130  mTopologyManager->addStartTopologiesToHistory(startTopologies);
131  }
132 
134  {
136 
137  mStarTopologies = mTopologyManager->getStarTopologies();
139 
140  //We normalise the number of false positives between 0 (the amount of false positives
141  //of the fully meshed topology) and the highest amount of false positves of all starTopologies.
142  //The recogniton runtime is normalised between the fastest runtime which can be found among
143  //the star topologies and the runtime of the fully meshed topology.
144 
145  mMaxFalsePositives = std::numeric_limits<unsigned int>::min();
146  mMinAverageRecognitionRuntime = std::numeric_limits<double>::max();
147 
148  for (TopologyPtr starTopology : mStarTopologies)
149  {
150  EvaluationResult er = starTopology->evaluationResult;
151 
154  }
155 
156  mMinFalsePositives = mFullyMeshedTopology->evaluationResult.falsePositives;
157  mMaxAverageRecognitionRuntime = mFullyMeshedTopology->evaluationResult.averageRecognitionRuntime;
158 
159  //The number of false positives of the fully meshed topology must be 0,
160  //because we used it to classify the testsets.
161  assert(mMinFalsePositives == 0);
162 
164 
165  //Add an epsilon to mMaxAverageRecognitionRuntime to account for uncertainties in the runtime measurement.
166  double epsilon = 0.05 * mMaxAverageRecognitionRuntime;
168  }
169 
171  {
173  {
174  case 0:
175  {
179  }
180  break;
181  default:
182  std::string errorMessage = std::to_string(mCostFunctionParameters.costFunctionId)
183  + " is not a valid costFunctionId!";
184  LogHelper::logMessage(errorMessage, LOG_ERROR);
185  throw std::runtime_error(errorMessage);
186  }
187  }
188 
190  {
192  {
193  case 0:
199  ));
200  break;
201  case 1:
202  {
209  ));
210 
213  mTopologyManager, mCostFunction, coolingSchedule
214  ));
215  }
216  break;
217  case 2:
218  {
223  ));
224 
227  mTopologyManager, mCostFunction, acceptanceFunction
228  ));
229  }
230  break;
231  default:
232  std::string errorMessage = std::to_string(mOptimizationAlgorithmParameters.optimizationAlgorithmId)
233  + " is not a valid optimizationAlgorithmId!";
234  LogHelper::logMessage(errorMessage, LOG_ERROR);
235  throw std::runtime_error(errorMessage);
236  }
237  }
238 
239  TopologyPtr OptimizationRunner::selectStartTopology(std::vector<TopologyPtr>& startTopologies)
240  {
241  logStartTopologies(startTopologies);
242  TopologyPtr bestStartTopology;
243  double bestCost = std::numeric_limits<unsigned int>::max();
244 
245  for (TopologyPtr startTopology : startTopologies)
246  {
247  double cost = mCostFunction->calculateCost(startTopology);
248 
249  if (cost < bestCost)
250  {
251  bestCost = cost;
252  bestStartTopology = startTopology;
253  }
254  }
255 
256  if (!bestStartTopology)
257  {
258  return bestStartTopology;
259  }
260 
261  logSelectedStartTopology(bestStartTopology);
262  TreePtr tree = TreePtr(new Tree(mCurrentPatternName, bestStartTopology->objectRelations));
263 
265  {
266  mDocumentationHelper->storeIsm((mCurrentPatternName + "_start"), tree->getISM());
267  }
268 
269 
270  mDocumentationHelper->storeTopology(bestStartTopology, mCurrentPatternName, "selected_start_topology",
271  tree->getISM()->voteSpecifiersPerObject);
272 
274 
275  return bestStartTopology;
276  }
277 
279  {
280  ptime endTime(boost::posix_time::microsec_clock::local_time());
281  time_duration td = endTime - mStartTime;
282 
283  std::vector<std::vector<std::pair<TopologyPtr, unsigned int>>> history = mTopologyManager->getHistory();
284 
285  //Mark the best topology in the history
286  const std::string bestTopologyIdentifier = bestTopology->identifier;
287  bool markedBest = false;
288  for (unsigned int i = 0; !markedBest && i < history.size(); i++)
289  {
290  for (unsigned int j = 0; !markedBest && j < history[i].size(); j++)
291  {
292  if (history[i][j].first->identifier == bestTopologyIdentifier)
293  {
294  history[i][j].second = 4;
295  markedBest = true;
296  }
297  }
298  }
299 
300  mDocumentationHelper->storeOptimizationRun(history, mCostFunction, td.total_seconds(),
302  }
303 
305  {
306  LogHelper::logMessage("Started combinatorial optimization to find the best the ism for the pattern " +
309  }
310 
312  {
313  std::ostringstream oss;
314  oss <<"Training for pattern " + mCurrentPatternName + " is done!" << std::endl << std::endl
315  <<"The overall best topology was: " << bestTopology->objectRelations << std::endl
316  << " with the evaluation result of its ISM beeing: " << std::endl
317  << bestTopology->evaluationResult.getDescription() << std::endl << std::endl
318  << "The cost of this topology was: " << std::endl
319  << mCostFunction->calculateCost(bestTopology);
322  }
323 
325  {
326  std::ostringstream oss;
327  oss << "Selected the following topology as the best start Topology."
328  << std::endl << std::endl << bestStartTopology->objectRelations
329  << "with the evaluation result of its ISM beeing: " << std::endl
330  << bestStartTopology->evaluationResult.getDescription() << std::endl << std::endl
331  << "The cost of this topology is: " << std::endl
332  << mCostFunction->calculateCost(bestStartTopology) << std::endl << std::endl
333  << "Generating its tree again: " << std::endl;
335  }
336 
338  {
339  std::ostringstream oss;
340  oss << "Evaluating all star topologies and the fully meshed topology "
341  << "to find the lowest and highest false positives and the slowest and fastest average recogniton runtime. "
342  << "Those values will be used to normalise the false positives and the recognition runtime of other topologies."
343  << std::endl << std::endl;
345  }
346 
348  {
349  std::ostringstream oss;
350  oss << "Evaluated all star topologies and the fully meshed topology. The results are:" << std::endl << std::endl
351  << "\t Lowest false positives: " << mMinFalsePositives << std::endl
352  << "\t Highest false positives: " << mMaxFalsePositives << std::endl
353  << "\t Shortest average recognition runtime: " << mMinAverageRecognitionRuntime << "s" << std::endl
354  << "\t Longest average recognition runtime: " << mMaxAverageRecognitionRuntime << "s" << std::endl;
357  }
358 
360  {
361  std::ostringstream oss;
362  oss << "The start topology was invalid. Optimzation Failed!";
365  }
366 
368  {
370  std::ostringstream oss;
371  oss << "Performing random restart!" << std::endl;
373  }
374 
375  void OptimizationRunner::logStartTopologies(const std::vector<TopologyPtr> & startTopologies)
376  {
377  std::ostringstream oss;
378  oss << "Possible start topologies are: " << std::endl << std::endl;
379  for (unsigned int i = 0; i < startTopologies.size(); ++i)
380  {
381  oss << "\t- Topology " << startTopologies[i]->identifier << std::endl;
382  }
383 
386  }
387 
388 
389 }
void logSelectedStartTopology(TopologyPtr bestStartTopology)
boost::shared_ptr< RecordHuntAlgorithm< InstanceType >> RecordHuntAlgorithmPtr
OptimizationAlgorithmPtr< TopologyPtr > mOptimizationAlgorithm
void logStartTopologies(const std::vector< TopologyPtr > &startTopologies)
boost::shared_ptr< CostFunction< InstanceType >> CostFunctionPtr
std::pair< double, TreePtr > runOptimization(const std::string &pattern, TopologyPtr startTopology=NULL)
CostFunctionParameters mCostFunctionParameters
static void logLine(LogLevel logLevel=LOG_INFO)
Definition: LogHelper.cpp:101
boost::shared_ptr< AcceptanceFunction > AcceptanceFunctionPtr
void logOptimizationFinish(TopologyPtr bestTopology)
std::string patternName
boost::shared_ptr< CostDeltaAcceptanceFunction > CostDeltaAcceptanceFunctionPtr
void prepareOptimizationRun(std::vector< TopologyPtr > &startTopologies)
boost::shared_ptr< Topology > TopologyPtr
Definition: Topology.hpp:51
boost::shared_ptr< SimulatedAnnealingAlgorithm< InstanceType >> SimulatedAnnealingAlgorithmPtr
TopologyPtr selectStartTopology(std::vector< TopologyPtr > &startTopologies)
static void logMessage(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
Definition: LogHelper.cpp:96
TopologyManagerPtr mTopologyManager
boost::shared_ptr< CoolingSchedule > CoolingSchedulePtr
boost::shared_ptr< ExponentialCoolingSchedule > ExponentialCoolingSchedulePtr
CostFunctionPtr< TopologyPtr > mCostFunction
boost::shared_ptr< Tree > TreePtr
Definition: typedef.hpp:46
DocumentationHelperPtr mDocumentationHelper
void logOptimizationStart(const std::string &patternName)
OptimizationAlgorithmParameters mOptimizationAlgorithmParameters
std::vector< TopologyPtr > mStarTopologies
std::uniform_real_distribution< double > mDistribution
this namespace contains all generally usable classes.
void documentOptimzationRun(TopologyPtr bestTopology)
std::default_random_engine mGenerator
boost::shared_ptr< HillClimbingAlogrithm< InstanceType >> HillClimbingAlogrithmPtr
const char * LOG_COLOR_OPTIMIZATION_STRATEGY


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