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