Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored VRP based on the code review (#1039)

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