Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarSampleExpressionTreeView.cs @ 14429

Last change on this file since 14429 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.ComponentModel;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.Random;
31
32namespace 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      foreach (var treeCreator in ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeCreator>()) {
42        treeCreatorComboBox.Items.Add(treeCreator);
43      }
44      treeCreatorComboBox.SelectedItem = treeCreatorComboBox.Items.OfType<ProbabilisticTreeCreator>().First();
45    }
46
47    private int maxSampleTreeLength;
48    public int MaxSampleTreeLength {
49      get { return maxSampleTreeLength; }
50      set {
51        if (maxSampleTreeLength != value) {
52          maxSampleTreeLength = value;
53          UpdateSampleTreeView();
54        }
55      }
56    }
57
58    private int maxSampleTreeDepth;
59    public int MaxSampleTreeDepth {
60      get { return maxSampleTreeDepth; }
61      set {
62        if (maxSampleTreeDepth != value) {
63          maxSampleTreeDepth = value;
64          UpdateSampleTreeView();
65        }
66      }
67    }
68
69    public new ISymbolicExpressionGrammar Content {
70      get { return (ISymbolicExpressionGrammar)base.Content; }
71      set { base.Content = value; }
72    }
73
74    protected override void SetEnabledStateOfControls() {
75      base.SetEnabledStateOfControls();
76      maxTreeLengthTextBox.Enabled = Content != null;
77      maxTreeDepthTextBox.Enabled = Content != null;
78      generateSampleTreeButton.Enabled = Content != null;
79    }
80
81    protected override void RegisterContentEvents() {
82      base.RegisterContentEvents();
83      Content.Changed += new EventHandler(Content_Changed);
84    }
85
86    protected override void DeregisterContentEvents() {
87      Content.Changed -= new EventHandler(Content_Changed);
88      base.DeregisterContentEvents();
89    }
90
91    protected override void OnContentChanged() {
92      base.OnContentChanged();
93      if (Content == null) sampleTreeView.Content = null;
94      else UpdateSampleTreeView();
95    }
96
97    private void Content_Changed(object sender, System.EventArgs e) {
98      UpdateSampleTreeView();
99    }
100
101    private void UpdateSampleTreeView() {
102      try {
103        ISymbolicExpressionTreeCreator creator = (ISymbolicExpressionTreeCreator)treeCreatorComboBox.SelectedItem;
104        ISymbolicExpressionTree tree = creator.CreateTree(random, Content, MaxSampleTreeLength, MaxSampleTreeDepth);
105        foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
106          node.SetGrammar(null);
107        sampleTreeView.Content = tree;
108      }
109      catch (Exception ex) {
110        sampleTreeView.Content = null;
111        ErrorHandling.ShowErrorDialog(ex);
112      }
113    }
114
115    #region control events
116    private void generateSampleTreeButton_Click(object sender, EventArgs e) {
117      UpdateSampleTreeView();
118    }
119
120    private void maxTreeLengthTextBox_Validating(object sender, CancelEventArgs e) {
121      int maxTreeLength;
122      if (int.TryParse(maxTreeLengthTextBox.Text, out maxTreeLength) && maxTreeLength > 3) {
123        errorProvider.SetError(maxTreeLengthTextBox, string.Empty);
124        e.Cancel = false;
125      } else {
126        errorProvider.SetError(maxTreeLengthTextBox, "Invalid value: maximum tree length must be larger than 3.");
127        e.Cancel = true;
128      }
129    }
130    private void maxTreeLengthTextBox_Validated(object sender, EventArgs e) {
131      int maxTreeLength;
132      if (int.TryParse(maxTreeLengthTextBox.Text, out maxTreeLength) && maxTreeLength > 3) {
133        MaxSampleTreeLength = maxTreeLength;
134      }
135    }
136
137    private void maxTreeDepthTextBox_Validating(object sender, CancelEventArgs e) {
138      int maxTreeDepth;
139      if (int.TryParse(maxTreeDepthTextBox.Text, out maxTreeDepth) && maxTreeDepth > 3) {
140        errorProvider.SetError(maxTreeDepthTextBox, string.Empty);
141        e.Cancel = false;
142      } else {
143        errorProvider.SetError(maxTreeDepthTextBox, "Invalid value: maximum tree depth must be larger than 3.");
144        e.Cancel = true;
145      }
146    }
147    private void maxTreeDepthTextBox_Validated(object sender, EventArgs e) {
148      int maxTreeDepth;
149      if (int.TryParse(maxTreeDepthTextBox.Text, out maxTreeDepth) && maxTreeDepth > 3) {
150        MaxSampleTreeDepth = maxTreeDepth;
151      }
152    }
153    #endregion
154
155    private void treeCreatorComboBox_SelectedIndexChanged(object sender, EventArgs e) {
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.