Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinLocalSearchManipulator.cs @ 17712

Last change on this file since 17712 was 17712, checked in by abeham, 4 years ago

#2521: working on VRP

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System.Collections.Generic;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
31  [Item("PotvinLocalSearchManipulator", "The LSM 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.")]
32  [StorableType("EF16AD46-5C58-4846-95CF-3C3DF78D2F68")]
33  public sealed class PotvinLocalSearchManipulator : PotvinManipulator, IVRPLocalSearchManipulator {
34    public IValueParameter<IntValue> Iterations {
35      get { return (IValueParameter<IntValue>)Parameters["Iterations"]; }
36    }
37
38    [StorableConstructor]
39    private PotvinLocalSearchManipulator(StorableConstructorFlag _) : base(_) { }
40
41    public PotvinLocalSearchManipulator()
42      : base() {
43      Parameters.Add(new ValueParameter<IntValue>("Iterations", "The number of max iterations.", new IntValue(100)));
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new PotvinLocalSearchManipulator(this, cloner);
48    }
49
50    private PotvinLocalSearchManipulator(PotvinLocalSearchManipulator original, Cloner cloner)
51      : base(original, cloner) {
52    }
53
54    private static bool FindBetterInsertionPlace(
55      PotvinEncodedSolution individual, IVRPProblemInstance instance, int tour, int city, int length,
56      out int insertionTour, out int insertionPlace) {
57      bool insertionFound = false;
58      insertionTour = -1;
59      insertionPlace = 1;
60
61      List<int> toBeDeleted = individual.Tours[tour].Stops.GetRange(city, length);
62      double distance = individual.GetTourLength(individual.Tours[tour]);
63      individual.Tours[tour].Stops.RemoveRange(city, length);
64      double removalBenefit = distance - individual.GetTourLength(individual.Tours[tour]);
65
66      int currentTour = 0;
67      while (currentTour < individual.Tours.Count && !insertionFound) {
68        int currentCity = 0;
69        while (currentCity <= individual.Tours[currentTour].Stops.Count && !insertionFound) {
70          distance = individual.GetTourLength(individual.Tours[currentTour]);
71          individual.Tours[currentTour].Stops.InsertRange(currentCity, toBeDeleted);
72          var tourEval = instance.EvaluateTour(individual.Tours[currentTour], individual);
73          if (tourEval.IsFeasible) {
74            double lengthIncrease =
75              individual.GetTourLength(individual.Tours[currentTour]) - distance;
76            if (removalBenefit > lengthIncrease) {
77              insertionTour = currentTour;
78              insertionPlace = currentCity;
79
80              insertionFound = true;
81            }
82          }
83          individual.Tours[currentTour].Stops.RemoveRange(currentCity, length);
84
85          currentCity++;
86        }
87        currentTour++;
88      }
89
90      individual.Tours[tour].Stops.InsertRange(city, toBeDeleted);
91
92      return insertionFound;
93    }
94
95    public static void ApplyManipulation(IRandom random, PotvinEncodedSolution individual, IVRPProblemInstance instance, int maxIterations) {
96      //only apply to feasible individuals
97      var eval = instance.Evaluate(individual);
98      if (eval.IsFeasible) {
99        bool insertionFound;
100        int iterations = 0;
101
102        do {
103          insertionFound = false;
104          int length = 3;
105          while (length > 0 && !insertionFound) {
106            int tour = 0;
107            while (tour < individual.Tours.Count && !insertionFound) {
108              int city = 0;
109              while (city <= individual.Tours[tour].Stops.Count - length && !insertionFound) {
110                int insertionTour, insertionPlace;
111                if (FindBetterInsertionPlace(individual, instance, tour, city, length,
112                 out insertionTour, out insertionPlace)) {
113                  insertionFound = true;
114
115                  List<int> toBeInserted = individual.Tours[tour].Stops.GetRange(city, length);
116
117                  individual.Tours[tour].Stops.RemoveRange(city, length);
118                  individual.Tours[insertionTour].Stops.InsertRange(
119                    insertionPlace,
120                    toBeInserted);
121                }
122                city++;
123              }
124              tour++;
125            }
126            length--;
127          }
128          iterations++;
129        } while (insertionFound &&
130          iterations < maxIterations);
131
132        IList<Tour> toBeRemoved = new List<Tour>();
133        foreach (Tour tour in individual.Tours) {
134          if (tour.Stops.Count == 0)
135            toBeRemoved.Add(tour);
136        }
137
138        foreach (Tour tour in toBeRemoved) {
139          individual.Tours.Remove(tour);
140        }
141      }
142    }
143 
144
145    protected override void Manipulate(IRandom random, PotvinEncodedSolution individual) {
146      ApplyManipulation(random, individual, ProblemInstance, Iterations.Value.Value);     
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.