Changeset 10798
- Timestamp:
- 05/06/14 12:19:07 (11 years ago)
- Location:
- branches/HeuristicLab.InteractiveTreeSimplifier
- Files:
-
- 3 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.InteractiveTreeSimplifier/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.Designer.cs
r10545 r10798 55 55 this.btnSimplify = new System.Windows.Forms.Button(); 56 56 this.treeStatusLabel = new System.Windows.Forms.Label(); 57 this.treeChart = new HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views.SymbolicExpressionTreeChart();57 this.treeChart = new InteractiveSymbolicExpressionTreeChart(); 58 58 this.grpViewHost = new System.Windows.Forms.GroupBox(); 59 59 ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit(); … … 230 230 #endregion 231 231 232 private HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views.SymbolicExpressionTreeChart treeChart;232 private InteractiveSymbolicExpressionTreeChart treeChart; 233 233 private System.Windows.Forms.SplitContainer splitContainer; 234 234 private HeuristicLab.MainForm.WindowsForms.ViewHost viewHost; -
branches/HeuristicLab.InteractiveTreeSimplifier/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r10564 r10798 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 3Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 33 33 public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView { 34 34 private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes; 35 private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> changedNodes; 35 36 private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts; 36 37 private enum TreeState { Valid, Invalid } 38 private TreeState treeState; 37 39 38 40 public InteractiveSymbolicDataAnalysisSolutionSimplifierView() { 39 41 InitializeComponent(); 40 42 foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); 43 changedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); 41 44 nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>(); 42 45 this.Caption = "Interactive Solution Simplifier"; 46 47 // initialize the tree modifier that will be used to perform edit operations over the tree 48 treeChart.ModifyTree = Modify; 49 } 50 51 /// <summary> 52 /// Remove, Replace or Insert subtrees 53 /// </summary> 54 /// <param name="tree">The symbolic expression tree</param> 55 /// <param name="parent">The insertion point (ie, the parent node who will receive a new child)</param> 56 /// <param name="oldChild">The subtree to be replaced</param> 57 /// <param name="newChild">The replacement subtree</param> 58 /// <param name="removeSubtree">Flag used to indicate if whole subtrees should be removed (default behavior), or just the subtree root</param> 59 private void Modify(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode oldChild, ISymbolicExpressionTreeNode newChild, bool removeSubtree = true) { 60 if (oldChild == null && newChild == null) throw new ArgumentNullException("Cannot deduce operation type from the arguments. Please provide non null operands."); 61 if (oldChild == null) { // insertion operation 62 parent.AddSubtree(newChild); 63 newChild.Parent = parent; 64 } else if (newChild == null) { // removal operation 65 parent.RemoveSubtree(parent.IndexOfSubtree(oldChild)); 66 changedNodes.Remove(oldChild); 67 foldedNodes.Remove(oldChild); 68 if (removeSubtree) { 69 foreach (var subtree in oldChild.IterateNodesPrefix()) { 70 changedNodes.Remove(subtree); 71 foldedNodes.Remove(subtree); 72 } 73 } else { 74 for (int i = oldChild.SubtreeCount - 1; i >= 0; --i) { 75 var subtree = oldChild.GetSubtree(i); 76 oldChild.RemoveSubtree(i); 77 parent.AddSubtree(subtree); 78 } 79 } 80 } else { // replacement operation 81 var replacementIndex = parent.IndexOfSubtree(oldChild); 82 parent.RemoveSubtree(replacementIndex); 83 parent.InsertSubtree(replacementIndex, newChild); 84 newChild.Parent = parent; 85 if (changedNodes.ContainsKey(oldChild)) { 86 changedNodes.Add(newChild, changedNodes[oldChild]); // so that on double click the original node is restored 87 changedNodes.Remove(oldChild); 88 } else { 89 changedNodes.Add(newChild, oldChild); 90 } 91 } 92 treeState = IsValid(tree) ? TreeState.Valid : TreeState.Invalid; 93 if (treeState == TreeState.Valid) UpdateModel(Content.Model.SymbolicExpressionTree); 94 } 95 96 private bool IsValid(ISymbolicExpressionTree tree) { 97 treeChart.Tree = tree; 98 treeChart.Repaint(); 99 bool valid = !tree.IterateNodesPostfix().Any(node => node.SubtreeCount < node.Symbol.MinimumArity || node.SubtreeCount > node.Symbol.MaximumArity); 100 if (valid) { 101 btnOptimizeConstants.Enabled = true; 102 btnSimplify.Enabled = true; 103 treeStatusValue.Text = "Valid"; 104 treeStatusValue.ForeColor = Color.Green; 105 } else { 106 btnOptimizeConstants.Enabled = false; 107 btnSimplify.Enabled = false; 108 treeStatusValue.Text = "Invalid"; 109 treeStatusValue.ForeColor = Color.Red; 110 } 111 this.Refresh(); 112 return valid; 43 113 } 44 114 … … 83 153 84 154 var impactAndReplacementValues = CalculateImpactAndReplacementValues(tree); 85 nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);86 155 var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2); 87 156 foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) { 88 157 foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value); 89 158 } 159 nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1); 90 160 PaintNodeImpacts(); 91 161 } … … 104 174 105 175 private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) { 176 if (treeState == TreeState.Invalid) return; 106 177 var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender; 178 if (visualNode.Content == null) { throw new Exception("VisualNode content cannot be null."); } 107 179 var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content; 108 if (symbExprTreeNode == null) return;109 180 var tree = Content.Model.SymbolicExpressionTree; 110 181 var parent = symbExprTreeNode.Parent; 111 182 int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode); 112 if (foldedNodes.ContainsKey(symbExprTreeNode)) { 183 if (changedNodes.ContainsKey(symbExprTreeNode)) { 184 // undo node change 185 parent.RemoveSubtree(indexOfSubtree); 186 var originalNode = changedNodes[symbExprTreeNode]; 187 parent.InsertSubtree(indexOfSubtree, originalNode); 188 changedNodes.Remove(symbExprTreeNode); 189 } else if (foldedNodes.ContainsKey(symbExprTreeNode)) { 113 190 // undo node folding 114 191 SwitchNodeWithReplacementNode(parent, indexOfSubtree); … … 119 196 private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) { 120 197 ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex); 121 parent.RemoveSubtree(subTreeIndex);122 198 if (foldedNodes.ContainsKey(subTree)) { 199 parent.RemoveSubtree(subTreeIndex); 123 200 var replacementNode = foldedNodes[subTree]; 124 201 parent.InsertSubtree(subTreeIndex, replacementNode); … … 133 210 double max = impacts.Max(); 134 211 double min = impacts.Min(); 135 foreach ( vartreeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {212 foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) { 136 213 VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode); 137 214 … … 158 235 } 159 236 if (visualTree != null) 160 if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) { 237 if (changedNodes.ContainsKey(treeNode)) { 238 visualTree.LineColor = Color.DodgerBlue; 239 } else if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) { 161 240 visualTree.LineColor = Color.DarkOrange; 162 241 } -
branches/HeuristicLab.InteractiveTreeSimplifier/3.4/InteractiveSymbolicExpressionTreeChart.cs
r10561 r10798 77 77 pasteToolStripMenuItem.Enabled = tempNode != null && insertNodeToolStripMenuItem.Enabled 78 78 && !(lastOp == EditOp.CutSubtree 79 79 && tempNode.IterateNodesBreadth().Contains(currSelected.Content)); 80 80 } 81 81 }
Note: See TracChangeset
for help on using the changeset viewer.