Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/QAPPopulationDiversityAnalyzer.cs @ 17270

Last change on this file since 17270 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Parameters;
29using HEAL.Attic;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Problems.QuadraticAssignment {
33  /// <summary>
34  /// An operator for analyzing the diversity of solutions of Quadratic Assignment Problems regarding their structural identity (number of equal facilty->location assignments).
35  /// </summary>
36  [Item("QAPPopulationDiversityAnalyzer", "An operator for analyzing the diversity of solutions of Quadratic Assignment Problems regarding their structural identity (number of equal facilty->location assignments).")]
37  [StorableType("255DFAD4-14E4-4CFB-905F-BA521815F593")]
38  [Obsolete("Use the PopulationSimilarityAnalyzer in the HeuristicLab.Analysis plugin instead.")]
39  [NonDiscoverableType]
40#pragma warning disable 0612
41  internal sealed class QAPPopulationDiversityAnalyzer : PopulationDiversityAnalyzer<Permutation> {
42#pragma warning restore 0612
43    public IValueParameter<BoolValue> UsePhenotypeSimilarityParameter {
44      get { return (IValueParameter<BoolValue>)Parameters["UsePhenotypeSimilarity"]; }
45    }
46    public ILookupParameter<DoubleMatrix> WeightsParameter {
47      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
48    }
49    public ILookupParameter<DoubleMatrix> DistancesParameter {
50      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
51    }
52
53    [StorableConstructor]
54    private QAPPopulationDiversityAnalyzer(StorableConstructorFlag _) : base(_) { }
55    private QAPPopulationDiversityAnalyzer(QAPPopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
56    public QAPPopulationDiversityAnalyzer()
57      : base() {
58      Parameters.Add(new ValueParameter<BoolValue>("UsePhenotypeSimilarity", "True if the similarity should be measured a level closer to the phenotype (the number of similar assignments of individual weights to distances). Set to false if the number of equal assignments (facility to location) should be counted.", new BoolValue(false)));
59      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
60      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new QAPPopulationDiversityAnalyzer(this, cloner);
65    }
66
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69      // BackwardsCompatibility3.3
70      #region Backwards compatible code, remove with 3.4
71      if (!Parameters.ContainsKey("UsePhenotypeSimilarity"))
72        Parameters.Add(new ValueParameter<BoolValue>("UsePhenotypeSimilarity", "True if the similarity should be measured a level closer to the phenotype (the number of similar assignments of individual weights to distances). Set to false if the number of equal assignments (facility to location) should be counted.", new BoolValue(false)));
73      if (!Parameters.ContainsKey("Weights"))
74        Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
75      if (!Parameters.ContainsKey("Distances"))
76        Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
77      #endregion
78    }
79
80    protected override double[,] CalculateSimilarities(Permutation[] solutions) {
81      DoubleMatrix weights = WeightsParameter.ActualValue, distances = DistancesParameter.ActualValue;
82      bool phenotypeSimilarity = UsePhenotypeSimilarityParameter.Value.Value;
83      int count = solutions.Length;
84      double[,] similarities = new double[count, count];
85
86      for (int i = 0; i < count; i++) {
87        similarities[i, i] = 1;
88        for (int j = i + 1; j < count; j++) {
89          if (phenotypeSimilarity)
90            similarities[i, j] = QAPPermutationProximityCalculator.CalculatePhenotypeSimilarity(solutions[i], solutions[j], weights, distances);
91          else similarities[i, j] = HammingSimilarityCalculator.CalculateSimilarity(solutions[i], solutions[j]);
92          similarities[j, i] = similarities[i, j];
93        }
94      }
95      return similarities;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.