Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/UpdateQualityOperator.cs @ 18242

Last change on this file since 18242 was 17434, checked in by bburlacu, 5 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
24using System.Collections.Generic;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.EvolutionTracking;
32using HeuristicLab.Parameters;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
35  [Item("UpdateQualityOperator", "Put the estimated values of the tree in the scope to be used by the phenotypic similarity calculator")]
36  [StorableType("09FFEC05-12F2-4215-8C72-1B3EECF61C00")]
37  public class UpdateQualityOperator : EvolutionTrackingOperator<ISymbolicExpressionTree> {
38    private const string ProblemDataParameterName = "ProblemData";
39    private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
40    private const string EstimationLimitsParameterName = "EstimationLimits";
41    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
42    private const string ScaleEstimatedValuesParameterName = "ScaleEstimatedValues";
43
44    #region parameters
45    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
46      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
47    }
48    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
49      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
50    }
51    public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
52      get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
53    }
54    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
55      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
56    }
57    public ILookupParameter<BoolValue> ScaleEstimatedValuesParameter {
58      get { return (ILookupParameter<BoolValue>)Parameters[ScaleEstimatedValuesParameterName]; }
59    }
60    #endregion
61
62    public UpdateQualityOperator() {
63      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
64      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
65      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
66      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
67      Parameters.Add(new LookupParameter<BoolValue>(ScaleEstimatedValuesParameterName));
68    }
69
70    [StorableConstructor]
71    protected UpdateQualityOperator(StorableConstructorFlag _) : base(_) { }
72
73    protected UpdateQualityOperator(UpdateQualityOperator original, Cloner cloner) : base(original, cloner) {
74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new UpdateQualityOperator(this, cloner);
78    }
79
80    public override IOperation Apply() {
81      var tree = SymbolicExpressionTreeParameter.ActualValue;
82      var problemData = ProblemDataParameter.ActualValue;
83      var estimationLimits = EstimationLimitsParameter.ActualValue;
84      var interpreter = InterpreterParameter.ActualValue;
85
86      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
87      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
88
89      var scaleEstimatedValues = ScaleEstimatedValuesParameter.ActualValue.Value;
90
91      IEnumerable<double> scaled;
92      if (scaleEstimatedValues) {
93        var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
94
95        var e1 = estimatedValues.GetEnumerator();
96        var e2 = targetValues.GetEnumerator();
97
98        int count = 0;
99        while (e1.MoveNext() && e2.MoveNext()) {
100          var estimated = e1.Current;
101          var target = e2.Current;
102          if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
103            linearScalingCalculator.Add(estimated, target);
104          ++count;
105        }
106        if (e1.MoveNext() || e2.MoveNext())
107          throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
108
109        double alpha = linearScalingCalculator.Alpha;
110        double beta = linearScalingCalculator.Beta;
111        if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
112          alpha = 0.0;
113          beta = 1.0;
114        }
115        scaled = estimatedValues.Select(x => x * beta + alpha).LimitToRange(estimationLimits.Lower, estimationLimits.Upper);
116      } else {
117        scaled = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper);
118      }
119
120      OnlineCalculatorError error;
121      var r = OnlinePearsonsRCalculator.Calculate(targetValues, scaled, out error);
122      if (error != OnlineCalculatorError.None) r = double.NaN;
123
124      var r2 = r * r;
125
126      var variables = ExecutionContext.Scope.Variables;
127
128      ((DoubleValue)variables["Quality"].Value).Value = r2;
129      //GenealogyGraph.GetByContent(tree).Quality = r2;
130
131      if (variables.ContainsKey("EstimatedValues")) {
132        variables["EstimatedValues"].Value = new DoubleArray(scaled.ToArray());
133      } else {
134        variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(scaled.ToArray())));
135      }
136      return base.Apply();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.