Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11970 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Analysis;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
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]
36  public sealed class QAPPopulationDiversityAnalyzer : PopulationDiversityAnalyzer<Permutation> {
37    public IValueParameter<BoolValue> UsePhenotypeSimilarityParameter {
38      get { return (IValueParameter<BoolValue>)Parameters["UsePhenotypeSimilarity"]; }
39    }
40    public ILookupParameter<DoubleMatrix> WeightsParameter {
41      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
42    }
43    public ILookupParameter<DoubleMatrix> DistancesParameter {
44      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
45    }
46
47    [StorableConstructor]
48    private QAPPopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
49    private QAPPopulationDiversityAnalyzer(QAPPopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
50    public QAPPopulationDiversityAnalyzer()
51      : base() {
52      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)));
53      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
54      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new QAPPopulationDiversityAnalyzer(this, cloner);
59    }
60
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      // BackwardsCompatibility3.3
64      #region Backwards compatible code, remove with 3.4
65      if (!Parameters.ContainsKey("UsePhenotypeSimilarity"))
66        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)));
67      if (!Parameters.ContainsKey("Weights"))
68        Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
69      if (!Parameters.ContainsKey("Distances"))
70        Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
71      #endregion
72    }
73
74    protected override double[,] CalculateSimilarities(Permutation[] solutions) {
75      DoubleMatrix weights = WeightsParameter.ActualValue, distances = DistancesParameter.ActualValue;
76      bool phenotypeSimilarity = UsePhenotypeSimilarityParameter.Value.Value;
77      int count = solutions.Length;
78      double[,] similarities = new double[count, count];
79
80      for (int i = 0; i < count; i++) {
81        similarities[i, i] = 1;
82        for (int j = i + 1; j < count; j++) {
83          if (phenotypeSimilarity)
84            similarities[i, j] = QAPPermutationProximityCalculator.CalculatePhenotypeSimilarity(solutions[i], solutions[j], weights, distances);
85          else similarities[i, j] = QAPPermutationProximityCalculator.CalculateGenotypeSimilarity(solutions[i], solutions[j]);
86          similarities[j, i] = similarities[i, j];
87        }
88      }
89      return similarities;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.