#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Analysis {
[Item("PopulationDiversityMDSAnalyzer", "Transforms the population similarity HeatMap into coordinates by making use of multidimensional scaling.")]
[StorableClass]
public class PopulationDiversityMDSAnalyzer : SingleSuccessorOperator, IAnalyzer {
public ILookupParameter ResultsParameter {
get { return (ILookupParameter)Parameters["Results"]; }
}
public ILookupParameter PopulationDiversityAnalyzerResultsParameter {
get { return (ILookupParameter)Parameters["PopulationDiversityAnalyzer Results"]; }
}
public ILookupParameter SolutionSimilaritiesParameter {
get { return (ILookupParameter)Parameters["Solution Similarities"]; }
}
/*public ILookupParameter CoordinatesHistoryParameter {
get { return (ILookupParameter)Parameters["CoordinatesHistory"]; }
}*/
[StorableConstructor]
protected PopulationDiversityMDSAnalyzer(bool deserializing) : base(deserializing) { }
protected PopulationDiversityMDSAnalyzer(PopulationDiversityMDSAnalyzer original, Cloner cloner) : base(original, cloner) { }
public PopulationDiversityMDSAnalyzer() {
Parameters.Add(new LookupParameter("Results", "The results collection in which the population diversity analyzer injects its result collection."));
Parameters.Add(new LookupParameter("PopulationDiversityAnalyzer Results", "The result collection in which the diversity analyzer injects its results."));
Parameters.Add(new LookupParameter("Solution Similarities", "The HeatMap that stores the solution similarities."));
//Parameters.Add(new LookupParameter("CoordinatesHistory", "The history of the assigned coordinates."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new PopulationDiversityMDSAnalyzer(this, cloner);
}
public override IOperation Apply() {
ResultCollection results = ResultsParameter.ActualValue;
ResultCollection popDivResults = (results[PopulationDiversityAnalyzerResultsParameter.ActualName] as IResult).Value as ResultCollection;
if (popDivResults == null) throw new InvalidOperationException("Results collection of population diversity analyzer cannot be found.");
HeatMap similarites = (popDivResults[SolutionSimilaritiesParameter.ActualName] as IResult).Value as HeatMap;
if (similarites == null) throw new InvalidOperationException("HeatMap cannot be found in the results collection of the population diversity analyzer.");
if (similarites.Rows != similarites.Columns) throw new InvalidOperationException("HeatMap is not a square matrix.");
CoordinatesHistory history = null;
if (popDivResults.ContainsKey("MDS"))
history = popDivResults["MDS"].Value as CoordinatesHistory;
else {
history = new CoordinatesHistory();
popDivResults.Add(new Result("MDS", history));
}
int dimension = similarites.Rows;
DoubleMatrix distances = new DoubleMatrix(dimension, dimension);
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
if (i == j) continue;
distances[i, j] = 1.0 - similarites[i, j];
}
}
double stress;
DoubleMatrix coordinates = null;
if (history.Count == 0) {
coordinates = MultidimensionalScaling.MetricByDistance(distances, out stress);
} else coordinates = MultidimensionalScaling.MetricByDistance(distances, out stress, history.Last().Clone() as DoubleMatrix);
history.Add(coordinates);
return base.Apply();
}
}
}