Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/PotvinEncoding.cs @ 5127

Last change on this file since 5127 was 5127, checked in by svonolfe, 14 years ago

Implemented unified tabu search (WIP) (#1177)

File size: 5.1 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System.Drawing;
28using System.Collections.Generic;
29using HeuristicLab.Problems.VehicleRouting.Encodings.General;
30using HeuristicLab.Problems.VehicleRouting.Variants;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
32using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
33
34namespace 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    [Storable]
42    public double PenaltyFactor { get; set; }
43
44    public PotvinEncoding(IVRPProblemInstance instance)
45      : base(instance) {
46      Unrouted = new List<int>();
47      PenaltyFactor = 1;
48    }
49
50    [StorableConstructor]
51    private PotvinEncoding(bool serializing)
52      : base(serializing) {
53    }
54
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new PotvinEncoding(this, cloner);
57    }
58
59    protected PotvinEncoding(PotvinEncoding original, Cloner cloner)
60      : base(original, cloner) {
61        this.Unrouted = new List<int>(original.Unrouted);
62        this.PenaltyFactor = original.PenaltyFactor;
63    }
64
65    public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
66      PotvinEncoding solution = new PotvinEncoding(instance);
67
68      TourEncoding.ConvertFrom(encoding, solution, instance);
69
70      return solution;
71    }
72
73    public static PotvinEncoding ConvertFrom(List<int> route, IVRPProblemInstance instance) {
74      PotvinEncoding solution = new PotvinEncoding(instance);
75
76      TourEncoding.ConvertFrom(route, solution);
77
78      return solution;
79    }
80
81    public double EvaluateTour(Tour tour) {
82      return ProblemInstance.Evaluate(tour);
83    }
84
85    public double GetTourLength(Tour tour) {
86      double length = 0;
87
88      if (tour.Stops.Count > 0) {
89        List<int> cities = new List<int>();
90        cities.Add(0);
91        foreach (int city in tour.Stops) {
92          cities.Add(city);
93        }
94        cities.Add(0);
95
96        for (int i = 1; i < cities.Count; i++) {
97          length += ProblemInstance.GetDistance(cities[i - 1], cities[i]);
98        }
99      } 
100
101      return length;
102    }
103
104    public int FindBestInsertionPlace(Tour tour, int city) {
105      int place = -1;
106      double minQuality = -1;
107
108      for (int i = 0; i <= tour.Stops.Count; i++) {
109        tour.Stops.Insert(i, city);
110
111        double quality = EvaluateTour(tour);
112
113        if (place < 0 || quality < minQuality) {
114          place = i;
115          minQuality = quality;
116        }
117
118        tour.Stops.RemoveAt(i);
119      }
120
121      if (place == -1)
122        place = 0;
123
124      return place;
125    }
126
127    public bool FindInsertionPlace(int city, int routeToAvoid, out int route, out int place) {
128      route = -1;
129      place = -1;
130      double minDetour = 0;
131
132      for (int tour = 0; tour < Tours.Count; tour++) {
133        if (tour != routeToAvoid) {
134          double length = GetTourLength(Tours[tour]);
135
136          for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
137            Tours[tour].Stops.Insert(i, city);
138
139            VRPEvaluation eval = ProblemInstance.EvaluatorParameter.Value.Evaluate(
140              ProblemInstance, Tours[tour]);
141            if (ProblemInstance.EvaluatorParameter.Value.Feasible(eval)) {
142              double newLength = eval.Distance;
143
144              double detour = newLength - length;
145
146              if (route <= 0 || detour < minDetour) {
147                route = tour;
148                place = i;
149                minDetour = detour;
150              }
151            }
152
153            Tours[tour].Stops.RemoveAt(i);
154          }
155        }
156      }
157
158      return route >= 0 && place >= 0;
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.