#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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.Linq;
using HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
namespace HeuristicLab.Analysis.FitnessLandscape {
[Item("Ruggedness Analyzer", "Analyzes autocorrelation for one mutation step and correlation length.")]
[StorableType("A1600221-53E7-4641-AB40-A7940708B5AF")]
public class RuggednessAnalyzer : SingleSuccessorOperator, IQualityTrailAnalyzer {
public bool EnabledByDefault {
get { return false; }
}
#region Parameters
public LookupParameter QualityTrailParameter {
get { return (LookupParameter)Parameters["Quality Trail"]; }
}
public LookupParameter ResultsParameter {
get { return (LookupParameter)Parameters["Results"]; }
}
public LookupParameter AutoCorrelation1Parameter {
get { return (LookupParameter)Parameters["AutoCorrelation1"]; }
}
public LookupParameter CorrelationLengthParameter {
get { return (LookupParameter)Parameters["CorrelationLength"]; }
}
#endregion
[StorableConstructor]
protected RuggednessAnalyzer(StorableConstructorFlag _) : base(_) { }
protected RuggednessAnalyzer(RuggednessAnalyzer original, Cloner cloner) : base(original, cloner) { }
public RuggednessAnalyzer() {
Parameters.Add(new LookupParameter("Quality Trail", "The quality of the solution"));
Parameters.Add(new LookupParameter("Results", "The collection of all results of this algorithm"));
Parameters.Add(new LookupParameter("AutoCorrelation1", "Autocorrelation for one mutation step."));
Parameters.Add(new LookupParameter("CorrelationLength", "The correlation length."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new RuggednessAnalyzer(this, cloner);
}
public override IOperation Apply() {
var qualityTrail = QualityTrailParameter.ActualValue;
if (qualityTrail == null || qualityTrail.Rows.Count <= 0) return base.Apply();
var results = ResultsParameter.ActualValue;
double[] autocorrelationValues;
var correlationLength = RuggednessCalculator.CalculateCorrelationLength(qualityTrail.Rows.First().Values.ToArray(), out autocorrelationValues);
var cl = new IntValue(correlationLength);
CorrelationLengthParameter.ActualValue = cl;
AddOrUpdateResult(results, CorrelationLengthParameter.Name, cl);
var ac1 = new DoubleValue(autocorrelationValues.Length > 1 ? autocorrelationValues[1] : 0.0);
AutoCorrelation1Parameter.ActualValue = ac1;
AddOrUpdateResult(results, AutoCorrelation1Parameter.Name, ac1);
return base.Apply();
}
private static void AddOrUpdateResult(ResultCollection results, string name, IItem item, bool clone = false) {
IResult r;
if (!results.TryGetValue(name, out r)) {
results.Add(new Result(name, clone ? (IItem)item.Clone() : item));
} else r.Value = clone ? (IItem)item.Clone() : item;
}
}
}