00001 #ifndef OPTFRAME_NSENUMVVSwapK_HPP_
00002 #define OPTFRAME_NSENUMVVSwapK_HPP_
00003
00004
00005 #include "../Move.hpp"
00006 #include "../NSEnum.hpp"
00007 #include "NSVector.hpp"
00008
00009 using namespace std;
00010
00011
00012
00013
00014
00015 template<class T, class M>
00016 class MoveVVSwapk : public Move<vector<vector<T> >, M>
00017 {
00018 public:
00019 int k1,k2,v1,p1,v2,p2;
00020
00021 MoveVVSwapk(int k1,int k2,int v1,int p1,int v2,int p2)
00022 {
00023 this->k1 = k1;
00024 this->k2 = k2;
00025 this->v1 = v1;
00026 this->p1 = p1;
00027 this->v2 = v2;
00028 this->p2 = p2;
00029 }
00030
00031 virtual bool canBeApplied(const vector<vector<T> >&)
00032 {
00033 return true;
00034 }
00035
00036 virtual Move<vector<vector<T> >, M>& apply(vector<vector<T> >& rep)
00037 {
00038 pair< pair<int,int> , pair < pair<int,int>,pair<int,int> > > m;
00039 m.first.first = k1;
00040 m.first.second = k2;
00041 m.second.first.first = v1;
00042 m.second.first.second = p1;
00043 m.second.second.first = v2;
00044 m.second.second.second = p2;
00045 NSVector<int>::swapk_apply(rep,m);
00046
00047 return * new MoveVVSwapk<T,M>(k1,k2,v2,p2,v1,p1);
00048 }
00049
00050 virtual Move<vector<vector<T> >, M>& apply(M& m, vector<vector<T> > & r)
00051 {
00052 if (!m.empty())
00053 {
00054 m[v1].first = m[v2].first = -1;
00055 m[v1].second.first = p1;
00056 m[v1].second.second = r[v1].size()-1;
00057 m[v2].second.first = p2;
00058 m[v2].second.second = r[v2].size()-1;
00059 } else
00060 {
00061
00062 m = MemVRPTW(r.size(),make_pair(-1,make_pair(0,r.size()-1)));
00063 }
00064
00065 return apply(r);
00066 }
00067
00068 virtual void print()
00069 {
00070 cout << "Move Vector Vector Swapk("<< k1 << " " << k2 << " " << v1 << " " << p1 << " " << v2 << " " << p2 <<")"<<endl;
00071 }
00072
00073 virtual bool operator==(const Move<vector<vector<T> >,M>& m) const
00074 {
00075 return false;
00076 }
00077 };
00078
00079
00080
00081
00082
00083
00084
00085 template<class T, class M>
00086 class NSEnumVVSwapk: public NSEnum< vector<vector<T> >, M >
00087 {
00088 protected:
00089 int k1,k2;
00090 vector< pair< pair<int,int> , pair< pair<int,int>,pair<int,int> > > > * moves;
00091
00092 public:
00093
00094 using NSEnum<RepVRPTW, MemVRPTW>::move;
00095
00096 NSEnumVVSwapk(int k1, int k2)
00097 {
00098 this->k1 = k1;
00099 this->k2 = k2;
00100 moves = new vector< pair< pair<int,int> , pair< pair<int,int>,pair<int,int> > > >;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 virtual NSIterator<vector<vector<T> > , M>& getIterator(const vector<vector<T> >& rep)
00115 {
00116 delete moves;
00117 moves = NSVector<int>::swapk_appliableMoves(rep,k1,k2);
00118 random_shuffle(moves->begin(),moves->end());
00119
00120 return *new NSEnumIterator<vector<vector<T> > , M> (*this);
00121 }
00122
00123 virtual Move<vector<vector<T> >,M>& move(unsigned int _k)
00124 {
00125 if(_k>size())
00126 {
00127 cerr << "Neighborhood Swap Error! Move " << _k << " does not exist! Valid Interval from 0 to " << (size()-1) << "." << endl;
00128 exit(1);
00129
00130
00131 }
00132
00133 return * new MoveVVSwapk<T,M>((*moves)[_k].first.first,(*moves)[_k].first.second,
00134 (*moves)[_k].second.first.first,(*moves)[_k].second.first.second,
00135 (*moves)[_k].second.second.first,(*moves)[_k].second.second.second);
00136 }
00137
00138 virtual Move<vector<vector<T> >,M>& move(const vector<vector<T> >& rep)
00139 {
00140
00141 int v1;
00142 do v1 = rand()%rep.size(); while (rep[v1].size() < k1);
00143
00144 int p1 = rand() % ( rep[v1].size() - k1 + 1 );
00145
00146 int v2;
00147 do v2 = rand()%rep.size(); while (rep[v2].size() < k2 || v1==v2);
00148
00149 int p2 = rand()%(rep[v2].size() - k2 + 1 );
00150
00151 return * new MoveVVSwapk<T,M>(k1,k2,v1,p1,v2,p2);
00152 };
00153
00154 virtual unsigned int size()
00155 {
00156 return moves->size();
00157 }
00158
00159 virtual void print()
00160 {
00161 cout << "NSEnum Vector Vector Swapk ("<< size() << ")" << endl;
00162 }
00163
00164 };
00165
00166 #endif