Changeset 5729
- Timestamp:
- 03/17/11 12:25:30 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring
- Files:
-
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs
r5717 r5729 54 54 protected override void UpdateModel(ISymbolicExpressionTree tree) { 55 55 Content.Model = new SymbolicRegressionModel(tree, Content.Model.Interpreter); 56 Content.ScaleModel(); 56 57 } 57 58 -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r5722 r5729 129 129 <Compile Include="SingleObjective\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" /> 130 130 <Compile Include="SymbolicRegressionSolution.cs" /> 131 <Compile Include="SymbolicRegressionSolutionLinearScaler.cs" />132 131 <None Include="HeuristicLab.snk" /> 133 132 <None Include="HeuristicLabProblemsDataAnalysisSymbolicRegressionPlugin.cs.frame" /> -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs
r5722 r5729 90 90 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated values produced by the symbolic regression model.")); 91 91 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated values produced by the symbolic regression model.")); 92 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue( false)));92 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); 93 93 } 94 94 … … 99 99 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) { 100 100 var model = new SymbolicRegressionModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 101 return new SymbolicRegressionSolution(model, ProblemData); 101 var solution = new SymbolicRegressionSolution(model, ProblemData); 102 if (ApplyLinearScaling.Value) 103 solution.ScaleModel(); 104 return solution; 102 105 } 103 106 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs
r5722 r5729 74 74 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated values produced by the symbolic regression model.")); 75 75 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated values produced by the symbolic regression model.")); 76 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue( false)));76 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); 77 77 } 78 78 public override IDeepCloneable Clone(Cloner cloner) { … … 82 82 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) { 83 83 var model = new SymbolicRegressionModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 84 return new SymbolicRegressionSolution(model, ProblemData); 84 var solution = new SymbolicRegressionSolution(model, ProblemData); 85 if (ApplyLinearScaling.Value) 86 solution.ScaleModel(); 87 return solution; 85 88 } 86 89 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer.cs
r5722 r5729 89 89 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated values produced by the symbolic regression model.")); 90 90 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated values produced by the symbolic regression model.")); 91 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue( false)));91 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); 92 92 } 93 93 public override IDeepCloneable Clone(Cloner cloner) { … … 97 97 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 98 98 var model = new SymbolicRegressionModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value); 99 return new SymbolicRegressionSolution(model, ProblemData); 99 var solution = new SymbolicRegressionSolution(model, ProblemData); 100 if (ApplyLinearScaling.Value) 101 solution.ScaleModel(); 102 return solution; 100 103 } 101 104 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer.cs
r5722 r5729 74 74 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated values produced by the symbolic regression model.")); 75 75 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated values produced by the symbolic regression model.")); 76 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue( false)));76 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); 77 77 } 78 78 … … 85 85 var solution = new SymbolicRegressionSolution(model, ProblemData); 86 86 if (ApplyLinearScaling.Value) 87 SymbolicRegressionSolutionLinearScaler.Scale(solution);87 solution.ScaleModel(); 88 88 return solution; 89 89 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs
r5720 r5729 59 59 return new SymbolicRegressionSolution(this, cloner); 60 60 } 61 62 public void ScaleModel() { 63 var dataset = ProblemData.Dataset; 64 var targetVariable = ProblemData.TargetVariable; 65 var rows = ProblemData.TrainingIndizes; 66 var estimatedValues = GetEstimatedValues(rows); 67 var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows); 68 double alpha; 69 double beta; 70 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta); 71 72 ConstantTreeNode alphaTreeNode = null; 73 ConstantTreeNode betaTreeNode = null; 74 // check if model has been scaled previously by analyzing the structure of the tree 75 var startNode = Model.SymbolicExpressionTree.Root.GetSubTree(0); 76 if (startNode.GetSubTree(0).Symbol is Addition) { 77 var addNode = startNode.GetSubTree(0); 78 if (addNode.SubtreesCount == 2 && addNode.GetSubTree(0).Symbol is Multiplication && addNode.GetSubTree(1).Symbol is Constant) { 79 alphaTreeNode = addNode.GetSubTree(1) as ConstantTreeNode; 80 var mulNode = addNode.GetSubTree(0); 81 if (mulNode.SubtreesCount == 2 && mulNode.GetSubTree(1).Symbol is Constant) { 82 betaTreeNode = mulNode.GetSubTree(1) as ConstantTreeNode; 83 } 84 } 85 } 86 // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes 87 if (alphaTreeNode != null && betaTreeNode != null) { 88 betaTreeNode.Value *= beta; 89 alphaTreeNode.Value *= beta; 90 alphaTreeNode.Value += alpha; 91 } else { 92 var mainBranch = startNode.GetSubTree(0); 93 startNode.RemoveSubTree(0); 94 var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha); 95 startNode.AddSubTree(scaledMainBranch); 96 } 97 98 OnModelChanged(EventArgs.Empty); 99 } 100 101 private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) { 102 if (alpha.IsAlmost(0.0)) { 103 return treeNode; 104 } else { 105 var node = (new Addition()).CreateTreeNode(); 106 var alphaConst = MakeConstant(alpha); 107 node.AddSubTree(treeNode); 108 node.AddSubTree(alphaConst); 109 return node; 110 } 111 } 112 113 private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) { 114 if (beta.IsAlmost(1.0)) { 115 return treeNode; 116 } else { 117 var node = (new Multiplication()).CreateTreeNode(); 118 var betaConst = MakeConstant(beta); 119 node.AddSubTree(treeNode); 120 node.AddSubTree(betaConst); 121 return node; 122 } 123 } 124 125 private static ISymbolicExpressionTreeNode MakeConstant(double c) { 126 var node = (ConstantTreeNode)(new Constant()).CreateTreeNode(); 127 node.Value = c; 128 return node; 129 } 61 130 } 62 131 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r5722 r5729 33 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views { 34 34 public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView { 35 private Dictionary<ISymbolicExpressionTreeNode, ConstantTreeNode> replacementNodes;35 private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> replacementNodes; 36 36 private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts; 37 37 38 38 public InteractiveSymbolicDataAnalysisSolutionSimplifierView() { 39 39 InitializeComponent(); 40 this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ConstantTreeNode>();40 this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); 41 41 this.nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>(); 42 42 this.Caption = "Interactive Solution Simplifier"; … … 81 81 82 82 private void CalculateReplacementNodesAndNodeImpacts() { 83 replacementNodes.Clear();84 83 if (Content != null && Content.Model != null && Content.ProblemData != null) { 85 84 var tree = Content.Model.SymbolicExpressionTree; 86 85 var replacementValues = CalculateReplacementValues(tree); 87 86 foreach (var pair in replacementValues) { 88 replacementNodes.Add(pair.Key, MakeConstantTreeNode(pair.Value)); 87 if (!(pair.Key is ConstantTreeNode)) { 88 replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value); 89 } 89 90 } 90 91 nodeImpacts = CalculateImpactValues(Content.Model.SymbolicExpressionTree); … … 96 97 var child = parent.GetSubTree(subTreeIndex); 97 98 if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(1.0)) { 98 ReplaceNodeWithConstant(parent, subTreeIndex);99 SwitchNodeWithReplacementNode(parent, subTreeIndex); 99 100 } 100 101 } … … 126 127 ISymbolicExpressionTreeNode subTree = treeNode.GetSubTree(i); 127 128 if (subTree == visualTreeNode.SymbolicExpressionTreeNode) { 128 ReplaceNodeWithConstant(treeNode, i);129 SwitchNodeWithReplacementNode(treeNode, i); 129 130 } 130 131 } … … 138 139 } 139 140 140 private void ReplaceNodeWithConstant(ISymbolicExpressionTreeNode parent, int subTreeIndex) {141 private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) { 141 142 ISymbolicExpressionTreeNode subTree = parent.GetSubTree(subTreeIndex); 142 143 parent.RemoveSubTree(subTreeIndex); 143 if (replacementNodes.ContainsKey(subTree)) 144 parent.InsertSubTree(subTreeIndex, replacementNodes[subTree]); 145 else if (subTree is ConstantTreeNode && replacementNodes.ContainsValue((ConstantTreeNode)subTree)) 146 parent.InsertSubTree(subTreeIndex, replacementNodes.Where(v => v.Value == subTree).Single().Key); 147 else if (!(subTree is ConstantTreeNode)) 144 if (replacementNodes.ContainsKey(subTree)) { 145 var replacementNode = replacementNodes[subTree]; 146 parent.InsertSubTree(subTreeIndex, replacementNode); 147 // exchange key and value 148 replacementNodes.Remove(subTree); 149 replacementNodes.Add(replacementNode, subTree); 150 } else { 148 151 throw new InvalidOperationException("Could not find replacement value."); 152 } 149 153 } 150 154 … … 155 159 foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) { 156 160 if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) { 157 double impact = this.nodeImpacts[treeNode]; 158 double replacementValue = this.replacementNodes[treeNode].Value; 161 double impact = nodeImpacts[treeNode]; 159 162 VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode); 160 163 … … 163 166 // impact > 0 if new solution is worse 164 167 if (impact < 0.0) { 168 // min is guaranteed to be < 0 165 169 visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red); 170 } else if (impact.IsAlmost(0.0)) { 171 visualTree.FillColor = Color.White; 166 172 } else { 173 // max is guaranteed to be > 0 167 174 visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green); 168 175 } 169 176 visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact; 170 visualTree.ToolTip += Environment.NewLine + "Replacement value: " + replacementValue; 177 var constantReplacementNode = replacementNodes[treeNode] as ConstantTreeNode; 178 if (constantReplacementNode != null) { 179 visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value; 180 } 171 181 } 172 182 } … … 177 187 private void PaintCollapsedNodes() { 178 188 foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) { 179 if (treeNode is ConstantTreeNode && replacementNodes.Contains Value((ConstantTreeNode)treeNode))189 if (treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode)) 180 190 this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange; 181 191 else {
Note: See TracChangeset
for help on using the changeset viewer.