Changeset 15906 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/UpdateQualityOperator.cs
- Timestamp:
- 04/16/18 08:35:59 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/UpdateQualityOperator.cs
r13876 r15906 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 6Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 21 21 22 22 using System; 23 using System.Collections; 24 using System.Collections.Generic; 23 25 using System.Linq; 24 26 using HeuristicLab.Common; … … 40 42 private const string ScaleEstimatedValuesParameterName = "ScaleEstimatedValues"; 41 43 44 #region parameters 42 45 public ILookupParameter<IRegressionProblemData> ProblemDataParameter { 43 46 get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; } … … 55 58 get { return (ILookupParameter<BoolValue>)Parameters[ScaleEstimatedValuesParameterName]; } 56 59 } 60 #endregion 57 61 58 62 public UpdateQualityOperator() { … … 76 80 public override IOperation Apply() { 77 81 var tree = SymbolicExpressionTreeParameter.ActualValue; 78 FixParentLinks(tree);79 82 var problemData = ProblemDataParameter.ActualValue; 80 83 var estimationLimits = EstimationLimitsParameter.ActualValue; 81 84 var interpreter = InterpreterParameter.ActualValue; 82 85 83 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices) .ToArray();84 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices) .ToArray();86 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices); 87 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices); 85 88 86 if (estimatedValues.Length != targetValues.Length) 87 throw new ArgumentException("Number of elements in target and estimated values enumeration do not match."); 89 var scaleEstimatedValues = ScaleEstimatedValuesParameter.ActualValue.Value; 88 90 89 var linearScalingCalculator = new OnlineLinearScalingParameterCalculator(); 91 IEnumerable<double> scaled; 92 if (scaleEstimatedValues) { 93 var linearScalingCalculator = new OnlineLinearScalingParameterCalculator(); 90 94 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); 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); 96 118 } 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(); 119 104 120 OnlineCalculatorError error; 105 121 var r = OnlinePearsonsRCalculator.Calculate(targetValues, scaled, out error); … … 111 127 112 128 ((DoubleValue)variables["Quality"].Value).Value = r2; 113 GenealogyGraph.GetByContent(tree).Quality = r2; 114 115 var scaleEstimatedValues = ScaleEstimatedValuesParameter.ActualValue; 116 if (!scaleEstimatedValues.Value) 117 scaled = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper).ToArray(); 129 //GenealogyGraph.GetByContent(tree).Quality = r2; 118 130 119 131 if (variables.ContainsKey("EstimatedValues")) { 120 variables["EstimatedValues"].Value = new DoubleArray(scaled );132 variables["EstimatedValues"].Value = new DoubleArray(scaled.ToArray()); 121 133 } else { 122 variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(scaled )));134 variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(scaled.ToArray()))); 123 135 } 124 136 return base.Apply(); 125 137 } 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 }135 138 } 136 139 }
Note: See TracChangeset
for help on using the changeset viewer.