Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Zhu/ZhuEncodedSolution.cs @ 17954

Last change on this file since 17954 was 17717, checked in by abeham, 4 years ago

#2521: working on VRP (refactoring all the capabilities, features, and operator discovery)

File size: 4.7 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 System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Zhu {
32  [Item("ZhuEncodedSolution", "Represents a Zhu encoded solution of the VRP. 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  [StorableType("1A5F5A1D-E4F5-4477-887E-45FC488BC459")]
34  public class ZhuEncodedSolution : General.PermutationEncodedSolution {
35    #region IVRPEncoding Members
36    public override int GetTourIndex(Tour tour) {
37      return 0;
38    }
39
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);
48        var evalNewTour = ProblemInstance.EvaluateTour(newTour, this);
49        if (!evalNewTour.IsFeasible) {
50          newTour.Stops.Remove(city);
51          if (newTour.Stops.Count > 0)
52            result.Add(newTour);
53
54          newTour = new Tour();
55          newTour.Stops.Add(city);
56        }
57      }
58
59      if (newTour.Stops.Count > 0)
60        result.Add(newTour);
61
62      //if there are too many vehicles - repair
63      while (result.Count > ProblemInstance.Vehicles.Value) {
64        Tour tour = result[result.Count - 1];
65
66        //find predecessor / successor in permutation
67        int predecessorIndex = Array.IndexOf(this.array, tour.Stops[0] - 1) - 1;
68        if (predecessorIndex >= 0) {
69          int predecessor = this[predecessorIndex] + 1;
70
71          foreach (Tour t in result) {
72            int insertPosition = t.Stops.IndexOf(predecessor) + 1;
73            if (insertPosition != -1) {
74              t.Stops.InsertRange(insertPosition, tour.Stops);
75              break;
76            }
77          }
78        } else {
79          int successorIndex = Array.IndexOf(this.array,
80            tour.Stops[tour.Stops.Count - 1] - 1) + 1;
81          int successor = this[successorIndex] + 1;
82
83          foreach (Tour t in result) {
84            int insertPosition = t.Stops.IndexOf(successor);
85            if (insertPosition != -1) {
86              t.Stops.InsertRange(insertPosition, tour.Stops);
87              break;
88            }
89          }
90        }
91
92        result.Remove(tour);
93      }
94
95      return result;
96    }
97    #endregion
98
99    public ZhuEncodedSolution(Permutation permutation, IVRPProblemInstance problemInstance)
100      : base(permutation, problemInstance) {
101    }
102
103    public override IDeepCloneable Clone(Cloner cloner) {
104      return new ZhuEncodedSolution(this, cloner);
105    }
106
107    protected ZhuEncodedSolution(ZhuEncodedSolution original, Cloner cloner)
108      : base(original, cloner) {
109    }
110
111    [StorableConstructor]
112    protected ZhuEncodedSolution(StorableConstructorFlag _) : base(_) {
113    }
114
115    public static ZhuEncodedSolution ConvertFrom(IVRPEncodedSolution encoding, IVRPProblemInstance problemInstance) {
116      List<Tour> tours = encoding.GetTours();
117      var count = tours.Sum(x => x.Stops.Count);
118      var route = new int[count];
119
120      var i = 0;
121      foreach (Tour tour in tours) {
122        foreach (int city in tour.Stops)
123          route[i++] = city - 1;
124      }
125
126      return new ZhuEncodedSolution(
127        new Permutation(PermutationTypes.RelativeUndirected, route), problemInstance);
128    }
129
130    public static ZhuEncodedSolution ConvertFrom(List<int> routeParam, IVRPProblemInstance problemInstance) {
131      var route = routeParam.Where(x => x != 0).Select(x => x - 1).ToArray();
132
133      return new ZhuEncodedSolution(
134        new Permutation(PermutationTypes.RelativeUndirected, route), problemInstance);
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.