[4174] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4174] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[4722] | 22 | using HeuristicLab.Common;
|
---|
[4174] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[6760] | 25 | using HeuristicLab.Data;
|
---|
[4174] | 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
[4346] | 28 | [Item("PotvinTwoLevelExchangeManipulator", "The 2M operator which manipulates a VRP representation. It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
|
---|
[4174] | 29 | [StorableClass]
|
---|
[4346] | 30 | public sealed class PotvinTwoLevelExchangeManipulator : PotvinManipulator {
|
---|
[4179] | 31 | [StorableConstructor]
|
---|
[4346] | 32 | private PotvinTwoLevelExchangeManipulator(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 33 | private PotvinTwoLevelExchangeManipulator(PotvinTwoLevelExchangeManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 34 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 35 | return new PotvinTwoLevelExchangeManipulator(this, cloner);
|
---|
| 36 | }
|
---|
| 37 | public PotvinTwoLevelExchangeManipulator() : base() { }
|
---|
[4179] | 38 |
|
---|
[6760] | 39 | public static void Apply(IRandom random, PotvinEncoding individual,
|
---|
| 40 | DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand,
|
---|
| 41 | DoubleValue capacity, DistanceMatrix distMatrix, bool allowInfeasible) {
|
---|
[4174] | 42 | int selectedIndex = SelectRandomTourBiasedByLength(random, individual);
|
---|
[6760] | 43 | Tour route1 = individual.Tours[selectedIndex];
|
---|
[4174] | 44 |
|
---|
[5368] | 45 | bool performed = false;
|
---|
| 46 | int customer1Position = 0;
|
---|
| 47 | while (customer1Position < route1.Cities.Count) {
|
---|
| 48 | performed = false;
|
---|
| 49 |
|
---|
[4174] | 50 | foreach (Tour tour in individual.Tours) {
|
---|
| 51 | if (tour != route1) {
|
---|
| 52 | for (int customer2Position = 0; customer2Position < tour.Cities.Count; customer2Position++) {
|
---|
| 53 | int customer1 = route1.Cities[customer1Position];
|
---|
| 54 | int customer2 = tour.Cities[customer2Position];
|
---|
| 55 |
|
---|
| 56 | //customer1 can be feasibly inserted at the location of customer2
|
---|
| 57 | tour.Cities[customer2Position] = customer1;
|
---|
[6760] | 58 | if (tour.Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) {
|
---|
[5368] | 59 | int routeIdx, place;
|
---|
[4174] | 60 | if (FindInsertionPlace(individual,
|
---|
[6760] | 61 | customer2, selectedIndex,
|
---|
| 62 | dueTime, serviceTime, readyTime, demand, capacity,
|
---|
| 63 | distMatrix, allowInfeasible,
|
---|
| 64 | out routeIdx, out place)) {
|
---|
| 65 | individual.Tours[routeIdx].Cities.Insert(place, customer2);
|
---|
| 66 | route1.Cities.RemoveAt(customer1Position);
|
---|
[4174] | 67 |
|
---|
[6760] | 68 | if (route1.Cities.Count == 0)
|
---|
[4174] | 69 | individual.Tours.Remove(route1);
|
---|
| 70 |
|
---|
| 71 | //two-level exchange has been performed
|
---|
[5368] | 72 | performed = true;
|
---|
| 73 | break;
|
---|
[4174] | 74 | } else {
|
---|
| 75 | tour.Cities[customer2Position] = customer2;
|
---|
| 76 | }
|
---|
| 77 | } else {
|
---|
| 78 | tour.Cities[customer2Position] = customer2;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[5368] | 82 |
|
---|
| 83 | if (performed)
|
---|
| 84 | break;
|
---|
[4174] | 85 | }
|
---|
[5368] | 86 |
|
---|
| 87 | if (!performed)
|
---|
| 88 | customer1Position++;
|
---|
[4174] | 89 | }
|
---|
| 90 | }
|
---|
[6760] | 91 |
|
---|
| 92 | protected override void Manipulate(IRandom random, PotvinEncoding individual) {
|
---|
| 93 | BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
|
---|
| 94 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
| 95 | DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
|
---|
| 96 | DoubleArray dueTime = DueTimeParameter.ActualValue;
|
---|
| 97 | DoubleArray readyTime = ReadyTimeParameter.ActualValue;
|
---|
| 98 | DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
|
---|
| 99 | DoubleArray demand = DemandParameter.ActualValue;
|
---|
| 100 | DoubleValue capacity = CapacityParameter.ActualValue;
|
---|
| 101 |
|
---|
| 102 | bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
|
---|
| 103 |
|
---|
| 104 | Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix, allowInfeasible);
|
---|
| 105 | }
|
---|
[4174] | 106 | }
|
---|
| 107 | }
|
---|