Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1331: added support for similarity calculation between multiple individuals

File size: 3.0 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization.Operators {
29  /// <summary>
30  /// A base class for items that perform similarity calculation between two solutions.
31  /// </summary>
32  [Item("SimilarityCalculator", "A base class for items that perform similarity calculation between two solutions.")]
33  [StorableClass]
34  public abstract class SimilarityCalculator : Item, ISimilarityCalculator {
35    #region Properties
36    [Storable]
37    private string target;
38    public string Target {
39      get { return target; }
40      set { target = value; }
41    }
42    #endregion
43
44    [StorableConstructor]
45    protected SimilarityCalculator(bool deserializing) : base(deserializing) { }
46    protected SimilarityCalculator(SimilarityCalculator original, Cloner cloner)
47      : base(original, cloner) {
48      if (original.Target != null)
49        this.target = (string)original.Target.Clone();
50    }
51    protected SimilarityCalculator() : base() { }
52
53    public double[][] CalculateCrowdSimilarity(IScope leftCrowd, IScope rightCrowd) {
54      if (leftCrowd == null || rightCrowd == null)
55        throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are null.");
56
57      var leftIndividuals = leftCrowd.SubScopes;
58      var rightIndividuals = rightCrowd.SubScopes;
59
60      if (!leftIndividuals.Any() || !rightIndividuals.Any())
61        throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are empty.");
62
63      var similarityMatrix = new double[leftIndividuals.Count][];
64      for (int i = 0; i < leftIndividuals.Count; i++) {
65        similarityMatrix[i] = new double[rightIndividuals.Count];
66        for (int j = 0; j < rightIndividuals.Count; j++) {
67          similarityMatrix[i][j] = CalculateIndividualSimilarity(leftIndividuals[i], rightIndividuals[j]);
68        }
69      }
70
71      return similarityMatrix;
72    }
73
74    public abstract double CalculateIndividualSimilarity(IScope leftIndividual, IScope rightIndividual);
75  }
76}
Note: See TracBrowser for help on using the repository browser.