Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Prins/PrinsEncoding.cs @ 10189

Last change on this file since 10189 was 7907, checked in by svonolfe, 13 years ago

Merged changes from trunk into branch (#1177)

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