Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Prins/PrinsEncodedSolution.cs @ 17718

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

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

File size: 5.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 System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
31
32namespace HeuristicLab.Problems.VehicleRouting.Encodings.Prins {
33  [Item("PrinsEncodedSolution", "Represents a Prins encoded solution of the VRP. It is implemented as described in Prins, C. (2004). A simple and effective evolutionary algorithm for the vehicle routing problem. Computers & Operations Research, 12:1985-2002.")]
34  [StorableType("A0E1EBC1-C0F5-4CD5-9279-1A669C6633CD")]
35  public class PrinsEncodedSolution : General.PermutationEncodedSolution {
36    #region IVRPEncoding Members
37    public override int GetTourIndex(Tour tour) {
38      return 0;
39    }
40
41    public override List<Tour> GetTours() {
42      List<Tour> result = new List<Tour>();
43
44      int cities = ProblemInstance.Cities.Value;
45
46      //Split permutation into vector P
47      int[] P = new int[cities + 1];
48      for (int i = 0; i <= cities; i++)
49        P[i] = -1;
50
51      double[] V = new double[cities + 1];
52      V[0] = 0;
53      for (int i = 1; i <= cities; i++) {
54        V[i] = double.MaxValue;
55      }
56
57      for (int i = 1; i <= cities; i++) {
58        int j = i;
59        Tour tour = new Tour();
60        bool feasible = true;
61
62        do {
63          tour.Stops.Add(this[j - 1] + 1);
64
65          VRPEvaluation eval =
66            ProblemInstance.EvaluateTour(tour, this);
67
68          double cost = eval.Quality;
69          feasible = eval.IsFeasible;
70
71          if (feasible || j == i) {
72            if (V[i - 1] + cost < V[j]) {
73              V[j] = V[i - 1] + cost;
74              P[j] = i - 1;
75            }
76            j++;
77          }
78
79        } while (j <= cities && feasible);
80      }
81
82      //extract VRP solution from vector P
83      int index = 0;
84      int index2 = cities;
85      Tour trip = null;
86      do {
87        index = P[index2];
88        trip = new Tour();
89
90        for (int k = index + 1; k <= index2; k++) {
91          trip.Stops.Add(this[k - 1] + 1);
92        }
93
94        if (trip.Stops.Count > 0)
95          result.Add(trip);
96
97        index2 = index;
98      } while (index != 0);
99
100      //if there are too many vehicles - repair
101      while (result.Count > ProblemInstance.Vehicles.Value) {
102        Tour tour = result[result.Count - 1];
103
104        //find predecessor / successor in permutation
105        int predecessorIndex = Array.IndexOf(this.array, tour.Stops[0] - 1) - 1;
106        if (predecessorIndex >= 0) {
107          int predecessor = this[predecessorIndex] + 1;
108
109          foreach (Tour t in result) {
110            int insertPosition = t.Stops.IndexOf(predecessor) + 1;
111            if (insertPosition != -1) {
112              t.Stops.InsertRange(insertPosition, tour.Stops);
113              break;
114            }
115          }
116        } else {
117          int successorIndex = Array.IndexOf(this.array,
118            tour.Stops[tour.Stops.Count - 1] - 1) + 1;
119          int successor = this[successorIndex] + 1;
120
121          foreach (Tour t in result) {
122            int insertPosition = t.Stops.IndexOf(successor);
123            if (insertPosition != -1) {
124              t.Stops.InsertRange(insertPosition, tour.Stops);
125              break;
126            }
127          }
128        }
129
130        result.Remove(tour);
131      }
132
133      return result;
134    }
135    #endregion
136    public PrinsEncodedSolution(Permutation permutation, IVRPProblemInstance problemInstance)
137      : base(permutation, problemInstance) {
138    }
139
140    [StorableConstructor]
141    protected PrinsEncodedSolution(StorableConstructorFlag _) : base(_) {
142    }
143
144    public override IDeepCloneable Clone(Cloner cloner) {
145      return new PrinsEncodedSolution(this, cloner);
146    }
147
148    protected PrinsEncodedSolution(PrinsEncodedSolution original, Cloner cloner)
149      : base(original, cloner) {
150    }
151
152    public static PrinsEncodedSolution ConvertFrom(IVRPEncodedSolution encoding, IVRPProblemInstance problemInstance) {
153      List<Tour> tours = encoding.GetTours();
154      List<int> route = new List<int>();
155
156      foreach (Tour tour in tours) {
157        foreach (int city in tour.Stops)
158          route.Add(city - 1);
159      }
160
161      return new PrinsEncodedSolution(
162        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
163    }
164
165    public static PrinsEncodedSolution ConvertFrom(List<int> routeParam, IVRPProblemInstance problemInstance) {
166      var route = routeParam.Where(x => x != 0).Select(x => x - 1).ToArray();
167
168      return new PrinsEncodedSolution(
169        new Permutation(PermutationTypes.RelativeUndirected, route), problemInstance);
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.