source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs @ 16581

Last change on this file since 16581 was 16581, checked in by chaider, 6 months ago

#2971

  • Check if treeNode exists for specific intervals in SimplifierView
  • GridView fills space in NamedIntevalsView
File size: 15.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
27using System.Threading.Tasks;
28using System.Windows.Forms;
29using HeuristicLab.Common;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
32using HeuristicLab.MainForm;
33using HeuristicLab.MainForm.WindowsForms;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
36  public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
37    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes;
38    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> changedNodes;
39    private Dictionary<ISymbolicExpressionTreeNode, Interval> intervals;
40    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
41
42    private readonly ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator;
43
44    private readonly Progress progress = new Progress();
45    private CancellationTokenSource cancellationTokenSource;
46
47    private enum TreeState { Valid, Invalid }
48    private TreeState treeState;
49
50    protected InteractiveSymbolicDataAnalysisSolutionSimplifierView(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator) {
51      InitializeComponent();
52      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
53      changedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
54      nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
55      this.Caption = "Interactive Solution Simplifier";
56      this.impactCalculator = impactCalculator;
57
58      // initialize the tree modifier that will be used to perform edit operations over the tree
59      treeChart.ModifyTree = Modify;
60    }
61
62    /// <summary>
63    /// Remove, Replace or Insert subtrees
64    /// </summary>
65    /// <param name="tree">The symbolic expression tree</param>
66    /// <param name="parent">The insertion point (ie, the parent node who will receive a new child)</param>
67    /// <param name="oldChild">The subtree to be replaced</param>
68    /// <param name="newChild">The replacement subtree</param>
69    /// <param name="removeSubtree">Flag used to indicate if whole subtrees should be removed (default behavior), or just the subtree root</param>
70    private void Modify(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent,
71      ISymbolicExpressionTreeNode oldChild, ISymbolicExpressionTreeNode newChild, bool removeSubtree = true) {
72      if (oldChild == null && newChild == null)
73        throw new ArgumentNullException("Cannot deduce operation type from the arguments. Please provide non null operands.");
74      if (oldChild == null) {
75        // insertion operation
76        parent.AddSubtree(newChild);
77        newChild.Parent = parent;
78      } else if (newChild == null) {
79        // removal operation
80        parent.RemoveSubtree(parent.IndexOfSubtree(oldChild));
81        if (!removeSubtree) {
82          for (int i = oldChild.SubtreeCount - 1; i >= 0; --i) {
83            var subtree = oldChild.GetSubtree(i);
84            oldChild.RemoveSubtree(i);
85            parent.AddSubtree(subtree);
86          }
87        }
88      } else {
89        // replacement operation
90        var replacementIndex = parent.IndexOfSubtree(oldChild);
91        parent.RemoveSubtree(replacementIndex);
92        parent.InsertSubtree(replacementIndex, newChild);
93        newChild.Parent = parent;
94        if (changedNodes.ContainsKey(oldChild)) {
95          changedNodes.Add(newChild, changedNodes[oldChild]); // so that on double click the original node is restored
96          changedNodes.Remove(oldChild);
97        } else {
98          changedNodes.Add(newChild, oldChild);
99        }
100      }
101      treeState = IsValid(tree) ? TreeState.Valid : TreeState.Invalid;
102      switch (treeState) {
103        case TreeState.Valid:
104          this.grpViewHost.Enabled = true;
105          UpdateModel(Content.Model.SymbolicExpressionTree);
106          break;
107        case TreeState.Invalid:
108          this.grpViewHost.Enabled = false;
109          break;
110      }
111    }
112
113    // the optimizer always assumes 2 children for multiplication and addition nodes
114    // thus, we enforce that the tree stays valid so that the constant optimization won't throw an exception
115    // by returning 2 as the minimum allowed arity for addition and multiplication symbols
116    private readonly Func<ISymbol, int> GetMinArity = symbol => {
117      var min = symbol.MinimumArity;
118      if (symbol is Multiplication || symbol is Division) return Math.Max(2, min);
119      return min;
120    };
121    private bool IsValid(ISymbolicExpressionTree tree) {
122      treeChart.Tree = tree;
123      treeChart.Repaint();
124      // check if all nodes have a legal arity
125      var nodes = tree.IterateNodesPostfix().ToList();
126      bool valid = !nodes.Any(node => node.SubtreeCount < GetMinArity(node.Symbol) || node.SubtreeCount > node.Symbol.MaximumArity);
127
128      if (valid) {
129        // check if all variables are contained in the dataset
130        var variables = new HashSet<string>(Content.ProblemData.Dataset.DoubleVariables);
131        valid = nodes.OfType<VariableTreeNode>().All(x => variables.Contains(x.VariableName));
132      }
133
134      if (valid) {
135        btnOptimizeConstants.Enabled = true;
136        btnSimplify.Enabled = true;
137        treeStatusValue.Visible = false;
138      } else {
139        btnOptimizeConstants.Enabled = false;
140        btnSimplify.Enabled = false;
141        treeStatusValue.Visible = true;
142      }
143      this.Refresh();
144      return valid;
145    }
146
147    public new ISymbolicDataAnalysisSolution Content {
148      get { return (ISymbolicDataAnalysisSolution)base.Content; }
149      set { base.Content = value; }
150    }
151
152    protected override void RegisterContentEvents() {
153      base.RegisterContentEvents();
154      Content.ModelChanged += Content_Changed;
155      Content.ProblemDataChanged += Content_Changed;
156      treeChart.Repainted += treeChart_Repainted;
157      Progress.ShowOnControl(grpSimplify, progress);
158      progress.StopRequested += progress_StopRequested;
159    }
160    protected override void DeregisterContentEvents() {
161      base.DeregisterContentEvents();
162      Content.ModelChanged -= Content_Changed;
163      Content.ProblemDataChanged -= Content_Changed;
164      treeChart.Repainted -= treeChart_Repainted;
165      Progress.HideFromControl(grpSimplify, false);
166      progress.StopRequested -= progress_StopRequested;
167    }
168
169    private void Content_Changed(object sender, EventArgs e) {
170      UpdateView();
171    }
172
173    protected override void OnContentChanged() {
174      base.OnContentChanged();
175      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
176      UpdateView();
177      viewHost.Content = this.Content;
178    }
179
180    private void treeChart_Repainted(object sender, EventArgs e) {
181      if (nodeImpacts != null && nodeImpacts.Count > 0)
182        PaintNodeImpacts();
183    }
184
185    private void progress_StopRequested(object sender, EventArgs e) {
186      cancellationTokenSource.Cancel();
187    }
188
189    private async void UpdateView() {
190      if (Content == null || Content.Model == null || Content.ProblemData == null) return;
191      var tree = Content.Model.SymbolicExpressionTree;
192      treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
193
194      progress.Start("Calculate Impact and Replacement Values ...");
195      progress.CanBeStopped = true;
196      cancellationTokenSource = new CancellationTokenSource();
197      var interpreter = new IntervalInterpreter();
198
199      var impactAndReplacementValues = await Task.Run(() => CalculateImpactAndReplacementValues(tree));
200      var customIntervals = (Content.ProblemData as RegressionProblemData).VariableRangesParameter.Value;
201      Dictionary<String, Interval> variableRanges = new Dictionary<string, Interval>();
202      foreach (var keyValuePair in customIntervals.VariableIntervals) {
203        variableRanges.Add(keyValuePair.Key, keyValuePair.Value);
204      }
205      var resultIntervals =  interpreter.GetSymbolicExressionTreeIntervals(tree, variableRanges, out intervals);
206      try {
207        await Task.Delay(500, cancellationTokenSource.Token); // wait for progressbar to finish animation
208      } catch (OperationCanceledException) { }
209      var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
210      foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
211        foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
212      }
213      nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
214      progress.Finish();
215      progress.CanBeStopped = false;
216      PaintNodeImpacts();
217    }
218
219    protected virtual Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {
220      var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();
221      foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
222        if (progress.ProgressState == ProgressState.StopRequested) continue;
223        double impactValue, replacementValue, newQualityForImpactsCalculation;
224        impactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
225        double newProgressValue = progress.ProgressValue + 1.0 / (tree.Length - 2);
226        progress.ProgressValue = Math.Min(newProgressValue, 1);
227        impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
228      }
229      return impactAndReplacementValues;
230    }
231
232    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
233
234    protected virtual ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, IProgress progress) {
235      return tree;
236    }
237
238    private static ConstantTreeNode MakeConstantTreeNode(double value) {
239      var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
240      var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
241      constantTreeNode.Value = value;
242      return constantTreeNode;
243    }
244
245    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
246      if (treeState == TreeState.Invalid) return;
247      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
248      if (visualNode.Content == null) { throw new Exception("VisualNode content cannot be null."); }
249      var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content;
250      var tree = Content.Model.SymbolicExpressionTree;
251      var parent = symbExprTreeNode.Parent;
252      int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode);
253      if (changedNodes.ContainsKey(symbExprTreeNode)) {
254        // undo node change
255        parent.RemoveSubtree(indexOfSubtree);
256        var originalNode = changedNodes[symbExprTreeNode];
257        parent.InsertSubtree(indexOfSubtree, originalNode);
258        changedNodes.Remove(symbExprTreeNode);
259      } else if (foldedNodes.ContainsKey(symbExprTreeNode)) {
260        // undo node folding
261        SwitchNodeWithReplacementNode(parent, indexOfSubtree);
262      }
263      UpdateModel(tree);
264    }
265
266    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
267      ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
268      if (foldedNodes.ContainsKey(subTree)) {
269        parent.RemoveSubtree(subTreeIndex);
270        var replacementNode = foldedNodes[subTree];
271        parent.InsertSubtree(subTreeIndex, replacementNode);
272        // exchange key and value
273        foldedNodes.Remove(subTree);
274        foldedNodes.Add(replacementNode, subTree);
275      }
276    }
277
278    private void PaintNodeImpacts() {
279      var impacts = nodeImpacts.Values;
280      double max = impacts.Max();
281      double min = impacts.Min();
282      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
283        VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
284
285        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
286          visualTree.ToolTip = visualTree.Content.ToString();
287          double impact = nodeImpacts[treeNode];
288
289          // impact = 0 if no change
290          // impact < 0 if new solution is better
291          // impact > 0 if new solution is worse
292          if (impact < 0.0) {
293            // min is guaranteed to be < 0
294            visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
295          } else if (impact.IsAlmost(0.0)) {
296            visualTree.FillColor = Color.White;
297          } else {
298            // max is guaranteed to be > 0
299            visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
300          }
301          visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
302          var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
303          if (constantReplacementNode != null) {
304            visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
305          }
306        }
307        if (visualTree != null) {
308          if (intervals.ContainsKey(treeNode))
309            visualTree.ToolTip += String.Format($"{Environment.NewLine}Intervals: [{intervals[treeNode].LowerBound:G5} ... {intervals[treeNode].UpperBound:G5}]");
310          if (changedNodes.ContainsKey(treeNode)) {
311            visualTree.LineColor = Color.DodgerBlue;
312          } else if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
313            visualTree.LineColor = Color.DarkOrange;
314          }
315        }
316      }
317      treeChart.RepaintNodes();
318    }
319
320    private void btnSimplify_Click(object sender, EventArgs e) {
321      var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree);
322      UpdateModel(simplifiedExpressionTree);
323    }
324
325    private async void btnOptimizeConstants_Click(object sender, EventArgs e) {
326      progress.Start("Optimizing Constants ...");
327      var tree = (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone();
328      var newTree = await Task.Run(() => OptimizeConstants(tree, progress));
329      await Task.Delay(500); // wait for progressbar to finish animation
330      UpdateModel(newTree); // UpdateModel calls Progress.Finish (via Content_Changed)
331    }
332  }
333}
Note: See TracBrowser for help on using the repository browser.