[4379] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[4379] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[4379] | 28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 29 |
|
---|
| 30 | namespace 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.")]
|
---|
[16565] | 32 | [StorableType("1A5F5A1D-E4F5-4477-887E-45FC488BC459")]
|
---|
[11961] | 33 | public class ZhuEncoding : General.PermutationEncoding {
|
---|
[4379] | 34 | #region IVRPEncoding Members
|
---|
[7856] | 35 | public override int GetTourIndex(Tour tour) {
|
---|
| 36 | return 0;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[4379] | 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);
|
---|
[6838] | 47 | if (!ProblemInstance.TourFeasible(newTour, this)) {
|
---|
[4379] | 48 | newTour.Stops.Remove(city);
|
---|
| 49 | if (newTour.Stops.Count > 0)
|
---|
[8053] | 50 | result.Add(newTour);
|
---|
[4379] | 51 |
|
---|
[8053] | 52 | newTour = new Tour();
|
---|
| 53 | newTour.Stops.Add(city);
|
---|
[4379] | 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 |
|
---|
[4752] | 97 | public ZhuEncoding(Permutation permutation, IVRPProblemInstance problemInstance)
|
---|
| 98 | : base(permutation, problemInstance) {
|
---|
| 99 | }
|
---|
[4379] | 100 |
|
---|
[4752] | 101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 102 | return new ZhuEncoding(this, cloner);
|
---|
[4379] | 103 | }
|
---|
| 104 |
|
---|
[4752] | 105 | protected ZhuEncoding(ZhuEncoding original, Cloner cloner)
|
---|
| 106 | : base(original, cloner) {
|
---|
[4379] | 107 | }
|
---|
| 108 |
|
---|
| 109 | [StorableConstructor]
|
---|
[16565] | 110 | protected ZhuEncoding(StorableConstructorFlag _) : base(_) {
|
---|
[4379] | 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 | }
|
---|