Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Zhu/ZhuEncoding.cs @ 9456

Last change on this file since 9456 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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