- Main Page
- Classes
- Files

- File List

# OptFrame/Util/NSSeqMultiRouteAdapter.hpp

    00001 #ifndef OPTFRAME_NSSEQMULTIROUTEADAPTER_HPP_
    00002 #define OPTFRAME_NSSEQMULTIROUTEADAPTER_HPP_
    00003
    00004 // Framework includes
    00005 #include "../Move.hpp"
    00006 #include "../NSSeq.hpp"
    00007
    00008 using namespace std;
    00009
    00010 template<class T, class M = OPTFRAME_DEFAULT_MEMORY>
    00011 class MoveMultiRoute: public Move<vector<vector<T> > , M>
    00012 {
    00013         typedef vector<T> Route;
    00014         typedef vector<vector<T> > MultiRoute;
    00015
    00016 protected:
    00017         int k; // route number
    00018         Move<Route, M>& m;
    00019
    00020 public:
    00021
    00022         MoveMultiRoute(int _k, Move<Route, M>& _m) :
    00023                 k(_k), m(_m)
    00024         {
    00025         }
    00026
    00027         int get_k()
    00028         {
    00029                 return k;
    00030         }
    00031
    00032         Move<Route, M>& get_m()
    00033         {
    00034                 return m;
    00035         }
    00036
    00037         virtual ~MoveMultiRoute()
    00038         {
    00039                 delete &m;
    00040         }
    00041
    00042         bool canBeApplied(const MultiRoute& rep)
    00043         {
    00044                 return m.canBeApplied(rep[k]);
    00045         }
    00046
    00047         Move<MultiRoute, M>& apply(MultiRoute& rep)
    00048         {
    00049                 return *new MoveMultiRoute<T, M> (k, m.apply(rep[k]));
    00050         }
    00051
    00052         Move<MultiRoute, M>& apply(M& mem, MultiRoute& rep)
    00053         {
    00054                 return *new MoveMultiRoute<T, M> (k, m.apply(mem, rep[k]));
    00055         }
    00056
    00057         virtual bool operator==(const Move<MultiRoute, M>& _m) const
    00058         {
    00059                 const MoveMultiRoute<T, M>& m1 = (const MoveMultiRoute<T, M>&) _m;
    00060                 if (k == m1.k)
    00061                         return m == m1.m;
    00062                 else
    00063                         return false;
    00064         }
    00065
    00066         void print()
    00067         {
    00068                 cout << "MoveMultiRoute: k=" << k << "; move = ";
    00069                 m.print();
    00070         }
    00071 };
    00072
    00073 class NSSeqMultiRouteIteratorOutOfBound
    00074 {
    00075 public:
    00076         NSSeqMultiRouteIteratorOutOfBound()
    00077         {
    00078         }
    00079 };
    00080
    00081 template<class T, class M = OPTFRAME_DEFAULT_MEMORY, class MOVE = MoveMultiRoute<T, M> >
    00082 class IteratorNSSeqMultiRoute: public NSIterator<vector<vector<T> > , M>
    00083 {
    00084         typedef vector<T> Route;
    00085         typedef vector<vector<T> > MultiRoute;
    00086
    00087 private:
    00088         vector<NSIterator<Route, M>*>& iterators;
    00089         int i;
    00090
    00091 public:
    00092
    00093         IteratorNSSeqMultiRoute(vector<NSIterator<Route, M>*>& it) :
    00094                 iterators(it)
    00095         {
    00096                 i = 0;
    00097         }
    00098
    00099         virtual ~IteratorNSSeqMultiRoute()
    00100         {
    00101                 for (int j = 0; j < iterators.size(); j++)
    00102                         delete iterators[j];
    00103                 delete &iterators;
    00104         }
    00105
    00106         void first()
    00107         {
    00108                 for (int j = 0; j < iterators.size(); j++)
    00109                         iterators[j]->first();
    00110
    00111                 i = 0;
    00112                 while (i < iterators.size())
    00113                         if (!iterators[i]->isDone())
    00114                                 break;
    00115                         else
    00116                                 i++;
    00117         }
    00118
    00119         void next()
    00120         {
    00121                 iterators[i]->next();
    00122                 while (i < iterators.size())
    00123                         if (!iterators[i]->isDone())
    00124                                 break;
    00125                         else
    00126                                 i++;
    00127         }
    00128
    00129         bool isDone()
    00130         {
    00131                 for (int j = i; j < iterators.size(); j++)
    00132                         if (!iterators[j]->isDone())
    00133                                 return false;
    00134                 return true;
    00135         }
    00136
    00137         Move<MultiRoute, M>& current()
    00138         {
    00139                 if ((i < iterators.size()) && (!iterators[i]->isDone()))
    00140                         return *new MOVE(i, iterators[i]->current());
    00141                 else
    00142                         throw NSSeqMultiRouteIteratorOutOfBound();
    00143         }
    00144 };
    00145
    00146 template<class T, class M = OPTFRAME_DEFAULT_MEMORY, class MOVE = MoveMultiRoute<T, M> >
    00147 class NSSeqMultiRouteAdapter: public NSSeq<vector<vector<T> > , M>
    00148 {
    00149         typedef vector<T> Route;
    00150         typedef vector<vector<T> > MultiRoute;
    00151
    00152 private:
    00153         NSSeq<Route, M>& ns;
    00154
    00155 public:
    00156
    00157         NSSeqMultiRouteAdapter(NSSeq<Route, M>& _ns) :
    00158                 ns(_ns)
    00159         {
    00160         }
    00161
    00162         virtual ~NSSeqMultiRouteAdapter()
    00163         {
    00164         }
    00165
    00166         Move<MultiRoute, M>& move(const MultiRoute& r)
    00167         {
    00168                 int x = rand() % r.size();
    00169                 return *new MOVE(x, ns.move(r[x]));
    00170         }
    00171
    00172         virtual NSIterator<MultiRoute, M>& getIterator(const MultiRoute& r)
    00173         {
    00174                 vector<NSIterator<Route, M>*>& iterators = *new vector<NSIterator<Route, M>*> ;
    00175                 for (int i = 0; i < r.size(); i++)
    00176                         iterators.push_back(&ns.getIterator(r[i]));
    00177
    00178                 return *new IteratorNSSeqMultiRoute<T, M, MOVE> (iterators);
    00179         }
    00180
    00181         virtual void print()
    00182         {
    00183                 cout << "NSSeqMultiRouteAdapter {" << endl;
    00184                 ns.print();
    00185                 cout << "}" << endl;
    00186         }
    00187 };
    00188
    00189 #endif /*OPTFRAME_NSSEQMULTIROUTEADAPTER_HPP_*/

All Classes Functions