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

Last change on this file since 16823 was 16823, checked in by gkronber, 3 years ago

#2971: also use IDictionary instead of Dictionary in SimplifierView

File size: 15.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 IDictionary<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 variableRanges = (Content.ProblemData as RegressionProblemData).VariableRangesParameter.Value.VariableIntervals;
201
202      var resultIntervals = interpreter.GetSymbolicExpressionTreeIntervals(tree, variableRanges, out intervals);
203      try {
204        await Task.Delay(500, cancellationTokenSource.Token); // wait for progressbar to finish animation
205      } catch (OperationCanceledException) { }
206      var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
207      foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
208        foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
209      }
210      nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
211      progress.Finish();
212      progress.CanBeStopped = false;
213      PaintNodeImpacts();
214    }
215
216    protected virtual Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {
217      var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();
218      foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
219        if (progress.ProgressState == ProgressState.StopRequested) continue;
220        double impactValue, replacementValue, newQualityForImpactsCalculation;
221        impactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
222        double newProgressValue = progress.ProgressValue + 1.0 / (tree.Length - 2);
223        progress.ProgressValue = Math.Min(newProgressValue, 1);
224        impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
225      }
226      return impactAndReplacementValues;
227    }
228
229    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
230
231    protected virtual ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, IProgress progress) {
232      return tree;
233    }
234
235    private static ConstantTreeNode MakeConstantTreeNode(double value) {
236      var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
237      var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
238      constantTreeNode.Value = value;
239      return constantTreeNode;
240    }
241
242    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
243      if (treeState == TreeState.Invalid) return;
244      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
245      if (visualNode.Content == null) { throw new Exception("VisualNode content cannot be null."); }
246      var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content;
247      var tree = Content.Model.SymbolicExpressionTree;
248      var parent = symbExprTreeNode.Parent;
249      int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode);
250      if (changedNodes.ContainsKey(symbExprTreeNode)) {
251        // undo node change
252        parent.RemoveSubtree(indexOfSubtree);
253        var originalNode = changedNodes[symbExprTreeNode];
254        parent.InsertSubtree(indexOfSubtree, originalNode);
255        changedNodes.Remove(symbExprTreeNode);
256      } else if (foldedNodes.ContainsKey(symbExprTreeNode)) {
257        // undo node folding
258        SwitchNodeWithReplacementNode(parent, indexOfSubtree);
259      }
260      UpdateModel(tree);
261    }
262
263    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
264      ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
265      if (foldedNodes.ContainsKey(subTree)) {
266        parent.RemoveSubtree(subTreeIndex);
267        var replacementNode = foldedNodes[subTree];
268        parent.InsertSubtree(subTreeIndex, replacementNode);
269        // exchange key and value
270        foldedNodes.Remove(subTree);
271        foldedNodes.Add(replacementNode, subTree);
272      }
273    }
274
275    private void PaintNodeImpacts() {
276      var impacts = nodeImpacts.Values;
277      double max = impacts.Max();
278      double min = impacts.Min();
279      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
280        VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
281
282        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
283          visualTree.ToolTip = visualTree.Content.ToString();
284          double impact = nodeImpacts[treeNode];
285
286          // impact = 0 if no change
287          // impact < 0 if new solution is better
288          // impact > 0 if new solution is worse
289          if (impact < 0.0) {
290            // min is guaranteed to be < 0
291            visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
292          } else if (impact.IsAlmost(0.0)) {
293            visualTree.FillColor = Color.White;
294          } else {
295            // max is guaranteed to be > 0
296            visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
297          }
298          visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
299          var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
300          if (constantReplacementNode != null) {
301            visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
302          }
303        }
304        if (visualTree != null) {
305          if (intervals.ContainsKey(treeNode))
306            visualTree.ToolTip += String.Format($"{Environment.NewLine}Intervals: [{intervals[treeNode].LowerBound:G5} ... {intervals[treeNode].UpperBound:G5}]");
307          if (changedNodes.ContainsKey(treeNode)) {
308            visualTree.LineColor = Color.DodgerBlue;
309          } else if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
310            visualTree.LineColor = Color.DarkOrange;
311          }
312        }
313      }
314      treeChart.RepaintNodes();
315    }
316
317    private void btnSimplify_Click(object sender, EventArgs e) {
318      var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree);
319      UpdateModel(simplifiedExpressionTree);
320    }
321
322    private async void btnOptimizeConstants_Click(object sender, EventArgs e) {
323      progress.Start("Optimizing Constants ...");
324      var tree = (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone();
325      var newTree = await Task.Run(() => OptimizeConstants(tree, progress));
326      await Task.Delay(500); // wait for progressbar to finish animation
327      UpdateModel(newTree); // UpdateModel calls Progress.Finish (via Content_Changed)
328    }
329  }
330}
Note: See TracBrowser for help on using the repository browser.