[12951] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15906] | 3 | * Copyright (C) 2002-2018 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;
|
---|
[15906] | 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
[12951] | 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 30 | using HeuristicLab.EvolutionTracking;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 |
|
---|
[12958] | 34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[13527] | 35 | [Item("UpdateQualityOperator", "Put the estimated values of the tree in the scope to be used by the phenotypic similarity calculator")]
|
---|
[12951] | 36 | [StorableClass]
|
---|
[13527] | 37 | public class UpdateQualityOperator : EvolutionTrackingOperator<ISymbolicExpressionTree> {
|
---|
[12951] | 38 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 39 | private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
| 40 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
| 41 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
[12988] | 42 | private const string ScaleEstimatedValuesParameterName = "ScaleEstimatedValues";
|
---|
[12951] | 43 |
|
---|
[15906] | 44 | #region parameters
|
---|
[12951] | 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 | }
|
---|
[12988] | 57 | public ILookupParameter<BoolValue> ScaleEstimatedValuesParameter {
|
---|
| 58 | get { return (ILookupParameter<BoolValue>)Parameters[ScaleEstimatedValuesParameterName]; }
|
---|
| 59 | }
|
---|
[15906] | 60 | #endregion
|
---|
[12951] | 61 |
|
---|
[13527] | 62 | public UpdateQualityOperator() {
|
---|
[12951] | 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));
|
---|
[12988] | 67 | Parameters.Add(new LookupParameter<BoolValue>(ScaleEstimatedValuesParameterName));
|
---|
[12951] | 68 | }
|
---|
| 69 |
|
---|
| 70 | [StorableConstructor]
|
---|
[13527] | 71 | protected UpdateQualityOperator(bool deserializing) : base(deserializing) { }
|
---|
[12951] | 72 |
|
---|
[13527] | 73 | protected UpdateQualityOperator(UpdateQualityOperator original, Cloner cloner) : base(original, cloner) {
|
---|
[12951] | 74 | }
|
---|
| 75 |
|
---|
| 76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13527] | 77 | return new UpdateQualityOperator(this, cloner);
|
---|
[12951] | 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 |
|
---|
[15906] | 86 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
|
---|
| 87 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
|
---|
[12951] | 88 |
|
---|
[15906] | 89 | var scaleEstimatedValues = ScaleEstimatedValuesParameter.ActualValue.Value;
|
---|
[12979] | 90 |
|
---|
[15906] | 91 | IEnumerable<double> scaled;
|
---|
| 92 | if (scaleEstimatedValues) {
|
---|
| 93 | var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
|
---|
[12979] | 94 |
|
---|
[15906] | 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);
|
---|
[12979] | 118 | }
|
---|
[15906] | 119 |
|
---|
[12979] | 120 | OnlineCalculatorError error;
|
---|
| 121 | var r = OnlinePearsonsRCalculator.Calculate(targetValues, scaled, out error);
|
---|
[12988] | 122 | if (error != OnlineCalculatorError.None) r = double.NaN;
|
---|
[12979] | 123 |
|
---|
| 124 | var r2 = r * r;
|
---|
| 125 |
|
---|
[12951] | 126 | var variables = ExecutionContext.Scope.Variables;
|
---|
[13496] | 127 |
|
---|
[12979] | 128 | ((DoubleValue)variables["Quality"].Value).Value = r2;
|
---|
[15906] | 129 | //GenealogyGraph.GetByContent(tree).Quality = r2;
|
---|
[12979] | 130 |
|
---|
| 131 | if (variables.ContainsKey("EstimatedValues")) {
|
---|
[15906] | 132 | variables["EstimatedValues"].Value = new DoubleArray(scaled.ToArray());
|
---|
[12979] | 133 | } else {
|
---|
[15906] | 134 | variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(scaled.ToArray())));
|
---|
[12979] | 135 | }
|
---|
[12951] | 136 | return base.Apply();
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|