[7789] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7789] | 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;
|
---|
[12085] | 26 | using HeuristicLab.Data;
|
---|
[8299] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7789] | 28 |
|
---|
| 29 | namespace HeuristicLab.Optimization.Operators {
|
---|
| 30 | /// <summary>
|
---|
[8086] | 31 | /// A base class for items that perform similarity calculation between two solutions.
|
---|
[7789] | 32 | /// </summary>
|
---|
[8086] | 33 | [Item("SimilarityCalculator", "A base class for items that perform similarity calculation between two solutions.")]
|
---|
[8299] | 34 | [StorableClass]
|
---|
[8322] | 35 | public abstract class SolutionSimilarityCalculator : Item, ISolutionSimilarityCalculator {
|
---|
[12070] | 36 | protected abstract bool IsCommutative { get; }
|
---|
| 37 |
|
---|
[12085] | 38 | #region Properties
|
---|
| 39 | [Storable]
|
---|
| 40 | public string SolutionVariableName { get; set; }
|
---|
| 41 | [Storable]
|
---|
| 42 | public string QualityVariableName { get; set; }
|
---|
| 43 | #endregion
|
---|
| 44 |
|
---|
[8299] | 45 | [StorableConstructor]
|
---|
[8319] | 46 | protected SolutionSimilarityCalculator(bool deserializing) : base(deserializing) { }
|
---|
[12085] | 47 |
|
---|
| 48 | protected SolutionSimilarityCalculator(SolutionSimilarityCalculator original, Cloner cloner)
|
---|
| 49 | : base(original, cloner) {
|
---|
| 50 | this.SolutionVariableName = original.SolutionVariableName;
|
---|
| 51 | this.QualityVariableName = original.QualityVariableName;
|
---|
| 52 | }
|
---|
[8319] | 53 | protected SolutionSimilarityCalculator() : base() { }
|
---|
[7789] | 54 |
|
---|
[8319] | 55 | public double[][] CalculateSolutionCrowdSimilarity(IScope leftSolutionCrowd, IScope rightSolutionCrowd) {
|
---|
| 56 | if (leftSolutionCrowd == null || rightSolutionCrowd == null)
|
---|
[8304] | 57 | throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are null.");
|
---|
[7789] | 58 |
|
---|
[8319] | 59 | var leftIndividuals = leftSolutionCrowd.SubScopes;
|
---|
| 60 | var rightIndividuals = rightSolutionCrowd.SubScopes;
|
---|
[8304] | 61 |
|
---|
| 62 | if (!leftIndividuals.Any() || !rightIndividuals.Any())
|
---|
| 63 | throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are empty.");
|
---|
| 64 |
|
---|
| 65 | var similarityMatrix = new double[leftIndividuals.Count][];
|
---|
| 66 | for (int i = 0; i < leftIndividuals.Count; i++) {
|
---|
| 67 | similarityMatrix[i] = new double[rightIndividuals.Count];
|
---|
| 68 | for (int j = 0; j < rightIndividuals.Count; j++) {
|
---|
[8319] | 69 | similarityMatrix[i][j] = CalculateSolutionSimilarity(leftIndividuals[i], rightIndividuals[j]);
|
---|
[8304] | 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | return similarityMatrix;
|
---|
[7789] | 74 | }
|
---|
| 75 |
|
---|
[8319] | 76 | public double[][] CalculateSolutionCrowdSimilarity(IScope solutionCrowd) {
|
---|
| 77 | if (solutionCrowd == null)
|
---|
| 78 | throw new ArgumentException("Cannot calculate similarity because the provided crowd is null.");
|
---|
| 79 |
|
---|
| 80 | var individuals = solutionCrowd.SubScopes;
|
---|
| 81 |
|
---|
| 82 | if (!individuals.Any())
|
---|
| 83 | throw new ArgumentException("Cannot calculate similarity because the provided crowd is empty.");
|
---|
| 84 |
|
---|
| 85 | var similarityMatrix = new double[individuals.Count][];
|
---|
[8413] | 86 | for (int i = 0; i < individuals.Count; i++) similarityMatrix[i] = new double[individuals.Count];
|
---|
| 87 |
|
---|
[12070] | 88 | if (IsCommutative) {
|
---|
| 89 | for (int i = 0; i < individuals.Count; i++) {
|
---|
| 90 | for (int j = i; j < individuals.Count; j++) {
|
---|
| 91 | similarityMatrix[i][j] = similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
|
---|
| 92 | }
|
---|
[8319] | 93 | }
|
---|
[12070] | 94 | } else {
|
---|
| 95 | for (int i = 0; i < individuals.Count; i++) {
|
---|
| 96 | for (int j = i; j < individuals.Count; j++) {
|
---|
| 97 | similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
|
---|
| 98 | if (i == j) continue;
|
---|
| 99 | similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
[8319] | 102 | }
|
---|
| 103 |
|
---|
| 104 | return similarityMatrix;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public abstract double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution);
|
---|
[12085] | 108 |
|
---|
| 109 | public virtual bool Equals(IScope x, IScope y) {
|
---|
| 110 | if (ReferenceEquals(x, y)) return true;
|
---|
| 111 | if (x == null || y == null) return false;
|
---|
| 112 |
|
---|
[12126] | 113 | var q1 = x.Variables[QualityVariableName].Value;
|
---|
| 114 | var q2 = y.Variables[QualityVariableName].Value;
|
---|
[12085] | 115 |
|
---|
| 116 | return CheckQualityEquality(q1, q2) && CalculateSolutionSimilarity(x, y).IsAlmost(1.0);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public virtual int GetHashCode(IScope scope) {
|
---|
| 120 | var quality = scope.Variables[QualityVariableName].Value;
|
---|
| 121 | var dv = quality as DoubleValue;
|
---|
| 122 | if (dv != null)
|
---|
| 123 | return dv.Value.GetHashCode();
|
---|
| 124 |
|
---|
| 125 | var da = quality as DoubleArray;
|
---|
| 126 | if (da != null) {
|
---|
| 127 | int hash = 17;
|
---|
| 128 | unchecked {
|
---|
| 129 | for (int i = 0; i < da.Length; ++i) {
|
---|
| 130 | hash += hash * 23 + da[i].GetHashCode();
|
---|
| 131 | }
|
---|
| 132 | return hash;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
[12126] | 135 | return 0;
|
---|
[12085] | 136 | }
|
---|
| 137 |
|
---|
| 138 | private static bool CheckQualityEquality(IItem q1, IItem q2) {
|
---|
| 139 | var d1 = q1 as DoubleValue;
|
---|
| 140 | var d2 = q2 as DoubleValue;
|
---|
| 141 |
|
---|
| 142 | if (d1 != null && d2 != null)
|
---|
| 143 | return d1.Value.IsAlmost(d2.Value);
|
---|
| 144 |
|
---|
| 145 | var da1 = q1 as DoubleArray;
|
---|
| 146 | var da2 = q2 as DoubleArray;
|
---|
| 147 |
|
---|
[12129] | 148 | if (da1 != null && da2 != null) {
|
---|
| 149 | if (da1.Length != da2.Length)
|
---|
| 150 | throw new ArgumentException("The quality arrays must have the same length.");
|
---|
[12085] | 151 |
|
---|
[12129] | 152 | for (int i = 0; i < da1.Length; ++i) {
|
---|
| 153 | if (!da1[i].IsAlmost(da2[i]))
|
---|
| 154 | return false;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | return true;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[12085] | 160 | throw new ArgumentException("Could not determine quality equality.");
|
---|
| 161 | }
|
---|
[7789] | 162 | }
|
---|
| 163 | }
|
---|