Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10230 was 10230, checked in by mkommend, 10 years ago

#1997: Updated symbolic island algorithm evaluators and added automatic wiring of evaluators.

File size: 8.2 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
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, IStochasticOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator {
37    private const string RandomParameterName = "Random";
38    private const string ProblemDataParameterName = "ProblemData";
39    private const string EvaluatorParameterName = "ProblemEvaluator";
40    private const string QualityParameterName = "Quality";
41    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
42    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
43    private const string DataMigrationIntervalParameterName = "DataMigrationInterval";
44    private const string RandomSamplesParameterName = "RandomSamples";
45    private const string IterationsParameterName = "Iterations";
46    private const string MaximumIterationsParameterName = "Maximum Iterations";
47
48    #region parameter properties
49    public ILookupParameter<IRandom> RandomParameter {
50      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
51    }
52    public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
53      get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
54    }
55    public ILookupParameter<IOperator> EvaluatorParameter {
56      get { return (ILookupParameter<IOperator>)Parameters[EvaluatorParameterName]; }
57    }
58    public ILookupParameter<DoubleValue> QualityParameter {
59      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
60    }
61    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
62      get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
63    }
64    public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
65      get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
66    }
67    public IValueLookupParameter<IntValue> DataMigrationIntervalParameter {
68      get { return (IValueLookupParameter<IntValue>)Parameters[DataMigrationIntervalParameterName]; }
69    }
70    public IFixedValueParameter<IntValue> RandomSamplesParameter {
71      get { return (IFixedValueParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
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 int 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<IRandom>(RandomParameterName, "The random generator to use."));
101      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
102      Parameters.Add(new LookupParameter<IOperator>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis  problem."));
103      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
104      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
105      Parameters.Add(new LookupParameter<IntRange>(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
106      Parameters.Add(new FixedValueParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new IntValue()));
107      Parameters.Add(new ValueLookupParameter<IntValue>(DataMigrationIntervalParameterName, "The number of generations that should pass between data migration phases."));
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 = RandomSamples;
119
120      var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value;
121      var generationValue = IterationsParameter.ActualValue;
122      var generation = generationValue == null ? 0 : generationValue.Value;
123
124      //calculat new rows for evaluation
125      if (generation % dataMigrationInterval == 0) {
126        //create fixed rows enumerable
127        var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size);
128        //create randomly chosen rows enumerable
129        if (randomSamples > 0) {
130          if (randomSamples > samples.Size - fixedSamples.Size) {
131            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);
132            throw new OperatorExecutionException(this, error);
133          }
134          var randomRows = Enumerable.Range(samples.Start, samples.Size).Where(r => r < fixedSamples.Start || r >= fixedSamples.End);
135          randomRows = randomRows.SampleRandomWithoutRepetition(RandomParameter.ActualValue, randomSamples, samples.Size - fixedSamples.Size);
136
137          rows = rows.Concat(randomRows);
138        }
139        //filter out test rows
140        rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End);
141        ExecutionContext.Scope.Variables.Remove("Rows");
142        ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows)));
143      }
144
145      var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
146      var successor = evaluator.Execute(executionContext, this.CancellationToken);
147      return new OperationCollection(successor, base.Apply());
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.