Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Optimization.Operators/3.3/SolutionSimilarityCalculator.cs @ 8319

Last change on this file since 8319 was 8319, checked in by jkarder, 12 years ago

#1331:

  • applied some of the changes suggested by ascheibe in comment:32:ticket:1331
  • restructured path relinking and improvement operators and similarity calculators
  • fixed bug in TSPMultipleGuidesPathRelinker
File size: 3.9 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.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization.Operators {
30  /// <summary>
31  /// A base class for items that perform similarity calculation between two solutions.
32  /// </summary>
33  [Item("SimilarityCalculator", "A base class for items that perform similarity calculation between two solutions.")]
34  [StorableClass]
35  public abstract class SolutionSimilarityCalculator : Item, ISolutionSimilarityCalculator, IEqualityComparer<IScope> {
36    [StorableConstructor]
37    protected SolutionSimilarityCalculator(bool deserializing) : base(deserializing) { }
38    protected SolutionSimilarityCalculator(SolutionSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
39    protected SolutionSimilarityCalculator() : base() { }
40
41    public double[][] CalculateSolutionCrowdSimilarity(IScope leftSolutionCrowd, IScope rightSolutionCrowd) {
42      if (leftSolutionCrowd == null || rightSolutionCrowd == null)
43        throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are null.");
44
45      var leftIndividuals = leftSolutionCrowd.SubScopes;
46      var rightIndividuals = rightSolutionCrowd.SubScopes;
47
48      if (!leftIndividuals.Any() || !rightIndividuals.Any())
49        throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are empty.");
50
51      var similarityMatrix = new double[leftIndividuals.Count][];
52      for (int i = 0; i < leftIndividuals.Count; i++) {
53        similarityMatrix[i] = new double[rightIndividuals.Count];
54        for (int j = 0; j < rightIndividuals.Count; j++) {
55          similarityMatrix[i][j] = CalculateSolutionSimilarity(leftIndividuals[i], rightIndividuals[j]);
56        }
57      }
58
59      return similarityMatrix;
60    }
61
62    public double[][] CalculateSolutionCrowdSimilarity(IScope solutionCrowd) {
63      if (solutionCrowd == null)
64        throw new ArgumentException("Cannot calculate similarity because the provided crowd is null.");
65
66      var individuals = solutionCrowd.SubScopes;
67
68      if (!individuals.Any())
69        throw new ArgumentException("Cannot calculate similarity because the provided crowd is empty.");
70
71      var similarityMatrix = new double[individuals.Count][];
72      for (int i = 0; i < individuals.Count; i++) {
73        similarityMatrix[i] = new double[individuals.Count];
74        for (int j = i; j < individuals.Count; j++) {
75          similarityMatrix[i][j] = similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
76        }
77      }
78
79      return similarityMatrix;
80    }
81
82    public bool Equals(IScope x, IScope y) {
83      if (object.ReferenceEquals(x, y)) return true;
84      if (x == null || y == null) return false;
85      return CalculateSolutionSimilarity(x, y) == 1.0;
86    }
87
88    public abstract double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution);
89    public abstract int GetHashCode(IScope obj);
90  }
91}
Note: See TracBrowser for help on using the repository browser.