Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Prins/PrinsEncoding.cs @ 17835

Last change on this file since 17835 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HEAL.Attic;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
30
31namespace 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("A0E1EBC1-C0F5-4CD5-9279-1A669C6633CD")]
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 _) : base(_) {
141    }
142
143    public override IDeepCloneable Clone(Cloner cloner) {
144      return new PrinsEncoding(this, cloner);
145    }
146
147    protected PrinsEncoding(PrinsEncoding original, Cloner cloner)
148      : base(original, cloner) {
149    }
150
151    public static PrinsEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
152      List<Tour> tours = encoding.GetTours();
153      List<int> route = new List<int>();
154
155      foreach (Tour tour in tours) {
156        foreach (int city in tour.Stops)
157          route.Add(city - 1);
158      }
159
160      return new PrinsEncoding(
161        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
162    }
163
164    public static PrinsEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance problemInstance) {
165      List<int> route = new List<int>(routeParam);
166
167      while (route.Remove(0)) { //remove all delimiters (0)
168      }
169
170      for (int i = 0; i < route.Count; i++)
171        route[i]--;
172
173      return new PrinsEncoding(
174        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.