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 |
|
---|
22 | using HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
24 | using HeuristicLab.Parameters;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using System.Collections.Generic;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
31 | [Item("PotvinCrossover", "A VRP crossover operation.")]
|
---|
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 Tour FindRoute(PotvinEncoding solution, int city) {
|
---|
56 | Tour found = null;
|
---|
57 |
|
---|
58 | foreach (Tour tour in solution.Tours) {
|
---|
59 | if (tour.Cities.Contains(city)) {
|
---|
60 | found = tour;
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | return found;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected bool Repair(IRandom random, PotvinEncoding solution, Tour newTour) {
|
---|
69 | bool success = true;
|
---|
70 |
|
---|
71 | //remove duplicates from new tour
|
---|
72 | for (int i = 0; i < newTour.Cities.Count; i++) {
|
---|
73 | for (int j = 0; j < newTour.Cities.Count; j++) {
|
---|
74 | if (newTour.Cities[i] == newTour.Cities[j] && i != j) {
|
---|
75 | if (random.NextDouble() < 0.5)
|
---|
76 | newTour.Cities[i] = 0;
|
---|
77 | else
|
---|
78 | newTour.Cities[j] = 0;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | while(newTour.Cities.Contains(0))
|
---|
83 | newTour.Cities.Remove(0);
|
---|
84 |
|
---|
85 | //remove duplicates from old tours
|
---|
86 | for (int i = 0; i < newTour.Cities.Count; i++) {
|
---|
87 | foreach (Tour tour in solution.Tours) {
|
---|
88 | if (tour != newTour && tour.Cities.Contains(newTour.Cities[i])) {
|
---|
89 | tour.Cities.Remove(newTour.Cities[i]);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | //remove empty tours
|
---|
95 | List<Tour> toBeDeleted = new List<Tour>();
|
---|
96 | foreach (Tour tour in solution.Tours) {
|
---|
97 | if (tour.Cities.Count == 0)
|
---|
98 | toBeDeleted.Add(tour);
|
---|
99 | }
|
---|
100 | foreach (Tour tour in toBeDeleted) {
|
---|
101 | solution.Tours.Remove(tour);
|
---|
102 | }
|
---|
103 |
|
---|
104 | //route unrouted vehicles
|
---|
105 | int index = 0;
|
---|
106 | while (index < solution.Unrouted.Count && success) {
|
---|
107 | int unrouted = solution.Unrouted[index];
|
---|
108 |
|
---|
109 | int route, place;
|
---|
110 | if(FindInsertionPlace(solution, unrouted, out route, out place)) {
|
---|
111 | solution.Tours[route].Cities.Insert(place, unrouted);
|
---|
112 | } else {
|
---|
113 | success = false;
|
---|
114 | }
|
---|
115 |
|
---|
116 | index++;
|
---|
117 | }
|
---|
118 |
|
---|
119 | for (int i = 0; i < index; i++)
|
---|
120 | solution.Unrouted.RemoveAt(0);
|
---|
121 |
|
---|
122 | return success;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public override IOperation Apply() {
|
---|
126 | ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
|
---|
127 | for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
|
---|
128 | IVRPEncoding solution = ParentsParameter.ActualValue[i];
|
---|
129 |
|
---|
130 | if (!(solution is PotvinEncoding)) {
|
---|
131 | parents[i] = PotvinEncoding.ConvertFrom(solution);
|
---|
132 | } else {
|
---|
133 | parents[i] = solution;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | ParentsParameter.ActualValue = parents;
|
---|
137 |
|
---|
138 | ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as PotvinEncoding, parents[1] as PotvinEncoding);
|
---|
139 |
|
---|
140 | return base.Apply();
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|