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


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