Free cookie consent management tool by TermsFeed Policy Generator

source: branches/dataset-ids-2695/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Zhu/ZhuEncoding.cs @ 15529

Last change on this file since 15529 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Encodings.PermutationEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Zhu {
31  [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.")]
32  [StorableClass]
33  public class ZhuEncoding : General.PermutationEncoding {
34    #region IVRPEncoding Members
35    public override int GetTourIndex(Tour tour) {
36      return 0;
37    }
38
39    public override List<Tour> GetTours() {
40      List<Tour> result = new List<Tour>();
41
42      Tour newTour = new Tour();
43
44      for (int i = 0; i < this.Length; i++) {
45        int city = this[i] + 1;
46        newTour.Stops.Add(city);
47        if (!ProblemInstance.TourFeasible(newTour, this)) {
48          newTour.Stops.Remove(city);
49          if (newTour.Stops.Count > 0)
50            result.Add(newTour);
51
52          newTour = new Tour();
53          newTour.Stops.Add(city);
54        }
55      }
56
57      if (newTour.Stops.Count > 0)
58        result.Add(newTour);
59
60      //if there are too many vehicles - repair
61      while (result.Count > ProblemInstance.Vehicles.Value) {
62        Tour tour = result[result.Count - 1];
63
64        //find predecessor / successor in permutation
65        int predecessorIndex = Array.IndexOf(this.array, tour.Stops[0] - 1) - 1;
66        if (predecessorIndex >= 0) {
67          int predecessor = this[predecessorIndex] + 1;
68
69          foreach (Tour t in result) {
70            int insertPosition = t.Stops.IndexOf(predecessor) + 1;
71            if (insertPosition != -1) {
72              t.Stops.InsertRange(insertPosition, tour.Stops);
73              break;
74            }
75          }
76        } else {
77          int successorIndex = Array.IndexOf(this.array,
78            tour.Stops[tour.Stops.Count - 1] - 1) + 1;
79          int successor = this[successorIndex] + 1;
80
81          foreach (Tour t in result) {
82            int insertPosition = t.Stops.IndexOf(successor);
83            if (insertPosition != -1) {
84              t.Stops.InsertRange(insertPosition, tour.Stops);
85              break;
86            }
87          }
88        }
89
90        result.Remove(tour);
91      }
92
93      return result;
94    }
95    #endregion
96
97    public ZhuEncoding(Permutation permutation, IVRPProblemInstance problemInstance)
98      : base(permutation, problemInstance) {
99    }
100
101    public override IDeepCloneable Clone(Cloner cloner) {
102      return new ZhuEncoding(this, cloner);
103    }
104
105    protected ZhuEncoding(ZhuEncoding original, Cloner cloner)
106      : base(original, cloner) {
107    }
108
109    [StorableConstructor]
110    protected ZhuEncoding(bool serializing)
111      : base(serializing) {
112    }
113
114    public static ZhuEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
115      List<Tour> tours = encoding.GetTours();
116      List<int> route = new List<int>();
117
118      foreach (Tour tour in tours) {
119        foreach (int city in tour.Stops)
120          route.Add(city - 1);
121      }
122
123      return new ZhuEncoding(
124        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
125    }
126
127    public static ZhuEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance problemInstance) {
128      List<int> route = new List<int>(routeParam);
129
130      while (route.Remove(0)) { //remove all delimiters (0)
131      }
132
133      for (int i = 0; i < route.Count; i++)
134        route[i]--;
135
136      return new ZhuEncoding(
137        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.