Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.VehicleRouting/3.3/SimilarityCalculators/VRPSimilarityCalculator.cs @ 7952

Last change on this file since 7952 was 7815, checked in by svonolfe, 13 years ago

Added first working version of VRP path relinker (#1331)

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
30using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
31using HeuristicLab.Data;
32using HeuristicLab.Parameters;
33
34namespace HeuristicLab.Problems.VehicleRouting {
35  /// <summary>
36  /// An operator that performs similarity calculation between two vehicle routing solutions.
37  /// </summary>
38  [Item("VRPSimilarityCalculator", "An operator that performs similarity calculation between two vehicle routing solutions.")]
39  [StorableClass]
40  public sealed class VRPSimilarityCalculator : SimilarityCalculator {
41    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
42      get {
43        if (Parameters.ContainsKey("DistanceMatrix"))
44          return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"];
45        else
46          return null;
47      }
48    }
49
50    [StorableConstructor]
51    private VRPSimilarityCalculator(bool deserializing) : base(deserializing) { }
52    private VRPSimilarityCalculator(VRPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
53    public VRPSimilarityCalculator() : base() {
54      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new VRPSimilarityCalculator(this, cloner);
59    }
60
61    public static double CalculateSimilarity(PotvinEncoding e1, PotvinEncoding e2) {
62      // extract edges from first solution
63      var edges1 = new List<Tuple<int, int>>();
64      foreach (Tour tour in e1.Tours) {
65        edges1.Add(new Tuple<int, int>(0, tour.Cities[0]));
66        for (int i = 0; i < tour.Cities.Count - 1; i++)
67          edges1.Add(new Tuple<int, int>(tour.Cities[i], tour.Cities[i + 1]));
68        edges1.Add(new Tuple<int, int>(tour.Cities[tour.Cities.Count - 1], 0));
69      }
70
71      // extract edges from second solution
72      var edges2 = new List<Tuple<int, int>>();
73      foreach (Tour tour in e2.Tours) {
74        edges2.Add(new Tuple<int, int>(0, tour.Cities[0]));
75        for (int i = 0; i < tour.Cities.Count - 1; i++)
76          edges2.Add(new Tuple<int, int>(tour.Cities[i], tour.Cities[i + 1]));
77        edges2.Add(new Tuple<int, int>(tour.Cities[tour.Cities.Count - 1], 0));
78      }
79
80      if (edges1.Count + edges2.Count == 0)
81        throw new ArgumentException("Cannot calculate diversity because no tours exist.");
82
83      int identicalEdges = 0;
84      foreach (var edge in edges1) {
85        if (edges2.Any(x => x.Equals(edge)))
86          identicalEdges++;
87      }
88
89      return identicalEdges * 2.0 / (edges1.Count + edges2.Count);
90    }
91
92    protected override double CalculateSimilarity(IScope left, IScope right) {
93      IVRPEncoding e1 = left.Variables[TargetParameter.ActualName].Value as IVRPEncoding;
94      if (!(e1 is PotvinEncoding))
95        e1 = PotvinEncoding.ConvertFrom(e1 as IVRPEncoding, DistanceMatrixParameter);
96
97      IVRPEncoding e2 = right.Variables[TargetParameter.ActualName].Value as IVRPEncoding;
98      if (!(e2 is PotvinEncoding))
99        e2 = PotvinEncoding.ConvertFrom(e2 as IVRPEncoding, DistanceMatrixParameter);
100
101      return CalculateSimilarity(e1 as PotvinEncoding, e2 as PotvinEncoding);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.