Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8587 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 9.7 KB
RevLine 
[3915]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3915]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;
[4068]29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
[3915]30using HeuristicLab.MainForm.WindowsForms;
31
[5699]32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
33  public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
[5729]34    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> replacementNodes;
[5699]35    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
[6113]36    private bool updateInProgress = false;
[3915]37
[5699]38    public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
[3915]39      InitializeComponent();
[5729]40      this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
[5699]41      this.nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
[3915]42      this.Caption = "Interactive Solution Simplifier";
43    }
44
[5699]45    public new ISymbolicDataAnalysisSolution Content {
46      get { return (ISymbolicDataAnalysisSolution)base.Content; }
[3915]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) {
[5699]62      OnModelChanged();
[3915]63    }
64    private void Content_ProblemDataChanged(object sender, EventArgs e) {
[5699]65      OnProblemDataChanged();
66    }
67
68    protected virtual void OnModelChanged() {
[3915]69      this.CalculateReplacementNodesAndNodeImpacts();
70    }
71
[5699]72    protected virtual void OnProblemDataChanged() {
73      this.CalculateReplacementNodesAndNodeImpacts();
74    }
75
[3915]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) {
[5722]84        var tree = Content.Model.SymbolicExpressionTree;
85        var replacementValues = CalculateReplacementValues(tree);
[5699]86        foreach (var pair in replacementValues) {
[5729]87          if (!(pair.Key is ConstantTreeNode)) {
88            replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
89          }
[5699]90        }
[5722]91        nodeImpacts = CalculateImpactValues(Content.Model.SymbolicExpressionTree);
[3915]92
[6113]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) {
[6803]97            for (int subTreeIndex = 0; subTreeIndex < parent.SubtreeCount; subTreeIndex++) {
[6113]98              var child = parent.GetSubtree(subTreeIndex);
99              if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) {
100                SwitchNodeWithReplacementNode(parent, subTreeIndex);
101              }
[5455]102            }
103          }
104        }
[6113]105
[5993]106        // show only interesting part of solution
[6803]107        if (tree.Root.SubtreeCount > 1)
[5993]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
[3915]111        this.PaintNodeImpacts();
112      }
113    }
114
[5717]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);
[3915]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;
[5722]130      var tree = Content.Model.SymbolicExpressionTree;
131      foreach (SymbolicExpressionTreeNode treeNode in tree.IterateNodesPostfix()) {
[6803]132        for (int i = 0; i < treeNode.SubtreeCount; i++) {
[5736]133          ISymbolicExpressionTreeNode subTree = treeNode.GetSubtree(i);
[5993]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)) {
[5729]136            SwitchNodeWithReplacementNode(treeNode, i);
[5993]137
138            // show only interesting part of solution
[6803]139            if (tree.Root.SubtreeCount > 1)
[5993]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
[6108]143
[6113]144            updateInProgress = true;
[6108]145            UpdateModel(tree);
[6113]146            updateInProgress = false;
[5993]147            return; // break all loops
[3915]148          }
149        }
150      }
151    }
152
[5729]153    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
[5736]154      ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
155      parent.RemoveSubtree(subTreeIndex);
[5729]156      if (replacementNodes.ContainsKey(subTree)) {
157        var replacementNode = replacementNodes[subTree];
[5736]158        parent.InsertSubtree(subTreeIndex, replacementNode);
[5729]159        // exchange key and value
160        replacementNodes.Remove(subTree);
161        replacementNodes.Add(replacementNode, subTree);
162      }
[5455]163    }
164
[3915]165    private void PaintNodeImpacts() {
166      var impacts = nodeImpacts.Values;
167      double max = impacts.Max();
168      double min = impacts.Min();
[5722]169      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
[4477]170        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
[5729]171          double impact = nodeImpacts[treeNode];
[3915]172          VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
[5722]173
[5717]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) {
[5729]178            // min is guaranteed to be < 0
[5717]179            visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
[5729]180          } else if (impact.IsAlmost(0.0)) {
181            visualTree.FillColor = Color.White;
[5717]182          } else {
[5729]183            // max is guaranteed to be > 0
[5717]184            visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
185          }
[3915]186          visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
[5729]187          var constantReplacementNode = replacementNodes[treeNode] as ConstantTreeNode;
188          if (constantReplacementNode != null) {
189            visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
190          }
[6108]191        }
[3915]192      }
193      this.PaintCollapsedNodes();
194      this.treeChart.Repaint();
195    }
196
197    private void PaintCollapsedNodes() {
[5722]198      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
[5729]199        if (treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode))
[3915]200          this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange;
[4477]201        else {
202          VisualSymbolicExpressionTreeNode visNode = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
203          if (visNode != null)
204            visNode.LineColor = Color.Black;
205        }
[3915]206      }
207    }
[3927]208
209    private void btnSimplify_Click(object sender, EventArgs e) {
[5722]210      SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
211      var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
212      UpdateModel(simplifiedExpressionTree);
[3927]213    }
[6256]214
215    protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
[3915]216  }
217}
Note: See TracBrowser for help on using the repository browser.