Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored VRP, added some Potvin operators (WIP) (#1039)

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