#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 System;
using System.Collections.Generic;
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;
using HeuristicLab.Problems.DataAnalysis;
using HeuristicLab.Problems.DataAnalysis.Symbolic;
namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
[StorableClass]
public sealed class ConsecutiveSamplesEvaluator : SingleSuccessorOperator, IIterationBasedOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator {
private const string ProblemDataParameterName = "ProblemData";
private const string EvaluatorParameterName = "ProblemEvaluator";
private const string QualityParameterName = "Quality";
private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
private const string ConsecutiveSamplesParameterName = "ConsecutiveSamples";
private const string OverlapParameterName = "Overlap";
private const string DataMigrationIntervalParameterName = "DataMigrationInterval";
private const string IslandIndexParameterName = "IslandIndex";
private const string IterationsParameterName = "Iterations";
private const string MaximumIterationsParameterName = "Maximum Iterations";
#region parameter properties
public ILookupParameter ProblemDataParameter {
get { return (ILookupParameter)Parameters[ProblemDataParameterName]; }
}
public ILookupParameter EvaluatorParameter {
get { return (ILookupParameter)Parameters[EvaluatorParameterName]; }
}
public ILookupParameter QualityParameter {
get { return (ILookupParameter)Parameters[QualityParameterName]; }
}
public ILookupParameter FitnessCalculationPartitionParameter {
get { return (ILookupParameter)Parameters[FitnessCalculationPartitionParameterName]; }
}
public ILookupParameter FixedSamplesPartitionParameter {
get { return (ILookupParameter)Parameters[FixedSamplesPartitionParameterName]; }
}
public IFixedValueParameter ConsecutiveSamplesParameter {
get { return (IFixedValueParameter)Parameters[ConsecutiveSamplesParameterName]; }
}
public IFixedValueParameter OverlapParameter {
get { return (IFixedValueParameter)Parameters[OverlapParameterName]; }
}
public IValueLookupParameter DataMigrationIntervalParameter {
get { return (IValueLookupParameter)Parameters[DataMigrationIntervalParameterName]; }
}
public ILookupParameter IslandIndexParameter {
get { return (ILookupParameter)Parameters[IslandIndexParameterName]; }
}
public ILookupParameter IterationsParameter {
get { return (ILookupParameter)Parameters[IterationsParameterName]; }
}
public IValueLookupParameter MaximumIterationsParameter {
get { return (IValueLookupParameter)Parameters[MaximumIterationsParameterName]; }
}
#endregion
#region properties
public double ConsecutiveSamples {
get { return ConsecutiveSamplesParameter.Value.Value; }
set { ConsecutiveSamplesParameter.Value.Value = value; }
}
public double Overlap {
get { return OverlapParameter.Value.Value; }
set { OverlapParameter.Value.Value = value; }
}
#endregion
[StorableConstructor]
private ConsecutiveSamplesEvaluator(bool deserializing) : base(deserializing) { }
private ConsecutiveSamplesEvaluator(ConsecutiveSamplesEvaluator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ConsecutiveSamplesEvaluator(this, cloner);
}
public ConsecutiveSamplesEvaluator()
: base() {
Parameters.Add(new LookupParameter(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
Parameters.Add(new LookupParameter(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis problem."));
Parameters.Add(new LookupParameter(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
Parameters.Add(new LookupParameter(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
Parameters.Add(new LookupParameter(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
Parameters.Add(new FixedValueParameter(ConsecutiveSamplesParameterName, "The relative number of consecutive samples used for fitness calculation in each island.", new PercentValue()));
Parameters.Add(new FixedValueParameter(OverlapParameterName, "The relative overlap for the consecutive samples used for every island.", new PercentValue()));
Parameters.Add(new ValueLookupParameter(DataMigrationIntervalParameterName, "The number of generations that should pass between data migration phases."));
Parameters.Add(new LookupParameter(IslandIndexParameterName, "The index of the current island."));
Parameters.Add(new LookupParameter(IterationsParameterName, "The number of performed iterations."));
Parameters.Add(new ValueLookupParameter(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
}
public override IOperation Apply() {
var evaluator = EvaluatorParameter.ActualValue;
var problemData = ProblemDataParameter.ActualValue;
var samples = FitnessCalculationPartitionParameter.ActualValue;
var fixedSamples = FixedSamplesPartitionParameter.ActualValue;
var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value;
var generationValue = IterationsParameter.ActualValue;
var generation = generationValue == null ? 0 : generationValue.Value;
if (ConsecutiveSamples > 0 && dataMigrationInterval == 0)
throw new ArgumentException("The data migration interval must not be 0 if consecutive samples are used.");
//create fixed rows enumerable
var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size).Select(r => (r - samples.Start) % samples.Size + samples.Start);
//create consecutive rows enumerable
if (ConsecutiveSamples > 0) {
var islandIndex = IslandIndexParameter.ActualValue.Value;
var iteration = islandIndex + (generation / dataMigrationInterval);
var consecutiveSamples = (int)(ConsecutiveSamples * samples.Size);
var overlap = (int)Overlap * consecutiveSamples;
var consecutiveRows = GenerateRows(samples, fixedSamples, consecutiveSamples, overlap, iteration);
rows = rows.Concat(consecutiveRows);
}
//filter out test rows
rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End);
//TODO change to lookup parameter
ExecutionContext.Scope.Variables.Remove("Rows");
ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem(rows)));
var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
var successor = evaluator.Execute(executionContext, this.CancellationToken);
return new OperationCollection(successor, base.Apply());
}
public static IEnumerable GenerateRows(IntRange samples, IntRange fixedSamples, int consecutiveSamples, int overlap, int iteration) {
var consecutiveSamplesStart = (consecutiveSamples - overlap) * iteration;
consecutiveSamplesStart = consecutiveSamplesStart % (samples.Size - fixedSamples.Size);
var rows = Enumerable.Range(fixedSamples.End, samples.End - fixedSamples.End);
rows = rows.Concat(Enumerable.Range(samples.Start, fixedSamples.Start - samples.Start));
rows = rows.Concat(Enumerable.Range(fixedSamples.End, samples.End - fixedSamples.End));
rows = rows.Concat(Enumerable.Range(samples.Start, fixedSamples.Start - samples.Start));
return rows.Skip(consecutiveSamplesStart).Take(consecutiveSamples);
}
}
}