- Main Page
- Namespaces
- Classes
- Files

- Search for |

# eoVRPMutation.h

    00001 /*
    00002  * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
    00003  * (C) OPAC Team, LIFL, 2002-2007
    00004  *
    00005  * (c) Antonio LaTorre <atorre@fi.upm.es>, 2007
    00006  *
    00007  * This software is governed by the CeCILL license under French law and
    00008  * abiding by the rules of distribution of free software.  You can  use,
    00009  * modify and/ or redistribute the software under the terms of the CeCILL
    00010  * license as circulated by CEA, CNRS and INRIA at the following URL
    00011  * "http://www.cecill.info".
    00012  *
    00013  * As a counterpart to the access to the source code and  rights to copy,
    00014  * modify and redistribute granted by the license, users are provided only
    00015  * with a limited warranty  and the software's author,  the holder of the
    00016  * economic rights,  and the successive licensors  have only  limited liability.
    00017  *
    00018  * In this respect, the user's attention is drawn to the risks associated
    00019  * with loading,  using,  modifying and/or developing or reproducing the
    00020  * software by the user in light of its specific status of free software,
    00021  * that may mean  that it is complicated to manipulate,  and  that  also
    00022  * therefore means  that it is reserved for developers  and  experienced
    00023  * professionals having in-depth computer knowledge. Users are therefore
    00024  * encouraged to load and test the software's suitability as regards their
    00025  * requirements in conditions enabling the security of their systems and/or
    00026  * data to be ensured and,  more generally, to use and operate it in the
    00027  * same conditions as regards security.
    00028  * The fact that you are presently reading this means that you have had
    00029  * knowledge of the CeCILL license and that you accept its terms.
    00030  *
    00031  * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
    00032  * Contact: paradiseo-help@lists.gforge.inria.fr
    00033  *
    00034  */
    00035
    00036 #ifndef eoVRPMutation_H
    00037 #define eoVRPMutation_H
    00038
    00039 // General includes
    00040 #include <algorithm>
    00041
    00042 // The base definition of eoMonOp
    00043 #include <eoOp.h>
    00044
    00052 class eoVRPMutation: public eoMonOp <eoVRP> {
    00053
    00054 public:
    00055
    00060     eoVRPMutation () {
    00061
    00062     }
    00063
    00064
    00070     std::string className () const {
    00071
    00072         return "eoVRPMutation";
    00073
    00074     }
    00075
    00076
    00086     bool operator () (eoVRP& _genotype) {
    00087
    00088         bool   res = false;
    00089         double op  = rng.uniform ();
    00090
    00091
    00092         if (op <= 0.05)
    00093             res = swapMutation (_genotype);
    00094         else if ((op > 0.05) && (op <= 0.20))
    00095             res = inversionMutation (_genotype);
    00096         else if ((op > 0.20) && (op <= 0.25))
    00097             res = insertionMutation (_genotype);
    00098         else if ((op > 0.25) && (op <= 0.45))
    00099             res = DisplacementMutation (_genotype);
    00100
    00101         if (res)
    00102             _genotype.cleanRoutes ();
    00103
    00104         return res;
    00105
    00106     }
    00107
    00108
    00109 private:
    00110
    00111
    00119     bool swapMutation (eoVRP& _genotype) {
    00120
    00121         int p1 = rng.random (_genotype.size ());
    00122         int p2 = -1;
    00123
    00124         do {
    00125             p2 = rng.random (_genotype.size ());
    00126         } while (_genotype [p2] == _genotype [p1]);
    00127
    00128         std::swap (_genotype [p1], _genotype [p2]);
    00129
    00130         return true;
    00131
    00132     }
    00133
    00134
    00142     bool inversionMutation (eoVRP& _genotype) {
    00143
    00144         int p1 = rng.random (_genotype.size ());
    00145         int p2 = -1;
    00146
    00147         do {
    00148             p2 = rng.random (_genotype.size ());
    00149         } while (_genotype [p2] == _genotype [p1]);
    00150
    00151         if (p1 > p2)
    00152             std::swap (p1, p2);
    00153
    00154         // Reverse the subroute
    00155         reverse (_genotype.begin () + p1, _genotype.begin () + p2 + 1);
    00156
    00157
    00158         return false;
    00159
    00160     }
    00161
    00162
    00170     bool insertionMutation (eoVRP& _genotype) {
    00171
    00172         int p = -1;
    00173
    00174         // Selection of the client to be moved
    00175         do {
    00176             p = rng.random (_genotype.size ());
    00177         } while (_genotype [p] == -1);
    00178
    00179         // Temporary copy of the client
    00180         unsigned client = _genotype [p];
    00181
    00182         _genotype.erase (_genotype.begin () + p);
    00183
    00184         p = rng.random (_genotype.size () - 1);
    00185         _genotype.insert (_genotype.begin () + p, client);
    00186
    00187         return true;
    00188
    00189     }
    00190
    00191
    00199     bool DisplacementMutation (eoVRP& _genotype) {
    00200
    00201         int p1 = rng.random (_genotype.size ());
    00202         int p2 = -1;
    00203
    00204         do {
    00205             p2 = rng.random (_genotype.size ());
    00206         } while (_genotype [p2] == _genotype [p1]);
    00207
    00208         if (p1 > p2)
    00209             std::swap (p1, p2);
    00210
    00211         // Temporary copy of the fragment being moved
    00212         Route route;
    00213
    00214         for (unsigned i = p1; i <= p2; i++)
    00215             route.push_back (_genotype [i]);
    00216
    00217         _genotype.erase (_genotype.begin () + p1, _genotype.begin () + p2 + 1);
    00218
    00219         unsigned p = rng.random ((_genotype.size () > 0) ? _genotype.size () - 1 : 0);
    00220         _genotype.insert (_genotype.begin () + p, route.begin (), route.end ());
    00221
    00222         return true;
    00223
    00224     }
    00225
    00226
    00227 };
    00228
    00229 #endif