Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/SimilarityCalculators/VRPSimilarityCalculator.cs @ 18151

Last change on this file since 18151 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
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  [StorableType("16011E6B-62F5-4165-9F8D-E4BA5191FB7E")]
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    [StorableConstructor]
48    private VRPSimilarityCalculator(StorableConstructorFlag _) : base(_) { }
49    private VRPSimilarityCalculator(VRPSimilarityCalculator original, Cloner cloner)
50      : base(original, cloner) {
51      this.ProblemInstance = cloner.Clone(original.ProblemInstance);
52    }
53    public VRPSimilarityCalculator() : base() { }
54
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new VRPSimilarityCalculator(this, cloner);
57    }
58
59    public static double CalculateSimilarity(PotvinEncoding left, PotvinEncoding right) {
60      if (left == null || right == null)
61        throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
62      if (left == right) return 1.0;
63
64      // extract edges from first solution
65      var edges1 = new List<Tuple<int, int>>();
66      foreach (Tour tour in left.Tours) {
67        edges1.Add(new Tuple<int, int>(0, tour.Stops[0]));
68        for (int i = 0; i < tour.Stops.Count - 1; i++)
69          edges1.Add(new Tuple<int, int>(tour.Stops[i], tour.Stops[i + 1]));
70        edges1.Add(new Tuple<int, int>(tour.Stops[tour.Stops.Count - 1], 0));
71      }
72
73      // extract edges from second solution
74      var edges2 = new List<Tuple<int, int>>();
75      foreach (Tour tour in right.Tours) {
76        edges2.Add(new Tuple<int, int>(0, tour.Stops[0]));
77        for (int i = 0; i < tour.Stops.Count - 1; i++)
78          edges2.Add(new Tuple<int, int>(tour.Stops[i], tour.Stops[i + 1]));
79        edges2.Add(new Tuple<int, int>(tour.Stops[tour.Stops.Count - 1], 0));
80      }
81
82      if (edges1.Count + edges2.Count == 0)
83        throw new ArgumentException("Cannot calculate diversity because no tours exist.");
84
85      int identicalEdges = 0;
86      foreach (var edge in edges1) {
87        if (edges2.Any(x => x.Equals(edge)))
88          identicalEdges++;
89      }
90
91      return identicalEdges * 2.0 / (edges1.Count + edges2.Count);
92    }
93
94    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
95      var sol1 = leftSolution.Variables[SolutionVariableName].Value as IVRPEncoding;
96      var sol2 = rightSolution.Variables[SolutionVariableName].Value as IVRPEncoding;
97
98      var potvinSol1 = sol1 is PotvinEncoding ? sol1 as PotvinEncoding : PotvinEncoding.ConvertFrom(sol1, ProblemInstance);
99      var potvinSol2 = sol2 is PotvinEncoding ? sol2 as PotvinEncoding : PotvinEncoding.ConvertFrom(sol2, ProblemInstance);
100
101      return CalculateSimilarity(potvinSol1, potvinSol2);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.