[7141] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Drawing;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Common.Resources;
|
---|
| 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 9 | using HeuristicLab.Common;
|
---|
| 10 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 11 | using HeuristicLab.Problems.VehicleRouting;
|
---|
| 12 |
|
---|
| 13 | namespace 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; } }
|
---|
[7202] | 23 | public static new Image StaticItemImage { get { return VSImageLibrary.Function; } }
|
---|
[7141] | 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 | }
|
---|