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