Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1997: Added different evaluators for symbolic island algorithms, unit test for consecutive samples evaluator and injected island index into the according scopes.

File size: 6.8 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 RandomSamplesParameterName = "RandomSamples";
44
45    #region parameter properties
46    public ILookupParameter<IRandom> RandomParameter {
47      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
48    }
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 IFixedValueParameter<IntValue> RandomSamplesParameter {
65      get { return (IFixedValueParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
66    }
67    #endregion
68
69    #region properties
70
71    public int RandomSamples {
72      get { return RandomSamplesParameter.Value.Value; }
73      set { RandomSamplesParameter.Value.Value = value; }
74    }
75    #endregion
76
77    [StorableConstructor]
78    private RandomSamplesEvaluator(bool deserializing) : base(deserializing) { }
79    private RandomSamplesEvaluator(RandomSamplesEvaluator original, Cloner cloner)
80      : base(original, cloner) {
81    }
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new RandomSamplesEvaluator(this, cloner);
84    }
85
86    public RandomSamplesEvaluator()
87      : base() {
88      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
89      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
90      Parameters.Add(new LookupParameter<IOperator>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis  problem."));
91      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
92      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
93      Parameters.Add(new LookupParameter<IntRange>(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
94      Parameters.Add(new FixedValueParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new IntValue()));
95    }
96
97    public override IOperation Apply() {
98      var evaluator = EvaluatorParameter.ActualValue;
99      var problemData = ProblemDataParameter.ActualValue;
100
101      var samples = FitnessCalculationPartitionParameter.ActualValue;
102      var fixedSamples = FixedSamplesPartitionParameter.ActualValue;
103      var randomSamples = RandomSamples;
104
105      //create fixed rows enumerable
106      var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size);
107      //create randomly chosen rows enumerable
108      if (randomSamples > 0) {
109        if (randomSamples > samples.Size - fixedSamples.Size) {
110          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);
111          throw new OperatorExecutionException(this, error);
112        }
113        var randomRows = Enumerable.Range(samples.Start, samples.Size).Where(r => r < fixedSamples.Start || r >= fixedSamples.End);
114        randomRows = randomRows.SampleRandomWithoutRepetition(RandomParameter.ActualValue, randomSamples, samples.Size - fixedSamples.Size);
115
116        rows = rows.Concat(randomRows);
117      }
118      //filter out test rows
119      rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End);
120
121      //execution context is created manually to be able to clear the rows parameter easily
122      var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
123
124      //TODO change to lookup parameter
125      executionContext.Scope.Variables.Remove("Rows");
126      executionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows)));
127      var successor = evaluator.Execute(executionContext, this.CancellationToken);
128      return new OperationCollection(successor, base.Apply());
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.