[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;
|
---|
[9750] | 12 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[7141] | 13 |
|
---|
| 14 | namespace 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; } }
|
---|
[7202] | 24 | public static new Image StaticItemImage { get { return VSImageLibrary.Function; } }
|
---|
[7141] | 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>();
|
---|
[9750] | 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]));
|
---|
[7141] | 69 | }
|
---|
[9750] | 70 | edges.Add(CreateUndirectedEdge(tour.Stops.Last(), 0));
|
---|
[7141] | 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 | }
|
---|