#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.SlidingWindow {
[StorableClass]
[Item("Sliding Window Visualizer", "Visualizes the actual sliding window position.")]
public sealed class SlidingWindowVisualizer : SymbolicDataAnalysisAnalyzer {
private const string ProblemDataParameterName = "ProblemData";
private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
private const string SlidingWindowResultName = "Sliding Window";
private const string SlidingWindowDataResultName = "Sliding Window Data";
private const string BestTrainingSolutionResultName = "Best training solution";
#region parameter properties
public IValueLookupParameter ProblemDataParameter {
get { return (IValueLookupParameter)Parameters[ProblemDataParameterName]; }
}
public ILookupParameter FitnessCalculationPartitionParameter {
get { return (ILookupParameter)Parameters[FitnessCalculationPartitionParameterName]; }
}
#endregion
[StorableConstructor]
private SlidingWindowVisualizer(bool deserializing) : base(deserializing) { }
private SlidingWindowVisualizer(SlidingWindowVisualizer original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new SlidingWindowVisualizer(this, cloner);
}
public SlidingWindowVisualizer()
: base() {
Parameters.Add(new ValueLookupParameter(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
Parameters.Add(new LookupParameter(FitnessCalculationPartitionParameterName, ""));
ProblemDataParameter.Hidden = true;
}
public override IOperation Apply() {
//create and update result
var results = ResultCollectionParameter.ActualValue;
IntRange slidingWindow;
if (!results.ContainsKey(SlidingWindowResultName)) {
slidingWindow = new IntRange();
results.Add(new Result(SlidingWindowResultName, slidingWindow));
} else slidingWindow = (IntRange)results[SlidingWindowResultName].Value;
slidingWindow.Start = FitnessCalculationPartitionParameter.ActualValue.Start;
slidingWindow.End = FitnessCalculationPartitionParameter.ActualValue.End;
SlidingWindowData slidingWindowData;
if (!results.ContainsKey(SlidingWindowDataResultName)) {
string targetVariable;
var classificationProblemData = ProblemDataParameter.ActualValue as IClassificationProblemData;
var regressionProblemData = ProblemDataParameter.ActualValue as IRegressionProblemData;
if (classificationProblemData != null) targetVariable = classificationProblemData.TargetVariable;
else if (regressionProblemData != null) targetVariable = regressionProblemData.TargetVariable;
else throw new NotSupportedException();
var targetData = ProblemDataParameter.ActualValue.Dataset.GetDoubleValues(targetVariable, ProblemDataParameter.ActualValue.TrainingIndices);
slidingWindowData = new SlidingWindowData(FitnessCalculationPartitionParameter.ActualValue, targetData);
results.Add(new Result(SlidingWindowDataResultName, slidingWindowData));
} else slidingWindowData = (SlidingWindowData)results[SlidingWindowDataResultName].Value;
IEnumerable estimatedValues = Enumerable.Empty();
if (results.ContainsKey(BestTrainingSolutionResultName)) {
var trainingSolution = results[BestTrainingSolutionResultName].Value;
var regressionSolution = trainingSolution as IRegressionSolution;
var classificationSolution = trainingSolution as IClassificationSolution;
if (regressionSolution != null) estimatedValues = regressionSolution.EstimatedTrainingValues;
if (classificationSolution != null) estimatedValues = classificationSolution.EstimatedTrainingClassValues;
}
slidingWindowData.SlidingWindowPosition.Start = FitnessCalculationPartitionParameter.ActualValue.Start;
slidingWindowData.SlidingWindowPosition.End = FitnessCalculationPartitionParameter.ActualValue.End;
slidingWindowData.EstimatedValues = estimatedValues;
return base.Apply();
}
}
}