00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef BTREEANNEAL_H
00042 #define BTREEANNEAL_H
00043
00044 #include "basepacking.h"
00045 #include "mixedpacking.h"
00046 #include "btree.h"
00047 #include "btreeslackeval.h"
00048 #include "btreecompact.h"
00049 #include "baseannealer.h"
00050
00051 #include "debug.h"
00052
00053
00054
00055
00056
00057
00058 #include "allparquet.h"
00059
00060 #include <vector>
00061 #include <cfloat>
00062 using namespace std;
00063
00064
00065 class BTreeAreaWireAnnealer : public BaseAnnealer
00066 {
00067 public:
00068 BTreeAreaWireAnnealer(const parquetfp::Command_Line *const params,
00069 parquetfp::DB *const db);
00070
00071 BTreeAreaWireAnnealer(MixedBlockInfoType& nBlockinfo,
00072 const parquetfp::Command_Line *const params,
00073 parquetfp::DB *const db);
00074
00075 inline virtual ~BTreeAreaWireAnnealer();
00076
00077 virtual bool go();
00078 bool anneal();
00079
00080 virtual bool packOneBlock();
00081
00082 inline const BTree& currSolution() const;
00083 inline const BTree& bestSolution() const;
00084
00085 virtual void takePlfromDB();
00086 virtual void compactSoln();
00087
00088 int makeMove(int& indexOrient,
00089 parquetfp::ORIENT& newOrient,
00090 parquetfp::ORIENT& oldOrient);
00091 int makeMoveSlacks();
00092 int makeMoveSlacksOrient() { return 10; }
00093 int makeMoveOrient() { return 10; }
00094 int makeARMove();
00095 int makeSoftBlMove(int& index, double& newWidth, double& newHeight);
00096
00097 inline int makeIndexSoftBlMove(int index,
00098 double& newWidth, double& newHeight);
00099
00100
00101 int makeHPWLMove();
00102 int makeARWLMove();
00103
00104 int packSoftBlocks(int numIter);
00105 inline int compactBlocks();
00106
00107 inline static void sort_slacks(const vector<double>& slacks,
00108 vector<int>& indices_sorted);
00109 void GenerateRandomSoln(BTree& soln, int blocknum) const;
00110
00111 protected:
00112 MixedBlockInfoType *const _blockinfo_cleaner;
00113 MixedBlockInfoType& _blockinfo;
00114
00115 public:
00116 const MixedBlockInfoType& blockinfo;
00117
00118 protected:
00119
00120 BTree in_curr_solution;
00121 BTree in_next_solution;
00122 BTree in_best_solution;
00123
00124
00125 vector< vector<parquetfp::ORIENT> > _physicalOrient;
00126
00127 BTreeSlackEval *const _slackEval;
00128
00129 void constructor_core();
00130 inline BTree::MoveType get_move() const;
00131 inline void perform_swap();
00132 inline void perform_move();
00133 inline void perform_rotate();
00134 inline void perform_rotate(int& blk,
00135 parquetfp::ORIENT& newOrient,
00136 parquetfp::ORIENT& oldOrient);
00137 class SlackInfo
00138 {
00139 public:
00140 double slack;
00141 int index;
00142
00143 inline bool operator <(const SlackInfo& si) const
00144 { return slack < si.slack; }
00145 };
00146
00147 void DBfromSoln(const BTree& soln);
00148
00149 void makeMoveSlacksCore(bool);
00150 void locateSearchBlocks(int, vector<int>&);
00151
00152
00153 int getSoftBlIndex(bool horizontal) const;
00154 int getSoftBlNewDimensions(int index,
00155 double& newWidth, double& newHeight) const;
00156
00157
00158 BTreeAreaWireAnnealer(MixedBlockInfoType& nBlockinfo);
00159
00160 };
00161
00162
00163
00164
00165
00166 BTreeAreaWireAnnealer::~BTreeAreaWireAnnealer()
00167 {
00168 if (_slackEval != NULL)
00169 delete _slackEval;
00170
00171 if (_blockinfo_cleaner != NULL)
00172 delete _blockinfo_cleaner;
00173 }
00174
00175 const BTree& BTreeAreaWireAnnealer::currSolution() const
00176 { return in_curr_solution; }
00177
00178 const BTree& BTreeAreaWireAnnealer::bestSolution() const
00179 { return in_best_solution; }
00180
00181 int BTreeAreaWireAnnealer::makeIndexSoftBlMove(int index,
00182 double& newWidth,
00183 double& newHeight)
00184 {
00185 _slackEval->evaluateSlacks(in_curr_solution);
00186 return getSoftBlNewDimensions(index, newWidth, newHeight);
00187 }
00188
00189 int BTreeAreaWireAnnealer::compactBlocks()
00190 {
00191 BTreeCompactor compactor(in_curr_solution);
00192 compactor.compact();
00193 in_curr_solution = compactor;
00194 return MISC;
00195 }
00196
00197 void BTreeAreaWireAnnealer::sort_slacks(const vector<double>& slacks,
00198 vector<int>& indices_sorted)
00199 {
00200 int blocknum = slacks.size();
00201 vector<SlackInfo> slackinfo(blocknum);
00202 for (int i = 0; i < blocknum; i++)
00203 {
00204 slackinfo[i].slack = slacks[i];
00205 slackinfo[i].index = i;
00206 }
00207 sort(slackinfo.begin(), slackinfo.end());
00208
00209 indices_sorted.resize(blocknum);
00210 for (int i = 0; i < blocknum; i++)
00211 indices_sorted[i] = slackinfo[i].index;
00212 }
00213
00214 BTree::MoveType BTreeAreaWireAnnealer::get_move() const
00215 {
00216
00217 double rand_num = rand() / (RAND_MAX + 1.0);
00218 if (rand_num < 0.3333)
00219 return BTree::SWAP;
00220 else if (rand_num < 0.6666)
00221 return BTree::ROTATE;
00222 else
00223 return BTree::MOVE;
00224 }
00225
00226 void BTreeAreaWireAnnealer::perform_swap()
00227 {
00228 int blocknum = blockinfo.currDimensions.blocknum();
00229 int blkA = int(blocknum * (rand() / (RAND_MAX + 1.0)));
00230 int blkB = int((blocknum-1) * (rand() / (RAND_MAX + 1.0)));
00231 blkB = (blkB >= blkA)? blkB+1 : blkB;
00232
00233 in_next_solution = in_curr_solution;
00234 in_next_solution.swap(blkA, blkB);
00235 }
00236
00237 void BTreeAreaWireAnnealer::perform_rotate()
00238 {
00239 int blocknum = blockinfo.currDimensions.blocknum();
00240 int blk = int(blocknum * (rand() / (RAND_MAX + 1.0)));
00241 int blk_orient = in_curr_solution.tree[blk].orient;
00242 int new_orient = (blk_orient+1) % 8;
00243 new_orient = _physicalOrient[blk][new_orient];
00244
00245
00246 in_next_solution.setTree(in_curr_solution.tree);
00247 in_next_solution.rotate(blk, new_orient);
00248 }
00249
00250 void BTreeAreaWireAnnealer::perform_move()
00251 {
00252 int blocknum = blockinfo.currDimensions.blocknum();
00253 int blk = int(blocknum * (rand() / (RAND_MAX + 1.0)));
00254
00255 int target_rand_num = int((2*blocknum-1) * (rand() / (RAND_MAX + 1.0)));
00256 int target = target_rand_num / 2;
00257 target = (target >= blk)? target+1 : target;
00258
00259 int leftChild = target_rand_num % 2;
00260
00261
00262 in_next_solution.setTree(in_curr_solution.tree);
00263 in_next_solution.move(blk, target, (leftChild == 0));
00264 }
00265
00266 void BTreeAreaWireAnnealer::perform_rotate(int& blk,
00267 parquetfp::ORIENT& newOrient,
00268 parquetfp::ORIENT& oldOrient)
00269 {
00270 int blocknum = blockinfo.currDimensions.blocknum();
00271 blk = int(blocknum * (rand() / (RAND_MAX + 1.0)));
00272 int blk_orient = in_curr_solution.tree[blk].orient;
00273 int new_orient = blk_orient;
00274 if (_params->minWL)
00275 {
00276 while (new_orient == blk_orient)
00277 new_orient = (blk_orient + rand() % 8) % 8;
00278 }
00279 else
00280 new_orient = (blk_orient+1) % 8;
00281 new_orient = _physicalOrient[blk][new_orient];
00282
00283
00284 in_next_solution.setTree(in_curr_solution.tree);
00285 in_next_solution.rotate(blk, new_orient);
00286
00287 newOrient = parquetfp::ORIENT(new_orient);
00288 oldOrient = parquetfp::ORIENT(blk_orient);
00289 }
00290
00291
00292 #endif