Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAP/HeuristicLab.Analysis/3.3/MultidimensionalScaling/PopulationDiversityMDSAnalyzer.cs @ 5728

Last change on this file since 5728 was 5723, checked in by abeham, 14 years ago

#1330

  • Moved MDS into Analysis
  • Added test with some simple examples
File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Analysis {
33  [Item("PopulationDiversityMDSAnalyzer", "Transforms the population similarity HeatMap into coordinates by making use of multidimensional scaling.")]
34  [StorableClass]
35  public class PopulationDiversityMDSAnalyzer : SingleSuccessorOperator, IAnalyzer {
36    public ILookupParameter<ResultCollection> ResultsParameter {
37      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
38    }
39    public ILookupParameter<ResultCollection> PopulationDiversityAnalyzerResultsParameter {
40      get { return (ILookupParameter<ResultCollection>)Parameters["PopulationDiversityAnalyzer Results"]; }
41    }
42    public ILookupParameter<HeatMap> SolutionSimilaritiesParameter {
43      get { return (ILookupParameter<HeatMap>)Parameters["Solution Similarities"]; }
44    }
45    /*public ILookupParameter<CoordinatesHistroy> CoordinatesHistoryParameter {
46      get { return (ILookupParameter<CoordinatesHistroy>)Parameters["CoordinatesHistory"]; }
47    }*/
48
49    [StorableConstructor]
50    protected PopulationDiversityMDSAnalyzer(bool deserializing) : base(deserializing) { }
51    protected PopulationDiversityMDSAnalyzer(PopulationDiversityMDSAnalyzer original, Cloner cloner) : base(original, cloner) { }
52    public PopulationDiversityMDSAnalyzer() {
53      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection in which the population diversity analyzer injects its result collection."));
54      Parameters.Add(new LookupParameter<ResultCollection>("PopulationDiversityAnalyzer Results", "The result collection in which the diversity analyzer injects its results."));
55      Parameters.Add(new LookupParameter<HeatMap>("Solution Similarities", "The HeatMap that stores the solution similarities."));
56      //Parameters.Add(new LookupParameter<CoordinatesHistroy>("CoordinatesHistory", "The history of the assigned coordinates."));
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new PopulationDiversityMDSAnalyzer(this, cloner);
61    }
62
63    public override IOperation Apply() {
64      ResultCollection results = ResultsParameter.ActualValue;
65      ResultCollection popDivResults = (results[PopulationDiversityAnalyzerResultsParameter.ActualName] as IResult).Value as ResultCollection;
66      if (popDivResults == null) throw new InvalidOperationException("Results collection of population diversity analyzer cannot be found.");
67      HeatMap similarites = (popDivResults[SolutionSimilaritiesParameter.ActualName] as IResult).Value as HeatMap;
68      if (similarites == null) throw new InvalidOperationException("HeatMap cannot be found in the results collection of the population diversity analyzer.");
69      if (similarites.Rows != similarites.Columns) throw new InvalidOperationException("HeatMap is not a square matrix.");
70
71      CoordinatesHistory history = null;
72      if (popDivResults.ContainsKey("MDS"))
73        history = popDivResults["MDS"].Value as CoordinatesHistory;
74      else {
75        history = new CoordinatesHistory();
76        popDivResults.Add(new Result("MDS", history));
77      }
78      int dimension = similarites.Rows;
79      DoubleMatrix distances = new DoubleMatrix(dimension, dimension);
80      for (int i = 0; i < dimension; i++) {
81        for (int j = 0; j < dimension; j++) {
82          if (i == j) continue;
83          distances[i, j] = 1.0 - similarites[i, j];
84        }
85      }
86      double stress;
87      DoubleMatrix coordinates = null;
88      if (history.Count == 0) {
89        coordinates = MultidimensionalScaling.MetricByDistance(distances, out stress);
90      } else coordinates = MultidimensionalScaling.MetricByDistance(distances, out stress, history.Last().Clone() as DoubleMatrix);
91      history.Add(coordinates);
92
93      return base.Apply();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.