Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/06/14 12:19:07 (11 years ago)
Author:
bburlacu
Message:

#1763: Branched tree edit functionality into /branches/HeuristicLab.InteractiveTreeSimplifier

Location:
branches/HeuristicLab.InteractiveTreeSimplifier
Files:
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.InteractiveTreeSimplifier/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.Designer.cs

    r10545 r10798  
    5555      this.btnSimplify = new System.Windows.Forms.Button();
    5656      this.treeStatusLabel = new System.Windows.Forms.Label();
    57       this.treeChart = new HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views.SymbolicExpressionTreeChart();
     57      this.treeChart = new InteractiveSymbolicExpressionTreeChart();
    5858      this.grpViewHost = new System.Windows.Forms.GroupBox();
    5959      ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit();
     
    230230    #endregion
    231231
    232     private HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views.SymbolicExpressionTreeChart treeChart;
     232    private InteractiveSymbolicExpressionTreeChart treeChart;
    233233    private System.Windows.Forms.SplitContainer splitContainer;
    234234    private HeuristicLab.MainForm.WindowsForms.ViewHost viewHost;
  • branches/HeuristicLab.InteractiveTreeSimplifier/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r10564 r10798  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3333  public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
    3434    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes;
     35    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> changedNodes;
    3536    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
    3637    private enum TreeState { Valid, Invalid }
     38    private TreeState treeState;
    3739
    3840    public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
    3941      InitializeComponent();
    4042      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
     43      changedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
    4144      nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
    4245      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;
    43113    }
    44114
     
    83153
    84154      var impactAndReplacementValues = CalculateImpactAndReplacementValues(tree);
    85       nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
    86155      var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
    87156      foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
    88157        foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
    89158      }
     159      nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
    90160      PaintNodeImpacts();
    91161    }
     
    104174
    105175    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
     176      if (treeState == TreeState.Invalid) return;
    106177      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
     178      if (visualNode.Content == null) { throw new Exception("VisualNode content cannot be null."); }
    107179      var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content;
    108       if (symbExprTreeNode == null) return;
    109180      var tree = Content.Model.SymbolicExpressionTree;
    110181      var parent = symbExprTreeNode.Parent;
    111182      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)) {
    113190        // undo node folding
    114191        SwitchNodeWithReplacementNode(parent, indexOfSubtree);
     
    119196    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
    120197      ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
    121       parent.RemoveSubtree(subTreeIndex);
    122198      if (foldedNodes.ContainsKey(subTree)) {
     199        parent.RemoveSubtree(subTreeIndex);
    123200        var replacementNode = foldedNodes[subTree];
    124201        parent.InsertSubtree(subTreeIndex, replacementNode);
     
    133210      double max = impacts.Max();
    134211      double min = impacts.Min();
    135       foreach (var treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
     212      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
    136213        VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
    137214
     
    158235        }
    159236        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)) {
    161240            visualTree.LineColor = Color.DarkOrange;
    162241          }
  • branches/HeuristicLab.InteractiveTreeSimplifier/3.4/InteractiveSymbolicExpressionTreeChart.cs

    r10561 r10798  
    7777        pasteToolStripMenuItem.Enabled = tempNode != null && insertNodeToolStripMenuItem.Enabled
    7878                                                          && !(lastOp == EditOp.CutSubtree
    79                                                                && tempNode.IterateNodesBreadth().Contains(currSelected.Content));
     79                                                          && tempNode.IterateNodesBreadth().Contains(currSelected.Content));
    8080      }
    8181    }
Note: See TracChangeset for help on using the changeset viewer.