Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Problems.VehicleRouting/3.4/SimilarityCalculators/VRPSimilarityCalculator.cs @ 15089

Last change on this file since 15089 was 12070, checked in by jkarder, 10 years ago

#2332: added SolutionSimilarityCalculator.IsCommutative

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
30using HeuristicLab.Problems.VehicleRouting.Interfaces;
31
32namespace HeuristicLab.Problems.VehicleRouting {
33  /// <summary>
34  /// An operator which performs similarity calculation between two VRP solutions.
35  /// </summary>
36  /// <remarks>
37  /// The operator calculates the similarity based on the number of edges the two solutions have in common.
38  /// </remarks>
39  [Item("VRPSimilarityCalculator", "An operator which performs similarity calculation between two VRP solutions.")]
40  [StorableClass]
41  public sealed class VRPSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
42    protected override bool IsCommutative { get { return true; } }
43
44    [Storable]
45    public IVRPProblemInstance ProblemInstance { get; set; }
46
47    private VRPSimilarityCalculator(bool deserializing) : base(deserializing) { }
48    private VRPSimilarityCalculator(VRPSimilarityCalculator original, Cloner cloner)
49      : base(original, cloner) {
50      this.ProblemInstance = cloner.Clone(original.ProblemInstance);
51    }
52    public VRPSimilarityCalculator() : base() { }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new VRPSimilarityCalculator(this, cloner);
56    }
57
58    public static double CalculateSimilarity(PotvinEncoding left, PotvinEncoding right) {
59      if (left == null || right == null)
60        throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
61      if (left == right) return 1.0;
62
63      // extract edges from first solution
64      var edges1 = new List<Tuple<int, int>>();
65      foreach (Tour tour in left.Tours) {
66        edges1.Add(new Tuple<int, int>(0, tour.Stops[0]));
67        for (int i = 0; i < tour.Stops.Count - 1; i++)
68          edges1.Add(new Tuple<int, int>(tour.Stops[i], tour.Stops[i + 1]));
69        edges1.Add(new Tuple<int, int>(tour.Stops[tour.Stops.Count - 1], 0));
70      }
71
72      // extract edges from second solution
73      var edges2 = new List<Tuple<int, int>>();
74      foreach (Tour tour in right.Tours) {
75        edges2.Add(new Tuple<int, int>(0, tour.Stops[0]));
76        for (int i = 0; i < tour.Stops.Count - 1; i++)
77          edges2.Add(new Tuple<int, int>(tour.Stops[i], tour.Stops[i + 1]));
78        edges2.Add(new Tuple<int, int>(tour.Stops[tour.Stops.Count - 1], 0));
79      }
80
81      if (edges1.Count + edges2.Count == 0)
82        throw new ArgumentException("Cannot calculate diversity because no tours exist.");
83
84      int identicalEdges = 0;
85      foreach (var edge in edges1) {
86        if (edges2.Any(x => x.Equals(edge)))
87          identicalEdges++;
88      }
89
90      return identicalEdges * 2.0 / (edges1.Count + edges2.Count);
91    }
92
93    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
94      var sol1 = leftSolution.Variables[SolutionVariableName].Value as IVRPEncoding;
95      var sol2 = rightSolution.Variables[SolutionVariableName].Value as IVRPEncoding;
96
97      var potvinSol1 = sol1 is PotvinEncoding ? sol1 as PotvinEncoding : PotvinEncoding.ConvertFrom(sol1, ProblemInstance);
98      var potvinSol2 = sol2 is PotvinEncoding ? sol2 as PotvinEncoding : PotvinEncoding.ConvertFrom(sol2, ProblemInstance);
99
100      return CalculateSimilarity(potvinSol1, potvinSol2);
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.