Changeset 8388 for branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
- Timestamp:
- 08/02/12 15:05:26 (12 years ago)
- Location:
- branches/HeuristicLab.TreeSimplifier
- Files:
-
- 3 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TreeSimplifier
-
Property
svn:ignore
set to
*.suo
_ReSharper.HeuristicLab.TreeSimplifier
bin
-
Property
svn:ignore
set to
-
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4
- Property svn:ignore
-
old new 1 *.user 1 2 bin 2 3 obj 3 *.user4 HeuristicLabProblemsDataAnalysisSymbolicViewsPlugin.cs5 *.vs10x6 Plugin.cs
-
- Property svn:ignore
-
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r7259 r8388 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 24 25 using System.Drawing; 25 26 using System.Linq; 26 27 using System.Windows.Forms; 27 28 using HeuristicLab.Common; 29 using HeuristicLab.Data; 28 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views; … … 34 36 private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> replacementNodes; 35 37 private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts; 38 private Dictionary<ISymbolicExpressionTreeNode, double> originalValues; 39 private Dictionary<ISymbolicExpressionTreeNode, string> originalVariableNames; 36 40 private bool updateInProgress = false; 41 private ISymbolicExpressionTree model; 37 42 38 43 public InteractiveSymbolicDataAnalysisSolutionSimplifierView() { 39 44 InitializeComponent(); 40 this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); 41 this.nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>(); 42 this.Caption = "Interactive Solution Simplifier"; 45 replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); 46 nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>(); 47 originalValues = new Dictionary<ISymbolicExpressionTreeNode, double>(); 48 originalVariableNames = new Dictionary<ISymbolicExpressionTreeNode, string>(); 49 Caption = "Interactive Solution Simplifier"; 43 50 } 44 51 … … 50 57 protected override void RegisterContentEvents() { 51 58 base.RegisterContentEvents(); 52 Content.ModelChanged += new EventHandler(Content_ModelChanged);53 Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);59 Content.ModelChanged += Content_ModelChanged; 60 Content.ProblemDataChanged += Content_ProblemDataChanged; 54 61 } 55 62 protected override void DeregisterContentEvents() { 56 63 base.DeregisterContentEvents(); 57 Content.ModelChanged -= new EventHandler(Content_ModelChanged);58 Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);64 Content.ModelChanged -= Content_ModelChanged; 65 Content.ProblemDataChanged -= Content_ProblemDataChanged; 59 66 } 60 67 … … 67 74 68 75 protected virtual void OnModelChanged() { 69 this.CalculateReplacementNodesAndNodeImpacts(); 76 model = (model == null) ? (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone() : Content.Model.SymbolicExpressionTree; 77 CalculateReplacementNodesAndNodeImpacts(model); 78 PaintModel(); 70 79 } 71 80 72 81 protected virtual void OnProblemDataChanged() { 73 this.CalculateReplacementNodesAndNodeImpacts(); 82 CalculateReplacementNodesAndNodeImpacts(); 83 PaintModel(); 74 84 } 75 85 76 86 protected override void OnContentChanged() { 87 model = (model == null) ? (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone() : Content.Model.SymbolicExpressionTree; 77 88 base.OnContentChanged(); 78 this.CalculateReplacementNodesAndNodeImpacts(); 79 this.viewHost.Content = this.Content; 89 CalculateReplacementNodesAndNodeImpacts(model); 90 PaintModel(); 91 viewHost.Content = this.Content; 80 92 } 81 93 82 94 private void CalculateReplacementNodesAndNodeImpacts() { 83 if (Content != null && Content.Model != null && Content.ProblemData != null) { 84 var tree = Content.Model.SymbolicExpressionTree; 85 var replacementValues = CalculateReplacementValues(tree); 86 foreach (var pair in replacementValues) { 87 if (!(pair.Key is ConstantTreeNode)) { 88 replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value); 89 } 90 } 91 nodeImpacts = CalculateImpactValues(Content.Model.SymbolicExpressionTree); 92 93 if (!updateInProgress) { 94 // automatically fold all branches with impact = 1 95 List<ISymbolicExpressionTreeNode> nodeList = Content.Model.SymbolicExpressionTree.Root.GetSubtree(0).IterateNodesPrefix().ToList(); 96 foreach (var parent in nodeList) { 97 for (int subTreeIndex = 0; subTreeIndex < parent.SubtreeCount; subTreeIndex++) { 98 var child = parent.GetSubtree(subTreeIndex); 99 if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) { 100 SwitchNodeWithReplacementNode(parent, subTreeIndex); 101 } 95 if (Content == null || Content.Model == null || Content.ProblemData == null) return; 96 var tree = model; 97 var replacementValues = CalculateReplacementValues(tree); 98 foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) { 99 replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value); 100 } 101 nodeImpacts = CalculateImpactValues(tree); 102 103 if (!updateInProgress) { 104 // automatically fold all branches with impact = 0 105 List<ISymbolicExpressionTreeNode> nodeList = tree.Root.GetSubtree(0).IterateNodesPrefix().ToList(); 106 foreach (var parent in nodeList) { 107 for (int subTreeIndex = 0; subTreeIndex < parent.SubtreeCount; subTreeIndex++) { 108 var child = parent.GetSubtree(subTreeIndex); 109 if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) { 110 SwitchNodeWithReplacementNode(parent, subTreeIndex); 102 111 } 103 112 } 104 113 } 105 106 // show only interesting part of solution 107 if (tree.Root.SubtreeCount > 1) 108 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs 109 else 110 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB 111 this.PaintNodeImpacts(); 112 } 114 } 115 PaintModel(); 116 } 117 118 private void CalculateReplacementNodesAndNodeImpacts(ISymbolicExpressionTree tree) { 119 var replacementValues = CalculateReplacementValues(tree); 120 foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) { 121 replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value); 122 } 123 nodeImpacts = CalculateImpactValues(tree); 124 125 if (!updateInProgress) { 126 // automatically fold all branches with impact = 0 127 List<ISymbolicExpressionTreeNode> nodeList = tree.Root.GetSubtree(0).IterateNodesPrefix().ToList(); 128 foreach (var parent in nodeList) { 129 for (int subTreeIndex = 0; subTreeIndex < parent.SubtreeCount; subTreeIndex++) { 130 var child = parent.GetSubtree(subTreeIndex); 131 if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) { 132 SwitchNodeWithReplacementNode(parent, subTreeIndex); 133 } 134 } 135 } 136 } 137 PaintModel(); 138 } 139 140 private void PaintModel() { 141 // show only interesting part of solution 142 treeChart.Tree = model.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(model.Root) : new SymbolicExpressionTree(model.Root.GetSubtree(0).GetSubtree(0)); 143 PaintNodeImpacts(); 113 144 } 114 145 … … 127 158 128 159 private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) { 129 VisualSymbolicExpressionTreeNode visualTreeNode = (VisualSymbolicExpressionTreeNode)sender; 130 var tree = Content.Model.SymbolicExpressionTree; 160 if (!treeChart.TreeValid) return; 161 var visualNode = (VisualSymbolicExpressionTreeNode)sender; 162 var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.SymbolicExpressionTreeNode; 163 if (symbExprTreeNode == null) return; 164 var tree = model; 165 // check if the node value/weight has been altered 166 // if so, the first double click will return the node to its original value/weight/variable name 167 // the next double click will replace the ConstantNode with the original SymbolicExpressionTreeNode 168 if (originalVariableNames.ContainsKey(symbExprTreeNode)) { 169 var variable = (VariableTreeNode)symbExprTreeNode; 170 variable.VariableName = originalVariableNames[symbExprTreeNode]; 171 originalVariableNames.Remove(variable); 172 updateInProgress = true; 173 UpdateModel(tree); 174 updateInProgress = false; 175 return; 176 } 177 if (originalValues.ContainsKey(symbExprTreeNode)) { 178 double value = originalValues[symbExprTreeNode]; 179 if (symbExprTreeNode.Symbol is Constant) 180 ((ConstantTreeNode)symbExprTreeNode).Value = value; 181 else if (symbExprTreeNode.Symbol is Variable) 182 ((VariableTreeNode)symbExprTreeNode).Weight = value; 183 originalValues.Remove(symbExprTreeNode); 184 updateInProgress = true; 185 UpdateModel(tree); 186 updateInProgress = false; 187 return; 188 } 131 189 foreach (SymbolicExpressionTreeNode treeNode in tree.IterateNodesPostfix()) { 132 190 for (int i = 0; i < treeNode.SubtreeCount; i++) { 133 191 ISymbolicExpressionTreeNode subTree = treeNode.GetSubtree(i); 134 192 // only allow to replace nodes for which a replacement value is known (replacement value for ADF nodes are not available) 135 if (subTree == visualTreeNode.SymbolicExpressionTreeNode && replacementNodes.ContainsKey(subTree)) {193 if (subTree == symbExprTreeNode && replacementNodes.ContainsKey(subTree)) { 136 194 SwitchNodeWithReplacementNode(treeNode, i); 137 138 195 // show only interesting part of solution 139 if (tree.Root.SubtreeCount > 1) 140 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs 141 else 142 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB 143 196 treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); 144 197 updateInProgress = true; 145 198 UpdateModel(tree); … … 149 202 } 150 203 } 204 } 205 206 private void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) { 207 // do stuff 208 } 209 210 private void treeChart_SymbolicExpressionTreeChanged(object sender, EventArgs e) { 211 CalculateReplacementNodesAndNodeImpacts(treeChart.Tree); 212 PaintModel(); 213 } 214 215 private void treeChart_SymbolicExpressionTreeNodeChanged(object sender, EventArgs e) { 216 var dialog = (ValueChangeDialog)sender; 217 bool flag1 = false, flag2 = false; 218 if (dialog.Content is VariableTreeNode) { 219 var variable = (VariableTreeNode)dialog.Content; 220 var weight = double.Parse(dialog.NewValueTextBox.Text); 221 var name = (string)dialog.VariableNameComboBox.SelectedItem; 222 if (!variable.Weight.Equals(weight)) { 223 flag1 = true; 224 originalValues[variable] = variable.Weight; 225 variable.Weight = weight; 226 } 227 if (!variable.VariableName.Equals(name)) { 228 flag2 = true; 229 originalVariableNames[variable] = variable.VariableName; 230 variable.VariableName = name; 231 } 232 } else if (dialog.Content is ConstantTreeNode) { 233 var constant = (ConstantTreeNode)dialog.Content; 234 var value = double.Parse(dialog.NewValueTextBox.Text); 235 if (!constant.Value.Equals(value)) { 236 flag1 = true; 237 originalValues[constant] = constant.Value; 238 constant.Value = value; 239 240 } 241 } 242 if (flag1 || flag2) { 243 CalculateReplacementNodesAndNodeImpacts(); 244 PaintModel(); 245 } 246 } 247 248 private void treeChart_SymbolicExpressionTreeNodeInserted(object sender, EventArgs e) { 151 249 } 152 250 … … 167 265 double max = impacts.Max(); 168 266 double min = impacts.Min(); 169 foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) { 267 foreach (ISymbolicExpressionTreeNode treeNode in model.IterateNodesPostfix()) { 268 VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode); 269 bool flag1 = replacementNodes.ContainsKey(treeNode); 270 bool flag2 = originalValues.ContainsKey(treeNode); 271 bool flag3 = treeNode is ConstantTreeNode; 272 273 if (flag2) // constant or variable node was changed 274 visualTree.ToolTip += Environment.NewLine + "Original value: " + originalValues[treeNode]; 275 else if (flag1 && flag3) // symbol node was folded to a constant 276 visualTree.ToolTip += Environment.NewLine + "Original node: " + replacementNodes[treeNode]; 277 170 278 if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) { 171 279 double impact = nodeImpacts[treeNode]; 172 VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);173 280 174 281 // impact = 0 if no change … … 196 303 197 304 private void PaintCollapsedNodes() { 198 foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {199 if (treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode))200 this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange;201 else{202 VisualSymbolicExpressionTreeNode visNode = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);203 if (visNode != null)204 visNode.LineColor = Color.Black;305 foreach (ISymbolicExpressionTreeNode treeNode in model.IterateNodesPostfix()) { 306 bool flag1 = replacementNodes.ContainsKey(treeNode); 307 bool flag2 = originalValues.ContainsKey(treeNode); 308 if (flag1 && treeNode is ConstantTreeNode) { 309 this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = flag2 ? Color.DarkViolet : Color.DarkOrange; 310 } else if (flag2) { 311 this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DodgerBlue; 205 312 } 206 313 } … … 208 315 209 316 private void btnSimplify_Click(object sender, EventArgs e) { 210 SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();211 var simplifiedExpressionTree = simplifier.Simplify( Content.Model.SymbolicExpressionTree);317 var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier(); 318 var simplifiedExpressionTree = simplifier.Simplify(model); 212 319 UpdateModel(simplifiedExpressionTree); 213 320 } 214 321 215 322 protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e); 323 324 private void btnPrune_Click(object sender, EventArgs e) { 325 btnPrune.Enabled = false; 326 backgroundWorker1.RunWorkerAsync(); 327 } 328 329 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { 330 var worker = sender as BackgroundWorker; 331 PruneTree(worker); 332 } 333 334 private void PruneTree(BackgroundWorker worker) { 335 var tree = model; 336 // get all tree nodes starting from depth 2 (below the root and start nodes) 337 foreach (var node in GetNodesAtDepth(tree, new IntRange(2, tree.Depth))) { 338 if (worker.CancellationPending) 339 break; // pruning cancelled 340 if (!(node.Symbol is Constant) && nodeImpacts.ContainsKey(node) && nodeImpacts[node] < 0.001) { 341 SwitchNodeWithReplacementNode(node.Parent, node.Parent.IndexOfSubtree(node)); 342 nodeImpacts = CalculateImpactValues(tree); 343 // we do not refresh the replacementValues because they won't change when folding nodes with such low impacts (<0.001) 344 } 345 } 346 } 347 348 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { 349 if (e.Cancelled) { 350 // The user canceled the operation. 351 } else if (e.Error != null) { 352 // There was an error during the operation. 353 // Error-handling 354 } else { 355 // The operation completed normally. We can update the model 356 UpdateModel(model); 357 } 358 btnPrune.Enabled = true; 359 } 360 361 #region helpers 362 private static IEnumerable<ISymbolicExpressionTreeNode> GetNodesAtDepth(ISymbolicExpressionTree tree, IntRange depthRange) { 363 var treeDepth = tree.Root.GetDepth(); 364 return from node in tree.Root.IterateNodesPostfix() 365 let depth = treeDepth - node.GetDepth() 366 where depthRange.Start <= depth 367 where depth <= depthRange.End 368 select node; 369 } 370 #endregion 216 371 } 217 372 }
Note: See TracChangeset
for help on using the changeset viewer.