1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
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.")]
|
---|
29 | [StorableClass]
|
---|
30 | public sealed class PotvinTwoLevelExchangeManipulator : PotvinManipulator {
|
---|
31 | [StorableConstructor]
|
---|
32 | private PotvinTwoLevelExchangeManipulator(bool deserializing) : base(deserializing) { }
|
---|
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() { }
|
---|
38 |
|
---|
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) {
|
---|
42 | int selectedIndex = SelectRandomTourBiasedByLength(random, individual);
|
---|
43 | Tour route1 = individual.Tours[selectedIndex];
|
---|
44 |
|
---|
45 | bool performed = false;
|
---|
46 | int customer1Position = 0;
|
---|
47 | while (customer1Position < route1.Cities.Count) {
|
---|
48 | performed = false;
|
---|
49 |
|
---|
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;
|
---|
58 | if (tour.Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) {
|
---|
59 | int routeIdx, place;
|
---|
60 | if (FindInsertionPlace(individual,
|
---|
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);
|
---|
67 |
|
---|
68 | if (route1.Cities.Count == 0)
|
---|
69 | individual.Tours.Remove(route1);
|
---|
70 |
|
---|
71 | //two-level exchange has been performed
|
---|
72 | performed = true;
|
---|
73 | break;
|
---|
74 | } else {
|
---|
75 | tour.Cities[customer2Position] = customer2;
|
---|
76 | }
|
---|
77 | } else {
|
---|
78 | tour.Cities[customer2Position] = customer2;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (performed)
|
---|
84 | break;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (!performed)
|
---|
88 | customer1Position++;
|
---|
89 | }
|
---|
90 | }
|
---|
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 | }
|
---|
106 | }
|
---|
107 | }
|
---|