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 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
33 | [Item("PotvinCrossover", "A VRP crossover operation.")]
|
---|
34 | [StorableClass]
|
---|
35 | public abstract class PotvinCrossover : VRPCrossover, IStochasticOperator, IPotvinOperator {
|
---|
36 | public ILookupParameter<IRandom> RandomParameter {
|
---|
37 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [StorableConstructor]
|
---|
41 | protected PotvinCrossover(bool deserializing) : base(deserializing) { }
|
---|
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 | city, -1, out route, out place);
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected Tour FindRoute(PotvinEncoding solution, int city) {
|
---|
55 | Tour found = null;
|
---|
56 |
|
---|
57 | foreach (Tour tour in solution.Tours) {
|
---|
58 | if (tour.Stops.Contains(city)) {
|
---|
59 | found = tour;
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | return found;
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected bool Repair(IRandom random, PotvinEncoding solution, Tour newTour) {
|
---|
68 | bool success = true;
|
---|
69 |
|
---|
70 | //remove duplicates from new tour
|
---|
71 | for (int i = 0; i < newTour.Stops.Count; i++) {
|
---|
72 | for (int j = 0; j < newTour.Stops.Count; j++) {
|
---|
73 | if (newTour.Stops[i] == newTour.Stops[j] && i != j) {
|
---|
74 | if (random.NextDouble() < 0.5)
|
---|
75 | newTour.Stops[i] = 0;
|
---|
76 | else
|
---|
77 | newTour.Stops[j] = 0;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | while (newTour.Stops.Contains(0))
|
---|
82 | newTour.Stops.Remove(0);
|
---|
83 |
|
---|
84 | //remove duplicates from old tours
|
---|
85 | for (int i = 0; i < newTour.Stops.Count; i++) {
|
---|
86 | foreach (Tour tour in solution.Tours) {
|
---|
87 | if (tour != newTour && tour.Stops.Contains(newTour.Stops[i])) {
|
---|
88 | tour.Stops.Remove(newTour.Stops[i]);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | //remove empty tours
|
---|
94 | List<Tour> toBeDeleted = new List<Tour>();
|
---|
95 | foreach (Tour tour in solution.Tours) {
|
---|
96 | if (tour.Stops.Count == 0)
|
---|
97 | toBeDeleted.Add(tour);
|
---|
98 | }
|
---|
99 | foreach (Tour tour in toBeDeleted) {
|
---|
100 | solution.Tours.Remove(tour);
|
---|
101 | }
|
---|
102 |
|
---|
103 | //route unrouted vehicles
|
---|
104 | int index = 0;
|
---|
105 | while (index < solution.Unrouted.Count && success) {
|
---|
106 | int unrouted = solution.Unrouted[index];
|
---|
107 |
|
---|
108 | int route, place;
|
---|
109 | if(FindInsertionPlace(solution, unrouted, out route, out place)) {
|
---|
110 | solution.Tours[route].Stops.Insert(place, unrouted);
|
---|
111 | } else {
|
---|
112 | success = false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | index++;
|
---|
116 | }
|
---|
117 |
|
---|
118 | for (int i = 0; i < index; i++)
|
---|
119 | solution.Unrouted.RemoveAt(0);
|
---|
120 |
|
---|
121 | return success;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public override IOperation Apply() {
|
---|
125 | ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
|
---|
126 | for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
|
---|
127 | IVRPEncoding solution = ParentsParameter.ActualValue[i];
|
---|
128 |
|
---|
129 | if (!(solution is PotvinEncoding)) {
|
---|
130 | parents[i] = PotvinEncoding.ConvertFrom(solution, ProblemInstance);
|
---|
131 | } else {
|
---|
132 | parents[i] = solution;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | ParentsParameter.ActualValue = parents;
|
---|
136 |
|
---|
137 | ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as PotvinEncoding, parents[1] as PotvinEncoding);
|
---|
138 |
|
---|
139 | return base.Apply();
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|