Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Zhu/ZhuEncoding.cs @ 4293

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

Added Zhu Encoding (#1039)

File size: 6.6 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;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Zhu {
32  [Item("ZhuEncoding", "Represents a Zhu encoding of VRP solutions. It is implemented as described in Zhu, K.Q. (2000). A New Genetic Algorithm For VRPTW. Proceedings of the International Conference on Artificial Intelligence.")]
33  [StorableClass]
34  public class ZhuEncoding : PermutationEncoding {
35    [Storable]
36    private int cities;
37
38    [Storable]
39    private DoubleMatrix coordinates;
40
41    [Storable]
42    private BoolValue useDistanceMatrix;
43
44    [Storable]
45    DoubleArray dueTimeArray;
46   
47    [Storable]
48    DoubleArray serviceTimeArray;
49
50    [Storable]
51    DoubleArray readyTimeArray;
52   
53    [Storable]
54    DoubleArray demandArray;
55
56    [Storable]
57    DoubleValue capacity;
58
59    #region IVRPEncoding Members
60    public override List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix, int maxVehicles = int.MaxValue) {
61      List<Tour> result = new List<Tour>();
62
63      Tour newTour = new Tour();
64
65      for (int i = 0; i < this.Length; i++) {
66        int city = this[i] + 1;
67        newTour.Cities.Add(city);
68        if (!newTour.Feasible(
69          dueTimeArray,
70          serviceTimeArray,
71          readyTimeArray,
72          demandArray,
73          capacity,
74          coordinates,
75          distanceMatrix,
76          useDistanceMatrix)) {
77            newTour.Cities.Remove(city);
78            if (newTour.Cities.Count > 0)
79              result.Add(newTour);
80
81            newTour = new Tour();
82            newTour.Cities.Add(city);
83        }
84      }
85
86      if (newTour.Cities.Count > 0)
87        result.Add(newTour);
88
89      //if there are too many vehicles - repair
90      while (result.Count > maxVehicles) {
91        Tour tour = result[result.Count - 1];
92
93        //find predecessor / successor in permutation
94        int predecessorIndex = Array.IndexOf(this.array, tour.Cities[0] - 1) - 1;
95        if (predecessorIndex >= 0) {
96          int predecessor = this[predecessorIndex] + 1;
97
98          foreach (Tour t in result) {
99            int insertPosition = t.Cities.IndexOf(predecessor) + 1;
100            if (insertPosition != -1) {
101              t.Cities.InsertRange(insertPosition, tour.Cities);
102              break;
103            }
104          }
105        } else {
106          int successorIndex = Array.IndexOf(this.array,
107            tour.Cities[tour.Cities.Count - 1] - 1) + 1;
108          int successor = this[successorIndex] + 1;
109
110          foreach (Tour t in result) {
111            int insertPosition = t.Cities.IndexOf(successor);
112            if (insertPosition != -1) {
113              t.Cities.InsertRange(insertPosition, tour.Cities);
114              break;
115            }
116          }
117        }
118
119        result.Remove(tour);
120      }
121
122      return result;
123    }
124    #endregion
125
126    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
127      ZhuEncoding clone = new ZhuEncoding(
128        new Permutation(this.PermutationType, this.array), cities,
129        dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity,
130        coordinates, useDistanceMatrix);
131
132      cloner.RegisterClonedObject(this, clone);
133      clone.readOnly = readOnly;
134      return clone;
135    }
136
137    public ZhuEncoding(Permutation permutation, int cities,
138      DoubleArray dueTimeArray, DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
139      DoubleMatrix coordinates, BoolValue useDistanceMatrix)
140      : base(permutation) {
141        this.cities = cities;
142        this.dueTimeArray = dueTimeArray;
143        this.serviceTimeArray = serviceTimeArray;
144        this.readyTimeArray = readyTimeArray;
145        this.demandArray = demandArray;
146        this.capacity = capacity;
147        this.coordinates = coordinates;
148        this.useDistanceMatrix = useDistanceMatrix;
149    }
150
151    [StorableConstructor]
152    private ZhuEncoding(bool serializing)
153      : base(serializing) {
154    }
155
156    public static ZhuEncoding ConvertFrom(IVRPEncoding encoding, int cities,
157      DoubleArray dueTimeArray, DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
158      DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {
159      List<Tour> tours = encoding.GetTours(distanceMatrix);
160      List<int> route = new List<int>();
161
162      foreach (Tour tour in tours) {
163        foreach (int city in tour.Cities)
164          route.Add(city - 1);
165      }
166
167      return new ZhuEncoding(
168        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), cities,
169        dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity,
170        coordinates, useDistanceMatrix);
171    }
172
173    public static ZhuEncoding ConvertFrom(List<int> routeParam, int cities,
174      DoubleArray dueTimeArray, DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
175      DoubleMatrix coordinates, BoolValue useDistanceMatrix) {
176      List<int> route = new List<int>(routeParam);
177
178      while (route.Remove(0)) { //remove all delimiters (0)
179      }
180
181      for (int i = 0; i < route.Count; i++)
182        route[i]--;
183
184      return new ZhuEncoding(
185        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), cities,
186        dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity,
187        coordinates, useDistanceMatrix);
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.