RecordHuntAlgorithm.hpp
Go to the documentation of this file.
1 
18 #pragma once
19 
20 #include <boost/shared_ptr.hpp>
23 #include "AcceptanceFunction.hpp"
24 
25 namespace ISM {
26 
27 using boost::filesystem::path;
28 
29 template <class InstanceType>
30 class RecordHuntAlgorithm : public OptimizationAlgorithm<InstanceType>
31 {
32  public:
34  CostFunctionPtr<InstanceType> costFunction,
35  AcceptanceFunctionPtr acceptanceFunction)
36  : OptimizationAlgorithm<InstanceType>(neighbourhoodFunction, costFunction)
37  , mAcceptanceFunction(acceptanceFunction)
38  {}
39 
40  InstanceType optimize(InstanceType startInstance)
41  {
42 
43  mAcceptanceFunction->reset();
44 
45  InstanceType selectedInstance = startInstance;
46  InstanceType bestInstance = startInstance;
47 
48  double lowestCost = this->mCostFunction->calculateCost(bestInstance);
49  bool keepOptimizing = true;
50 
51  while (keepOptimizing)
52  {
53  keepOptimizing = false;
54 
55  this->mNeighbourhoodFunction->setReferenceInstance(selectedInstance);
56  double selectedInstanceCost = this->mCostFunction->calculateCost(selectedInstance);
57 
58 
59  if (this->mNeighbourhoodFunction->hasNextNeighbour())
60  {
61  do
62  {
63  InstanceType nextInstance = this->mNeighbourhoodFunction->getNextNeighbour();
64  double nextInstanceCost = this->mCostFunction->calculateCost(nextInstance);
65 
66  if (mAcceptanceFunction->isNewCostAcceptable(nextInstanceCost, selectedInstanceCost))
67  {
68  keepOptimizing = true;
69  selectedInstance = nextInstance;
70  mAcceptanceFunction->update();
71 
72  if (nextInstanceCost < lowestCost)
73  {
74  bestInstance = nextInstance;
75  lowestCost = nextInstanceCost;
76  }
77 
78  break;
79  }
80 
81  } while (this->mNeighbourhoodFunction->hasNextNeighbour());
82  }
83  else
84  {
85  //The selected instance has no neighbours, we can't continue the optimization
86  return bestInstance;
87  }
88  }
89 
90  return bestInstance;
91  }
92 
93  private:
95 
96 };
97 
98 template<class InstanceType>
99 using RecordHuntAlgorithmPtr = boost::shared_ptr<RecordHuntAlgorithm<InstanceType>>;
100 
101 }
boost::shared_ptr< RecordHuntAlgorithm< InstanceType >> RecordHuntAlgorithmPtr
boost::shared_ptr< CostFunction< InstanceType >> CostFunctionPtr
boost::shared_ptr< AcceptanceFunction > AcceptanceFunctionPtr
CostFunctionPtr< InstanceType > mCostFunction
NeighbourhoodFunctionPtr< InstanceType > mNeighbourhoodFunction
boost::shared_ptr< NeighbourhoodFunction< InstanceType >> NeighbourhoodFunctionPtr
this namespace contains all generally usable classes.
RecordHuntAlgorithm(NeighbourhoodFunctionPtr< InstanceType > neighbourhoodFunction, CostFunctionPtr< InstanceType > costFunction, AcceptanceFunctionPtr acceptanceFunction)
AcceptanceFunctionPtr mAcceptanceFunction
InstanceType optimize(InstanceType startInstance)


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