[4379] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 | using System.Collections.Generic;
|
---|
| 28 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
| 29 | using System;
|
---|
| 30 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Zhu {
|
---|
| 33 | [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.")]
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public class ZhuEncoding : PermutationEncoding {
|
---|
| 36 | #region IVRPEncoding Members
|
---|
[7856] | 37 | public override int GetTourIndex(Tour tour) {
|
---|
| 38 | return 0;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[4379] | 41 | public override List<Tour> GetTours() {
|
---|
| 42 | List<Tour> result = new List<Tour>();
|
---|
| 43 |
|
---|
| 44 | Tour newTour = new Tour();
|
---|
| 45 |
|
---|
| 46 | for (int i = 0; i < this.Length; i++) {
|
---|
| 47 | int city = this[i] + 1;
|
---|
| 48 | newTour.Stops.Add(city);
|
---|
[6838] | 49 | if (!ProblemInstance.TourFeasible(newTour, this)) {
|
---|
[4379] | 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 |
|
---|
[4752] | 99 | public ZhuEncoding(Permutation permutation, IVRPProblemInstance problemInstance)
|
---|
| 100 | : base(permutation, problemInstance) {
|
---|
| 101 | }
|
---|
[4379] | 102 |
|
---|
[4752] | 103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 104 | return new ZhuEncoding(this, cloner);
|
---|
[4379] | 105 | }
|
---|
| 106 |
|
---|
[4752] | 107 | protected ZhuEncoding(ZhuEncoding original, Cloner cloner)
|
---|
| 108 | : base(original, cloner) {
|
---|
[4379] | 109 | }
|
---|
| 110 |
|
---|
| 111 | [StorableConstructor]
|
---|
[7907] | 112 | protected ZhuEncoding(bool serializing)
|
---|
[4379] | 113 | : base(serializing) {
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public static ZhuEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
|
---|
| 117 | List<Tour> tours = encoding.GetTours();
|
---|
| 118 | List<int> route = new List<int>();
|
---|
| 119 |
|
---|
| 120 | foreach (Tour tour in tours) {
|
---|
| 121 | foreach (int city in tour.Stops)
|
---|
| 122 | route.Add(city - 1);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | return new ZhuEncoding(
|
---|
| 126 | new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public static ZhuEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance problemInstance) {
|
---|
| 130 | List<int> route = new List<int>(routeParam);
|
---|
| 131 |
|
---|
| 132 | while (route.Remove(0)) { //remove all delimiters (0)
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | for (int i = 0; i < route.Count; i++)
|
---|
| 136 | route[i]--;
|
---|
| 137 |
|
---|
| 138 | return new ZhuEncoding(
|
---|
| 139 | new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|