- Main Page
- Classes
- Files

- File List

# OptFrame/Util/NSSeqVectorOrOptk.hpp

    00001 #ifndef OPTFRAME_NSSEQVECTOROROPTK_HPP_
    00002 #define OPTFRAME_NSSEQVECTOROROPTK_HPP_
    00003
    00004 // Framework includes
    00005 #include "../Move.hpp"
    00006 #include "../NSSeq.hpp"
    00007
    00008 using namespace std;
    00009
    00010 // Working structure: vector<vector<T> >
    00011
    00012 template<class T, class M = OPTFRAME_DEFAULT_MEMORY>
    00013 class MoveOrOptk: public Move<vector<T> , M>
    00014 {
    00015         typedef vector<T> Route;
    00016
    00017 private:
    00018         int i; // origin
    00019         int j; // destination
    00020         int k; // number of elements
    00021
    00022 public:
    00023
    00024         MoveOrOptk(int _i, int _j, int _k) :
    00025                 i(_i), j(_j), k(_k)
    00026         {
    00027         }
    00028
    00029         virtual ~MoveOrOptk()
    00030         {
    00031         }
    00032
    00033         bool canBeApplied(const Route& rep)
    00034         {
    00035                 //return (i != j) && (i + k <= rep.size());
    00036                 return (i != j);
    00037         }
    00038
    00039         Move<Route, M>& apply(Route& rep)
    00040         {
    00041                 vector<T> v_aux;
    00042                 v_aux.insert(v_aux.begin(), rep.begin() + i, rep.begin() + i + k - 1);
    00043                 rep.erase(rep.begin() + i, rep.begin() + i + k - 1);
    00044                 rep.insert(rep.begin() + j, v_aux.begin(), v_aux.end());
    00045
    00046                 return *new MoveOrOptk(j, i, k);
    00047         }
    00048
    00049         virtual bool operator==(const Move<Route, M>& _m) const
    00050         {
    00051                 const MoveOrOptk& m1 = (const MoveOrOptk&) _m;
    00052                 return (m1.i == i) && (m1.j == j) && (m1.k == k);
    00053         }
    00054
    00055         void print()
    00056         {
    00057                 cout << "MoveVectorOrOpt{K=" << k << "}";
    00058                 cout << "(" << i << ";" << j << ")" << endl;
    00059         }
    00060 };
    00061
    00062 template<class T, class M = OPTFRAME_DEFAULT_MEMORY>
    00063 class NSIteratorOrOptk: public NSIterator<vector<T> , M>
    00064 {
    00065         typedef vector<T> Route;
    00066
    00067 private:
    00068         int n, k;
    00069         int i, j;
    00070
    00071 public:
    00072
    00073         NSIteratorOrOptk(int _n, int _k) :
    00074                 n(_n), k(_k)
    00075         {
    00076         }
    00077
    00078         virtual ~NSIteratorOrOptk()
    00079         {
    00080         }
    00081
    00082         void first()
    00083         {
    00084                 i = 0;
    00085                 j = 1;
    00086         }
    00087
    00088         void next()
    00089         {
    00090                 j++;
    00091
    00092                 if (j == i)
    00093                         j++;
    00094
    00095                 if (j > n - k)
    00096                 {
    00097                         j = 0;
    00098                         i++;
    00099                 }
    00100         }
    00101
    00102         bool isDone()
    00103         {
    00104                 return i > n - k;
    00105         }
    00106
    00107         Move<Route, M>& current()
    00108         {
    00109                 if (isDone())
    00110                 {
    00111                         cout << "There isnt any current element!" << endl;
    00112                         cout << "OrOpt{K=" << k << "}. Aborting." << endl;
    00113                         exit(1);
    00114                 }
    00115
    00116                 return *new MoveOrOptk<T, M> (i, j, k);
    00117         }
    00118 };
    00119
    00120 template<class T, class M = OPTFRAME_DEFAULT_MEMORY>
    00121 class NSSeqVectorOrOptk: public NSSeq<vector<T> , M>
    00122 {
    00123         typedef vector<T> Route;
    00124         int k;
    00125
    00126 public:
    00127
    00128         NSSeqVectorOrOptk(int _k) :
    00129                 k(_k)
    00130         {
    00131         }
    00132
    00133         virtual ~NSSeqVectorOrOptk()
    00134         {
    00135         }
    00136
    00137         Move<Route, M>& move(const Route& rep)
    00138         {
    00139                 int n = rep.size();
    00140
    00141                 if (n - k <= 0)
    00142                         return *new MoveOrOptk<T, M> (0, 0, k);
    00143
    00144                 int i = rand() % (n - k + 1);
    00145
    00146                 int j = i;
    00147                 while (i == j)
    00148                         j = rand() % (n - k + 1);
    00149
    00150                 return *new MoveOrOptk<T, M> (i, j, k);
    00151         }
    00152
    00153         virtual NSIterator<Route, M>& getIterator(const Route& r)
    00154         {
    00155                 return *new NSIteratorOrOptk<T, M> (r.size(), k);
    00156         }
    00157
    00158         virtual void print()
    00159         {
    00160                 cout << "NSSeqVectorOrOpt{K=" << k << "}" << endl;
    00161         }
    00162 };
    00163
    00164 #endif /*OPTFRAME_NSSEQVECTOROROPTK_HPP_*/

All Classes Functions