Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Zhu/ZhuEncoding.cs @ 4687

Last change on this file since 4687 was 4687, checked in by mkommend, 13 years ago

Refactored VRP Encodings.General and Zhu (ticket #922).

File size: 6.1 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 System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.VehicleRouting.Encodings.General;
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    [StorableConstructor]
127    private ZhuEncoding(bool serializing)
128      : base(serializing) {
129    }
130
131    protected ZhuEncoding(ZhuEncoding original, Cloner cloner)
132      : base(original, cloner) {
133      this.cities = original.cities;
134      this.dueTimeArray = original.dueTimeArray;
135      this.serviceTimeArray = original.serviceTimeArray;
136      this.readyTimeArray = original.readyTimeArray;
137      this.demandArray = original.demandArray;
138      this.capacity = original.capacity;
139      this.coordinates = original.coordinates;
140      this.useDistanceMatrix = original.useDistanceMatrix;
141    }
142    public override IDeepCloneable Clone(Cloner cloner) {
143      return new ZhuEncoding(this, cloner);
144    }
145
146    public static ZhuEncoding ConvertFrom(IVRPEncoding encoding, int cities,
147      DoubleArray dueTimeArray, DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
148      DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {
149      List<Tour> tours = encoding.GetTours(distanceMatrix);
150      List<int> route = new List<int>();
151
152      foreach (Tour tour in tours) {
153        foreach (int city in tour.Cities)
154          route.Add(city - 1);
155      }
156
157      return new ZhuEncoding(
158        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), cities,
159        dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity,
160        coordinates, useDistanceMatrix);
161    }
162
163    public static ZhuEncoding ConvertFrom(List<int> routeParam, int cities,
164      DoubleArray dueTimeArray, DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
165      DoubleMatrix coordinates, BoolValue useDistanceMatrix) {
166      List<int> route = new List<int>(routeParam);
167
168      while (route.Remove(0)) { //remove all delimiters (0)
169      }
170
171      for (int i = 0; i < route.Count; i++)
172        route[i]--;
173
174      return new ZhuEncoding(
175        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), cities,
176        dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity,
177        coordinates, useDistanceMatrix);
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.