[7789] | 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 |
|
---|
| 22 | using System;
|
---|
[8304] | 23 | using System.Linq;
|
---|
[7789] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[8299] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7789] | 27 |
|
---|
| 28 | namespace HeuristicLab.Optimization.Operators {
|
---|
| 29 | /// <summary>
|
---|
[8086] | 30 | /// A base class for items that perform similarity calculation between two solutions.
|
---|
[7789] | 31 | /// </summary>
|
---|
[8086] | 32 | [Item("SimilarityCalculator", "A base class for items that perform similarity calculation between two solutions.")]
|
---|
[8299] | 33 | [StorableClass]
|
---|
[8322] | 34 | public abstract class SolutionSimilarityCalculator : Item, ISolutionSimilarityCalculator {
|
---|
[8299] | 35 | [StorableConstructor]
|
---|
[8319] | 36 | protected SolutionSimilarityCalculator(bool deserializing) : base(deserializing) { }
|
---|
| 37 | protected SolutionSimilarityCalculator(SolutionSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 38 | protected SolutionSimilarityCalculator() : base() { }
|
---|
[7789] | 39 |
|
---|
[8319] | 40 | public double[][] CalculateSolutionCrowdSimilarity(IScope leftSolutionCrowd, IScope rightSolutionCrowd) {
|
---|
| 41 | if (leftSolutionCrowd == null || rightSolutionCrowd == null)
|
---|
[8304] | 42 | throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are null.");
|
---|
[7789] | 43 |
|
---|
[8319] | 44 | var leftIndividuals = leftSolutionCrowd.SubScopes;
|
---|
| 45 | var rightIndividuals = rightSolutionCrowd.SubScopes;
|
---|
[8304] | 46 |
|
---|
| 47 | if (!leftIndividuals.Any() || !rightIndividuals.Any())
|
---|
| 48 | throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are empty.");
|
---|
| 49 |
|
---|
| 50 | var similarityMatrix = new double[leftIndividuals.Count][];
|
---|
| 51 | for (int i = 0; i < leftIndividuals.Count; i++) {
|
---|
| 52 | similarityMatrix[i] = new double[rightIndividuals.Count];
|
---|
| 53 | for (int j = 0; j < rightIndividuals.Count; j++) {
|
---|
[8319] | 54 | similarityMatrix[i][j] = CalculateSolutionSimilarity(leftIndividuals[i], rightIndividuals[j]);
|
---|
[8304] | 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | return similarityMatrix;
|
---|
[7789] | 59 | }
|
---|
| 60 |
|
---|
[8319] | 61 | public double[][] CalculateSolutionCrowdSimilarity(IScope solutionCrowd) {
|
---|
| 62 | if (solutionCrowd == null)
|
---|
| 63 | throw new ArgumentException("Cannot calculate similarity because the provided crowd is null.");
|
---|
| 64 |
|
---|
| 65 | var individuals = solutionCrowd.SubScopes;
|
---|
| 66 |
|
---|
| 67 | if (!individuals.Any())
|
---|
| 68 | throw new ArgumentException("Cannot calculate similarity because the provided crowd is empty.");
|
---|
| 69 |
|
---|
| 70 | var similarityMatrix = new double[individuals.Count][];
|
---|
[8413] | 71 | for (int i = 0; i < individuals.Count; i++) similarityMatrix[i] = new double[individuals.Count];
|
---|
| 72 |
|
---|
[8319] | 73 | for (int i = 0; i < individuals.Count; i++) {
|
---|
| 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);
|
---|
[7789] | 90 | }
|
---|
| 91 | }
|
---|