[5996] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16453] | 3 | * Copyright (C) 2002-2019 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 |
|
---|
[15069] | 22 | using System;
|
---|
[5996] | 23 | using HeuristicLab.Analysis;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[6628] | 26 | using HeuristicLab.Data;
|
---|
[5996] | 27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[6628] | 28 | using HeuristicLab.Parameters;
|
---|
[16559] | 29 | using HEAL.Attic;
|
---|
[15069] | 30 | using HeuristicLab.PluginInfrastructure;
|
---|
[5996] | 31 |
|
---|
| 32 | namespace 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).")]
|
---|
[16462] | 37 | [StorableType("255DFAD4-14E4-4CFB-905F-BA521815F593")]
|
---|
[15069] | 38 | [Obsolete("Use the PopulationSimilarityAnalyzer in the HeuristicLab.Analysis plugin instead.")]
|
---|
| 39 | [NonDiscoverableType]
|
---|
[14140] | 40 | #pragma warning disable 0612
|
---|
[15069] | 41 | internal sealed class QAPPopulationDiversityAnalyzer : PopulationDiversityAnalyzer<Permutation> {
|
---|
[14140] | 42 | #pragma warning restore 0612
|
---|
[6628] | 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 |
|
---|
[5996] | 53 | [StorableConstructor]
|
---|
[16462] | 54 | private QAPPopulationDiversityAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
[5996] | 55 | private QAPPopulationDiversityAnalyzer(QAPPopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
[6628] | 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 | }
|
---|
[5996] | 62 |
|
---|
| 63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 64 | return new QAPPopulationDiversityAnalyzer(this, cloner);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[6628] | 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 |
|
---|
[5996] | 80 | protected override double[,] CalculateSimilarities(Permutation[] solutions) {
|
---|
[6628] | 81 | DoubleMatrix weights = WeightsParameter.ActualValue, distances = DistancesParameter.ActualValue;
|
---|
| 82 | bool phenotypeSimilarity = UsePhenotypeSimilarityParameter.Value.Value;
|
---|
[5996] | 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++) {
|
---|
[6628] | 89 | if (phenotypeSimilarity)
|
---|
| 90 | similarities[i, j] = QAPPermutationProximityCalculator.CalculatePhenotypeSimilarity(solutions[i], solutions[j], weights, distances);
|
---|
[15069] | 91 | else similarities[i, j] = HammingSimilarityCalculator.CalculateSimilarity(solutions[i], solutions[j]);
|
---|
[5996] | 92 | similarities[j, i] = similarities[i, j];
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | return similarities;
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|