Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Zhu/ZhuEncoding.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
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  [StorableType("1A5F5A1D-E4F5-4477-887E-45FC488BC459")]
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(StorableConstructorFlag _) : base(_) {
111    }
112
113    public static ZhuEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
114      List<Tour> tours = encoding.GetTours();
115      List<int> route = new List<int>();
116
117      foreach (Tour tour in tours) {
118        foreach (int city in tour.Stops)
119          route.Add(city - 1);
120      }
121
122      return new ZhuEncoding(
123        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
124    }
125
126    public static ZhuEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance problemInstance) {
127      List<int> route = new List<int>(routeParam);
128
129      while (route.Remove(0)) { //remove all delimiters (0)
130      }
131
132      for (int i = 0; i < route.Count; i++)
133        route[i]--;
134
135      return new ZhuEncoding(
136        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.