Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Evaluators/SymbolicDataAnalysisEvaluator.cs @ 15941

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

#1997: Merged trunk changes to data analysis island GA branch.

File size: 11.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Random;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
36  [StorableClass]
37  public abstract class SymbolicDataAnalysisEvaluator<T> : InstrumentedOperator,
38    ISymbolicDataAnalysisEvaluator<T>, ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator, IStochasticOperator
39  where T : class, IDataAnalysisProblemData {
40    private const string RandomParameterName = "Random";
41    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
42    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
43    private const string ProblemDataParameterName = "ProblemData";
44    private const string EstimationLimitsParameterName = "EstimationLimits";
45    private const string EvaluationPartitionParameterName = "EvaluationPartition";
46    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
47    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
48    private const string ValidRowIndicatorParameterName = "ValidRowIndicator";
49    private const string RowsParameterName = "Rows";
50
51    public override bool CanChangeName { get { return false; } }
52
53    #region parameter properties
54    ILookupParameter<IRandom> IStochasticOperator.RandomParameter {
55      get { return RandomParameter; }
56    }
57
58    public IValueLookupParameter<IRandom> RandomParameter {
59      get { return (IValueLookupParameter<IRandom>)Parameters[RandomParameterName]; }
60    }
61    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
62      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
63    }
64    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
65      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
66    }
67    public IValueLookupParameter<T> ProblemDataParameter {
68      get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
69    }
70
71    public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
72      get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; }
73    }
74    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
75      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
76    }
77    public IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
78      get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
79    }
80    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
81      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
82    }
83    public IValueLookupParameter<StringValue> ValidRowIndicatorParameter {
84      get { return (IValueLookupParameter<StringValue>)Parameters[ValidRowIndicatorParameterName]; }
85    }
86    public ILookupParameter<EnumerableItem<int>> RowsParameter {
87      get { return (ILookupParameter<EnumerableItem<int>>)Parameters[RowsParameterName]; }
88    }
89    #endregion
90
91
92    [StorableConstructor]
93    protected SymbolicDataAnalysisEvaluator(bool deserializing) : base(deserializing) { }
94    protected SymbolicDataAnalysisEvaluator(SymbolicDataAnalysisEvaluator<T> original, Cloner cloner)
95      : base(original, cloner) {
96    }
97    public SymbolicDataAnalysisEvaluator()
98      : base() {
99      Parameters.Add(new ValueLookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
100      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
101      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic data analysis solution encoded as a symbolic expression tree."));
102      Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
103      Parameters.Add(new ValueLookupParameter<IntRange>(EvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
104      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees."));
105      Parameters.Add(new ValueLookupParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index."));
106      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
107      Parameters.Add(new ValueLookupParameter<StringValue>(ValidRowIndicatorParameterName, "An indicator variable in the data set that specifies which rows should be evaluated (those for which the indicator <> 0) (optional)."));
108      Parameters.Add(new LookupParameter<EnumerableItem<int>>(RowsParameterName, "TODO"));
109    }
110
111    [StorableHook(HookType.AfterDeserialization)]
112    private void AfterDeserialization() {
113      if (Parameters.ContainsKey(ApplyLinearScalingParameterName) && !(Parameters[ApplyLinearScalingParameterName] is LookupParameter<BoolValue>))
114        Parameters.Remove(ApplyLinearScalingParameterName);
115      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName))
116        Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
117      if (!Parameters.ContainsKey(ValidRowIndicatorParameterName))
118        Parameters.Add(new ValueLookupParameter<StringValue>(ValidRowIndicatorParameterName, "An indicator variable in the data set that specifies which rows should be evaluated (those for which the indicator <> 0) (optional)."));
119      if (!Parameters.ContainsKey(RowsParameterName))
120        Parameters.Add(new LookupParameter<EnumerableItem<int>>(RowsParameterName, "TODO"));
121    }
122
123    protected IEnumerable<int> GenerateRowsToEvaluate() {
124      IEnumerable<int> rows = null;
125      if (RowsParameter.ActualValue != null)
126        rows = RowsParameter.ActualValue.Enumerable;
127      if (rows == null) rows = GenerateRowsToEvaluate(RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
128      return rows;
129    }
130
131    protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
132      IEnumerable<int> rows;
133      int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
134      int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
135      int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
136      int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
137      if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
138
139      if (percentageOfRows.IsAlmost(1.0))
140        rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
141      else {
142        int seed = RandomParameter.ActualValue.Next();
143        int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
144        if (count == 0) count = 1;
145        rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
146      }
147
148      rows = rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
149      if (ValidRowIndicatorParameter.ActualValue != null) {
150        string indicatorVar = ValidRowIndicatorParameter.ActualValue.Value;
151        var problemData = ProblemDataParameter.ActualValue;
152        var indicatorRow = problemData.Dataset.GetReadOnlyDoubleValues(indicatorVar);
153        rows = rows.Where(r => !indicatorRow[r].IsAlmost(0.0));
154      }
155      return rows;
156    }
157
158    [ThreadStatic]
159    private static double[] cache;
160    protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
161      double lowerEstimationLimit, double upperEstimationLimit,
162      IOnlineCalculator calculator, int maxRows) {
163      if (cache == null || cache.Length < maxRows) {
164        cache = new double[maxRows];
165      }
166
167      // calculate linear scaling
168      int i = 0;
169      var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
170      var targetValuesEnumerator = targetValues.GetEnumerator();
171      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
172      while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
173        double target = targetValuesEnumerator.Current;
174        double estimated = estimatedValuesEnumerator.Current;
175        cache[i] = estimated;
176        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
177          linearScalingCalculator.Add(estimated, target);
178        i++;
179      }
180      if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
181        throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
182
183      double alpha = linearScalingCalculator.Alpha;
184      double beta = linearScalingCalculator.Beta;
185      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
186        alpha = 0.0;
187        beta = 1.0;
188      }
189
190      //calculate the quality by using the passed online calculator
191      targetValuesEnumerator = targetValues.GetEnumerator();
192      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
193        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
194
195      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
196        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
197      }
198    }
199  }
200}
Note: See TracBrowser for help on using the repository browser.