00001 #ifndef OPTFRAME_NSENUMVECTORSHIFT_HPP_
00002 #define OPTFRAME_NSENUMVECTORSHIFT_HPP_
00003
00004
00005 #include "../Move.hpp"
00006 #include "../NSEnum.hpp"
00007
00008 using namespace std;
00009
00010
00011
00012
00013
00014 template<class T, class M>
00015 class MoveVectorShift : public Move<vector<T>, M>
00016 {
00017 private:
00018 int t1, t2;
00019 int n;
00020
00021 public:
00022
00023 MoveVectorShift(int t1, int t2)
00024 {
00025 this->t1 = t1;
00026 this->t2 = t2;
00027 }
00028
00029 bool canBeApplied(Solution<vector<T> >* s)
00030 {
00031 return t1 != t2;
00032 }
00033
00034 Move<vector<T>, M>* apply(Solution<vector<T> >* s)
00035 {
00036 vector<T>* rep = s->getRepresentation();
00037
00038 T aux;
00039 if(t1 < t2)
00040 for(int i = t1; i < t2; i++)
00041 {
00042 aux = rep->at(i);
00043 (*rep)[i] = (*rep)[i+1];
00044 (*rep)[i+1] = aux;
00045 }
00046 else
00047 for(int i = t1; i > t2; i--)
00048 {
00049 aux = rep->at(i);
00050 (*rep)[i] = (*rep)[i-1];
00051 (*rep)[i-1] = aux;
00052 }
00053
00054 MoveVectorShift<T,M> * ms = new MoveVectorShift<T,M>(t2,t1);
00055
00056 return ms;
00057 }
00058
00059 void print()
00060 {
00061 cout << "Move Vector Shift("<< t1 << " -> " << t2 <<")"<<endl;
00062 }
00063 };
00064
00065
00066
00067
00068
00069
00070
00071 template<class T, class M>
00072 class NSEnumVectorShift: public NSEnum< vector<T>, M >
00073 {
00074 private:
00075 int n;
00076
00077 public:
00078
00079 NSEnumVectorShift(int n)
00080 {
00081 this->n = n;
00082 }
00083
00084
00085 virtual Move<vector<T>,M>* kmove(int k)
00086 {
00087 if(k>size())
00088 {
00089 cerr << "Neighborhood Shift Error! Move " << k << " doesnt exist! Valid Interval from 0 to " << (size()-1) << "." << endl;
00090 exit(1);
00091
00092 return NULL;
00093 }
00094
00095 return new MoveVectorShift<T,M>((k/n), (k%n) );
00096 }
00097
00098
00099 int size()
00100 {
00101 return n*n;
00102 }
00103
00104 virtual void print()
00105 {
00106 cout << "NSEnum Vector Shift ("<< size() << ")" << endl;
00107 }
00108
00109 };
00110
00111 #endif