- Main Page
- Classes
- Files

- File List

# OptFrame/Examples/EternityII/InitialSolutionRandom.hpp

    00001 #ifndef EtII_INITIALSOLUTION_Random_HPP_
    00002 #define EtII_INITIALSOLUTION_Random_HPP_
    00003
    00004 #include "../../../OptFrame/InitialSolution.h"
    00005 #include "../../../OptFrame/Util/TestSolution.hpp"
    00006
    00007 #include "../../RandGen.hpp"
    00008
    00009 #include "ProblemInstance.hpp"
    00010
    00011 #include "Representation.h"
    00012 #include "Solution.h"
    00013
    00014 #include "Evaluator.hpp"
    00015
    00016 #include <list>
    00017
    00018 #include <algorithm>
    00019 #include <stdlib.h>
    00020
    00021 using namespace std;
    00022
    00023 class EtIIInitialSolutionRandom: public InitialSolution<RepEtII>
    00024 {
    00025 private:
    00026         EtIIProblemInstance& pEtII;
    00027         RandGen& rg;
    00028         // Your private vars
    00029
    00030 public:
    00031
    00032         EtIIInitialSolutionRandom(EtIIProblemInstance& _pEtII, RandGen& _rg) : // If necessary, add more parameters
    00033                 pEtII(_pEtII), rg(_rg)
    00034         {
    00035         }
    00036
    00037         SolutionEtII& generateSolution()
    00038         {
    00039                 RepEtII* tab = new RepEtII(pEtII.height, pEtII.width);
    00040
    00041                 vector<Piece> corner_pieces;
    00042                 vector<Piece> side_pieces;
    00043                 vector<Piece> center_pieces;
    00044
    00045                 for (int i = 0; i < pEtII.pieces.size(); i++)
    00046                 {
    00047                         int zeros = 0;
    00048
    00049                         if (pEtII.pieces[i].down == 0)
    00050                                 zeros++;
    00051                         if (pEtII.pieces[i].left == 0)
    00052                                 zeros++;
    00053                         if (pEtII.pieces[i].up == 0)
    00054                                 zeros++;
    00055                         if (pEtII.pieces[i].right == 0)
    00056                                 zeros++;
    00057
    00058                         if (zeros == 2)
    00059                                 corner_pieces.push_back(pEtII.pieces[i]);
    00060                         if (zeros == 1)
    00061                                 side_pieces.push_back(pEtII.pieces[i]);
    00062                         if (zeros == 0)
    00063                                 center_pieces.push_back(pEtII.pieces[i]);
    00064                 }
    00065
    00066                 int x;
    00067                 Piece p;
    00068
    00069                 // random one piece for left-upper corner
    00070                 x = rg.rand(corner_pieces.size());
    00071                 p = corner_pieces[x];
    00072                 corner_pieces.erase(corner_pieces.begin() + x);
    00073
    00074                 while ((p.left != 0) || (p.up != 0))
    00075                         p.rotate();
    00076
    00077                 (*tab)(0, 0) = p;
    00078
    00079                 // random one piece for right-upper corner
    00080                 x = rg.rand(corner_pieces.size());
    00081                 p = corner_pieces[x];
    00082                 corner_pieces.erase(corner_pieces.begin() + x);
    00083
    00084                 while ((p.right != 0) || (p.up != 0))
    00085                         p.rotate();
    00086
    00087                 (*tab)(0, tab->getCols() - 1) = p;
    00088
    00089                 // random one piece for right-lower corner
    00090                 x = rg.rand(corner_pieces.size());
    00091                 p = corner_pieces[x];
    00092                 corner_pieces.erase(corner_pieces.begin() + x);
    00093
    00094                 while ((p.right != 0) || (p.down != 0))
    00095                         p.rotate();
    00096
    00097                 (*tab)(tab->getRows() - 1, tab->getCols() - 1) = p;
    00098
    00099                 // random one piece for left-lower corner
    00100                 x = rg.rand(corner_pieces.size());
    00101                 p = corner_pieces[x];
    00102                 corner_pieces.erase(corner_pieces.begin() + x);
    00103
    00104                 while ((p.left != 0) || (p.down != 0))
    00105                         p.rotate();
    00106
    00107                 (*tab)(tab->getRows() - 1, 0) = p;
    00108
    00109                 // random the pieces for upper-bottom sides
    00110                 for (int i = 1; i < tab->getCols() - 1; i++)
    00111                 {
    00112                         // top
    00113                         x = rg.rand(side_pieces.size());
    00114                         p = side_pieces[x];
    00115                         side_pieces.erase(side_pieces.begin() + x);
    00116
    00117                         while (p.up != 0)
    00118                                 p.rotate();
    00119                         (*tab)(0, i) = p;
    00120
    00121                         // bottom
    00122                         x = rg.rand(side_pieces.size());
    00123                         p = side_pieces[x];
    00124                         side_pieces.erase(side_pieces.begin() + x);
    00125
    00126                         while (p.down != 0)
    00127                                 p.rotate();
    00128                         (*tab)(tab->getRows() - 1, i) = p;
    00129                 }
    00130
    00131                 // random the pieces for left-right sides
    00132                 for (int i = 1; i < tab->getRows() - 1; i++)
    00133                 {
    00134                         // left
    00135                         x = rg.rand(side_pieces.size());
    00136                         p = side_pieces[x];
    00137                         side_pieces.erase(side_pieces.begin() + x);
    00138
    00139                         while (p.left != 0)
    00140                                 p.rotate();
    00141                         (*tab)(i, 0) = p;
    00142
    00143                         // right
    00144                         x = rg.rand(side_pieces.size());
    00145                         p = side_pieces[x];
    00146                         side_pieces.erase(side_pieces.begin() + x);
    00147
    00148                         while (p.right != 0)
    00149                                 p.rotate();
    00150                         (*tab)(i, tab->getCols() - 1) = p;
    00151                 }
    00152
    00153                 // random the center pieces
    00154                 for (int i = 1; i < tab->getRows() - 1; i++)
    00155                         for (int j = 1; j < tab->getCols() - 1; j++)
    00156                         {
    00157                                 x = rg.rand(center_pieces.size());
    00158                                 p = center_pieces[x];
    00159                                 center_pieces.erase(center_pieces.begin() + x);
    00160                                 (*tab)(i, j) = p;
    00161
    00162                                 int nRotation = rg.rand(4);
    00163                                 for (int r = 0; r < nRotation; r++)
    00164                                         (*tab)(i, j).rotate();
    00165                         }
    00166
    00167                 if (corner_pieces.size() > 0 || side_pieces.size() > 0 || center_pieces.size() > 0)
    00168                 {
    00169                         cout << "Warning: construction problem!" << endl;
    00170                 }
    00171
    00172                 return *new TestSolution<RepEtII> (*tab);
    00173         }
    00174
    00175 };
    00176
    00177 #endif /*EtII_INITIALSOLUTION_Random_HPP_*/

All Classes Functions