Changeset 15321
- Timestamp:
- 08/10/17 17:16:07 (7 years ago)
- Location:
- branches/SimplifierViewsProgress
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicClassificationSolutionSimplifierViewBase.cs
r14826 r15321 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 34 32 get { return (ISymbolicClassificationSolution)base.Content; } 35 33 set { base.Content = value; } 34 } 35 36 protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator { 37 get { return calculator; } 36 38 } 37 39 … … 60 62 } 61 63 62 protected override Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {63 var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();64 foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {65 double impactValue, replacementValue, newQualityForImpactsCalculation;66 calculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);67 impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));68 }69 return impactAndReplacementValues;70 }71 72 64 protected override void btnOptimizeConstants_Click(object sender, EventArgs e) { 73 65 -
branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs
r14826 r15321 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; 25 24 using HeuristicLab.Core; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 … … 40 38 protected SymbolicClassificationSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { } 41 39 42 public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, 43 ISymbolicExpressionTreeNode node, 44 IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, 45 out double newQualityForImpactsCalculation, 46 double qualityForImpactsCalculation = Double.NaN) { 40 protected override double CalculateQualityForImpacts(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IEnumerable<int> rows) { 47 41 var classificationModel = (ISymbolicClassificationModel)model; 48 42 var classificationProblemData = (IClassificationProblemData)problemData; 49 50 if (double.IsNaN(qualityForImpactsCalculation))51 qualityForImpactsCalculation = CalculateQualityForImpacts(classificationModel, classificationProblemData, rows);52 53 54 var cloner = new Cloner();55 var tempModel = cloner.Clone(classificationModel);56 var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);57 58 var tempModelParentNode = tempModelNode.Parent;59 int i = tempModelParentNode.IndexOfSubtree(tempModelNode);60 double bestReplacementValue = 0.0;61 double bestImpactValue = double.PositiveInfinity;62 newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize63 // try the potentially reasonable replacement values and use the best one64 foreach (var repValue in CalculateReplacementValues(node, classificationModel.SymbolicExpressionTree, classificationModel.Interpreter, classificationProblemData.Dataset, classificationProblemData.TrainingIndices)) {65 tempModelParentNode.RemoveSubtree(i);66 67 var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };68 tempModelParentNode.InsertSubtree(i, constantNode);69 70 var dataset = classificationProblemData.Dataset;71 var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);72 var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);73 OnlineCalculatorError errorState;74 newQualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues,75 out errorState);76 if (errorState != OnlineCalculatorError.None) newQualityForImpactsCalculation = 0.0;77 78 impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;79 80 if (impactValue < bestImpactValue) {81 bestImpactValue = impactValue;82 bestReplacementValue = repValue;83 }84 }85 replacementValue = bestReplacementValue;86 impactValue = bestImpactValue;87 }88 89 public static double CalculateQualityForImpacts(ISymbolicClassificationModel model, IClassificationProblemData problemData, IEnumerable<int> rows) {90 43 OnlineCalculatorError errorState; 91 44 var dataset = problemData.Dataset; 92 var targetClassValues = dataset.GetDoubleValues( problemData.TargetVariable, rows);93 var originalClassValues = model.GetEstimatedClassValues(dataset, rows);45 var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows); 46 var originalClassValues = classificationModel.GetEstimatedClassValues(dataset, rows); 94 47 var qualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState); 95 48 if (errorState != OnlineCalculatorError.None) qualityForImpactsCalculation = 0.0; 96 97 49 return qualityForImpactsCalculation; 98 50 } -
branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs
r15319 r15321 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Threading.Tasks; 25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 35 34 } 36 35 36 protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator { 37 get { return calculator; } 38 } 39 37 40 public InteractiveSymbolicRegressionSolutionSimplifierView() 38 41 : base() { … … 46 49 model.Scale(Content.ProblemData); 47 50 Content.Model = model; 48 }49 50 51 protected override Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {52 var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();53 foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {54 double impactValue, replacementValue, newQualityForImpactsCalculation;55 calculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);56 Progress.ProgressValue += 1.0 / (tree.Length - 2);57 impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));58 }59 return impactAndReplacementValues;60 51 } 61 52 -
branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionImpactValuesCalculator.cs
r14826 r15321 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; 25 24 using HeuristicLab.Core; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 … … 32 30 public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator { 33 31 public SymbolicRegressionSolutionImpactValuesCalculator() { } 34 35 32 protected SymbolicRegressionSolutionImpactValuesCalculator(SymbolicRegressionSolutionImpactValuesCalculator original, Cloner cloner) 36 33 : base(original, cloner) { } … … 38 35 return new SymbolicRegressionSolutionImpactValuesCalculator(this, cloner); 39 36 } 40 41 37 [StorableConstructor] 42 38 protected SymbolicRegressionSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { } 43 39 44 public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, 45 IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation, 46 double qualityForImpactsCalculation = double.NaN) { 40 protected override double CalculateQualityForImpacts(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IEnumerable<int> rows) { 47 41 var regressionModel = (ISymbolicRegressionModel)model; 48 42 var regressionProblemData = (IRegressionProblemData)problemData; 49 50 var dataset = regressionProblemData.Dataset; 51 var targetValues = dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows); 52 53 if (double.IsNaN(qualityForImpactsCalculation)) 54 qualityForImpactsCalculation = CalculateQualityForImpacts(regressionModel, regressionProblemData, rows); 55 56 var cloner = new Cloner(); 57 var tempModel = cloner.Clone(regressionModel); 58 var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node); 59 60 var tempModelParentNode = tempModelNode.Parent; 61 int i = tempModelParentNode.IndexOfSubtree(tempModelNode); 62 63 double bestReplacementValue = 0.0; 64 double bestImpactValue = double.PositiveInfinity; 65 newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize 66 // try the potentially reasonable replacement values and use the best one 67 foreach (var repValue in CalculateReplacementValues(node, regressionModel.SymbolicExpressionTree, regressionModel.Interpreter, regressionProblemData.Dataset, regressionProblemData.TrainingIndices)) { 68 69 tempModelParentNode.RemoveSubtree(i); 70 71 var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue }; 72 73 tempModelParentNode.InsertSubtree(i, constantNode); 74 75 var estimatedValues = tempModel.GetEstimatedValues(dataset, rows); 76 OnlineCalculatorError errorState; 77 double r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out errorState); 78 if (errorState != OnlineCalculatorError.None) r = 0.0; 79 newQualityForImpactsCalculation = r * r; 80 81 impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation; 82 if (impactValue < bestImpactValue) { 83 bestImpactValue = impactValue; 84 bestReplacementValue = repValue; 85 } 86 } 87 replacementValue = bestReplacementValue; 88 impactValue = bestImpactValue; 89 } 90 91 public static double CalculateQualityForImpacts(ISymbolicRegressionModel model, IRegressionProblemData problemData, IEnumerable<int> rows) { 92 var estimatedValues = model.GetEstimatedValues(problemData.Dataset, rows); // also bounds the values 93 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 43 var estimatedValues = regressionModel.GetEstimatedValues(problemData.Dataset, rows); // also bounds the values 44 var targetValues = problemData.Dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows); 94 45 OnlineCalculatorError errorState; 95 46 var r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out errorState); -
branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views/3.4/InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView.cs
r14826 r15321 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 using HeuristicLab.Common;26 23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 24 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 27 25 using HeuristicLab.Problems.DataAnalysis.Symbolic.Views; 28 26 29 27 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views { 30 28 public partial class InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView { 31 private readonly ConstantTreeNode constantNode; 32 private readonly SymbolicExpressionTree tempTree; 29 private readonly SymbolicRegressionSolutionImpactValuesCalculator calculator; 33 30 34 31 public new SymbolicTimeSeriesPrognosisSolution Content { … … 37 34 } 38 35 36 protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator { 37 get { return calculator; } 38 } 39 39 40 public InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView() 40 41 : base() { 41 42 InitializeComponent(); 42 43 this.Caption = "Interactive Time-Series Prognosis Solution Simplifier"; 43 44 constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode()); 45 ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode(); 46 ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode(); 47 root.AddSubtree(start); 48 tempTree = new SymbolicExpressionTree(root); 49 } 50 51 protected override Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) { 52 var interpreter = Content.Model.Interpreter; 53 var rows = Content.ProblemData.TrainingIndices; 54 var dataset = Content.ProblemData.Dataset; 55 var targetVariable = Content.ProblemData.TargetVariable; 56 var targetValues = dataset.GetDoubleValues(targetVariable, rows); 57 var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToArray(); 58 59 var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>(); 60 List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList(); 61 OnlineCalculatorError errorState; 62 double originalR = OnlinePearsonsRCalculator.Calculate(targetValues, originalOutput, out errorState); 63 if (errorState != OnlineCalculatorError.None) originalR = 0.0; 64 65 foreach (ISymbolicExpressionTreeNode node in nodes) { 66 var parent = node.Parent; 67 constantNode.Value = CalculateReplacementValue(node, tree); 68 ISymbolicExpressionTreeNode replacementNode = constantNode; 69 SwitchNode(parent, node, replacementNode); 70 var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows); 71 double newR = OnlinePearsonsRCalculator.Calculate(targetValues, newOutput, out errorState); 72 if (errorState != OnlineCalculatorError.None) newR = 0.0; 73 74 // impact = 0 if no change 75 // impact < 0 if new solution is better 76 // impact > 0 if new solution is worse 77 double impact = (originalR * originalR) - (newR * newR); 78 impactAndReplacementValues[node] = new Tuple<double, double>(impact, constantNode.Value); 79 SwitchNode(parent, replacementNode, node); 80 } 81 return impactAndReplacementValues; 44 calculator = new SymbolicRegressionSolutionImpactValuesCalculator(); 82 45 } 83 46 … … 88 51 } 89 52 90 private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) { 91 // remove old ADFs 92 while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1); 93 // clone ADFs of source tree 94 for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) { 95 tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone()); 96 } 97 var start = tempTree.Root.GetSubtree(0); 98 while (start.SubtreeCount > 0) start.RemoveSubtree(0); 99 start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone()); 100 var interpreter = Content.Model.Interpreter; 101 var rows = Content.ProblemData.TrainingIndices; 102 var allPrognosedValues = interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows); 53 protected override void btnOptimizeConstants_Click(object sender, EventArgs e) { 103 54 104 return allPrognosedValues.Median();105 }106 107 108 private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {109 for (int i = 0; i < root.SubtreeCount; i++) {110 if (root.GetSubtree(i) == oldBranch) {111 root.RemoveSubtree(i);112 root.InsertSubtree(i, newBranch);113 return;114 }115 }116 }117 118 protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {119 throw new NotImplementedException();120 55 } 121 56 } -
branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r15319 r15321 191 191 } 192 192 193 protected abstract Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree); 193 protected virtual Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) { 194 var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>(); 195 foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) { 196 double impactValue, replacementValue, newQualityForImpactsCalculation; 197 ImpactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation); 198 Progress.ProgressValue += 1.0 / (tree.Length - 2); 199 impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue)); 200 } 201 return impactAndReplacementValues; 202 } 203 194 204 protected abstract void UpdateModel(ISymbolicExpressionTree tree); 205 206 protected abstract ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator { get; } 195 207 196 208 private static ConstantTreeNode MakeConstantTreeNode(double value) { -
branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs
r14826 r15321 32 32 public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator : Item, ISymbolicDataAnalysisSolutionImpactValuesCalculator { 33 33 protected SymbolicDataAnalysisSolutionImpactValuesCalculator() { } 34 35 34 protected SymbolicDataAnalysisSolutionImpactValuesCalculator(SymbolicDataAnalysisSolutionImpactValuesCalculator original, Cloner cloner) 36 35 : base(original, cloner) { } 37 36 [StorableConstructor] 38 37 protected SymbolicDataAnalysisSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { } 39 public abstract void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation, double qualityForImpactsCalculation = double.NaN); 38 39 public virtual void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, 40 out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation, 41 double qualityForImpactsCalculation = double.NaN) { 42 if (double.IsNaN(qualityForImpactsCalculation)) 43 qualityForImpactsCalculation = CalculateQualityForImpacts(model, problemData, rows); 44 45 var cloner = new Cloner(); 46 var tempModel = cloner.Clone(model); 47 var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node); 48 49 var tempModelParentNode = tempModelNode.Parent; 50 int i = tempModelParentNode.IndexOfSubtree(tempModelNode); 51 52 double bestReplacementValue = 0.0; 53 double bestImpactValue = double.PositiveInfinity; 54 newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize 55 // try the potentially reasonable replacement values and use the best one 56 foreach (var repValue in CalculateReplacementValues(node, model.SymbolicExpressionTree, model.Interpreter, problemData.Dataset, problemData.TrainingIndices)) { 57 tempModelParentNode.RemoveSubtree(i); 58 59 var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue }; 60 tempModelParentNode.InsertSubtree(i, constantNode); 61 62 newQualityForImpactsCalculation = CalculateQualityForImpacts(tempModel, problemData, rows); 63 64 impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation; 65 if (impactValue < bestImpactValue) { 66 bestImpactValue = impactValue; 67 bestReplacementValue = repValue; 68 } 69 } 70 71 replacementValue = bestReplacementValue; 72 impactValue = bestImpactValue; 73 } 74 75 protected abstract double CalculateQualityForImpacts(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IEnumerable<int> rows); 40 76 41 77 protected IEnumerable<double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
Note: See TracChangeset
for help on using the changeset viewer.