Changeset 5736
- Timestamp:
- 03/17/11 15:14:45 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring
- Files:
-
- 21 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs
r5733 r5736 119 119 IClassificationProblemData problemData, 120 120 IEnumerable<int> rows) { 121 string targetVariable = problemData.TargetVariable; 122 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows); 123 var targetClassValues = problemData.Dataset.GetEnumeratedVariableValues(targetVariable, rows); 124 125 double[] classValues; 126 double[] thresholds; 127 NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(problemData, estimatedValues, targetClassValues, out classValues, out thresholds); 128 return new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, classValues, thresholds); 121 return new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter); 129 122 } 130 123 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView.cs
r5717 r5736 48 48 ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode(); 49 49 ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode(); 50 root.AddSub Tree(start);50 root.AddSubtree(start); 51 51 tempTree = new SymbolicExpressionTree(root); 52 52 } 53 53 54 54 protected override void UpdateModel(ISymbolicExpressionTree tree) { 55 var dataset = Content.ProblemData.Dataset; 56 var interpreter = Content.Model.Interpreter; 57 var rows = Content.ProblemData.TrainingIndizes; 58 string targetVariable = Content.ProblemData.TargetVariable; 59 60 var targetClassValues = dataset.GetEnumeratedVariableValues(targetVariable, rows); 61 var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows) 62 .ToArray(); 63 double[] classValues; 64 double[] thresholds; 65 NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, originalOutput, targetClassValues, out classValues, out thresholds); 66 Content.Model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter, classValues, thresholds); 55 Content.Model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter); 56 Content.SetClassDistibutionCutPointThresholds(); 67 57 } 68 58 … … 83 73 string targetVariable = Content.ProblemData.TargetVariable; 84 74 Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>(); 85 List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSub Tree(0).GetSubTree(0).IterateNodesPostfix().ToList();75 List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList(); 86 76 87 77 var targetClassValues = dataset.GetEnumeratedVariableValues(targetVariable, rows); 88 78 var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows) 79 .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit) 89 80 .ToArray(); 90 81 double[] classValues; 91 82 double[] thresholds; 92 83 NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, originalOutput, targetClassValues, out classValues, out thresholds); 93 var classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, classValues, thresholds); 84 var classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter); 85 classifier.SetThresholdsAndClassValues(thresholds, classValues); 94 86 double originalAccuracy = OnlineAccuracyEvaluator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows)); 95 87 … … 99 91 ISymbolicExpressionTreeNode replacementNode = constantNode; 100 92 SwitchNode(parent, node, replacementNode); 101 var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows); 93 var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows) 94 .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit) 95 .ToArray(); 102 96 NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, newOutput, targetClassValues, out classValues, out thresholds); 103 classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, classValues, thresholds); 97 classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter); 98 classifier.SetThresholdsAndClassValues(thresholds, classValues); 104 99 double newAccuracy = OnlineAccuracyEvaluator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows)); 105 100 … … 114 109 115 110 private double CalculateReplacementValue(ISymbolicExpressionTreeNode node) { 116 var start = tempTree.Root.GetSub Tree(0);117 while (start.Sub Trees.Count() > 0) start.RemoveSubTree(0);118 start.AddSub Tree((ISymbolicExpressionTreeNode)node.Clone());111 var start = tempTree.Root.GetSubtree(0); 112 while (start.SubtreesCount > 0) start.RemoveSubtree(0); 113 start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone()); 119 114 var interpreter = Content.Model.Interpreter; 120 115 var rows = Content.ProblemData.TrainingIndizes; … … 124 119 125 120 private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) { 126 for (int i = 0; i < root.Sub Trees.Count(); i++) {127 if (root.GetSub Tree(i) == oldBranch) {128 root.RemoveSub Tree(i);129 root.InsertSub Tree(i, newBranch);121 for (int i = 0; i < root.SubtreesCount; i++) { 122 if (root.GetSubtree(i) == oldBranch) { 123 root.RemoveSubtree(i); 124 root.InsertSubtree(i, newBranch); 130 125 return; 131 126 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/Interfaces/ISymbolicDiscriminantFunctionClassificationModel.cs
r5717 r5736 23 23 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification { 24 24 public interface ISymbolicDiscriminantFunctionClassificationModel : IDiscriminantFunctionClassificationModel, ISymbolicClassificationModel { 25 double LowerEstimationLimit { get; } 26 double UpperEstimationLimit { get; } 25 27 } 26 28 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer.cs
r5722 r5736 95 95 } 96 96 97 98 97 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) { 99 double[] classValues; 100 double[] thresholds; 101 var estimatedValues = SymbolicDataAnalysisTreeInterpreter.GetSymbolicExpressionTreeValues(bestTree, ProblemData.Dataset, ProblemData.TrainingIndizes) 102 .LimitToRange(LowerEstimationLimit.Value, UpperEstimationLimit.Value); 103 var targetValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes); 104 AccuracyMaximizationThresholdCalculator.CalculateThresholds(ProblemData, estimatedValues, targetValues, out classValues, out thresholds); 105 var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreter, classValues, thresholds, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 106 return new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemData); 98 var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 99 var solution = new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemData); 100 if (ApplyLinearScaling.Value) { 101 solution.ScaleModel(); 102 } 103 return solution; 107 104 } 108 105 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs
r5722 r5736 81 81 82 82 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQualities) { 83 double[] classValues; 84 double[] thresholds; 85 // calculate thresholds on the whole training set even for the validation best solution 86 var estimatedValues = SymbolicDataAnalysisTreeInterpreter.GetSymbolicExpressionTreeValues(bestTree, ProblemData.Dataset, ProblemData.TrainingIndizes) 87 .LimitToRange(LowerEstimationLimit.Value, UpperEstimationLimit.Value); 88 var targetClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes); 89 AccuracyMaximizationThresholdCalculator.CalculateThresholds(ProblemData, estimatedValues, targetClassValues, out classValues, out thresholds); 90 var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreter, classValues, thresholds, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 91 return new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemData); 83 var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 84 var solution = new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemData); 85 if (ApplyLinearScaling.Value) { 86 solution.ScaleModel(); 87 } 88 return solution; 92 89 } 93 90 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer.cs
r5722 r5736 96 96 97 97 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 98 double[] classValues; 99 double[] thresholds; 100 var estimatedValues = SymbolicDataAnalysisTreeInterpreter.GetSymbolicExpressionTreeValues(bestTree, ProblemData.Dataset, ProblemData.TrainingIndizes) 101 .LimitToRange(LowerEstimationLimit.Value, UpperEstimationLimit.Value); 102 var targetValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes); 103 AccuracyMaximizationThresholdCalculator.CalculateThresholds(ProblemData, estimatedValues, targetValues, out classValues, out thresholds); 104 var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreter, classValues, thresholds, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 105 return new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemData); 98 var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 99 var solution = new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemData); 100 if (ApplyLinearScaling.Value) { 101 solution.ScaleModel(); 102 } 103 return solution; 106 104 } 107 105 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer.cs
r5722 r5736 82 82 83 83 protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 84 double[] classValues; 85 double[] thresholds; 86 // calculate thresholds on the whole training set even for the validation best solution 87 var estimatedValues = SymbolicDataAnalysisTreeInterpreter.GetSymbolicExpressionTreeValues(bestTree, ProblemData.Dataset, ProblemData.TrainingIndizes) 88 .LimitToRange(LowerEstimationLimit.Value, UpperEstimationLimit.Value); 89 var targetClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes); 90 AccuracyMaximizationThresholdCalculator.CalculateThresholds(ProblemData, estimatedValues, targetClassValues, out classValues, out thresholds); 91 var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreter, classValues, thresholds, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 92 return new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemData); 84 var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 85 var solution = new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemData); 86 if (ApplyLinearScaling.Value) { 87 solution.ScaleModel(); 88 } 89 return solution; 93 90 } 94 91 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolution.cs
r5717 r5736 39 39 [Item(Name = "SymbolicClassificationSolution", Description = "Represents a symbolic classification solution (model + data) and attributes of the solution like accuracy and complexity.")] 40 40 public sealed class SymbolicClassificationSolution : ClassificationSolution, ISymbolicClassificationSolution { 41 #region ISymbolicClassificationSolution Members 41 private const string ModelLengthResultName = "ModelLength"; 42 private const string ModelDepthResultName = "ModelDepth"; 42 43 43 44 public new ISymbolicClassificationModel Model { … … 49 50 get { return (ISymbolicDataAnalysisModel)base.Model; } 50 51 } 52 public int ModelLength { 53 get { return ((IntValue)this[ModelLengthResultName].Value).Value; } 54 private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; } 55 } 51 56 52 #endregion 57 public int ModelDepth { 58 get { return ((IntValue)this[ModelDepthResultName].Value).Value; } 59 private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; } 60 } 61 53 62 [StorableConstructor] 54 63 private SymbolicClassificationSolution(bool deserializing) : base(deserializing) { } … … 58 67 public SymbolicClassificationSolution(ISymbolicClassificationModel model, IClassificationProblemData problemData) 59 68 : base(model, problemData) { 69 Add(new Result(ModelLengthResultName, "Length of the symbolic classification model.", new IntValue())); 70 Add(new Result(ModelDepthResultName, "Depth of the symbolic classification model.", new IntValue())); 71 RecalculateResults(); 60 72 } 61 73 … … 63 75 return new SymbolicClassificationSolution(this, cloner); 64 76 } 77 78 protected override void OnModelChanged(EventArgs e) { 79 base.OnModelChanged(e); 80 RecalculateResults(); 81 } 82 83 private new void RecalculateResults() { 84 ModelLength = Model.SymbolicExpressionTree.Length; 85 ModelDepth = Model.SymbolicExpressionTree.Depth; 86 } 65 87 } 66 88 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs
r5720 r5736 44 44 public IEnumerable<double> Thresholds { 45 45 get { return (IEnumerable<double>)thresholds.Clone(); } 46 set { 47 thresholds = value.ToArray(); 48 OnThresholdsChanged(EventArgs.Empty); 49 } 46 private set { thresholds = value.ToArray(); } 50 47 } 51 48 [Storable] … … 53 50 public IEnumerable<double> ClassValues { 54 51 get { return (IEnumerable<double>)classValues.Clone(); } 55 set { classValues = value.ToArray(); }52 private set { classValues = value.ToArray(); } 56 53 } 57 54 [Storable] … … 72 69 } 73 70 public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 74 IEnumerable<double> classValues, IEnumerable<double> thresholds,75 71 double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue) 76 72 : base(tree, interpreter) { 77 th is.classValues = classValues.ToArray();78 this.thresholds = thresholds.ToArray();73 thresholds = new double[] { double.NegativeInfinity }; 74 classValues = new double[] { 0.0 }; 79 75 this.lowerEstimationLimit = lowerEstimationLimit; 80 76 this.upperEstimationLimit = upperEstimationLimit; … … 85 81 } 86 82 83 public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) { 84 var classValuesArr = classValues.ToArray(); 85 var thresholdsArr = thresholds.ToArray(); 86 if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException(); 87 88 this.classValues = classValuesArr; 89 this.thresholds = thresholdsArr; 90 OnThresholdsChanged(EventArgs.Empty); 91 } 92 87 93 public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) { 88 return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows); 94 return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows) 95 .LimitToRange(lowerEstimationLimit, upperEstimationLimit); 89 96 } 90 97 -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationSolution.cs
r5717 r5736 39 39 [Item(Name = "SymbolicDiscriminantFunctionClassificationSolution", Description = "Represents a symbolic classification solution (model + data) and attributes of the solution like accuracy and complexity.")] 40 40 public sealed class SymbolicDiscriminantFunctionClassificationSolution : DiscriminantFunctionClassificationSolution, ISymbolicClassificationSolution { 41 private const string ModelLengthResultName = "ModelLength"; 42 private const string ModelDepthResultName = "ModelDepth"; 41 43 42 44 public new ISymbolicDiscriminantFunctionClassificationModel Model { … … 52 54 get { return Model; } 53 55 } 56 public int ModelLength { 57 get { return ((IntValue)this[ModelLengthResultName].Value).Value; } 58 private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; } 59 } 54 60 61 public int ModelDepth { 62 get { return ((IntValue)this[ModelDepthResultName].Value).Value; } 63 private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; } 64 } 55 65 [StorableConstructor] 56 66 private SymbolicDiscriminantFunctionClassificationSolution(bool deserializing) : base(deserializing) { } … … 60 70 public SymbolicDiscriminantFunctionClassificationSolution(ISymbolicDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) 61 71 : base(model, problemData) { 72 Add(new Result(ModelLengthResultName, "Length of the symbolic classification model.", new IntValue())); 73 Add(new Result(ModelDepthResultName, "Depth of the symbolic classification model.", new IntValue())); 74 RecalculateResults(); 62 75 } 63 76 … … 65 78 return new SymbolicDiscriminantFunctionClassificationSolution(this, cloner); 66 79 } 80 81 protected override void OnModelChanged(EventArgs e) { 82 base.OnModelChanged(e); 83 RecalculateResults(); 84 } 85 86 private new void RecalculateResults() { 87 ModelLength = Model.SymbolicExpressionTree.Length; 88 ModelDepth = Model.SymbolicExpressionTree.Depth; 89 } 90 91 public void ScaleModel() { 92 var dataset = ProblemData.Dataset; 93 var targetVariable = ProblemData.TargetVariable; 94 var rows = ProblemData.TrainingIndizes; 95 var estimatedValues = GetEstimatedValues(rows); 96 var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows); 97 double alpha; 98 double beta; 99 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta); 100 101 ConstantTreeNode alphaTreeNode = null; 102 ConstantTreeNode betaTreeNode = null; 103 // check if model has been scaled previously by analyzing the structure of the tree 104 var startNode = Model.SymbolicExpressionTree.Root.GetSubtree(0); 105 if (startNode.GetSubtree(0).Symbol is Addition) { 106 var addNode = startNode.GetSubtree(0); 107 if (addNode.SubtreesCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) { 108 alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode; 109 var mulNode = addNode.GetSubtree(0); 110 if (mulNode.SubtreesCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) { 111 betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode; 112 } 113 } 114 } 115 // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes 116 if (alphaTreeNode != null && betaTreeNode != null) { 117 betaTreeNode.Value *= beta; 118 alphaTreeNode.Value *= beta; 119 alphaTreeNode.Value += alpha; 120 } else { 121 var mainBranch = startNode.GetSubtree(0); 122 startNode.RemoveSubtree(0); 123 var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha); 124 startNode.AddSubtree(scaledMainBranch); 125 } 126 127 OnModelChanged(EventArgs.Empty); 128 } 129 130 private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) { 131 if (alpha.IsAlmost(0.0)) { 132 return treeNode; 133 } else { 134 var node = (new Addition()).CreateTreeNode(); 135 var alphaConst = MakeConstant(alpha); 136 node.AddSubtree(treeNode); 137 node.AddSubtree(alphaConst); 138 return node; 139 } 140 } 141 142 private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) { 143 if (beta.IsAlmost(1.0)) { 144 return treeNode; 145 } else { 146 var node = (new Multiplication()).CreateTreeNode(); 147 var betaConst = MakeConstant(beta); 148 node.AddSubtree(treeNode); 149 node.AddSubtree(betaConst); 150 return node; 151 } 152 } 153 154 private static ISymbolicExpressionTreeNode MakeConstant(double c) { 155 var node = (ConstantTreeNode)(new Constant()).CreateTreeNode(); 156 node.Value = c; 157 return node; 158 } 67 159 } 68 160 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs
r5729 r5736 48 48 ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode(); 49 49 ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode(); 50 root.AddSub Tree(start);50 root.AddSubtree(start); 51 51 tempTree = new SymbolicExpressionTree(root); 52 52 } … … 73 73 string targetVariable = Content.ProblemData.TargetVariable; 74 74 Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>(); 75 List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSub Tree(0).GetSubTree(0).IterateNodesPostfix().ToList();75 List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList(); 76 76 var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows) 77 77 .ToArray(); … … 98 98 99 99 private double CalculateReplacementValue(ISymbolicExpressionTreeNode node) { 100 var start = tempTree.Root.GetSub Tree(0);101 while (start.Sub Trees.Count() > 0) start.RemoveSubTree(0);102 start.AddSub Tree((ISymbolicExpressionTreeNode)node.Clone());100 var start = tempTree.Root.GetSubtree(0); 101 while (start.SubtreesCount > 0) start.RemoveSubtree(0); 102 start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone()); 103 103 var interpreter = Content.Model.Interpreter; 104 104 var rows = Content.ProblemData.TrainingIndizes; … … 108 108 109 109 private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) { 110 for (int i = 0; i < root.Sub Trees.Count(); i++) {111 if (root.GetSub Tree(i) == oldBranch) {112 root.RemoveSub Tree(i);113 root.InsertSub Tree(i, newBranch);110 for (int i = 0; i < root.SubtreesCount; i++) { 111 if (root.GetSubtree(i) == oldBranch) { 112 root.RemoveSubtree(i); 113 root.InsertSubtree(i, newBranch); 114 114 return; 115 115 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs
r5733 r5736 39 39 [Item(Name = "SymbolicRegressionSolution", Description = "Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity.")] 40 40 public sealed class SymbolicRegressionSolution : RegressionSolution, ISymbolicRegressionSolution { 41 private const string ModelLengthResultName = "ModelLength"; 42 private const string ModelDepthResultName = "ModelDepth"; 43 41 44 public new ISymbolicRegressionModel Model { 42 45 get { return (ISymbolicRegressionModel)base.Model; } … … 45 48 ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model { 46 49 get { return (ISymbolicDataAnalysisModel)base.Model; } 50 } 51 public int ModelLength { 52 get { return ((IntValue)this[ModelLengthResultName].Value).Value; } 53 private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; } 54 } 55 56 public int ModelDepth { 57 get { return ((IntValue)this[ModelDepthResultName].Value).Value; } 58 private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; } 47 59 } 48 60 … … 54 66 public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData) 55 67 : base(model, problemData) { 68 Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue())); 69 Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue())); 70 RecalculateResults(); 56 71 } 57 72 58 73 public override IDeepCloneable Clone(Cloner cloner) { 59 74 return new SymbolicRegressionSolution(this, cloner); 75 } 76 77 protected override void OnModelChanged(EventArgs e) { 78 base.OnModelChanged(e); 79 RecalculateResults(); 80 } 81 82 private new void RecalculateResults() { 83 ModelLength = Model.SymbolicExpressionTree.Length; 84 ModelDepth = Model.SymbolicExpressionTree.Depth; 60 85 } 61 86 -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r5729 r5736 92 92 93 93 // automatically fold all branches with impact = 1 94 List<ISymbolicExpressionTreeNode> nodeList = Content.Model.SymbolicExpressionTree.Root.GetSub Tree(0).IterateNodesPrefix().ToList();94 List<ISymbolicExpressionTreeNode> nodeList = Content.Model.SymbolicExpressionTree.Root.GetSubtree(0).IterateNodesPrefix().ToList(); 95 95 foreach (var parent in nodeList) { 96 for (int subTreeIndex = 0; subTreeIndex < parent.Sub Trees.Count(); subTreeIndex++) {97 var child = parent.GetSub Tree(subTreeIndex);96 for (int subTreeIndex = 0; subTreeIndex < parent.SubtreesCount; subTreeIndex++) { 97 var child = parent.GetSubtree(subTreeIndex); 98 98 if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(1.0)) { 99 99 SwitchNodeWithReplacementNode(parent, subTreeIndex); … … 102 102 } 103 103 // show only interesting part of solution 104 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSub Tree(0).GetSubTree(0));104 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); 105 105 this.PaintNodeImpacts(); 106 106 } … … 124 124 var tree = Content.Model.SymbolicExpressionTree; 125 125 foreach (SymbolicExpressionTreeNode treeNode in tree.IterateNodesPostfix()) { 126 for (int i = 0; i < treeNode.Sub Trees.Count(); i++) {127 ISymbolicExpressionTreeNode subTree = treeNode.GetSub Tree(i);126 for (int i = 0; i < treeNode.SubtreesCount; i++) { 127 ISymbolicExpressionTreeNode subTree = treeNode.GetSubtree(i); 128 128 if (subTree == visualTreeNode.SymbolicExpressionTreeNode) { 129 129 SwitchNodeWithReplacementNode(treeNode, i); … … 133 133 134 134 // show only interesting part of solution 135 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSub Tree(0).GetSubTree(0));135 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); 136 136 137 137 UpdateModel(tree); 138 this.PaintNodeImpacts();139 138 } 140 139 141 140 private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) { 142 ISymbolicExpressionTreeNode subTree = parent.GetSub Tree(subTreeIndex);143 parent.RemoveSub Tree(subTreeIndex);141 ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex); 142 parent.RemoveSubtree(subTreeIndex); 144 143 if (replacementNodes.ContainsKey(subTree)) { 145 144 var replacementNode = replacementNodes[subTree]; 146 parent.InsertSub Tree(subTreeIndex, replacementNode);145 parent.InsertSubtree(subTreeIndex, replacementNode); 147 146 // exchange key and value 148 147 replacementNodes.Remove(subTree); 149 148 replacementNodes.Add(replacementNode, subTree); 150 } else {151 throw new InvalidOperationException("Could not find replacement value.");152 149 } 153 150 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/DiscriminantFunctionClassificationSolutionView.cs
r5717 r5736 98 98 } 99 99 private void Content_ModelChanged(object sender, EventArgs e) { 100 Content.Model.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged); 100 101 UpdateChart(); 101 102 } 102 private void Content_ThresholdsChanged(object sender, EventArgs e) {103 private void Model_ThresholdsChanged(object sender, EventArgs e) { 103 104 AddThresholds(); 104 105 } … … 244 245 245 246 thresholds[classIndex] = e.NewLocationY; 246 Content.Model. Thresholds = thresholds;247 Content.Model.SetThresholdsAndClassValues(thresholds, Content.Model.ClassValues); 247 248 } 248 249 -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationSolution.cs
r5730 r5736 82 82 } 83 83 84 pr ivatevoid RecalculateResults() {84 protected void RecalculateResults() { 85 85 double[] estimatedTrainingClassValues = EstimatedTrainingClassValues.ToArray(); // cache values 86 86 IEnumerable<double> originalTrainingClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes); -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationModel.cs
r5730 r5736 40 40 [Storable] 41 41 private IRegressionModel model; 42 42 43 [Storable] 43 44 private double[] classValues; 44 // class values are not necessarily sorted in ascending order45 45 public IEnumerable<double> ClassValues { 46 46 get { return (double[])classValues.Clone(); } 47 set { 48 if (value == null) throw new ArgumentException(); 49 double[] newValue = value.ToArray(); 50 if (newValue.Length != classValues.Length) throw new ArgumentException(); 51 classValues = newValue; 52 } 47 private set { classValues = value.ToArray(); } 53 48 } 49 54 50 [Storable] 55 51 private double[] thresholds; 56 52 public IEnumerable<double> Thresholds { 57 53 get { return (IEnumerable<double>)thresholds.Clone(); } 58 set { 59 thresholds = value.ToArray(); 60 OnThresholdsChanged(EventArgs.Empty); 61 } 54 private set { thresholds = value.ToArray(); } 62 55 } 63 56 … … 71 64 thresholds = (double[])original.thresholds.Clone(); 72 65 } 73 public DiscriminantFunctionClassificationModel(IRegressionModel model, IEnumerable<double> classValues, IEnumerable<double> thresholds) 66 67 public DiscriminantFunctionClassificationModel(IRegressionModel model) 74 68 : base() { 75 69 this.name = ItemName; 76 70 this.description = ItemDescription; 77 71 this.model = model; 78 this.classValues = classValues.ToArray();79 this.thresholds = thresholds.ToArray();72 this.classValues = new double[] { 0.0 }; 73 this.thresholds = new double[] { double.NegativeInfinity }; 80 74 } 81 75 82 76 public override IDeepCloneable Clone(Cloner cloner) { 83 77 return new DiscriminantFunctionClassificationModel(this, cloner); 78 } 79 80 public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) { 81 var classValuesArr = classValues.ToArray(); 82 var thresholdsArr = thresholds.ToArray(); 83 if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException(); 84 85 this.classValues = classValuesArr; 86 this.thresholds = thresholdsArr; 87 OnThresholdsChanged(EventArgs.Empty); 84 88 } 85 89 … … 96 100 else break; 97 101 } 98 yield return classValues.ElementAt(classIndex );102 yield return classValues.ElementAt(classIndex - 1); 99 103 } 100 104 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs
r5730 r5736 40 40 public new IDiscriminantFunctionClassificationModel Model { 41 41 get { return (IDiscriminantFunctionClassificationModel)base.Model; } 42 protected set { base.Model = value; } 42 protected set { 43 if (value != null && value != Model) { 44 if (Model != null) { 45 Model.ThresholdsChanged -= new EventHandler(Model_ThresholdsChanged); 46 } 47 value.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged); 48 base.Model = value; 49 } 50 } 43 51 } 44 52 … … 47 55 protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner) 48 56 : base(original, cloner) { 57 RegisterEventHandler(); 49 58 } 50 public DiscriminantFunctionClassificationSolution(IRegressionModel model, IClassificationProblemData problemData , IEnumerable<double> classValues, IEnumerable<double> thresholds)51 : this(new DiscriminantFunctionClassificationModel(model , classValues, thresholds), problemData) {59 public DiscriminantFunctionClassificationSolution(IRegressionModel model, IClassificationProblemData problemData) 60 : this(new DiscriminantFunctionClassificationModel(model), problemData) { 52 61 } 53 62 public DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) 54 63 : base(model, problemData) { 64 RegisterEventHandler(); 65 SetAccuracyMaximizingThresholds(); 66 } 67 68 [StorableHook(HookType.AfterDeserialization)] 69 private void AfterDeserialization() { 70 RegisterEventHandler(); 71 } 72 73 private void RegisterEventHandler() { 74 Model.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged); 75 } 76 private void Model_ThresholdsChanged(object sender, EventArgs e) { 77 OnModelThresholdsChanged(e); 78 } 79 80 public void SetAccuracyMaximizingThresholds() { 81 double[] classValues; 82 double[] thresholds; 83 var targetClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes); 84 AccuracyMaximizationThresholdCalculator.CalculateThresholds(ProblemData, EstimatedTrainingValues, targetClassValues, out classValues, out thresholds); 85 86 Model.SetThresholdsAndClassValues(thresholds, classValues); 87 } 88 89 public void SetClassDistibutionCutPointThresholds() { 90 double[] classValues; 91 double[] thresholds; 92 var targetClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes); 93 NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(ProblemData, EstimatedTrainingValues, targetClassValues, out classValues, out thresholds); 94 95 Model.SetThresholdsAndClassValues(thresholds, classValues); 96 } 97 98 protected override void OnModelChanged(EventArgs e) { 99 base.OnModelChanged(e); 100 SetAccuracyMaximizingThresholds(); 101 } 102 103 protected override void OnProblemDataChanged(EventArgs e) { 104 base.OnProblemDataChanged(e); 105 SetAccuracyMaximizingThresholds(); 106 } 107 protected virtual void OnModelThresholdsChanged(EventArgs e) { 108 RecalculateResults(); 55 109 } 56 110 -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ThresholdCalculators/AccuracyMaximizationThresholdCalculator.cs
r5730 r5736 69 69 classValues = problemData.ClassValues.OrderBy(x => x).ToArray(); 70 70 int nClasses = classValues.Length; 71 thresholds = new double[nClasses + 1];71 thresholds = new double[nClasses]; 72 72 thresholds[0] = double.NegativeInfinity; 73 thresholds[thresholds.Length - 1] = double.PositiveInfinity;73 // thresholds[thresholds.Length - 1] = double.PositiveInfinity; 74 74 75 75 // incrementally calculate accuracy of all possible thresholds 76 76 int[,] confusionMatrix = new int[nClasses, nClasses]; 77 77 78 for (int i = 1; i < thresholds.Length - 1; i++) {78 for (int i = 1; i < thresholds.Length; i++) { 79 79 double lowerThreshold = thresholds[i - 1]; 80 80 double actualThreshold = Math.Max(lowerThreshold, minEstimatedValue); -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ThresholdCalculators/NormalDistributionCutPointsThresholdCalculator.cs
r5730 r5736 88 88 thresholdList.Sort(); 89 89 thresholdList.Insert(0, double.NegativeInfinity); 90 thresholdList.Add(double.PositiveInfinity);91 90 92 91 // determine class values for each partition separated by a threshold by calculating the density of all class distributions 93 92 // all points in the partition are classified as the class with the maximal density in the parition 94 93 List<double> classValuesList = new List<double>(); 95 for (int i = 0; i < thresholdList.Count - 1; i++) {94 for (int i = 0; i < thresholdList.Count; i++) { 96 95 double m; 97 96 if (double.IsNegativeInfinity(thresholdList[i])) { 98 97 m = thresholdList[i + 1] - 1.0; // smaller than the smalles non-infinity threshold 99 } else if (double.IsPositiveInfinity(thresholdList[i + 1])) { 100 m = thresholdList[i] + 1.0; // larger than the largest non-infinity threshold 98 } else if (i == thresholdList.Count - 1) { 99 // last threshold 100 m = thresholdList[i] + 1.0; // larger than the last threshold 101 101 } else { 102 102 m = thresholdList[i] + (thresholdList[i + 1] - thresholdList[i]) / 2.0; // middle of partition … … 135 135 } 136 136 } 137 filteredThresholds.Add(double.PositiveInfinity);138 137 thresholds = filteredThresholds.ToArray(); 139 138 classValues = filteredClassValues.ToArray(); -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolution.cs
r5730 r5736 111 111 } 112 112 113 pr ivatevoid RecalculateResults() {113 protected void RecalculateResults() { 114 114 double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values 115 115 IEnumerable<double> originalTrainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes); -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IDiscriminantFunctionClassificationModel.cs
r5717 r5736 24 24 namespace HeuristicLab.Problems.DataAnalysis { 25 25 public interface IDiscriminantFunctionClassificationModel : IClassificationModel { 26 IEnumerable<double> Thresholds { get; set; } 27 IEnumerable<double> ClassValues { get; set; } 26 IEnumerable<double> Thresholds { get; } 27 IEnumerable<double> ClassValues { get; } 28 // class values and thresholds can only be assigned simultaniously 29 void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues); 28 30 IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows); 29 31
Note: See TracChangeset
for help on using the changeset viewer.