Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs @ 18075

Last change on this file since 18075 was 18075, checked in by dpiringe, 2 years ago

#3136

  • added a hidden interpreter parameter for StructuredSymbolicRegressionSingleObjectiveProblem
  • fixed a bug which crashed the application by changing ProblemData with different variables
  • fixed a bug which crashed the application by running the problem with an empty StructureTemplate
  • added a better output of exceptions of type AggregateException
  • added and resize event handler to repaint nodes of type SubFunctionTreeNode
  • code cleanup
File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Text;
6using System.Windows.Forms;
7using HeuristicLab.Collections;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
11using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
12using HeuristicLab.MainForm;
13using HeuristicLab.MainForm.WindowsForms;
14
15namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
16
17  [View("StructureTemplate View")]
18  [Content(typeof(StructureTemplate), true)]
19  public partial class StructureTemplateView : AsynchronousContentView {
20    public new StructureTemplate Content {
21      get => (StructureTemplate)base.Content;
22      set => base.Content = value;
23    }
24
25    public StructureTemplateView() {
26      InitializeComponent();
27      infoLabel.Text = "";
28      this.Resize += StructureTemplateViewResize;
29      treeChart.SymbolicExpressionTreeNodeClicked += SymbolicExpressionTreeNodeClicked;
30     
31    }
32
33    private void StructureTemplateViewResize(object sender, EventArgs e) {
34      PaintTree();
35    }
36
37    private void SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
38      var visualTreeNode = sender as VisualTreeNode<ISymbolicExpressionTreeNode>;
39      if(visualTreeNode != null) {
40        var subFunctionTreeNode = visualTreeNode.Content as SubFunctionTreeNode;
41        if(subFunctionTreeNode != null && Content.SubFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction subFunction))
42          viewHost.Content = subFunction;
43      }
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      if (Content == null) return;
49      expressionInput.Text = Content.Template;
50      linearScalingCheckBox.Checked = Content.ApplyLinearScaling;
51      PaintTree();
52      infoLabel.Text = "";
53    }
54
55    private void ParseButtonClick(object sender, EventArgs e) {
56      Parse();
57    }
58
59    private void ExpressionInputTextChanged(object sender, EventArgs e) {
60      infoLabel.Text = "Unparsed changes! Press parse button to save changes.";
61      infoLabel.ForeColor = Color.DarkOrange;
62    }
63
64    private void PaintTree() {
65      if(Content != null && Content.Tree != null) {
66        treeChart.Tree = Content.Tree;
67        foreach (var n in Content.Tree.IterateNodesPrefix()) {
68          if (n.Symbol is SubFunctionSymbol) {
69            var visualNode = treeChart.GetVisualSymbolicExpressionTreeNode(n);
70            visualNode.FillColor = Color.LightCyan;
71            visualNode.LineColor = Color.SlateGray;
72          }
73        }
74        treeChart.RepaintNodes();
75      }
76    }
77
78    private void ExpressionInputKeyUp(object sender, KeyEventArgs e) {
79      if (e.KeyCode == Keys.Enter)
80        Parse();
81    }
82
83    private void Parse() {
84      viewHost.Content = null; // reset active detail view
85      if (!string.IsNullOrEmpty(expressionInput.Text)) {
86        try {
87          Content.Template = expressionInput.Text;
88          PaintTree();
89          infoLabel.Text = "Template structure successfully parsed.";
90          infoLabel.ForeColor = Color.DarkGreen;
91        } catch (AggregateException ex) {
92          infoLabel.Text = string.Join("\n", ex.InnerExceptions.Select(x => x.Message));
93          infoLabel.ForeColor = Color.DarkRed;
94        } catch (Exception ex) {
95          infoLabel.Text = ex.Message;
96          infoLabel.ForeColor = Color.DarkRed;
97        }
98      }
99    }
100
101    private void LinearScalingCheckBoxCheckStateChanged(object sender, EventArgs e) {
102      Content.ApplyLinearScaling = linearScalingCheckBox.Checked;
103      PaintTree();
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.