Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16586 was 16586, checked in by chaider, 5 years ago

#2971
Added IntervalConstraint Parameter and some fixes

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