[5996] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 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 |
|
---|
| 22 | using HeuristicLab.Analysis;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
[6628] | 25 | using HeuristicLab.Data;
|
---|
[5996] | 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[6628] | 27 | using HeuristicLab.Parameters;
|
---|
[5996] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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> {
|
---|
[6628] | 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 |
|
---|
[5996] | 47 | [StorableConstructor]
|
---|
| 48 | private QAPPopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 49 | private QAPPopulationDiversityAnalyzer(QAPPopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
[6628] | 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 | }
|
---|
[5996] | 56 |
|
---|
| 57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 58 | return new QAPPopulationDiversityAnalyzer(this, cloner);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[6628] | 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 |
|
---|
[5996] | 74 | protected override double[,] CalculateSimilarities(Permutation[] solutions) {
|
---|
[6628] | 75 | DoubleMatrix weights = WeightsParameter.ActualValue, distances = DistancesParameter.ActualValue;
|
---|
| 76 | bool phenotypeSimilarity = UsePhenotypeSimilarityParameter.Value.Value;
|
---|
[5996] | 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++) {
|
---|
[6628] | 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]);
|
---|
[5996] | 86 | similarities[j, i] = similarities[i, j];
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | return similarities;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|