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.Drawing;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
31 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
32 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
35 | [Item("PotvinEncoding", "Represents a potvin encoding of VRP solutions. It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class PotvinEncoding : TourEncoding, ISingleDepotEncoding, ITimeWindowedEncoding, IHomogenousCapacitatedEncoding {
|
---|
38 | [Storable]
|
---|
39 | public List<int> Unrouted { get; set; }
|
---|
40 |
|
---|
41 | public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
|
---|
42 | PotvinEncoding clone = new PotvinEncoding(ProblemInstance);
|
---|
43 | cloner.RegisterClonedObject(this, clone);
|
---|
44 | clone.Tours = (ItemList<Tour>)cloner.Clone(this.Tours);
|
---|
45 | clone.Unrouted = new List<int>(Unrouted);
|
---|
46 | return clone;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public PotvinEncoding(IVRPProblemInstance instance)
|
---|
50 | : base(instance) {
|
---|
51 | Unrouted = new List<int>();
|
---|
52 | }
|
---|
53 |
|
---|
54 | [StorableConstructor]
|
---|
55 | private PotvinEncoding(bool serializing)
|
---|
56 | : base(serializing) {
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
|
---|
60 | PotvinEncoding solution = new PotvinEncoding(instance);
|
---|
61 |
|
---|
62 | TourEncoding.ConvertFrom(encoding, solution, instance);
|
---|
63 |
|
---|
64 | return solution;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static PotvinEncoding ConvertFrom(List<int> route, IVRPProblemInstance instance) {
|
---|
68 | PotvinEncoding solution = new PotvinEncoding(instance);
|
---|
69 |
|
---|
70 | TourEncoding.ConvertFrom(route, solution);
|
---|
71 |
|
---|
72 | return solution;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public double GetTourLength(Tour tour) {
|
---|
76 | double length = 0;
|
---|
77 |
|
---|
78 | if (tour.Stops.Count > 0) {
|
---|
79 | List<int> cities = new List<int>();
|
---|
80 | cities.Add(0);
|
---|
81 | foreach (int city in tour.Stops) {
|
---|
82 | cities.Add(city);
|
---|
83 | }
|
---|
84 | cities.Add(0);
|
---|
85 |
|
---|
86 | for (int i = 1; i < cities.Count; i++) {
|
---|
87 | length += ProblemInstance.GetDistance(cities[i - 1], cities[i]);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | return length;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public bool FindInsertionPlace(int city, int routeToAvoid, out int route, out int place) {
|
---|
95 | route = -1;
|
---|
96 | place = -1;
|
---|
97 | double minDetour = 0;
|
---|
98 |
|
---|
99 | for (int tour = 0; tour < Tours.Count; tour++) {
|
---|
100 | if (tour != routeToAvoid) {
|
---|
101 | double length = GetTourLength(Tours[tour]);
|
---|
102 |
|
---|
103 | for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
|
---|
104 | Tours[tour].Stops.Insert(i, city);
|
---|
105 |
|
---|
106 | VRPEvaluation eval = ProblemInstance.EvaluatorParameter.Value.Evaluate(
|
---|
107 | ProblemInstance, Tours[tour]);
|
---|
108 | if (ProblemInstance.EvaluatorParameter.Value.Feasible(eval)) {
|
---|
109 | double newLength = eval.Distance;
|
---|
110 |
|
---|
111 | double detour = newLength - length;
|
---|
112 |
|
---|
113 | if (route <= 0 || detour < minDetour) {
|
---|
114 | route = tour;
|
---|
115 | place = i;
|
---|
116 | minDetour = detour;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | Tours[tour].Stops.RemoveAt(i);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return route >= 0 && place >= 0;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|