Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7729 was 7202, checked in by epitzer, 13 years ago

#1696: Add static item image according to #1651

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