Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/UpdateEstimatedValuesOperator.cs @ 12979

Last change on this file since 12979 was 12979, checked in by bburlacu, 9 years ago

#1772:

  • added parameters to the SchemaCreator for calculating things in parallel
  • added parallel code to the SchemaEvaluator
  • implemented quality calculation and saving of estimated values in the scope in the UpdateEstimatedValuesOperator
  • small refactoring of QueryMatch (added some useful methods and made NodeInfo internal)
  • updated query match unit test
File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.EvolutionTracking;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [Item("UpdateEstimatedValuesOperator", "Put the estimated values of the tree in the scope to be used by the phenotypic similarity calculator")]
34  [StorableClass]
35  public class UpdateEstimatedValuesOperator : EvolutionTrackingOperator<ISymbolicExpressionTree> {
36    private const string ProblemDataParameterName = "ProblemData";
37    private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
38    private const string EstimationLimitsParameterName = "EstimationLimits";
39    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
40
41    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
42      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
43    }
44    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
45      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
46    }
47    public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
48      get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
49    }
50    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
51      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
52    }
53
54
55    public UpdateEstimatedValuesOperator() {
56      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
57      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
58      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
59      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
60    }
61
62    [StorableConstructor]
63    protected UpdateEstimatedValuesOperator(bool deserializing) : base(deserializing) { }
64
65    protected UpdateEstimatedValuesOperator(UpdateEstimatedValuesOperator original, Cloner cloner) : base(original, cloner) {
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new UpdateEstimatedValuesOperator(this, cloner);
70    }
71
72    public override IOperation Apply() {
73      var tree = SymbolicExpressionTreeParameter.ActualValue;
74      var problemData = ProblemDataParameter.ActualValue;
75      var estimationLimits = EstimationLimitsParameter.ActualValue;
76      var interpreter = InterpreterParameter.ActualValue;
77
78      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices).ToArray();
79      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray();
80
81      if (estimatedValues.Length != targetValues.Length)
82        throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
83
84      var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
85
86      for (int i = 0; i < estimatedValues.Length; ++i) {
87        var estimated = estimatedValues[i];
88        var target = targetValues[i];
89        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
90          linearScalingCalculator.Add(estimated, target);
91      }
92      double alpha = linearScalingCalculator.Alpha;
93      double beta = linearScalingCalculator.Beta;
94      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
95        alpha = 0.0;
96        beta = 1.0;
97      }
98
99      var scaled = estimatedValues.Select(x => x * beta + alpha).LimitToRange(estimationLimits.Lower, estimationLimits.Upper).ToArray();
100      OnlineCalculatorError error;
101      var r = OnlinePearsonsRCalculator.Calculate(targetValues, scaled, out error);
102      if (error != OnlineCalculatorError.None) r = 0;
103
104      var r2 = r * r;
105      if (r2 > 1.0) r2 = 1.0;
106
107      var variables = ExecutionContext.Scope.Variables;
108      ((DoubleValue)variables["Quality"].Value).Value = r2;
109
110      if (variables.ContainsKey("EstimatedValues")) {
111        variables["EstimatedValues"].Value = new DoubleArray(scaled);
112      } else {
113        variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(scaled)));
114      }
115      return base.Apply();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.