Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4379 was 4379, checked in by svonolfe, 14 years ago

Added remaining encodings (#1177)

File size: 5.8 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 List<Tour> GetTours() {
39      List<Tour> result = new List<Tour>();
40
41      int cities = ProblemInstance.Cities.Value;
42
43      //Split permutation into vector P
44      int[] P = new int[cities + 1];
45      for (int i = 0; i <= cities; i++)
46        P[i] = -1;
47
48      double[] V = new double[cities + 1];
49      V[0] = 0;
50      for (int i = 1; i <= cities; i++) {
51        V[i] = int.MaxValue;
52      }
53
54      for (int i = 1; i <= cities; i++) {
55        int j = i;
56        Tour tour = new Tour();
57        bool feasible = true;
58
59        do {
60          tour.Stops.Add(this[j-1] + 1);
61
62          VRPEvaluation eval =
63            ProblemInstance.EvaluatorParameter.Value.Evaluate(ProblemInstance,
64             tour);
65
66          double cost = eval.Quality;
67          feasible = ProblemInstance.EvaluatorParameter.Value.Feasible(eval);
68
69          if (feasible) {
70            if (V[i - 1] + cost < V[j]) {
71              V[j] = V[i - 1] + cost;
72              P[j] = i - 1;
73            }
74            j++;
75          }
76
77        } while (j <= cities && feasible);
78      }
79
80      //extract VRP solution from vector P
81      int index = 0;
82      int index2 = cities;
83      Tour trip = null;
84      do {
85        index = P[index2];
86        trip = new Tour();
87
88        for (int k = index + 1; k <= index2; k++) {
89          trip.Stops.Add(this[k - 1] + 1);
90        }
91
92        if (trip.Stops.Count > 0)
93          result.Add(trip);
94
95        index2 = index;
96      } while (index != 0);
97
98      //if there are too many vehicles - repair
99      while (result.Count > ProblemInstance.Vehicles.Value) {
100        Tour tour = result[result.Count - 1];
101
102        //find predecessor / successor in permutation
103        int predecessorIndex = Array.IndexOf(this.array, tour.Stops[0] - 1) - 1;
104        if (predecessorIndex >= 0) {
105          int predecessor = this[predecessorIndex] + 1;
106
107          foreach (Tour t in result) {
108            int insertPosition = t.Stops.IndexOf(predecessor) + 1;
109            if (insertPosition != -1) {
110              t.Stops.InsertRange(insertPosition, tour.Stops);
111              break;
112            }
113          }
114        } else {
115          int successorIndex = Array.IndexOf(this.array,
116            tour.Stops[tour.Stops.Count - 1] - 1) + 1;
117          int successor = this[successorIndex] + 1;
118
119          foreach (Tour t in result) {
120            int insertPosition = t.Stops.IndexOf(successor);
121            if (insertPosition != -1) {
122              t.Stops.InsertRange(insertPosition, tour.Stops);
123              break;
124            }
125          }
126        }
127
128        result.Remove(tour);
129      }
130
131      return result;
132    }
133    #endregion
134
135    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
136      PrinsEncoding clone = new PrinsEncoding(
137        new Permutation(this.PermutationType, this.array), ProblemInstance);
138
139      cloner.RegisterClonedObject(this, clone);
140      clone.readOnly = readOnly;
141      return clone;
142    }
143
144    public PrinsEncoding(Permutation permutation, IVRPProblemInstance problemInstance)
145      : base(permutation, problemInstance) {
146    }
147
148    [StorableConstructor]
149    private PrinsEncoding(bool serializing)
150      : base(serializing) {
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}
Note: See TracBrowser for help on using the repository browser.