torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
placer/Placer.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the Placer class.
18 
19 #ifndef TORC_PLACER_PLACER_HPP
20 #define TORC_PLACER_PLACER_HPP
21 
25 #include <boost/timer.hpp>
26 
27 namespace torc {
28 namespace placer {
29 
30  /// \brief Simulated annealing algorithm class.
31  class Placer {
32  protected:
33  //types
35  typedef boost::uint32_t uint32;
36 
37  DDB& mDB;
40 
41  public:
42  // the heuristic may be the site mapping structure...
43  // heuristic should contain most of the numeric constants here
44  // probably functions for cooling schedule and such as well
45  // in fact, the heuristic may wrap the placement entirely... as we do in routing
46  Placer(DDB& inDB, PlacerHeuristicBase& inHeuristic) : mDB(inDB), mHeuristic(inHeuristic) {}
47 
48  ~Placer() {}
49 
50  void generatePlacement(Placement& placement) {
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  }
175 
176  }; // class Placer
177 } // namespace placer
178 } // namespace torc
179 
180 #endif // TORC_PLACER_PLACER_HPP
boost::uint32_t uint32
Simulated annealing algorithm class.
Device database, including complete wiring and logic support.
Definition: DDB.hpp:42
Placer(DDB &inDB, PlacerHeuristicBase &inHeuristic)
Header for the Placement class.
Wrapper of the Design for placing the design.
Definition: Placement.hpp:39
PlacerHeuristicBase & mHeuristic
void updateCostFull(bool debug)
Definition: Placement.hpp:303
Simulated annealing algorithm class.
Header for the Placer class.
architecture::DDB DDB
void generatePlacement(Placement &placement)
Header for the DDB class.
void undoMove(bool debug)
Definition: Placement.hpp:419
bool randomMove(bool debug)
Definition: Placement.hpp:365