Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Operators/3.3/SolutionSimilarityCalculator.cs @ 16437

Last change on this file since 16437 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 7.6 KB
RevLine 
[7789]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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
22using System;
[8304]23using System.Linq;
[13170]24using System.Threading.Tasks;
[7789]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[12280]27using HeuristicLab.Data;
[8299]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[7789]29
30namespace HeuristicLab.Optimization.Operators {
31  /// <summary>
[8086]32  /// A base class for items that perform similarity calculation between two solutions.
[7789]33  /// </summary>
[8086]34  [Item("SimilarityCalculator", "A base class for items that perform similarity calculation between two solutions.")]
[8299]35  [StorableClass]
[8322]36  public abstract class SolutionSimilarityCalculator : Item, ISolutionSimilarityCalculator {
[12280]37    protected abstract bool IsCommutative { get; }
38
39    #region Properties
40    [Storable]
41    public string SolutionVariableName { get; set; }
42    [Storable]
43    public string QualityVariableName { get; set; }
[13170]44    [Storable]
45    public bool ExecuteInParallel { get; set; }
46    [Storable]
47    public int MaxDegreeOfParallelism { get; set; }
[12280]48    #endregion
49
[8299]50    [StorableConstructor]
[8319]51    protected SolutionSimilarityCalculator(bool deserializing) : base(deserializing) { }
[12280]52
53    protected SolutionSimilarityCalculator(SolutionSimilarityCalculator original, Cloner cloner)
54      : base(original, cloner) {
[13170]55      SolutionVariableName = original.SolutionVariableName;
56      QualityVariableName = original.QualityVariableName;
57      ExecuteInParallel = original.ExecuteInParallel;
58      MaxDegreeOfParallelism = original.MaxDegreeOfParallelism;
[12280]59    }
[7789]60
[13170]61    protected SolutionSimilarityCalculator() : base() {
62      ExecuteInParallel = false;
63      MaxDegreeOfParallelism = -1;
64    }
65
66    [StorableHook(HookType.AfterDeserialization)]
67    private void AfterDeserialization() {
68      if (MaxDegreeOfParallelism == 0) {
69        ExecuteInParallel = false;
70        MaxDegreeOfParallelism = -1;
71      }
72    }
73
[8319]74    public double[][] CalculateSolutionCrowdSimilarity(IScope leftSolutionCrowd, IScope rightSolutionCrowd) {
75      if (leftSolutionCrowd == null || rightSolutionCrowd == null)
[8304]76        throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are null.");
[7789]77
[8319]78      var leftIndividuals = leftSolutionCrowd.SubScopes;
79      var rightIndividuals = rightSolutionCrowd.SubScopes;
[8304]80
81      if (!leftIndividuals.Any() || !rightIndividuals.Any())
82        throw new ArgumentException("Cannot calculate similarity because one of the provided crowds or both are empty.");
83
84      var similarityMatrix = new double[leftIndividuals.Count][];
85      for (int i = 0; i < leftIndividuals.Count; i++) {
86        similarityMatrix[i] = new double[rightIndividuals.Count];
87        for (int j = 0; j < rightIndividuals.Count; j++) {
[8319]88          similarityMatrix[i][j] = CalculateSolutionSimilarity(leftIndividuals[i], rightIndividuals[j]);
[8304]89        }
90      }
91
92      return similarityMatrix;
[7789]93    }
94
[8319]95    public double[][] CalculateSolutionCrowdSimilarity(IScope solutionCrowd) {
[13170]96      if (solutionCrowd == null) {
[8319]97        throw new ArgumentException("Cannot calculate similarity because the provided crowd is null.");
[13170]98      }
[8319]99      var individuals = solutionCrowd.SubScopes;
100
[13170]101      if (!individuals.Any()) {
[8319]102        throw new ArgumentException("Cannot calculate similarity because the provided crowd is empty.");
[13170]103      }
[8319]104
105      var similarityMatrix = new double[individuals.Count][];
[13170]106      for (int i = 0; i < individuals.Count; i++) {
107        similarityMatrix[i] = new double[individuals.Count];
108      }
[8413]109
[13170]110      if (ExecuteInParallel) {
111        var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = MaxDegreeOfParallelism };
112        if (IsCommutative) {
113          Parallel.For(0, individuals.Count, parallelOptions, i => {
114            for (int j = i; j < individuals.Count; j++) {
115              similarityMatrix[i][j] =
116                similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
117            }
118          });
119        } else {
120          Parallel.For(0, individuals.Count, parallelOptions, i => {
121            for (int j = i; j < individuals.Count; j++) {
122              similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
123              if (i == j) continue;
124              similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]);
125            }
126          });
[8319]127        }
[12280]128      } else {
[13170]129        if (IsCommutative) {
130          for (int i = 0; i < individuals.Count; i++) {
131            for (int j = i; j < individuals.Count; j++) {
132              similarityMatrix[i][j] =
133                similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
134            }
[12280]135          }
[13170]136        } else {
137          for (int i = 0; i < individuals.Count; i++) {
138            for (int j = i; j < individuals.Count; j++) {
139              similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
140              if (i == j) continue;
141              similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]);
142            }
143          }
[12280]144        }
[8319]145      }
146
147      return similarityMatrix;
148    }
149
150    public abstract double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution);
[12280]151
152    public virtual bool Equals(IScope x, IScope y) {
153      if (ReferenceEquals(x, y)) return true;
154      if (x == null || y == null) return false;
155
156      var q1 = x.Variables[QualityVariableName].Value;
157      var q2 = y.Variables[QualityVariableName].Value;
158
159      return CheckQualityEquality(q1, q2) && CalculateSolutionSimilarity(x, y).IsAlmost(1.0);
160    }
161
162    public virtual int GetHashCode(IScope scope) {
163      var quality = scope.Variables[QualityVariableName].Value;
164      var dv = quality as DoubleValue;
165      if (dv != null)
166        return dv.Value.GetHashCode();
167
168      var da = quality as DoubleArray;
169      if (da != null) {
170        int hash = 17;
171        unchecked {
172          for (int i = 0; i < da.Length; ++i) {
173            hash += hash * 23 + da[i].GetHashCode();
174          }
175          return hash;
176        }
177      }
178      return 0;
179    }
180
181    private static bool CheckQualityEquality(IItem q1, IItem q2) {
182      var d1 = q1 as DoubleValue;
183      var d2 = q2 as DoubleValue;
184
185      if (d1 != null && d2 != null)
186        return d1.Value.IsAlmost(d2.Value);
187
188      var da1 = q1 as DoubleArray;
189      var da2 = q2 as DoubleArray;
190
191      if (da1 != null && da2 != null) {
192        if (da1.Length != da2.Length)
193          throw new ArgumentException("The quality arrays must have the same length.");
194
195        for (int i = 0; i < da1.Length; ++i) {
196          if (!da1[i].IsAlmost(da2[i]))
197            return false;
198        }
199
200        return true;
201      }
202
203      throw new ArgumentException("Could not determine quality equality.");
204    }
[7789]205  }
206}
Note: See TracBrowser for help on using the repository browser.