Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15069 was 15069, checked in by abeham, 5 years ago

#2706:

  • Added or updated similarity calculators and population similarity analysis for several problems (BinPacking, LAP, Orienteering, Parameter optimization, PTSP, QAP, TF, TSP, VRP)
  • Made TSPSimilarityCalculator obsolete since it's essentially the same as the one in the permutation plugin
  • Made QAPPopulationDiversityAnalyzer obsolete as it is replaced by the newer PopulationSimilarityAnalyzer
  • Removed genotype specific similarity code in QAPPermutationProximityCalculator (again identical to the permutation plugin)
  • Changed QAPSimilarityCalculator to perform phenotype similarity instead of genotype similarity (has not been previously used)
File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
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(bool deserializing) : base(deserializing) { }
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.