Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added usage of the TS memory in the customer relocation move (#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 GetTourLength(Tour tour) {
82      double length = 0;
83
84      if (tour.Stops.Count > 0) {
85        List<int> cities = new List<int>();
86        cities.Add(0);
87        foreach (int city in tour.Stops) {
88          cities.Add(city);
89        }
90        cities.Add(0);
91
92        for (int i = 1; i < cities.Count; i++) {
93          length += ProblemInstance.GetDistance(cities[i - 1], cities[i]);
94        }
95      } 
96
97      return length;
98    }
99
100    public int FindBestInsertionPlace(Tour tour, int city) {
101      int place = -1;
102      double minQuality = -1;
103
104      for (int i = 0; i <= tour.Stops.Count; i++) {
105        tour.Stops.Insert(i, city);
106
107        VRPEvaluation eval = ProblemInstance.EvaluatorParameter.Value.Evaluate(ProblemInstance, tour);
108        double quality = eval.Quality + eval.Penalty * (PenaltyFactor - 1.0);
109
110        if (place < 0 || quality < minQuality) {
111          place = i;
112          minQuality = quality;
113        }
114
115        tour.Stops.RemoveAt(i);
116      }
117
118      if (place == -1)
119        place = 0;
120
121      return place;
122    }
123
124    public bool FindInsertionPlace(int city, int routeToAvoid, out int route, out int place) {
125      route = -1;
126      place = -1;
127      double minDetour = 0;
128
129      for (int tour = 0; tour < Tours.Count; tour++) {
130        if (tour != routeToAvoid) {
131          double length = GetTourLength(Tours[tour]);
132
133          for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
134            Tours[tour].Stops.Insert(i, city);
135
136            VRPEvaluation eval = ProblemInstance.EvaluatorParameter.Value.Evaluate(
137              ProblemInstance, Tours[tour]);
138            if (ProblemInstance.EvaluatorParameter.Value.Feasible(eval)) {
139              double newLength = eval.Distance;
140
141              double detour = newLength - length;
142
143              if (route <= 0 || detour < minDetour) {
144                route = tour;
145                place = i;
146                minDetour = detour;
147              }
148            }
149
150            Tours[tour].Stops.RemoveAt(i);
151          }
152        }
153      }
154
155      return route >= 0 && place >= 0;
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.