#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.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; using HeuristicLab.Random; namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic { [StorableClass] public sealed class RandomSamplesEvaluator : 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 FixedSamplesPartitionParameterName = "FixedSamplesPartition"; 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 ILookupParameter FixedSamplesPartitionParameter { get { return (ILookupParameter)Parameters[FixedSamplesPartitionParameterName]; } } 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 RandomSamplesEvaluator(bool deserializing) : base(deserializing) { } private RandomSamplesEvaluator(RandomSamplesEvaluator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new RandomSamplesEvaluator(this, cloner); } public RandomSamplesEvaluator() : 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(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 fixedSamples = FixedSamplesPartitionParameter.ActualValue; var randomSamples = (int)(RandomSamples * samples.Size); var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value; var generationValue = IterationsParameter.ActualValue; var generation = generationValue == null ? 0 : generationValue.Value; if (randomSamples > 0 && dataMigrationInterval == 0) throw new ArgumentException("The data migration interval must not be 0 if random 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 randomly chosen rows enumerable if (randomSamples > 0) { var islandIndex = IslandIndexParameter.ActualValue.Value; var random = new FastRandom(islandIndex + (generation / dataMigrationInterval)); if (randomSamples > samples.Size - fixedSamples.Size) { var error = string.Format("Could not select {0} random samples, because there are {1} total samples present from which {2} where used in the fixed partition. Please lower the number of random samples in the algorithm configuration.", randomSamples, samples.Size, fixedSamples.Size); throw new OperatorExecutionException(this, error); } var randomRows = Enumerable.Range(samples.Start, samples.Size).Where(r => r < fixedSamples.Start || r >= fixedSamples.End); randomRows = randomRows.SampleRandomWithoutRepetition(random, randomSamples, samples.Size - fixedSamples.Size); rows = rows.Concat(randomRows); } //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()); } } }