Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added support for incremental evaluation to improve performance (#1177)

File size: 4.4 KB
RevLine 
[4376]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]
[6710]37  public class PotvinEncoding : TourEncoding {   
[4376]38    [Storable]
39    public List<int> Unrouted { get; set; }
40
41    public PotvinEncoding(IVRPProblemInstance instance)
42      : base(instance) {
43      Unrouted = new List<int>();
44    }
45
46    [StorableConstructor]
47    private PotvinEncoding(bool serializing)
48      : base(serializing) {
49    }
50
[4752]51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new PotvinEncoding(this, cloner);
53    }
54
55    protected PotvinEncoding(PotvinEncoding original, Cloner cloner)
56      : base(original, cloner) {
57        this.Unrouted = new List<int>(original.Unrouted);
58    }
59
[4376]60    public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
61      PotvinEncoding solution = new PotvinEncoding(instance);
62
63      TourEncoding.ConvertFrom(encoding, solution, instance);
64
65      return solution;
66    }
67
68    public static PotvinEncoding ConvertFrom(List<int> route, IVRPProblemInstance instance) {
69      PotvinEncoding solution = new PotvinEncoding(instance);
70
71      TourEncoding.ConvertFrom(route, solution);
72
73      return solution;
74    }
75
76    public double GetTourLength(Tour tour) {
[6607]77      return tour.GetTourLength(ProblemInstance);
[4376]78    }
79
[5127]80    public int FindBestInsertionPlace(Tour tour, int city) {
81      int place = -1;
82      double minQuality = -1;
83
[6752]84      VRPEvaluation eval = ProblemInstance.Evaluate(tour);
85
[5127]86      for (int i = 0; i <= tour.Stops.Count; i++) {
[6752]87        bool feasible;
88        double quality = ProblemInstance.GetInsertionCosts(eval, city, 0, i, out feasible);
[5127]89        if (place < 0 || quality < minQuality) {
90          place = i;
91          minQuality = quality;
92        }
93      }
94
95      if (place == -1)
96        place = 0;
97
98      return place;
99    }
100
[6607]101    public bool FindInsertionPlace(int city, int routeToAvoid, bool allowInfeasible, out int route, out int place) {
[4376]102      route = -1;
103      place = -1;
[6607]104      double minDetour = double.MaxValue;
[4376]105
[6752]106      VRPEvaluation eval = ProblemInstance.Evaluate(this);
107      bool originalFeasible = ProblemInstance.Feasible(eval);
108
[4376]109      for (int tour = 0; tour < Tours.Count; tour++) {
110        if (tour != routeToAvoid) {
[6710]111          double length = eval.Quality;
112
[6607]113          for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
[6752]114            bool feasible;
115            double detour = ProblemInstance.GetInsertionCosts(eval, city, tour, i, out feasible);
116           
117            if (feasible || allowInfeasible) {
118              if (route < 0 || detour < minDetour) {
[4376]119                route = tour;
120                place = i;
121                minDetour = detour;
122              }
123            }
124          }
125        }
126      }
127
128      return route >= 0 && place >= 0;
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.