Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6710 was 6710, checked in by svonolfe, 13 years ago

Added support for pickups and deliveries (#1177)

File size: 4.8 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 {   
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      return tour.GetTourLength(ProblemInstance);
83    }
84
85    public int FindBestInsertionPlace(Tour tour, int city) {
86      int place = -1;
87      double minQuality = -1;
88
89      for (int i = 0; i <= tour.Stops.Count; i++) {
90        tour.Stops.Insert(i, city);
91
92        VRPEvaluation eval = ProblemInstance.Evaluate(tour);
93        double quality = eval.Quality + eval.Penalty * (PenaltyFactor - 1.0);
94
95        if (place < 0 || quality < minQuality) {
96          place = i;
97          minQuality = quality;
98        }
99
100        tour.Stops.RemoveAt(i);
101      }
102
103      if (place == -1)
104        place = 0;
105
106      return place;
107    }
108
109    public bool FindInsertionPlace(int city, int routeToAvoid, bool allowInfeasible, out int route, out int place) {
110      route = -1;
111      place = -1;
112      bool bestFeasible = false;
113      double minDetour = double.MaxValue;
114
115      for (int tour = 0; tour < Tours.Count; tour++) {
116        if (tour != routeToAvoid) {
117          VRPEvaluation eval = ProblemInstance.Evaluate(Tours[tour]);
118          double length = eval.Quality;
119
120          for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
121            Tours[tour].Stops.Insert(i, city);
122
123            eval = ProblemInstance.Evaluate(Tours[tour]);
124            bool feasible = ProblemInstance.Feasible(eval);
125
126            if (feasible || allowInfeasible && !bestFeasible) {
127              double newLength = eval.Quality;
128              double detour = newLength - length;
129
130              if (route < 0 || detour < minDetour || feasible && !bestFeasible) {
131                route = tour;
132                place = i;
133                minDetour = detour;
134
135                if (feasible)
136                  bestFeasible = true;
137              }
138            }
139
140            Tours[tour].Stops.RemoveAt(i);
141          }
142        }
143      }
144
145      return route >= 0 && place >= 0;
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.