Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5155 was 4752, checked in by svonolfe, 14 years ago

Merged relevant changes from the trunk into the branch (cloning,...) (#1177)

File size: 5.0 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    [StorableConstructor]
42    protected PotvinCrossover(bool deserializing) : base(deserializing) { }
43
44    public PotvinCrossover() {
45      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
46    }
47
48    protected PotvinCrossover(PotvinCrossover original, Cloner cloner)
49      : base(original, cloner) {
50    }
51
52    protected abstract PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2);
53
54    protected bool FindInsertionPlace(PotvinEncoding individual, int city, out int route, out int place) {
55      return individual.FindInsertionPlace(
56        city, -1, out route, out place);
57    }
58
59    protected Tour FindRoute(PotvinEncoding solution, int city) {
60      Tour found = null;
61
62      foreach (Tour tour in solution.Tours) {
63        if (tour.Stops.Contains(city)) {
64          found = tour;
65          break;
66        }
67      }
68
69      return found;
70    }
71
72    protected bool Repair(IRandom random, PotvinEncoding solution, Tour newTour) {
73      bool success = true;
74     
75      //remove duplicates from new tour     
76      for (int i = 0; i < newTour.Stops.Count; i++) {
77        for (int j = 0; j < newTour.Stops.Count; j++) {
78          if (newTour.Stops[i] == newTour.Stops[j] && i != j) {
79            if (random.NextDouble() < 0.5)
80              newTour.Stops[i] = 0;
81            else
82              newTour.Stops[j] = 0;
83          }
84        }
85      }
86      while (newTour.Stops.Contains(0))
87        newTour.Stops.Remove(0);
88
89      //remove duplicates from old tours
90      for (int i = 0; i < newTour.Stops.Count; i++) {
91        foreach (Tour tour in solution.Tours) {
92          if (tour != newTour && tour.Stops.Contains(newTour.Stops[i])) {
93            tour.Stops.Remove(newTour.Stops[i]);
94          }
95        }
96      }
97
98      //remove empty tours
99      List<Tour> toBeDeleted = new List<Tour>();
100      foreach (Tour tour in solution.Tours) {
101        if (tour.Stops.Count == 0)
102          toBeDeleted.Add(tour);
103      }
104      foreach (Tour tour in toBeDeleted) {
105        solution.Tours.Remove(tour);
106      }
107
108      //route unrouted vehicles
109      int index = 0;
110      while (index < solution.Unrouted.Count && success) {
111        int unrouted = solution.Unrouted[index];
112
113        int route, place;
114        if(FindInsertionPlace(solution, unrouted, out route, out place)) {
115          solution.Tours[route].Stops.Insert(place, unrouted);
116        } else {
117          success = false;
118        }
119
120        index++;
121      }
122
123      for (int i = 0; i < index; i++)
124        solution.Unrouted.RemoveAt(0);
125
126      return success;
127    }
128
129    public override IOperation Apply() {
130      ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
131      for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
132        IVRPEncoding solution = ParentsParameter.ActualValue[i];
133
134        if (!(solution is PotvinEncoding)) {
135          parents[i] = PotvinEncoding.ConvertFrom(solution, ProblemInstance);
136        } else {
137          parents[i] = solution;
138        }
139      }
140      ParentsParameter.ActualValue = parents;
141
142      ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as PotvinEncoding, parents[1] as PotvinEncoding);
143
144      return base.Apply();
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.