#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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 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.Problems.DataAnalysis.Symbolic {
[StorableClass]
[Item("Sliding Window GP Analyzer", "Base class for concrete sliding window GP analyzers.")]
public abstract class SlidingWindowAnalyzer : SymbolicDataAnalysisAnalyzer {
private const string ProblemDataParameterName = "ProblemData";
private const string EvaluatorParameterName = "Evaluator";
private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
private const string ValidationPartitionParameterName = "ValidationPartition";
private const string SlidingWindowSizeParameterName = "Sliding Window Size";
private const string ValidationSlidingWindowSizeParameterName = "Validation Sliding Window Size";
private const string SlidingWindowStepWidthParameterName = "Sliding Window Step Width";
private const string InitialSlidingWindowParameterName = "Initial Sliding Window";
private const string TerminateSlidingWindowParameterName = "TerminateSlidingWindow";
#region parameter properties
public IValueLookupParameter ProblemDataParameter {
get { return (IValueLookupParameter)Parameters[ProblemDataParameterName]; }
}
public ILookupParameter FitnessCalculationPartitionParameter {
get { return (ILookupParameter)Parameters[FitnessCalculationPartitionParameterName]; }
}
public ILookupParameter ValidationPartitionParameter {
get { return (ILookupParameter)Parameters[ValidationPartitionParameterName]; }
}
public IFixedValueParameter SlidingWindowSizeParameter {
get { return (IFixedValueParameter)Parameters[SlidingWindowSizeParameterName]; }
}
public IFixedValueParameter ValidationSlidingWindowSizeParameter {
get { return (IFixedValueParameter)Parameters[ValidationSlidingWindowSizeParameterName]; }
}
public IFixedValueParameter SlidingWindowStepWidthParameter {
get { return (IFixedValueParameter)Parameters[SlidingWindowStepWidthParameterName]; }
}
public IFixedValueParameter InitialSlidingWindowParameter {
get { return (IFixedValueParameter)Parameters[InitialSlidingWindowParameterName]; }
}
public ILookupParameter TerminateSlidingWindowParameter {
get { return (ILookupParameter)Parameters[TerminateSlidingWindowParameterName]; }
}
public ILookupParameter EvaluatorParameter {
get { return (ILookupParameter)Parameters[EvaluatorParameterName]; }
}
#endregion
#region properties
public override bool EnabledByDefault { get { return false; } }
public IntValue SlidingWindowSize { get { return SlidingWindowSizeParameter.Value; } }
public IntValue ValidiationSlidingWindowSize { get { return ValidationSlidingWindowSizeParameter.Value; } }
public IntValue SlidingWindowStepWidth { get { return SlidingWindowStepWidthParameter.Value; } }
public IntRange InitialSlidingWindow { get { return InitialSlidingWindowParameter.Value; } }
#endregion
[StorableConstructor]
protected SlidingWindowAnalyzer(bool deserializing) : base(deserializing) { }
protected SlidingWindowAnalyzer(SlidingWindowAnalyzer original, Cloner cloner)
: base(original, cloner) { }
protected SlidingWindowAnalyzer()
: base() {
Parameters.Add(new ValueLookupParameter(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
Parameters.Add(new LookupParameter(EvaluatorParameterName, ""));
Parameters.Add(new LookupParameter(FitnessCalculationPartitionParameterName, ""));
Parameters.Add(new LookupParameter(ValidationPartitionParameterName, ""));
Parameters.Add(new FixedValueParameter(SlidingWindowSizeParameterName, "", new IntValue(1)));
Parameters.Add(new FixedValueParameter(ValidationSlidingWindowSizeParameterName, "", new IntValue(0)));
Parameters.Add(new FixedValueParameter(SlidingWindowStepWidthParameterName, "", new IntValue(1)));
Parameters.Add(new FixedValueParameter(InitialSlidingWindowParameterName, "", new IntRange(0, 1)));
Parameters.Add(new LookupParameter(TerminateSlidingWindowParameterName, ""));
ProblemDataParameter.Hidden = true;
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
if (!Parameters.ContainsKey(EvaluatorParameterName))
Parameters.Add(new LookupParameter(EvaluatorParameterName, ""));
}
public override IOperation Apply() {
//intialize sliding window
TerminateSlidingWindowParameter.ActualValue = new BoolValue(false);
if (FitnessCalculationPartitionParameter.ActualValue == null) {
FitnessCalculationPartitionParameter.ActualValue = (IntRange)InitialSlidingWindow.Clone();
ValidationPartitionParameter.ActualValue = new IntRange(InitialSlidingWindow.End, InitialSlidingWindow.End + ValidiationSlidingWindowSize.Value);
return base.Apply();
}
if (!CheckForUpdate()) return base.Apply();
//update necessary - move sliding window
var fitnessPartition = (IntRange)FitnessCalculationPartitionParameter.ActualValue.Clone();
if (fitnessPartition.End - fitnessPartition.Start == SlidingWindowSize.Value)
fitnessPartition.Start += SlidingWindowStepWidth.Value;
fitnessPartition.End += SlidingWindowStepWidth.Value;
if (fitnessPartition.End - fitnessPartition.Start > SlidingWindowSize.Value)
fitnessPartition.End = fitnessPartition.Start + SlidingWindowSize.Value;
//check if update should be performed or if the algorithm should stop
if (fitnessPartition.End > ProblemDataParameter.ActualValue.TrainingPartition.End) {
TerminateSlidingWindowParameter.ActualValue.Value = true;
return base.Apply();
}
FitnessCalculationPartitionParameter.ActualValue.Start = fitnessPartition.Start;
FitnessCalculationPartitionParameter.ActualValue.End = fitnessPartition.End;
ValidationPartitionParameter.ActualValue.Start = fitnessPartition.End;
ValidationPartitionParameter.ActualValue.End = ValidationPartitionParameter.ActualValue.Start + ValidiationSlidingWindowSize.Value;
//reevaluate all individuals with new sliding window
UniformSubScopesProcessor subScopesProcessor = new UniformSubScopesProcessor();
subScopesProcessor.Operator = EvaluatorParameter.ActualValue;
subScopesProcessor.Depth.Value = 1;
var operation = ExecutionContext.CreateChildOperation(subScopesProcessor);
var successor = base.Apply();
return new OperationCollection() { operation, successor };
}
protected abstract bool CheckForUpdate();
}
}