Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/RandomSamplesEvaluator .cs @ 13599

Last change on this file since 13599 was 10825, checked in by mkommend, 11 years ago

#1997: Allowed fixed samples to be wrap around the samples partition.

File size: 8.5 KB
RevLine 
[9051]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[10579]22using System;
[9077]23using System.Linq;
[9051]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
[9077]28using HeuristicLab.Parameters;
[9051]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
[9077]32using HeuristicLab.Random;
[9051]33
34namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
35  [StorableClass]
[10579]36  public sealed class RandomSamplesEvaluator : SingleSuccessorOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator {
[9077]37    private const string ProblemDataParameterName = "ProblemData";
38    private const string EvaluatorParameterName = "ProblemEvaluator";
39    private const string QualityParameterName = "Quality";
40    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
41    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
[10230]42    private const string DataMigrationIntervalParameterName = "DataMigrationInterval";
[9051]43    private const string RandomSamplesParameterName = "RandomSamples";
[10579]44    private const string IslandIndexParameterName = "IslandIndex";
[10230]45    private const string IterationsParameterName = "Iterations";
46    private const string MaximumIterationsParameterName = "Maximum Iterations";
[9051]47
48    #region parameter properties
[10156]49    public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
50      get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
[9051]51    }
[10156]52    public ILookupParameter<IOperator> EvaluatorParameter {
53      get { return (ILookupParameter<IOperator>)Parameters[EvaluatorParameterName]; }
[9051]54    }
[9077]55    public ILookupParameter<DoubleValue> QualityParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
[9051]57    }
[10177]58    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
59      get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
[9051]60    }
[9077]61    public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
62      get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
63    }
[10230]64    public IValueLookupParameter<IntValue> DataMigrationIntervalParameter {
65      get { return (IValueLookupParameter<IntValue>)Parameters[DataMigrationIntervalParameterName]; }
66    }
[10353]67    public IFixedValueParameter<PercentValue> RandomSamplesParameter {
68      get { return (IFixedValueParameter<PercentValue>)Parameters[RandomSamplesParameterName]; }
[9077]69    }
[10579]70    public ILookupParameter<IntValue> IslandIndexParameter {
71      get { return (ILookupParameter<IntValue>)Parameters[IslandIndexParameterName]; }
72    }
[10230]73    public ILookupParameter<IntValue> IterationsParameter {
74      get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
75    }
76    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
77      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
78    }
[9051]79    #endregion
80
[10177]81    #region properties
82
[10353]83    public double RandomSamples {
[10177]84      get { return RandomSamplesParameter.Value.Value; }
85      set { RandomSamplesParameter.Value.Value = value; }
86    }
87    #endregion
88
[9051]89    [StorableConstructor]
[10177]90    private RandomSamplesEvaluator(bool deserializing) : base(deserializing) { }
91    private RandomSamplesEvaluator(RandomSamplesEvaluator original, Cloner cloner)
[9051]92      : base(original, cloner) {
93    }
94    public override IDeepCloneable Clone(Cloner cloner) {
[10177]95      return new RandomSamplesEvaluator(this, cloner);
[9051]96    }
97
[10177]98    public RandomSamplesEvaluator()
[9051]99      : base() {
[10156]100      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
101      Parameters.Add(new LookupParameter<IOperator>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis  problem."));
[9077]102      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
[10177]103      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
[9077]104      Parameters.Add(new LookupParameter<IntRange>(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
[10353]105      Parameters.Add(new FixedValueParameter<PercentValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new PercentValue()));
[10230]106      Parameters.Add(new ValueLookupParameter<IntValue>(DataMigrationIntervalParameterName, "The number of generations that should pass between data migration phases."));
[10579]107      Parameters.Add(new LookupParameter<IntValue>(IslandIndexParameterName, "The index of the current island."));
[10230]108      Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
109      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
[9051]110    }
[9077]111
112    public override IOperation Apply() {
113      var evaluator = EvaluatorParameter.ActualValue;
[9182]114      var problemData = ProblemDataParameter.ActualValue;
115
[10142]116      var samples = FitnessCalculationPartitionParameter.ActualValue;
117      var fixedSamples = FixedSamplesPartitionParameter.ActualValue;
[10421]118      var randomSamples = (int)(RandomSamples * samples.Size);
[9077]119
[10230]120      var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value;
121      var generationValue = IterationsParameter.ActualValue;
122      var generation = generationValue == null ? 0 : generationValue.Value;
123
124
[10579]125      if (randomSamples > 0 && dataMigrationInterval == 0)
126        throw new ArgumentException("The data migration interval must not be 0 if random samples are used.");
127
128      //create fixed rows enumerable
[10825]129      var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size).Select(r => (r - samples.Start) % samples.Size + samples.Start);
[10579]130      //create randomly chosen rows enumerable
131      if (randomSamples > 0) {
132        var islandIndex = IslandIndexParameter.ActualValue.Value;
133        var random = new FastRandom(islandIndex + (generation / dataMigrationInterval));
134
135        if (randomSamples > samples.Size - fixedSamples.Size) {
136          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);
137          throw new OperatorExecutionException(this, error);
[10142]138        }
[10579]139        var randomRows = Enumerable.Range(samples.Start, samples.Size).Where(r => r < fixedSamples.Start || r >= fixedSamples.End);
140        randomRows = randomRows.SampleRandomWithoutRepetition(random, randomSamples, samples.Size - fixedSamples.Size);
141        rows = rows.Concat(randomRows);
[10142]142      }
143
[10579]144      //filter out test rows       
145      rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End);
146      //TODO change to lookup parameter
147      ExecutionContext.Scope.Variables.Remove("Rows");
148      ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows)));
149
[9077]150      var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
[10142]151      var successor = evaluator.Execute(executionContext, this.CancellationToken);
152      return new OperationCollection(successor, base.Apply());
[9077]153    }
[9051]154  }
155}
Note: See TracBrowser for help on using the repository browser.