Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9716 was 9478, checked in by swagner, 12 years ago

Removed unnecessary local variable (#1890)

File size: 7.2 KB
RevLine 
[3915]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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 {
[9006]34    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes;
[5699]35    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
[9006]36    private enum TreeState { Valid, Invalid }
[3915]37
[5699]38    public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
[3915]39      InitializeComponent();
[9006]40      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
[8946]41      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();
[8946]52      Content.ModelChanged += Content_Changed;
53      Content.ProblemDataChanged += Content_Changed;
[3915]54    }
55    protected override void DeregisterContentEvents() {
56      base.DeregisterContentEvents();
[8946]57      Content.ModelChanged -= Content_Changed;
58      Content.ProblemDataChanged -= Content_Changed;
[3915]59    }
60
[8946]61    private void Content_Changed(object sender, EventArgs e) {
62      UpdateView();
[3915]63    }
[5699]64
[3915]65    protected override void OnContentChanged() {
66      base.OnContentChanged();
[9006]67      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
[8946]68      UpdateView();
69      viewHost.Content = this.Content;
[3915]70    }
71
[8946]72    private void UpdateView() {
73      if (Content == null || Content.Model == null || Content.ProblemData == null) return;
74      var tree = Content.Model.SymbolicExpressionTree;
[8990]75      treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
[3915]76
[8946]77      var replacementValues = CalculateReplacementValues(tree);
78      foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
[9006]79        foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
[8946]80      }
[6113]81
[8946]82      nodeImpacts = CalculateImpactValues(tree);
83      PaintNodeImpacts();
[3915]84    }
85
[5717]86    protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
87    protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
88    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
[3915]89
[8946]90    private static ConstantTreeNode MakeConstantTreeNode(double value) {
91      var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
92      var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
[3915]93      constantTreeNode.Value = value;
94      return constantTreeNode;
95    }
96
97    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
[8946]98      var visualNode = (VisualSymbolicExpressionTreeNode)sender;
99      var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.SymbolicExpressionTreeNode;
100      if (symbExprTreeNode == null) return;
[5722]101      var tree = Content.Model.SymbolicExpressionTree;
[9006]102      var parent = symbExprTreeNode.Parent;
103      int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode);
[9445]104      if (foldedNodes.ContainsKey(symbExprTreeNode)) {
[9043]105        // undo node folding
[9006]106        SwitchNodeWithReplacementNode(parent, indexOfSubtree);
107      }
[9055]108      UpdateModel(tree);
[3915]109    }
110
[5729]111    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
[5736]112      ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
113      parent.RemoveSubtree(subTreeIndex);
[9006]114      if (foldedNodes.ContainsKey(subTree)) {
115        var replacementNode = foldedNodes[subTree];
[5736]116        parent.InsertSubtree(subTreeIndex, replacementNode);
[5729]117        // exchange key and value
[9006]118        foldedNodes.Remove(subTree);
119        foldedNodes.Add(replacementNode, subTree);
[5729]120      }
[5455]121    }
122
[3915]123    private void PaintNodeImpacts() {
124      var impacts = nodeImpacts.Values;
125      double max = impacts.Max();
126      double min = impacts.Min();
[5722]127      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
[8946]128        VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
129
[4477]130        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
[5729]131          double impact = nodeImpacts[treeNode];
[5722]132
[5717]133          // impact = 0 if no change
134          // impact < 0 if new solution is better
135          // impact > 0 if new solution is worse
136          if (impact < 0.0) {
[5729]137            // min is guaranteed to be < 0
[5717]138            visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
[5729]139          } else if (impact.IsAlmost(0.0)) {
140            visualTree.FillColor = Color.White;
[5717]141          } else {
[5729]142            // max is guaranteed to be > 0
[5717]143            visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
144          }
[3915]145          visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
[9006]146          var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
[5729]147          if (constantReplacementNode != null) {
148            visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
149          }
[6108]150        }
[9043]151        if (visualTree != null)
[9445]152          if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
[9043]153            visualTree.LineColor = Color.DarkOrange;
154          }
[3915]155      }
[8980]156      treeChart.RepaintNodes();
[3915]157    }
[3927]158
159    private void btnSimplify_Click(object sender, EventArgs e) {
[8946]160      var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
[5722]161      var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
162      UpdateModel(simplifiedExpressionTree);
[3927]163    }
[6256]164
165    protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
[3915]166  }
167}
Note: See TracBrowser for help on using the repository browser.