Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged relevant changes from the trunk into the branch (cloning,...) (#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 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    public PrinsEncoding(Permutation permutation, IVRPProblemInstance problemInstance)
135      : base(permutation, problemInstance) {
136    }
137
138    [StorableConstructor]
139    private PrinsEncoding(bool serializing)
140      : base(serializing) {
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.