#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.Optimization; 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, IStochasticOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator { private const string RandomParameterName = "Random"; 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 RandomSamplesParameterName = "RandomSamples"; #region parameter properties public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters[RandomParameterName]; } } 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 RandomSamplesParameter { get { return (IFixedValueParameter)Parameters[RandomSamplesParameterName]; } } #endregion #region properties public int 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(RandomParameterName, "The random generator to use.")); 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 IntValue())); } public override IOperation Apply() { var evaluator = EvaluatorParameter.ActualValue; var problemData = ProblemDataParameter.ActualValue; var samples = FitnessCalculationPartitionParameter.ActualValue; var fixedSamples = FixedSamplesPartitionParameter.ActualValue; var randomSamples = RandomSamples; //create fixed rows enumerable var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size); //create randomly chosen rows enumerable if (randomSamples > 0) { 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(RandomParameter.ActualValue, 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); //execution context is created manually to be able to clear the rows parameter easily var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope); //TODO change to lookup parameter executionContext.Scope.Variables.Remove("Rows"); executionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem(rows))); var successor = evaluator.Execute(executionContext, this.CancellationToken); return new OperationCollection(successor, base.Apply()); } } }