00001 #ifndef OPTFRAME_NSSEQVVSWAPINTRA_HPP_
00002 #define OPTFRAME_NSSEQVVSWAPINTRA_HPP_
00003
00004
00005 #include "../Move.hpp"
00006 #include "../NSEnum.hpp"
00007
00008 using namespace std;
00009
00010
00011
00012 template<class T, class M = OPTFRAME_DEFAULT_MEMORY>
00013 class MoveVVSwapIntra: public Move<vector<vector<T> > , M>
00014 {
00015 typedef vector<vector<T> > Routes;
00016
00017 private:
00018 int i, j, k;
00019
00020 public:
00021
00022 MoveVVSwapIntra(int i, int j, int k)
00023 {
00024 this->i = i;
00025 this->j = j;
00026 this->k = k;
00027 }
00028
00029 virtual ~MoveVVSwapIntra()
00030 {
00031 }
00032
00033 bool canBeApplied(const Routes& rep)
00034 {
00035 return (j != k);
00036 }
00037
00038 Move<Routes, M>& apply(Routes& rep)
00039 {
00040 T aux = rep[i][j];
00041 rep[i][j] = rep[i][k];
00042 rep[i][k] = aux;
00043
00044 return *new MoveVVSwapIntra(i, k, j);
00045 }
00046
00047 virtual bool operator==(const Move<Routes, M>& _m) const
00048 {
00049 const MoveVVSwapIntra& m = (const MoveVVSwapIntra&) _m;
00050 return (m.i == i) && ((m.j == j && m.k == k) || (m.j == k && m.k == j));
00051 }
00052
00053 void print()
00054 {
00055 cout << "MoveVVSwapIntra( " << i << " , ( " << j << " , " << k << " ) )" << endl;
00056 }
00057 };
00058
00059 template<class T, class M = OPTFRAME_DEFAULT_MEMORY>
00060 class NSIteratorVVSwapIntra: public NSIterator<vector<vector<T> > , M>
00061 {
00062 typedef vector<vector<T> > Routes;
00063 const Routes& routes;
00064
00065 int m_i, m_j, m_k;
00066 public:
00067
00068 NSIteratorVVSwapIntra(const Routes& r) :
00069 routes(r)
00070 {
00071 }
00072
00073 virtual ~NSIteratorVVSwapIntra()
00074 {
00075 }
00076
00077 void first()
00078 {
00079
00080 m_i = 0;
00081 m_j = 0;
00082 m_k = 0;
00083
00084
00085 next();
00086 }
00087
00088 void next()
00089 {
00090 int n2 = routes.at(m_i).size();
00091
00092 if (m_k < n2 - 1)
00093 m_k++;
00094 else if (++m_j < n2 - 1)
00095 {
00096 m_k = m_j + 1;
00097 }
00098 else
00099 {
00100 m_i++;
00101 m_j = 0;
00102 m_k = 0;
00103 }
00104 }
00105
00106 bool isDone()
00107 {
00108 int n1 = routes.size();
00109 int n2 = routes.at(m_i).size();
00110
00111 if ((m_i >= n1 - 1) && (m_j >= n2 - 2) && (m_k >= n2 - 1))
00112 return true;
00113 else
00114 return false;
00115 }
00116
00117 Move<Routes, M>& current()
00118 {
00119 return *new MoveVVSwapIntra<T, M> (m_i, m_j, m_k);
00120 }
00121 };
00122
00123 template<class T, class M = OPTFRAME_DEFAULT_MEMORY>
00124 class NSSeqVVSwapIntra: public NSSeq<vector<vector<T> > , M>
00125 {
00126 typedef vector<vector<T> > Routes;
00127
00128 public:
00129
00130 NSSeqVVSwapIntra()
00131 {
00132 }
00133
00134 virtual ~NSSeqVVSwapIntra()
00135 {
00136 }
00137
00138 Move<Routes, M>& move(const Routes& rep)
00139 {
00140 int i = rand() % rep.size();
00141
00142 int n = rep.at(i).size();
00143
00144 int j = -1;
00145
00146 if (n > 0)
00147 j = rand() % n;
00148
00149 int k = j;
00150
00151 if (n > 1)
00152 while (k == j)
00153 k = rand() % n;
00154
00155 return *new MoveVVSwapIntra<T, M> (i, j, k);
00156 }
00157
00158 virtual NSIterator<Routes, M>& getIterator(const Routes& r)
00159 {
00160 return *new NSIteratorVVSwapIntra<T, M> (r);
00161 }
00162
00163 virtual void print()
00164 {
00165 cout << "NSSeqVVSwapIntra" << endl;
00166 }
00167
00168 };
00169
00170 #endif