- Main Page
- Classes
- Files

- File List

# OptFrame/Util/NSEnumVVSwapkIntra.hpp

    00001 #ifndef OPTFRAME_NSENUMVVSwapkIntra_HPP_
    00002 #define OPTFRAME_NSENUMVVSwapkIntra_HPP_
    00003
    00004 // Framework includes
    00005 #include "../Move.hpp"
    00006 #include "../NSEnum.hpp"
    00007 #include "NSVector.hpp"
    00008
    00009 using namespace std;
    00010
    00011 //============================================================================
    00012 //                           VVSwapkIntra Move
    00013 //============================================================================
    00014
    00015 template<class T, class M>
    00016 class MoveVVSwapkIntra : public Move<vector<vector<T> >, M>
    00017 {
    00018 public:
    00019         int k1,k2,v,p1,p2;
    00020
    00021         MoveVVSwapkIntra(int k1,int k2,int v,int p1,int p2)
    00022         {
    00023                 this->k1 = k1;
    00024                 this->k2 = k2;
    00025                 this->v = v;
    00026                 this->p1 = p1;
    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<pair<int,int>,pair<int,int> > m;
    00038                 m.first.first = k1;
    00039                 m.first.second = k2;
    00040                 m.second.first = p1;
    00041                 m.second.second = p2;
    00042                 NSVector<T>::swapk_apply(rep[v],m); //TODO
    00043
    00044                 return * new MoveVVSwapkIntra<T,M>(k1,k2,v,p2,p1);
    00045         }
    00046
    00047         virtual Move<vector<vector<T> >, M>& apply(M& m, vector<vector<T> > & r)
    00048         {
    00049                 if (!m.empty())
    00050                 {
    00051                         m[v].first = -1;
    00052                         m[v].second.first = p1;
    00053                         m[v].second.second = r[v].size()-1;
    00054                 } else
    00055                 {
    00056                         //e->setMemory(new MemVRP(r.size(),make_pair(-1,make_pair(0,r.size()-1))));
    00057                         m = MemVRPTW(r.size(),make_pair(-1,make_pair(0,r.size()-1)));
    00058                 }
    00059
    00060                 return apply(r);
    00061         }
    00062
    00063         virtual void print()
    00064         {
    00065                 cout << "Move VRP SwapkIntra("<< k1 << " " << k2 << " " << v << " " << p1 << " " << p2 <<")"<<endl;
    00066         }
    00067
    00068         virtual bool operator==(const Move<vector<vector<T> >,M>& m) const
    00069         {
    00070                 return false; //TODO
    00071         }
    00072 };
    00073
    00074
    00075 //============================================================================
    00076 //                  Swap Neighborhood Structure
    00077 //============================================================================
    00078
    00079
    00080 template<class T, class M>
    00081 class NSEnumVVSwapkIntra: public NSEnum< vector<vector<T> >, M >
    00082 {
    00083 protected:
    00084         int k1,k2;
    00085         vector < vector< pair<pair<int,int>,pair<int,int> > > * > moves;
    00086         vector< pair<int,int> > moveindex;
    00087
    00088 public:
    00089
    00090         using NSEnum<RepVRPTW, MemVRPTW>::move; // prevents name hiding
    00091
    00092         NSEnumVVSwapkIntra(int k1,int k2)
    00093         {
    00094                 this->k1 = k1;
    00095                 this->k2 = k2;
    00096         }
    00097
    00098         /*virtual void init(Solution<vector<vector<int> > >* s)
    00099         {
    00100                 init(s->getR());
    00101         }
    00102
    00103         virtual void init(vector<vector<int> >& rep)
    00104         {
    00105                 delete moves;
    00106                 moves = NSVector<int>::SwapkIntra_appliableMoves(rep,k);
    00107         }*/
    00108
    00109         virtual NSIterator<vector<vector<T> > , M>& getIterator(const vector<vector<T> >& rep)
    00110         {
    00111                 for (int i = 0 ; i < moves.size() ; i++) delete moves[i];
    00112                 moves.clear();
    00113                 moveindex.clear();
    00114
    00115                 for (int i = 0 ; i < rep.size() ; i++)
    00116                 {
    00117                         moves.push_back( NSVector<int>::swapk_appliableMoves(rep[i],k1,k2) ); // TODO
    00118                         for (int j = 0 ; j < moves.back()->size() ; j++)
    00119                                 moveindex.push_back(make_pair(i,j));
    00120                 }
    00121
    00122                 return *new NSEnumIterator<vector<vector<T> > , M> (*this);
    00123         }
    00124
    00125         virtual Move<vector<vector<T> >,M>& move(unsigned int _k)
    00126         {
    00127                 if(_k>size())
    00128                 {
    00129                         cerr << "Neighborhood Swap Error! Move " << _k << " does not exist! Valid Interval from 0 to " << (size()-1) << "." << endl;
    00130                         exit(1);
    00131
    00132                         //return NULL;
    00133                 }
    00134
    00135                 int i = moveindex[_k].first;
    00136                 int j = moveindex[_k].second;
    00137
    00138                 int k1 = moves[i]->at(j).first.first;
    00139                 int k2 = moves[i]->at(j).first.second;
    00140                 int v = i;
    00141                 int p1 = moves[i]->at(j).second.first;
    00142                 int p2 = moves[i]->at(j).second.second;
    00143
    00144                 return * new MoveVVSwapkIntra<T,M>(k1,k2,v,p1,p2);
    00145         }
    00146
    00147         virtual Move<vector<vector<T> >,M>& move(const vector<vector<T> >& rep)//TODO
    00148         {
    00149                 //cout << "*";
    00150                 int v;
    00151                 do v = rand()%rep.size(); while (rep[v].size() < k1 + k2);
    00152
    00153                 int p1 = rand() % ( rep[v].size() - k1 + 1 );
    00154
    00155                 int p2 = rand()%(rep[v].size()+1);
    00156
    00157                 return * new MoveVVSwapkIntra<T,M>(k1,k2,v,p1,p2);
    00158         };
    00159
    00160         virtual unsigned int size()
    00161         {
    00162                 return moves.size();
    00163         }
    00164
    00165         virtual void print()
    00166         {
    00167                 cout << "NSEnum Vector Vector SwapkIntra ("<< size() << ")" << endl;
    00168         }
    00169
    00170 };
    00171
    00172 #endif /*OPTFRAME_NSENUMVVSwapkIntra_HPP_*/

All Classes Functions