#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.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; 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 GrowingRandomSamplesEvaluator : SingleSuccessorOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator { private const string ProblemDataParameterName = "ProblemData"; private const string EvaluatorParameterName = "ProblemEvaluator"; private const string QualityParameterName = "Quality"; private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition"; private const string DataMigrationIntervalParameterName = "DataMigrationInterval"; private const string RandomSamplesParameterName = "RandomSamples"; 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 IValueLookupParameter DataMigrationIntervalParameter { get { return (IValueLookupParameter)Parameters[DataMigrationIntervalParameterName]; } } public IFixedValueParameter RandomSamplesParameter { get { return (IFixedValueParameter)Parameters[RandomSamplesParameterName]; } } 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 RandomSamples { get { return RandomSamplesParameter.Value.Value; } set { RandomSamplesParameter.Value.Value = value; } } #endregion [StorableConstructor] private GrowingRandomSamplesEvaluator(bool deserializing) : base(deserializing) { } private GrowingRandomSamplesEvaluator(GrowingRandomSamplesEvaluator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new GrowingRandomSamplesEvaluator(this, cloner); } public GrowingRandomSamplesEvaluator() : 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 FixedValueParameter(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each 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 islandIndex = IslandIndexParameter.ActualValue.Value; var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value; var generationValue = IterationsParameter.ActualValue; var generation = generationValue == null ? 0 : generationValue.Value; var maximumGenerations = MaximumIterationsParameter.ActualValue.Value; var growth = (1.0 - RandomSamples) * ((double)dataMigrationInterval) / (maximumGenerations - dataMigrationInterval); var randomSamples = (int)((RandomSamples + growth * ((int)generation / dataMigrationInterval)) * samples.Size); //var random = new FastRandom(islandIndex + generation / dataMigrationInterval); //var rows = Enumerable.Range(samples.Start, samples.Size).SampleRandomWithoutRepetition(random, randomSamples, samples.Size); var rows = Enumerable.Range(samples.Start, randomSamples); //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()); } } }