Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9456 was 9456, checked in by swagner, 11 years ago

Updated copyright year and added some missing license headers (#1889)

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