1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Prins {
|
---|
33 | [Item("PrinsEncoding", "Represents an Prins encoding of VRP solutions. It is implemented as described in Prins, C. (2004). A simple and effective evolutionary algorithm for the vehicle routing problem. Computers & Operations Research, 12:1985-2002.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class PrinsEncoding : PermutationEncoding {
|
---|
36 | #region IVRPEncoding Members
|
---|
37 | public override int GetTourIndex(Tour tour) {
|
---|
38 | return 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override List<Tour> GetTours() {
|
---|
42 | List<Tour> result = new List<Tour>();
|
---|
43 |
|
---|
44 | int cities = ProblemInstance.Cities.Value;
|
---|
45 |
|
---|
46 | //Split permutation into vector P
|
---|
47 | int[] P = new int[cities + 1];
|
---|
48 | for (int i = 0; i <= cities; i++)
|
---|
49 | P[i] = -1;
|
---|
50 |
|
---|
51 | double[] V = new double[cities + 1];
|
---|
52 | V[0] = 0;
|
---|
53 | for (int i = 1; i <= cities; i++) {
|
---|
54 | V[i] = double.MaxValue;
|
---|
55 | }
|
---|
56 |
|
---|
57 | for (int i = 1; i <= cities; i++) {
|
---|
58 | int j = i;
|
---|
59 | Tour tour = new Tour();
|
---|
60 | bool feasible = true;
|
---|
61 |
|
---|
62 | do {
|
---|
63 | tour.Stops.Add(this[j - 1] + 1);
|
---|
64 |
|
---|
65 | VRPEvaluation eval =
|
---|
66 | ProblemInstance.EvaluateTour(tour, this);
|
---|
67 |
|
---|
68 | double cost = eval.Quality;
|
---|
69 | feasible = ProblemInstance.Feasible(eval);
|
---|
70 |
|
---|
71 | if (feasible || j == i) {
|
---|
72 | if (V[i - 1] + cost < V[j]) {
|
---|
73 | V[j] = V[i - 1] + cost;
|
---|
74 | P[j] = i - 1;
|
---|
75 | }
|
---|
76 | j++;
|
---|
77 | }
|
---|
78 |
|
---|
79 | } while (j <= cities && feasible);
|
---|
80 | }
|
---|
81 |
|
---|
82 | //extract VRP solution from vector P
|
---|
83 | int index = 0;
|
---|
84 | int index2 = cities;
|
---|
85 | Tour trip = null;
|
---|
86 | do {
|
---|
87 | index = P[index2];
|
---|
88 | trip = new Tour();
|
---|
89 |
|
---|
90 | for (int k = index + 1; k <= index2; k++) {
|
---|
91 | trip.Stops.Add(this[k - 1] + 1);
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (trip.Stops.Count > 0)
|
---|
95 | result.Add(trip);
|
---|
96 |
|
---|
97 | index2 = index;
|
---|
98 | } while (index != 0);
|
---|
99 |
|
---|
100 | //if there are too many vehicles - repair
|
---|
101 | while (result.Count > ProblemInstance.Vehicles.Value) {
|
---|
102 | Tour tour = result[result.Count - 1];
|
---|
103 |
|
---|
104 | //find predecessor / successor in permutation
|
---|
105 | int predecessorIndex = Array.IndexOf(this.array, tour.Stops[0] - 1) - 1;
|
---|
106 | if (predecessorIndex >= 0) {
|
---|
107 | int predecessor = this[predecessorIndex] + 1;
|
---|
108 |
|
---|
109 | foreach (Tour t in result) {
|
---|
110 | int insertPosition = t.Stops.IndexOf(predecessor) + 1;
|
---|
111 | if (insertPosition != -1) {
|
---|
112 | t.Stops.InsertRange(insertPosition, tour.Stops);
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | } else {
|
---|
117 | int successorIndex = Array.IndexOf(this.array,
|
---|
118 | tour.Stops[tour.Stops.Count - 1] - 1) + 1;
|
---|
119 | int successor = this[successorIndex] + 1;
|
---|
120 |
|
---|
121 | foreach (Tour t in result) {
|
---|
122 | int insertPosition = t.Stops.IndexOf(successor);
|
---|
123 | if (insertPosition != -1) {
|
---|
124 | t.Stops.InsertRange(insertPosition, tour.Stops);
|
---|
125 | break;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | result.Remove(tour);
|
---|
131 | }
|
---|
132 |
|
---|
133 | return result;
|
---|
134 | }
|
---|
135 | #endregion
|
---|
136 | public PrinsEncoding(Permutation permutation, IVRPProblemInstance problemInstance)
|
---|
137 | : base(permutation, problemInstance) {
|
---|
138 | }
|
---|
139 |
|
---|
140 | [StorableConstructor]
|
---|
141 | protected PrinsEncoding(bool serializing)
|
---|
142 | : base(serializing) {
|
---|
143 | }
|
---|
144 |
|
---|
145 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
146 | return new PrinsEncoding(this, cloner);
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected PrinsEncoding(PrinsEncoding original, Cloner cloner)
|
---|
150 | : base(original, cloner) {
|
---|
151 | }
|
---|
152 |
|
---|
153 | public static PrinsEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
|
---|
154 | List<Tour> tours = encoding.GetTours();
|
---|
155 | List<int> route = new List<int>();
|
---|
156 |
|
---|
157 | foreach (Tour tour in tours) {
|
---|
158 | foreach (int city in tour.Stops)
|
---|
159 | route.Add(city - 1);
|
---|
160 | }
|
---|
161 |
|
---|
162 | return new PrinsEncoding(
|
---|
163 | new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
|
---|
164 | }
|
---|
165 |
|
---|
166 | public static PrinsEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance problemInstance) {
|
---|
167 | List<int> route = new List<int>(routeParam);
|
---|
168 |
|
---|
169 | while (route.Remove(0)) { //remove all delimiters (0)
|
---|
170 | }
|
---|
171 |
|
---|
172 | for (int i = 0; i < route.Count; i++)
|
---|
173 | route[i]--;
|
---|
174 |
|
---|
175 | return new PrinsEncoding(
|
---|
176 | new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|