HillClimbingAlogrithm.hpp
Go to the documentation of this file.
1 
18 #pragma once
19 
20 #include <boost/shared_ptr.hpp>
21 #include <random>
24 
25 namespace ISM {
26 
27 template <class InstanceType>
28 class HillClimbingAlogrithm : public OptimizationAlgorithm<InstanceType>
29 {
30  public:
32  CostFunctionPtr<InstanceType> costFunction, bool randomWalkProbability = 0.0)
33  : OptimizationAlgorithm<InstanceType>(neighbourhoodFunction, costFunction)
34  , mRandomWalkProbability(randomWalkProbability)
35  {}
36 
37  InstanceType optimize(InstanceType startInstance)
38  {
39  InstanceType selectedInstance = startInstance;
40 
41  double lowestCost = this->mCostFunction->calculateCost(selectedInstance);
42  bool keepOptimizing = true;
43 
44  while (keepOptimizing)
45  {
46  keepOptimizing = false;
47 
48  this->mNeighbourhoodFunction->setReferenceInstance(selectedInstance);
49 
50  if (this->mNeighbourhoodFunction->hasNextNeighbour())
51  {
52  //Check if random walk shuld be performed
54  {
55  selectedInstance = this->mNeighbourhoodFunction->getNextNeighbour();
56  lowestCost = this->mCostFunction->calculateCost(selectedInstance);
57  keepOptimizing = true;
58  }
59  else
60  {
61  do
62  {
63  InstanceType nextInstance = this->mNeighbourhoodFunction->getNextNeighbour();
64  double nextInstanceCost = this->mCostFunction->calculateCost(nextInstance);
65 
66  if (nextInstanceCost < lowestCost)
67  {
68  selectedInstance = nextInstance;
69  lowestCost = nextInstanceCost;
70  keepOptimizing = true;
71  }
72  } while (this->mNeighbourhoodFunction->hasNextNeighbour());
73  }
74  }
75  else
76  {
77  //The selected instance has no neighbours, we can't continue the optimization
78  return selectedInstance;
79  }
80 
81  }
82 
83  return selectedInstance;
84  }
85 
86  private:
88  std::default_random_engine mGenerator;
89  std::uniform_real_distribution<double> mDistribution = std::uniform_real_distribution<double>(0.0, 1.0);
90 };
91 
92 template<class InstanceType>
93 using HillClimbingAlogrithmPtr = boost::shared_ptr<HillClimbingAlogrithm<InstanceType>>;
94 
95 }
InstanceType optimize(InstanceType startInstance)
boost::shared_ptr< CostFunction< InstanceType >> CostFunctionPtr
CostFunctionPtr< InstanceType > mCostFunction
NeighbourhoodFunctionPtr< InstanceType > mNeighbourhoodFunction
std::default_random_engine mGenerator
std::uniform_real_distribution< double > mDistribution
HillClimbingAlogrithm(NeighbourhoodFunctionPtr< InstanceType > neighbourhoodFunction, CostFunctionPtr< InstanceType > costFunction, bool randomWalkProbability=0.0)
boost::shared_ptr< NeighbourhoodFunction< InstanceType >> NeighbourhoodFunctionPtr
this namespace contains all generally usable classes.
boost::shared_ptr< HillClimbingAlogrithm< InstanceType >> HillClimbingAlogrithmPtr


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