Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinCrossover.cs @ 9308

Last change on this file since 9308 was 6960, checked in by svonolfe, 13 years ago

Added pairwise manipulators, removed relocation manipulator (#1177)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
23using HeuristicLab.Encodings.PermutationEncoding;
24using HeuristicLab.Parameters;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using System.Collections.Generic;
29using HeuristicLab.Problems.VehicleRouting.Encodings.General;
30using HeuristicLab.Problems.VehicleRouting.Interfaces;
31using HeuristicLab.Common;
32
33namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
34  [Item("PotvinCrossover", "A VRP crossover operation.")]
35  [StorableClass]
36  public abstract class PotvinCrossover : VRPCrossover, IStochasticOperator, IPotvinOperator {
37    public ILookupParameter<IRandom> RandomParameter {
38      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
39    }
40
41    public IValueParameter<BoolValue> AllowInfeasibleSolutions {
42      get { return (IValueParameter<BoolValue>)Parameters["AllowInfeasibleSolutions"]; }
43    }
44
45    [StorableConstructor]
46    protected PotvinCrossover(bool deserializing) : base(deserializing) { }
47
48    public PotvinCrossover() {
49      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
50      Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false)));
51    }
52
53    protected PotvinCrossover(PotvinCrossover original, Cloner cloner)
54      : base(original, cloner) {
55    }
56
57    protected abstract PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2);
58
59    protected static bool FindInsertionPlace(PotvinEncoding individual, int city, bool allowInfeasible,
60        out int route, out int place) {
61      return individual.FindInsertionPlace(
62        city, -1, allowInfeasible,
63        out route, out place);
64    }
65
66    protected Tour FindRoute(PotvinEncoding solution, int city) {
67      Tour found = null;
68
69      foreach (Tour tour in solution.Tours) {
70        if (tour.Stops.Contains(city)) {
71          found = tour;
72          break;
73        }
74      }
75
76      return found;
77    }
78
79    protected static bool RouteUnrouted(PotvinEncoding solution, bool allowInfeasible) {
80      bool success = true;
81      int index = 0;
82      while (index < solution.Unrouted.Count && success) {
83        int unrouted = solution.Unrouted[index];
84
85        int route, place;
86        if (FindInsertionPlace(solution, unrouted, allowInfeasible,
87          out route, out place)) {
88          solution.Tours[route].Stops.Insert(place, unrouted);
89        } else {
90          success = false;
91        }
92
93        index++;
94      }
95
96      for (int i = 0; i < index; i++)
97        solution.Unrouted.RemoveAt(0);
98
99      return success;
100    }
101
102    protected static bool Repair(IRandom random, PotvinEncoding solution, Tour newTour, IVRPProblemInstance instance, bool allowInfeasible) {
103      bool success = true;
104
105      //remove duplicates from new tour     
106      for (int i = 0; i < newTour.Stops.Count; i++) {
107        for (int j = 0; j < newTour.Stops.Count; j++) {
108          if (newTour.Stops[i] == newTour.Stops[j] && i != j) {
109            if (random.NextDouble() < 0.5)
110              newTour.Stops[i] = 0;
111            else
112              newTour.Stops[j] = 0;
113          }
114        }
115      }
116      while (newTour.Stops.Contains(0))
117        newTour.Stops.Remove(0);
118
119      //remove duplicates from old tours
120      for (int i = 0; i < newTour.Stops.Count; i++) {
121        foreach (Tour tour in solution.Tours) {
122          if (tour != newTour && tour.Stops.Contains(newTour.Stops[i])) {
123            tour.Stops.Remove(newTour.Stops[i]);
124          }
125        }
126      }
127
128      //remove empty tours
129      List<Tour> toBeDeleted = new List<Tour>();
130      foreach (Tour tour in solution.Tours) {
131        if (tour.Stops.Count == 0)
132          toBeDeleted.Add(tour);
133      }
134      foreach (Tour tour in toBeDeleted) {
135        solution.Tours.Remove(tour);
136      }
137
138      if (newTour.Stops.Count > 0 && !allowInfeasible && !instance.TourFeasible(newTour, solution))
139        return false;
140
141      //route unrouted vehicles
142      success = RouteUnrouted(solution, allowInfeasible);
143
144      return success;
145    }
146
147    public override IOperation Apply() {
148      ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
149      for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
150        IVRPEncoding solution = ParentsParameter.ActualValue[i];
151
152        if (!(solution is PotvinEncoding)) {
153          parents[i] = PotvinEncoding.ConvertFrom(solution, ProblemInstance);
154        } else {
155          parents[i] = solution;
156        }
157      }
158      ParentsParameter.ActualValue = parents;
159
160      ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as PotvinEncoding, parents[1] as PotvinEncoding);
161      (ChildParameter.ActualValue as PotvinEncoding).Repair();
162
163      return base.Apply();
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.