Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs @ 6256

Last change on this file since 6256 was 6256, checked in by mkommend, 13 years ago

#1478: Added optimize button in symbolic regression simplifier view.

File size: 9.7 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;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
33  public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
34    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> replacementNodes;
35    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
36    private bool updateInProgress = false;
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        if (!updateInProgress) {
94          // automatically fold all branches with impact = 1
95          List<ISymbolicExpressionTreeNode> nodeList = Content.Model.SymbolicExpressionTree.Root.GetSubtree(0).IterateNodesPrefix().ToList();
96          foreach (var parent in nodeList) {
97            for (int subTreeIndex = 0; subTreeIndex < parent.SubtreesCount; subTreeIndex++) {
98              var child = parent.GetSubtree(subTreeIndex);
99              if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) {
100                SwitchNodeWithReplacementNode(parent, subTreeIndex);
101              }
102            }
103          }
104        }
105
106        // show only interesting part of solution
107        if (tree.Root.SubtreesCount > 1)
108          this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs
109        else
110          this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB
111        this.PaintNodeImpacts();
112      }
113    }
114
115    protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
116    protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
117    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
118
119    private ConstantTreeNode MakeConstantTreeNode(double value) {
120      Constant constant = new Constant();
121      constant.MinValue = value - 1;
122      constant.MaxValue = value + 1;
123      ConstantTreeNode constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
124      constantTreeNode.Value = value;
125      return constantTreeNode;
126    }
127
128    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
129      VisualSymbolicExpressionTreeNode visualTreeNode = (VisualSymbolicExpressionTreeNode)sender;
130      var tree = Content.Model.SymbolicExpressionTree;
131      foreach (SymbolicExpressionTreeNode treeNode in tree.IterateNodesPostfix()) {
132        for (int i = 0; i < treeNode.SubtreesCount; i++) {
133          ISymbolicExpressionTreeNode subTree = treeNode.GetSubtree(i);
134          // only allow to replace nodes for which a replacement value is known (replacement value for ADF nodes are not available)
135          if (subTree == visualTreeNode.SymbolicExpressionTreeNode && replacementNodes.ContainsKey(subTree)) {
136            SwitchNodeWithReplacementNode(treeNode, i);
137
138            // show only interesting part of solution
139            if (tree.Root.SubtreesCount > 1)
140              this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs
141            else
142              this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB
143
144            updateInProgress = true;
145            UpdateModel(tree);
146            updateInProgress = false;
147            return; // break all loops
148          }
149        }
150      }
151    }
152
153    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
154      ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
155      parent.RemoveSubtree(subTreeIndex);
156      if (replacementNodes.ContainsKey(subTree)) {
157        var replacementNode = replacementNodes[subTree];
158        parent.InsertSubtree(subTreeIndex, replacementNode);
159        // exchange key and value
160        replacementNodes.Remove(subTree);
161        replacementNodes.Add(replacementNode, subTree);
162      }
163    }
164
165    private void PaintNodeImpacts() {
166      var impacts = nodeImpacts.Values;
167      double max = impacts.Max();
168      double min = impacts.Min();
169      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
170        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
171          double impact = nodeImpacts[treeNode];
172          VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
173
174          // impact = 0 if no change
175          // impact < 0 if new solution is better
176          // impact > 0 if new solution is worse
177          if (impact < 0.0) {
178            // min is guaranteed to be < 0
179            visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
180          } else if (impact.IsAlmost(0.0)) {
181            visualTree.FillColor = Color.White;
182          } else {
183            // max is guaranteed to be > 0
184            visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
185          }
186          visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
187          var constantReplacementNode = replacementNodes[treeNode] as ConstantTreeNode;
188          if (constantReplacementNode != null) {
189            visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
190          }
191        }
192      }
193      this.PaintCollapsedNodes();
194      this.treeChart.Repaint();
195    }
196
197    private void PaintCollapsedNodes() {
198      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
199        if (treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode))
200          this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange;
201        else {
202          VisualSymbolicExpressionTreeNode visNode = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
203          if (visNode != null)
204            visNode.LineColor = Color.Black;
205        }
206      }
207    }
208
209    private void btnSimplify_Click(object sender, EventArgs e) {
210      SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
211      var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
212      UpdateModel(simplifiedExpressionTree);
213    }
214
215    protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
216  }
217}
Note: See TracBrowser for help on using the repository browser.