Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/QAPPopulationDiversityAnalyzer.cs @ 14676

Last change on this file since 14676 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.9 KB
RevLine 
[5996]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5996]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 HeuristicLab.Analysis;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
[6628]25using HeuristicLab.Data;
[5996]26using HeuristicLab.Encodings.PermutationEncoding;
[6628]27using HeuristicLab.Parameters;
[5996]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.QuadraticAssignment {
31  /// <summary>
32  /// An operator for analyzing the diversity of solutions of Quadratic Assignment Problems regarding their structural identity (number of equal facilty->location assignments).
33  /// </summary>
34  [Item("QAPPopulationDiversityAnalyzer", "An operator for analyzing the diversity of solutions of Quadratic Assignment Problems regarding their structural identity (number of equal facilty->location assignments).")]
35  [StorableClass]
[14140]36#pragma warning disable 0612
[5996]37  public sealed class QAPPopulationDiversityAnalyzer : PopulationDiversityAnalyzer<Permutation> {
[14140]38#pragma warning restore 0612
[6628]39    public IValueParameter<BoolValue> UsePhenotypeSimilarityParameter {
40      get { return (IValueParameter<BoolValue>)Parameters["UsePhenotypeSimilarity"]; }
41    }
42    public ILookupParameter<DoubleMatrix> WeightsParameter {
43      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
44    }
45    public ILookupParameter<DoubleMatrix> DistancesParameter {
46      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
47    }
48
[5996]49    [StorableConstructor]
50    private QAPPopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
51    private QAPPopulationDiversityAnalyzer(QAPPopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
[6628]52    public QAPPopulationDiversityAnalyzer()
53      : base() {
54      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)));
55      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
56      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
57    }
[5996]58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new QAPPopulationDiversityAnalyzer(this, cloner);
61    }
62
[6628]63    [StorableHook(HookType.AfterDeserialization)]
64    private void AfterDeserialization() {
65      // BackwardsCompatibility3.3
66      #region Backwards compatible code, remove with 3.4
67      if (!Parameters.ContainsKey("UsePhenotypeSimilarity"))
68        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)));
69      if (!Parameters.ContainsKey("Weights"))
70        Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
71      if (!Parameters.ContainsKey("Distances"))
72        Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
73      #endregion
74    }
75
[5996]76    protected override double[,] CalculateSimilarities(Permutation[] solutions) {
[6628]77      DoubleMatrix weights = WeightsParameter.ActualValue, distances = DistancesParameter.ActualValue;
78      bool phenotypeSimilarity = UsePhenotypeSimilarityParameter.Value.Value;
[5996]79      int count = solutions.Length;
80      double[,] similarities = new double[count, count];
81
82      for (int i = 0; i < count; i++) {
83        similarities[i, i] = 1;
84        for (int j = i + 1; j < count; j++) {
[6628]85          if (phenotypeSimilarity)
86            similarities[i, j] = QAPPermutationProximityCalculator.CalculatePhenotypeSimilarity(solutions[i], solutions[j], weights, distances);
87          else similarities[i, j] = QAPPermutationProximityCalculator.CalculateGenotypeSimilarity(solutions[i], solutions[j]);
[5996]88          similarities[j, i] = similarities[i, j];
89        }
90      }
91      return similarities;
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.