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
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32using HeuristicLab.Random;
33
34namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
35  [StorableClass]
36  public sealed class RandomSamplesEvaluator : SingleSuccessorOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator {
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";
42    private const string DataMigrationIntervalParameterName = "DataMigrationInterval";
43    private const string RandomSamplesParameterName = "RandomSamples";
44    private const string IslandIndexParameterName = "IslandIndex";
45    private const string IterationsParameterName = "Iterations";
46    private const string MaximumIterationsParameterName = "Maximum Iterations";
47
48    #region parameter properties
49    public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
50      get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
51    }
52    public ILookupParameter<IOperator> EvaluatorParameter {
53      get { return (ILookupParameter<IOperator>)Parameters[EvaluatorParameterName]; }
54    }
55    public ILookupParameter<DoubleValue> QualityParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
57    }
58    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
59      get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
60    }
61    public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
62      get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
63    }
64    public IValueLookupParameter<IntValue> DataMigrationIntervalParameter {
65      get { return (IValueLookupParameter<IntValue>)Parameters[DataMigrationIntervalParameterName]; }
66    }
67    public IFixedValueParameter<PercentValue> RandomSamplesParameter {
68      get { return (IFixedValueParameter<PercentValue>)Parameters[RandomSamplesParameterName]; }
69    }
70    public ILookupParameter<IntValue> IslandIndexParameter {
71      get { return (ILookupParameter<IntValue>)Parameters[IslandIndexParameterName]; }
72    }
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    }
79    #endregion
80
81    #region properties
82
83    public double RandomSamples {
84      get { return RandomSamplesParameter.Value.Value; }
85      set { RandomSamplesParameter.Value.Value = value; }
86    }
87    #endregion
88
89    [StorableConstructor]
90    private RandomSamplesEvaluator(bool deserializing) : base(deserializing) { }
91    private RandomSamplesEvaluator(RandomSamplesEvaluator original, Cloner cloner)
92      : base(original, cloner) {
93    }
94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new RandomSamplesEvaluator(this, cloner);
96    }
97
98    public RandomSamplesEvaluator()
99      : base() {
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."));
102      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
103      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
104      Parameters.Add(new LookupParameter<IntRange>(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
105      Parameters.Add(new FixedValueParameter<PercentValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new PercentValue()));
106      Parameters.Add(new ValueLookupParameter<IntValue>(DataMigrationIntervalParameterName, "The number of generations that should pass between data migration phases."));
107      Parameters.Add(new LookupParameter<IntValue>(IslandIndexParameterName, "The index of the current island."));
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 });
110    }
111
112    public override IOperation Apply() {
113      var evaluator = EvaluatorParameter.ActualValue;
114      var problemData = ProblemDataParameter.ActualValue;
115
116      var samples = FitnessCalculationPartitionParameter.ActualValue;
117      var fixedSamples = FixedSamplesPartitionParameter.ActualValue;
118      var randomSamples = (int)(RandomSamples * samples.Size);
119
120      var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value;
121      var generationValue = IterationsParameter.ActualValue;
122      var generation = generationValue == null ? 0 : generationValue.Value;
123
124
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
129      var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size).Select(r => (r - samples.Start) % samples.Size + samples.Start);
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);
138        }
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);
142      }
143
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
150      var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
151      var successor = evaluator.Execute(executionContext, this.CancellationToken);
152      return new OperationCollection(successor, base.Apply());
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.