Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/DistanceCalcualtors/VRPDistanceCalculator.cs @ 11552

Last change on this file since 11552 was 9750, checked in by gkronber, 11 years ago

#1591: adapted FLA branch to reference most recent version of ALGLIB (3.7.0) and VRP (3.4). Several major changes were necessary to port the implementation to the new VRP problem.

File size: 2.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Text;
6using HeuristicLab.Core;
7using HeuristicLab.Common.Resources;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Common;
10using HeuristicLab.Encodings.PermutationEncoding;
11using HeuristicLab.Problems.VehicleRouting;
12using HeuristicLab.Problems.VehicleRouting.Interfaces;
13
14namespace HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators.VRP {
15
16  [Item("VRPDistanceCalculator", "Calculates the distance of two VRP solution candidates.")]
17  [StorableClass]
18  public class VRPDistanceCalculator : NamedItem, IItemDistanceCalculator {
19
20    #region Properties
21
22    public override bool CanChangeName { get { return false; } }
23    public override bool CanChangeDescription { get { return false; } }
24    public static new Image StaticItemImage { get { return VSImageLibrary.Function; } }
25
26    #endregion
27
28    #region Construction & Cloning
29
30    [StorableConstructor]
31    protected VRPDistanceCalculator(bool deserializing) : base(deserializing) { }
32
33    protected VRPDistanceCalculator(VRPDistanceCalculator original, Cloner cloner)
34      : base(original, cloner) {
35    }
36
37    public VRPDistanceCalculator() {
38      name = ItemName;
39      description = ItemDescription;
40    }
41
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new VRPDistanceCalculator(this, cloner);
44    }
45
46    #endregion
47
48    #region IItemDistanceCalculator Members
49
50    public Type ItemType {
51      get { return typeof(Permutation); }
52    }
53
54    public double Distance(IItem x, IItem y) {
55      var a = GetEdgeSet((IVRPEncoding)x);
56      var b = GetEdgeSet((IVRPEncoding)y);
57      var aCount = a.Count;
58      a.IntersectWith(b);
59      return Math.Max(aCount, b.Count) - a.Count;
60    }
61
62    private HashSet<Point> GetEdgeSet(IVRPEncoding vrpSolution) {
63      HashSet<Point> edges = new HashSet<Point>();
64      foreach (var tour in vrpSolution.GetTours()) {
65        if (tour.Stops.Count > 0) {
66          edges.Add(CreateUndirectedEdge(0, tour.Stops[0]));
67          for (int i = 0; i < tour.Stops.Count - 1; i++) {
68            edges.Add(CreateUndirectedEdge(tour.Stops[i], tour.Stops[i + 1]));
69          }
70          edges.Add(CreateUndirectedEdge(tour.Stops.Last(), 0));
71        }
72      }
73      return edges;
74    }
75
76    private Point CreateUndirectedEdge(int x, int y) {
77      return new Point(Math.Min(x, y), Math.Max(x, y));
78    }
79
80    #endregion
81  }
82}
Note: See TracBrowser for help on using the repository browser.