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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Core.Views;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 | using HeuristicLab.Random;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
33 | [View("Symbolic Expression Grammar Sample Tree")]
|
---|
34 | public partial class SymbolicExpressionGrammarSampleExpressionTreeView : NamedItemView {
|
---|
35 | private IRandom random;
|
---|
36 | public SymbolicExpressionGrammarSampleExpressionTreeView() {
|
---|
37 | InitializeComponent();
|
---|
38 | random = new MersenneTwister();
|
---|
39 | maxSampleTreeLength = int.Parse(maxTreeLengthTextBox.Text);
|
---|
40 | maxSampleTreeDepth = int.Parse(maxTreeDepthTextBox.Text);
|
---|
41 | }
|
---|
42 |
|
---|
43 | private int maxSampleTreeLength;
|
---|
44 | public int MaxSampleTreeLength {
|
---|
45 | get { return maxSampleTreeLength; }
|
---|
46 | set {
|
---|
47 | if (maxSampleTreeLength != value) {
|
---|
48 | maxSampleTreeLength = value;
|
---|
49 | UpdateSampleTreeView();
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private int maxSampleTreeDepth;
|
---|
55 | public int MaxSampleTreeDepth {
|
---|
56 | get { return maxSampleTreeDepth; }
|
---|
57 | set {
|
---|
58 | if (maxSampleTreeDepth != value) {
|
---|
59 | maxSampleTreeDepth = value;
|
---|
60 | UpdateSampleTreeView();
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public new ISymbolicExpressionGrammar Content {
|
---|
66 | get { return (ISymbolicExpressionGrammar)base.Content; }
|
---|
67 | set { base.Content = value; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected override void SetEnabledStateOfControls() {
|
---|
71 | base.SetEnabledStateOfControls();
|
---|
72 | maxTreeLengthTextBox.Enabled = Content != null;
|
---|
73 | maxTreeDepthTextBox.Enabled = Content != null;
|
---|
74 | generateSampleTreeButton.Enabled = Content != null;
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override void RegisterContentEvents() {
|
---|
78 | base.RegisterContentEvents();
|
---|
79 | Content.Changed += new EventHandler(Content_Changed);
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected override void DeregisterContentEvents() {
|
---|
83 | Content.Changed -= new EventHandler(Content_Changed);
|
---|
84 | base.DeregisterContentEvents();
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected override void OnContentChanged() {
|
---|
88 | base.OnContentChanged();
|
---|
89 | if (Content == null) sampleTreeView.Content = null;
|
---|
90 | else UpdateSampleTreeView();
|
---|
91 | }
|
---|
92 |
|
---|
93 | private void Content_Changed(object sender, System.EventArgs e) {
|
---|
94 | UpdateSampleTreeView();
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void UpdateSampleTreeView() {
|
---|
98 | try {
|
---|
99 | ISymbolicExpressionTree tree = ProbabilisticTreeCreator.Create(random, Content, MaxSampleTreeLength, MaxSampleTreeDepth);
|
---|
100 | foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
|
---|
101 | node.SetGrammar(null);
|
---|
102 | sampleTreeView.Content = tree;
|
---|
103 | }
|
---|
104 | catch (Exception ex) {
|
---|
105 | sampleTreeView.Content = null;
|
---|
106 | ErrorHandling.ShowErrorDialog(ex);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | #region control events
|
---|
111 | private void generateSampleTreeButton_Click(object sender, EventArgs e) {
|
---|
112 | UpdateSampleTreeView();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void maxTreeLengthTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
116 | int maxTreeLength;
|
---|
117 | if (int.TryParse(maxTreeLengthTextBox.Text, out maxTreeLength) && maxTreeLength > 3) {
|
---|
118 | errorProvider.SetError(maxTreeLengthTextBox, string.Empty);
|
---|
119 | e.Cancel = false;
|
---|
120 | } else {
|
---|
121 | errorProvider.SetError(maxTreeLengthTextBox, "Invalid value: maximum tree length must be larger than 3.");
|
---|
122 | e.Cancel = true;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | private void maxTreeLengthTextBox_Validated(object sender, EventArgs e) {
|
---|
126 | int maxTreeLength;
|
---|
127 | if (int.TryParse(maxTreeLengthTextBox.Text, out maxTreeLength) && maxTreeLength > 3) {
|
---|
128 | MaxSampleTreeLength = maxTreeLength;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | private void maxTreeDepthTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
133 | int maxTreeDepth;
|
---|
134 | if (int.TryParse(maxTreeDepthTextBox.Text, out maxTreeDepth) && maxTreeDepth > 3) {
|
---|
135 | errorProvider.SetError(maxTreeDepthTextBox, string.Empty);
|
---|
136 | e.Cancel = false;
|
---|
137 | } else {
|
---|
138 | errorProvider.SetError(maxTreeDepthTextBox, "Invalid value: maximum tree depth must be larger than 3.");
|
---|
139 | e.Cancel = true;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | private void maxTreeDepthTextBox_Validated(object sender, EventArgs e) {
|
---|
143 | int maxTreeDepth;
|
---|
144 | if (int.TryParse(maxTreeDepthTextBox.Text, out maxTreeDepth) && maxTreeDepth > 3) {
|
---|
145 | MaxSampleTreeDepth = maxTreeDepth;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 | }
|
---|
150 | }
|
---|