Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs @ 15321

Last change on this file since 15321 was 15321, checked in by pfleck, 7 years ago

#1666

  • Moved CalculateImpactAndReplacementValues into base-class.
  • Use SymRegImpactCalculator for SymTimeSeriesPrognosis.
  • Unified CalculateImpactAndReplacementValues for all SimplifierViews.
File size: 13.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Threading.Tasks;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
35  public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
36    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes;
37    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> changedNodes;
38    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
39
40    protected IProgress Progress = new Progress();
41
42    private enum TreeState { Valid, Invalid }
43    private TreeState treeState;
44
45    protected InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
46      InitializeComponent();
47      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
48      changedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
49      nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
50      this.Caption = "Interactive Solution Simplifier";
51
52      // initialize the tree modifier that will be used to perform edit operations over the tree
53      treeChart.ModifyTree = Modify;
54    }
55
56    /// <summary>
57    /// Remove, Replace or Insert subtrees
58    /// </summary>
59    /// <param name="tree">The symbolic expression tree</param>
60    /// <param name="parent">The insertion point (ie, the parent node who will receive a new child)</param>
61    /// <param name="oldChild">The subtree to be replaced</param>
62    /// <param name="newChild">The replacement subtree</param>
63    /// <param name="removeSubtree">Flag used to indicate if whole subtrees should be removed (default behavior), or just the subtree root</param>
64    private void Modify(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent,
65      ISymbolicExpressionTreeNode oldChild, ISymbolicExpressionTreeNode newChild, bool removeSubtree = true) {
66      if (oldChild == null && newChild == null)
67        throw new ArgumentNullException("Cannot deduce operation type from the arguments. Please provide non null operands.");
68      if (oldChild == null) {
69        // insertion operation
70        parent.AddSubtree(newChild);
71        newChild.Parent = parent;
72      } else if (newChild == null) {
73        // removal operation
74        parent.RemoveSubtree(parent.IndexOfSubtree(oldChild));
75        if (!removeSubtree) {
76          for (int i = oldChild.SubtreeCount - 1; i >= 0; --i) {
77            var subtree = oldChild.GetSubtree(i);
78            oldChild.RemoveSubtree(i);
79            parent.AddSubtree(subtree);
80          }
81        }
82      } else {
83        // replacement operation
84        var replacementIndex = parent.IndexOfSubtree(oldChild);
85        parent.RemoveSubtree(replacementIndex);
86        parent.InsertSubtree(replacementIndex, newChild);
87        newChild.Parent = parent;
88        if (changedNodes.ContainsKey(oldChild)) {
89          changedNodes.Add(newChild, changedNodes[oldChild]); // so that on double click the original node is restored
90          changedNodes.Remove(oldChild);
91        } else {
92          changedNodes.Add(newChild, oldChild);
93        }
94      }
95      treeState = IsValid(tree) ? TreeState.Valid : TreeState.Invalid;
96      switch (treeState) {
97        case TreeState.Valid:
98          this.grpViewHost.Enabled = true;
99          UpdateModel(Content.Model.SymbolicExpressionTree);
100          break;
101        case TreeState.Invalid:
102          this.grpViewHost.Enabled = false;
103          break;
104      }
105    }
106
107    // the optimizer always assumes 2 children for multiplication and addition nodes
108    // thus, we enforce that the tree stays valid so that the constant optimization won't throw an exception
109    // by returning 2 as the minimum allowed arity for addition and multiplication symbols
110    private readonly Func<ISymbol, int> GetMinArity = symbol => {
111      var min = symbol.MinimumArity;
112      if (symbol is Multiplication || symbol is Division) return Math.Max(2, min);
113      return min;
114    };
115    private bool IsValid(ISymbolicExpressionTree tree) {
116      treeChart.Tree = tree;
117      treeChart.Repaint();
118      // check if all nodes have a legal arity
119      var nodes = tree.IterateNodesPostfix().ToList();
120      bool valid = !nodes.Any(node => node.SubtreeCount < GetMinArity(node.Symbol) || node.SubtreeCount > node.Symbol.MaximumArity);
121
122      if (valid) {
123        // check if all variables are contained in the dataset
124        var variables = new HashSet<string>(Content.ProblemData.Dataset.DoubleVariables);
125        valid = nodes.OfType<VariableTreeNode>().All(x => variables.Contains(x.VariableName));
126      }
127
128      if (valid) {
129        btnOptimizeConstants.Enabled = true;
130        btnSimplify.Enabled = true;
131        treeStatusValue.Visible = false;
132      } else {
133        btnOptimizeConstants.Enabled = false;
134        btnSimplify.Enabled = false;
135        treeStatusValue.Visible = true;
136      }
137      this.Refresh();
138      return valid;
139    }
140
141    public new ISymbolicDataAnalysisSolution Content {
142      get { return (ISymbolicDataAnalysisSolution)base.Content; }
143      set { base.Content = value; }
144    }
145
146    protected override void RegisterContentEvents() {
147      base.RegisterContentEvents();
148      Content.ModelChanged += Content_Changed;
149      Content.ProblemDataChanged += Content_Changed;
150      treeChart.Repainted += treeChart_Repainted;
151      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(grpSimplify, Progress);
152    }
153    protected override void DeregisterContentEvents() {
154      base.DeregisterContentEvents();
155      Content.ModelChanged -= Content_Changed;
156      Content.ProblemDataChanged -= Content_Changed;
157      treeChart.Repainted -= treeChart_Repainted;
158      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(grpSimplify, false);
159    }
160
161    private void Content_Changed(object sender, EventArgs e) {
162      UpdateView();
163    }
164
165    protected override void OnContentChanged() {
166      base.OnContentChanged();
167      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
168      UpdateView();
169      viewHost.Content = this.Content;
170    }
171
172    private void treeChart_Repainted(object sender, EventArgs e) {
173      if (nodeImpacts != null && nodeImpacts.Count > 0)
174        PaintNodeImpacts();
175    }
176
177    private async void UpdateView() {
178      if (Content == null || Content.Model == null || Content.ProblemData == null) return;
179      var tree = Content.Model.SymbolicExpressionTree;
180      treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
181
182      Progress.Start("Calculate Impact and Replacement Values ...", 0);
183      var impactAndReplacementValues = await Task.Run(() => CalculateImpactAndReplacementValues(tree));
184      var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
185      foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
186        foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
187      }
188      nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
189      Progress.Finish();
190      PaintNodeImpacts();
191    }
192
193    protected virtual Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {
194      var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();
195      foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
196        double impactValue, replacementValue, newQualityForImpactsCalculation;
197        ImpactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
198        Progress.ProgressValue += 1.0 / (tree.Length - 2);
199        impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
200      }
201      return impactAndReplacementValues;
202    }
203
204    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
205
206    protected abstract ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator { get; }
207
208    private static ConstantTreeNode MakeConstantTreeNode(double value) {
209      var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
210      var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
211      constantTreeNode.Value = value;
212      return constantTreeNode;
213    }
214
215    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
216      if (treeState == TreeState.Invalid) return;
217      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
218      if (visualNode.Content == null) { throw new Exception("VisualNode content cannot be null."); }
219      var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content;
220      var tree = Content.Model.SymbolicExpressionTree;
221      var parent = symbExprTreeNode.Parent;
222      int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode);
223      if (changedNodes.ContainsKey(symbExprTreeNode)) {
224        // undo node change
225        parent.RemoveSubtree(indexOfSubtree);
226        var originalNode = changedNodes[symbExprTreeNode];
227        parent.InsertSubtree(indexOfSubtree, originalNode);
228        changedNodes.Remove(symbExprTreeNode);
229      } else if (foldedNodes.ContainsKey(symbExprTreeNode)) {
230        // undo node folding
231        SwitchNodeWithReplacementNode(parent, indexOfSubtree);
232      }
233      UpdateModel(tree);
234    }
235
236    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
237      ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
238      if (foldedNodes.ContainsKey(subTree)) {
239        parent.RemoveSubtree(subTreeIndex);
240        var replacementNode = foldedNodes[subTree];
241        parent.InsertSubtree(subTreeIndex, replacementNode);
242        // exchange key and value
243        foldedNodes.Remove(subTree);
244        foldedNodes.Add(replacementNode, subTree);
245      }
246    }
247
248    private void PaintNodeImpacts() {
249      var impacts = nodeImpacts.Values;
250      double max = impacts.Max();
251      double min = impacts.Min();
252      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
253        VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
254
255        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
256          visualTree.ToolTip = visualTree.Content.ToString();
257          double impact = nodeImpacts[treeNode];
258
259          // impact = 0 if no change
260          // impact < 0 if new solution is better
261          // impact > 0 if new solution is worse
262          if (impact < 0.0) {
263            // min is guaranteed to be < 0
264            visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
265          } else if (impact.IsAlmost(0.0)) {
266            visualTree.FillColor = Color.White;
267          } else {
268            // max is guaranteed to be > 0
269            visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
270          }
271          visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
272          var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
273          if (constantReplacementNode != null) {
274            visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
275          }
276        }
277        if (visualTree != null)
278          if (changedNodes.ContainsKey(treeNode)) {
279            visualTree.LineColor = Color.DodgerBlue;
280          } else if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
281            visualTree.LineColor = Color.DarkOrange;
282          }
283      }
284      treeChart.RepaintNodes();
285    }
286
287    private void btnSimplify_Click(object sender, EventArgs e) {
288      var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree);
289      UpdateModel(simplifiedExpressionTree);
290    }
291
292    protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
293  }
294}
Note: See TracBrowser for help on using the repository browser.