torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
torc::placer::Placer Class Reference

Simulated annealing algorithm class. More...

#include <Placer.hpp>

+ Collaboration diagram for torc::placer::Placer:

Public Member Functions

 Placer (DDB &inDB, PlacerHeuristicBase &inHeuristic)
 
 ~Placer ()
 
void generatePlacement (Placement &placement)
 

Protected Types

typedef architecture::DDB DDB
 
typedef boost::uint32_t uint32
 

Protected Attributes

DDBmDB
 
PlacerHeuristicBasemHeuristic
 
uint32 mMovesPerTemperature
 

Detailed Description

Simulated annealing algorithm class.

Definition at line 31 of file placer/Placer.hpp.

Member Typedef Documentation

Definition at line 34 of file placer/Placer.hpp.

typedef boost::uint32_t torc::placer::Placer::uint32
protected

Definition at line 35 of file placer/Placer.hpp.

Constructor & Destructor Documentation

torc::placer::Placer::Placer ( DDB inDB,
PlacerHeuristicBase inHeuristic 
)
inline

Definition at line 46 of file placer/Placer.hpp.

46 : mDB(inDB), mHeuristic(inHeuristic) {}
PlacerHeuristicBase & mHeuristic
torc::placer::Placer::~Placer ( )
inline

Definition at line 48 of file placer/Placer.hpp.

48 {}

Member Function Documentation

void torc::placer::Placer::generatePlacement ( Placement placement)
inline

Definition at line 50 of file placer/Placer.hpp.

50  {
51  //double initialtemperature = 10000;
52  placement.updateCostFull(false);
53  int currentCost = placement.getCost();
54  int newCost = 9999999;
55  int goodMoves = 0;
56  int movesUndone = 0;
57  int acceptedBad = 0;
58  int zeroCostMoves = 0;
59  int illegalMoves = 0;
60  int bestCost = 999999;
61  bool done = false;
62  double temperature = mHeuristic.getInitialTemperature();
63  double acceptrate = 0;
64  int doneCount = 0;
65 
66  boost::timer epochTimer;
67  boost::timer totalTimer;
68 
70  //mMovesPerTemperature = 10 * sqrt(pow(placement.getNumInstances(), 3));
71  //mMovesPerTemperature = 100000;
72  std::cout << "Moves per temperature: " << mMovesPerTemperature << std::endl;
73  //placement.updateCostFull(false);
74  while (!done) {
75  epochTimer.restart();
76  //placement.updateCostFull(false);
77  goodMoves = 0;
78  movesUndone = 0;
79  acceptedBad = 0;
80  zeroCostMoves = 0;
81  illegalMoves = 0;
82  std::cout << "currentCost for epoch: " << currentCost;
83 
84  for (uint32 movei = 0; movei < mMovesPerTemperature; movei++) {
85  if (!placement.randomMove(false)) {
86  //std::cout << "FAILED MOVED" << std::endl;
87  illegalMoves++;
88  continue;
89  }
90  newCost = placement.getCost();
91  if (newCost < bestCost) {
92  bestCost = newCost;
93  //fp.savePlacement();
94  }
95  if (currentCost < newCost) {
96  //if (mRandom.nextDouble() >=
97  // Math.exp(((double)currentCost - (double)newCost / temperature))
98  double irand = (double)rand() / (double)(std::numeric_limits<int>::max());
99  if (irand >= exp(((double)currentCost - (double)newCost) / temperature)) {
100  movesUndone++;
101  placement.undoMove(false);
102  //std::cout << " UNDO" << std::endl;
103  } else {
104  acceptedBad++;
105  currentCost = newCost;
106  //std::cout << " ACCEPT BAD" << std::endl;
107  }
108  } else if (currentCost == newCost) {
109  zeroCostMoves++;
110  } else {
111  goodMoves++;
112  currentCost = newCost;
113  }
114  }
115 
116  double epochTime = epochTimer.elapsed();
117  //currentCost = newCost;
118  acceptrate = (double)acceptedBad / ((double)acceptedBad + (double)movesUndone);
119 
120  // temperature adjustment
121  double tempadjust = 0;
122  if (acceptrate > 0.96) {
123  temperature = 0.5 * temperature;
124  tempadjust = (double)0.5;
125  } else if (acceptrate <= 0.96 && acceptrate > 0.8) {
126  temperature = 0.9 * temperature;
127  tempadjust = (double)0.9;
128  } else if (acceptrate <= 0.8 && acceptrate > 0.15) {
129  temperature = 0.95 * temperature;
130  tempadjust = (double)0.95;
131  } else {
132  temperature = 0.8 * temperature;
133  tempadjust = (double)0.8;
134  }
135 
136  // termination condition
137  //if (acceptedBad == 0)
138  if (goodMoves == 0) {
139  doneCount++;
140  } else {
141  doneCount = 0;
142  }
143  if (doneCount == 10) {
144  done = true;
145  }
146 
147  //%.3f%n
148  std::cout << " Good moves: " << goodMoves;
149  std::cout << " Accepted bad: " << acceptedBad;
150  std::cout << " Undone: " << movesUndone;
151  std::cout << " ZeroCostMoves: " << zeroCostMoves;
152  std::cout << " Best Cost: " << bestCost;
153  std::cout << " Attemped Illegal Moves: " << illegalMoves;
154  std::cout << " Acceptance rate: " << acceptrate;
155  std::cout << " new temp: " << temperature;
156  std::cout << " tempadjust: " << tempadjust;
157  //std::cout << " time: " << epochTimer.elapsed();
158  std::cout << " time: " << epochTime;
159  //std::cout << " | ";
160  std::cout << std::endl;
161  //fp.getCost(temperature, initialtemperature, true);
162  }
163  std::cout << "Total time: " << totalTimer.elapsed();
164  std::cout << std::endl;
165  //fp.displayPlacementWithCost();
166  //std::cout << "Here's where we ended:";
167  //fp.displayPlacement();
168  //((AmorphousFloorPlan)fp).getCostAllPairsDistance(true);
169  //fp.restorePlacement();
170  //fp.validatePlacement();
171  //fp.displayPlacement();
172  placement.updateCostFull(false);
173  std::cout << "Actual end cost: " << placement.getCost() << std::endl;
174  }
boost::uint32_t uint32
PlacerHeuristicBase & mHeuristic

+ Here is the call graph for this function:

Field Documentation

DDB& torc::placer::Placer::mDB
protected

Definition at line 37 of file placer/Placer.hpp.

PlacerHeuristicBase& torc::placer::Placer::mHeuristic
protected

Definition at line 38 of file placer/Placer.hpp.

uint32 torc::placer::Placer::mMovesPerTemperature
protected

Definition at line 39 of file placer/Placer.hpp.


The documentation for this class was generated from the following file: