Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Evaluators/SymbolicDataAnalysisEvaluator.cs @ 16682

Last change on this file since 16682 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 10.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
33using HeuristicLab.Random;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
36  [StorableType("61CB69D9-959B-4759-94EB-A969A519DDBB")]
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
50    public override bool CanChangeName { get { return false; } }
51
52    #region parameter properties
53    ILookupParameter<IRandom> IStochasticOperator.RandomParameter {
54      get { return RandomParameter; }
55    }
56
57    public IValueLookupParameter<IRandom> RandomParameter {
58      get { return (IValueLookupParameter<IRandom>)Parameters[RandomParameterName]; }
59    }
60    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
61      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
62    }
63    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
64      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
65    }
66    public IValueLookupParameter<T> ProblemDataParameter {
67      get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
68    }
69
70    public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
71      get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; }
72    }
73    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
74      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
75    }
76    public IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
77      get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
78    }
79    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
80      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
81    }
82    public IValueLookupParameter<StringValue> ValidRowIndicatorParameter {
83      get { return (IValueLookupParameter<StringValue>)Parameters[ValidRowIndicatorParameterName]; }
84    }
85    #endregion
86
87
88    [StorableConstructor]
89    protected SymbolicDataAnalysisEvaluator(StorableConstructorFlag _) : base(_) { }
90    protected SymbolicDataAnalysisEvaluator(SymbolicDataAnalysisEvaluator<T> original, Cloner cloner)
91      : base(original, cloner) {
92    }
93    public SymbolicDataAnalysisEvaluator()
94      : base() {
95      Parameters.Add(new ValueLookupParameter<IRandom>(RandomParameterName, "The random generator to use.") { Hidden = true });
96      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree.") { Hidden = true });
97      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic data analysis solution encoded as a symbolic expression tree.") { Hidden = true });
98      Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated.") { Hidden = true });
99      Parameters.Add(new ValueLookupParameter<IntRange>(EvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated.") { Hidden = true });
100      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.") { Hidden = true });
101      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.") { Hidden = true });
102      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.") { Hidden = true });
103      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).") { Hidden = true });
104    }
105
106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserialization() {
108      if (Parameters.ContainsKey(ApplyLinearScalingParameterName) && !(Parameters[ApplyLinearScalingParameterName] is LookupParameter<BoolValue>))
109        Parameters.Remove(ApplyLinearScalingParameterName);
110      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName))
111        Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
112      if (!Parameters.ContainsKey(ValidRowIndicatorParameterName))
113        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)."));
114    }
115
116    protected IEnumerable<int> GenerateRowsToEvaluate() {
117      return GenerateRowsToEvaluate(RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
118    }
119
120    protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
121      IEnumerable<int> rows;
122      int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
123      int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
124      int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
125      int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
126      if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
127
128      if (percentageOfRows.IsAlmost(1.0))
129        rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
130      else {
131        int seed = RandomParameter.ActualValue.Next();
132        int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
133        if (count == 0) count = 1;
134        rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
135      }
136
137      rows = rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
138      if (ValidRowIndicatorParameter.ActualValue != null) {
139        string indicatorVar = ValidRowIndicatorParameter.ActualValue.Value;
140        var problemData = ProblemDataParameter.ActualValue;
141        var indicatorRow = problemData.Dataset.GetReadOnlyDoubleValues(indicatorVar);
142        rows = rows.Where(r => !indicatorRow[r].IsAlmost(0.0));
143      }
144      return rows;
145    }
146
147    [ThreadStatic]
148    private static double[] cache;
149    protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
150      double lowerEstimationLimit, double upperEstimationLimit,
151      IOnlineCalculator calculator, int maxRows) {
152      if (cache == null || cache.Length < maxRows) {
153        cache = new double[maxRows];
154      }
155
156      // calculate linear scaling
157      int i = 0;
158      var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
159      var targetValuesEnumerator = targetValues.GetEnumerator();
160      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
161      while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
162        double target = targetValuesEnumerator.Current;
163        double estimated = estimatedValuesEnumerator.Current;
164        cache[i] = estimated;
165        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
166          linearScalingCalculator.Add(estimated, target);
167        i++;
168      }
169      if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
170        throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
171
172      double alpha = linearScalingCalculator.Alpha;
173      double beta = linearScalingCalculator.Beta;
174      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
175        alpha = 0.0;
176        beta = 1.0;
177      }
178
179      //calculate the quality by using the passed online calculator
180      targetValuesEnumerator = targetValues.GetEnumerator();
181      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
182        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
183
184      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
185        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
186      }
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.