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