Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers/PotvinCrossover.cs @ 5959

Last change on this file since 5959 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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