#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization.Operators; using HeuristicLab.Problems.VehicleRouting.Encodings; using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin; namespace HeuristicLab.Problems.VehicleRouting { /// /// An operator that performs similarity calculation between two vehicle routing solutions. /// [Item("VRPSimilarityCalculator", "An operator that performs similarity calculation between two vehicle routing solutions.")] public sealed class VRPSimilarityCalculator : SimilarityCalculator { private VRPSimilarityCalculator(bool deserializing) : base(deserializing) { } private VRPSimilarityCalculator(VRPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { } public VRPSimilarityCalculator() : base() { } public override IDeepCloneable Clone(Cloner cloner) { return new VRPSimilarityCalculator(this, cloner); } public static double CalculateSimilarity(PotvinEncoding left, PotvinEncoding right) { if (left == null || right == null) throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null."); // extract edges from first solution var edges1 = new List>(); foreach (Tour tour in left.Tours) { edges1.Add(new Tuple(0, tour.Cities[0])); for (int i = 0; i < tour.Cities.Count - 1; i++) edges1.Add(new Tuple(tour.Cities[i], tour.Cities[i + 1])); edges1.Add(new Tuple(tour.Cities[tour.Cities.Count - 1], 0)); } // extract edges from second solution var edges2 = new List>(); foreach (Tour tour in right.Tours) { edges2.Add(new Tuple(0, tour.Cities[0])); for (int i = 0; i < tour.Cities.Count - 1; i++) edges2.Add(new Tuple(tour.Cities[i], tour.Cities[i + 1])); edges2.Add(new Tuple(tour.Cities[tour.Cities.Count - 1], 0)); } if (edges1.Count + edges2.Count == 0) throw new ArgumentException("Cannot calculate diversity because no tours exist."); int identicalEdges = 0; foreach (var edge in edges1) { if (edges2.Any(x => x.Equals(edge))) identicalEdges++; } return identicalEdges * 2.0 / (edges1.Count + edges2.Count); } protected override double CalculateSimilarity(IScope left, IScope right) { PotvinEncoding sol1 = left.Variables[Target].Value as PotvinEncoding; PotvinEncoding sol2 = right.Variables[Target].Value as PotvinEncoding; return CalculateSimilarity(sol1, sol2); } } }