Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs @ 5729

Last change on this file since 5729 was 5729, checked in by gkronber, 13 years ago

#1418 moved liner scaling method into symbolic regression model and fixed bug in interactive solution simplifier

File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Windows.Forms;
27using HeuristicLab.Common;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
34  public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
35    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> replacementNodes;
36    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
37
38    public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
39      InitializeComponent();
40      this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
41      this.nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
42      this.Caption = "Interactive Solution Simplifier";
43    }
44
45    public new ISymbolicDataAnalysisSolution Content {
46      get { return (ISymbolicDataAnalysisSolution)base.Content; }
47      set { base.Content = value; }
48    }
49
50    protected override void RegisterContentEvents() {
51      base.RegisterContentEvents();
52      Content.ModelChanged += new EventHandler(Content_ModelChanged);
53      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
54    }
55    protected override void DeregisterContentEvents() {
56      base.DeregisterContentEvents();
57      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
58      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
59    }
60
61    private void Content_ModelChanged(object sender, EventArgs e) {
62      OnModelChanged();
63    }
64    private void Content_ProblemDataChanged(object sender, EventArgs e) {
65      OnProblemDataChanged();
66    }
67
68    protected virtual void OnModelChanged() {
69      this.CalculateReplacementNodesAndNodeImpacts();
70    }
71
72    protected virtual void OnProblemDataChanged() {
73      this.CalculateReplacementNodesAndNodeImpacts();
74    }
75
76    protected override void OnContentChanged() {
77      base.OnContentChanged();
78      this.CalculateReplacementNodesAndNodeImpacts();
79      this.viewHost.Content = this.Content;
80    }
81
82    private void CalculateReplacementNodesAndNodeImpacts() {
83      if (Content != null && Content.Model != null && Content.ProblemData != null) {
84        var tree = Content.Model.SymbolicExpressionTree;
85        var replacementValues = CalculateReplacementValues(tree);
86        foreach (var pair in replacementValues) {
87          if (!(pair.Key is ConstantTreeNode)) {
88            replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
89          }
90        }
91        nodeImpacts = CalculateImpactValues(Content.Model.SymbolicExpressionTree);
92
93        // automatically fold all branches with impact = 1
94        List<ISymbolicExpressionTreeNode> nodeList = Content.Model.SymbolicExpressionTree.Root.GetSubTree(0).IterateNodesPrefix().ToList();
95        foreach (var parent in nodeList) {
96          for (int subTreeIndex = 0; subTreeIndex < parent.SubTrees.Count(); subTreeIndex++) {
97            var child = parent.GetSubTree(subTreeIndex);
98            if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(1.0)) {
99              SwitchNodeWithReplacementNode(parent, subTreeIndex);
100            }
101          }
102        }
103        // show only interesting part of solution
104        this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubTree(0).GetSubTree(0));
105        this.PaintNodeImpacts();
106      }
107    }
108
109    protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
110    protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
111    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
112
113    private ConstantTreeNode MakeConstantTreeNode(double value) {
114      Constant constant = new Constant();
115      constant.MinValue = value - 1;
116      constant.MaxValue = value + 1;
117      ConstantTreeNode constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
118      constantTreeNode.Value = value;
119      return constantTreeNode;
120    }
121
122    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
123      VisualSymbolicExpressionTreeNode visualTreeNode = (VisualSymbolicExpressionTreeNode)sender;
124      var tree = Content.Model.SymbolicExpressionTree;
125      foreach (SymbolicExpressionTreeNode treeNode in tree.IterateNodesPostfix()) {
126        for (int i = 0; i < treeNode.SubTrees.Count(); i++) {
127          ISymbolicExpressionTreeNode subTree = treeNode.GetSubTree(i);
128          if (subTree == visualTreeNode.SymbolicExpressionTreeNode) {
129            SwitchNodeWithReplacementNode(treeNode, i);
130          }
131        }
132      }
133
134      // show only interesting part of solution
135      this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubTree(0).GetSubTree(0));
136
137      UpdateModel(tree);
138      this.PaintNodeImpacts();
139    }
140
141    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
142      ISymbolicExpressionTreeNode subTree = parent.GetSubTree(subTreeIndex);
143      parent.RemoveSubTree(subTreeIndex);
144      if (replacementNodes.ContainsKey(subTree)) {
145        var replacementNode = replacementNodes[subTree];
146        parent.InsertSubTree(subTreeIndex, replacementNode);
147        // exchange key and value
148        replacementNodes.Remove(subTree);
149        replacementNodes.Add(replacementNode, subTree);
150      } else {
151        throw new InvalidOperationException("Could not find replacement value.");
152      }
153    }
154
155    private void PaintNodeImpacts() {
156      var impacts = nodeImpacts.Values;
157      double max = impacts.Max();
158      double min = impacts.Min();
159      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
160        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
161          double impact = nodeImpacts[treeNode];
162          VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
163
164          // impact = 0 if no change
165          // impact < 0 if new solution is better
166          // impact > 0 if new solution is worse
167          if (impact < 0.0) {
168            // min is guaranteed to be < 0
169            visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
170          } else if (impact.IsAlmost(0.0)) {
171            visualTree.FillColor = Color.White;
172          } else {
173            // max is guaranteed to be > 0
174            visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
175          }
176          visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
177          var constantReplacementNode = replacementNodes[treeNode] as ConstantTreeNode;
178          if (constantReplacementNode != null) {
179            visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
180          }
181        }
182      }
183      this.PaintCollapsedNodes();
184      this.treeChart.Repaint();
185    }
186
187    private void PaintCollapsedNodes() {
188      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
189        if (treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode))
190          this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange;
191        else {
192          VisualSymbolicExpressionTreeNode visNode = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
193          if (visNode != null)
194            visNode.LineColor = Color.Black;
195        }
196      }
197    }
198
199    private void btnSimplify_Click(object sender, EventArgs e) {
200      SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
201      var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
202      UpdateModel(simplifiedExpressionTree);
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.