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 |
|
---|
22 | using HeuristicLab.Analysis;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
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 | #pragma warning disable 0612
|
---|
37 | public sealed class QAPPopulationDiversityAnalyzer : PopulationDiversityAnalyzer<Permutation> {
|
---|
38 | #pragma warning restore 0612
|
---|
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 |
|
---|
49 | [StorableConstructor]
|
---|
50 | private QAPPopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
51 | private QAPPopulationDiversityAnalyzer(QAPPopulationDiversityAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
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 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new QAPPopulationDiversityAnalyzer(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
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 |
|
---|
76 | protected override double[,] CalculateSimilarities(Permutation[] solutions) {
|
---|
77 | DoubleMatrix weights = WeightsParameter.ActualValue, distances = DistancesParameter.ActualValue;
|
---|
78 | bool phenotypeSimilarity = UsePhenotypeSimilarityParameter.Value.Value;
|
---|
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++) {
|
---|
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]);
|
---|
88 | similarities[j, i] = similarities[i, j];
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return similarities;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|