[12951] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[13876] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12951] | 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 |
|
---|
[12979] | 22 | using System;
|
---|
[12951] | 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.EvolutionTracking;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
[12958] | 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[13527] | 33 | [Item("UpdateQualityOperator", "Put the estimated values of the tree in the scope to be used by the phenotypic similarity calculator")]
|
---|
[12951] | 34 | [StorableClass]
|
---|
[13527] | 35 | public class UpdateQualityOperator : EvolutionTrackingOperator<ISymbolicExpressionTree> {
|
---|
[12951] | 36 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 37 | private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
| 38 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
| 39 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
[12988] | 40 | private const string ScaleEstimatedValuesParameterName = "ScaleEstimatedValues";
|
---|
[12951] | 41 |
|
---|
| 42 | public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
|
---|
| 43 | get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
| 44 | }
|
---|
| 45 | public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
|
---|
| 46 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
|
---|
| 47 | }
|
---|
| 48 | public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
| 49 | get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
| 50 | }
|
---|
| 51 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 52 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
| 53 | }
|
---|
[12988] | 54 | public ILookupParameter<BoolValue> ScaleEstimatedValuesParameter {
|
---|
| 55 | get { return (ILookupParameter<BoolValue>)Parameters[ScaleEstimatedValuesParameterName]; }
|
---|
| 56 | }
|
---|
[12951] | 57 |
|
---|
[13527] | 58 | public UpdateQualityOperator() {
|
---|
[12951] | 59 | Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
|
---|
| 60 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
|
---|
| 61 | Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
|
---|
| 62 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
|
---|
[12988] | 63 | Parameters.Add(new LookupParameter<BoolValue>(ScaleEstimatedValuesParameterName));
|
---|
[12951] | 64 | }
|
---|
| 65 |
|
---|
| 66 | [StorableConstructor]
|
---|
[13527] | 67 | protected UpdateQualityOperator(bool deserializing) : base(deserializing) { }
|
---|
[12951] | 68 |
|
---|
[13527] | 69 | protected UpdateQualityOperator(UpdateQualityOperator original, Cloner cloner) : base(original, cloner) {
|
---|
[12951] | 70 | }
|
---|
| 71 |
|
---|
| 72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13527] | 73 | return new UpdateQualityOperator(this, cloner);
|
---|
[12951] | 74 | }
|
---|
| 75 |
|
---|
| 76 | public override IOperation Apply() {
|
---|
| 77 | var tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
[13876] | 78 | FixParentLinks(tree);
|
---|
[12951] | 79 | var problemData = ProblemDataParameter.ActualValue;
|
---|
| 80 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
| 81 | var interpreter = InterpreterParameter.ActualValue;
|
---|
| 82 |
|
---|
[12979] | 83 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices).ToArray();
|
---|
| 84 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray();
|
---|
[12951] | 85 |
|
---|
[12979] | 86 | if (estimatedValues.Length != targetValues.Length)
|
---|
| 87 | throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
|
---|
| 88 |
|
---|
| 89 | var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
|
---|
| 90 |
|
---|
| 91 | for (int i = 0; i < estimatedValues.Length; ++i) {
|
---|
| 92 | var estimated = estimatedValues[i];
|
---|
| 93 | var target = targetValues[i];
|
---|
| 94 | if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
|
---|
| 95 | linearScalingCalculator.Add(estimated, target);
|
---|
| 96 | }
|
---|
| 97 | double alpha = linearScalingCalculator.Alpha;
|
---|
| 98 | double beta = linearScalingCalculator.Beta;
|
---|
| 99 | if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
|
---|
| 100 | alpha = 0.0;
|
---|
| 101 | beta = 1.0;
|
---|
| 102 | }
|
---|
| 103 | var scaled = estimatedValues.Select(x => x * beta + alpha).LimitToRange(estimationLimits.Lower, estimationLimits.Upper).ToArray();
|
---|
| 104 | OnlineCalculatorError error;
|
---|
| 105 | var r = OnlinePearsonsRCalculator.Calculate(targetValues, scaled, out error);
|
---|
[12988] | 106 | if (error != OnlineCalculatorError.None) r = double.NaN;
|
---|
[12979] | 107 |
|
---|
| 108 | var r2 = r * r;
|
---|
| 109 |
|
---|
[12951] | 110 | var variables = ExecutionContext.Scope.Variables;
|
---|
[13496] | 111 |
|
---|
[12979] | 112 | ((DoubleValue)variables["Quality"].Value).Value = r2;
|
---|
[13527] | 113 | GenealogyGraph.GetByContent(tree).Quality = r2;
|
---|
[12979] | 114 |
|
---|
[12988] | 115 | var scaleEstimatedValues = ScaleEstimatedValuesParameter.ActualValue;
|
---|
| 116 | if (!scaleEstimatedValues.Value)
|
---|
| 117 | scaled = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper).ToArray();
|
---|
| 118 |
|
---|
[12979] | 119 | if (variables.ContainsKey("EstimatedValues")) {
|
---|
| 120 | variables["EstimatedValues"].Value = new DoubleArray(scaled);
|
---|
| 121 | } else {
|
---|
| 122 | variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(scaled)));
|
---|
| 123 | }
|
---|
[12951] | 124 | return base.Apply();
|
---|
| 125 | }
|
---|
[13876] | 126 |
|
---|
| 127 | private static void FixParentLinks(ISymbolicExpressionTree tree) {
|
---|
| 128 | foreach (var node in tree.IterateNodesPrefix().Where(x => x.SubtreeCount > 0)) {
|
---|
| 129 | foreach (var s in node.Subtrees) {
|
---|
| 130 | if (s.Parent != node)
|
---|
| 131 | s.Parent = node;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
[12951] | 135 | }
|
---|
| 136 | }
|
---|