Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Symbols/VariableView.cs @ 17740

Last change on this file since 17740 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
24using System.Windows.Forms;
25using HeuristicLab.Collections;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
30using HeuristicLab.MainForm;
31
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
34  [View("Variable View")]
35  [Content(typeof(VariableBase), true)]
36  public partial class VariableView : SymbolView {
37    private CheckedItemCollectionView<StringValue> variableNamesView;
38
39    public new VariableBase Content {
40      get { return (VariableBase)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public VariableView() {
45      InitializeComponent();
46      variableNamesView = new CheckedItemCollectionView<StringValue>();
47      variableNamesView.Dock = DockStyle.Fill;
48      variableNamesTabPage.Controls.Add(variableNamesView);
49      variableNamesView.Content = new CheckedItemCollection<StringValue>();
50
51      RegisterVariableNamesViewContentEvents();
52    }
53
54    private void RegisterVariableNamesViewContentEvents() {
55      variableNamesView.Content.ItemsAdded += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
56      variableNamesView.Content.ItemsRemoved += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Removed);
57      variableNamesView.Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
58      variableNamesView.Content.CollectionReset += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
59      foreach (var variable in variableNamesView.Content) {
60        variable.ValueChanged += new EventHandler(Variable_ValueChanged);
61      }
62    }
63
64
65    private void DeregisterVariableNamesViewContentEvents() {
66      variableNamesView.Content.ItemsAdded -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
67      variableNamesView.Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Removed);
68      variableNamesView.Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
69      variableNamesView.Content.CollectionReset -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
70      foreach (var variable in variableNamesView.Content) {
71        variable.ValueChanged -= new EventHandler(Variable_ValueChanged);
72      }
73    }
74
75
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.Changed += new EventHandler(Content_Changed);
79    }
80
81    protected override void DeregisterContentEvents() {
82      base.DeregisterContentEvents();
83      Content.Changed -= new EventHandler(Content_Changed);
84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      UpdateControl();
89    }
90
91    protected override void SetEnabledStateOfControls() {
92      base.SetEnabledStateOfControls();
93      enabledCheckBox.Enabled = Content != null && Content.VariableNames.Any() && !Locked && !ReadOnly;
94      weightInitializationMuTextBox.Enabled = Content != null;
95      weightInitializationMuTextBox.ReadOnly = ReadOnly;
96      weightInitializationSigmaTextBox.Enabled = Content != null;
97      weightInitializationSigmaTextBox.ReadOnly = ReadOnly;
98      additiveWeightChangeSigmaTextBox.Enabled = Content != null;
99      additiveWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
100      multiplicativeWeightChangeSigmaTextBox.Enabled = Content != null;
101      multiplicativeWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
102      varChangeProbTextBox.Enabled = Content != null;
103      varChangeProbTextBox.ReadOnly = ReadOnly;
104    }
105
106    #region content event handlers
107    private void Content_Changed(object sender, EventArgs e) {
108      UpdateControl();
109    }
110    #endregion
111
112    #region control event handlers
113    private void VariableNames_Changed(object sender, CollectionItemsChangedEventArgs<StringValue> e) {
114      foreach (var newVar in e.Items)
115        newVar.ValueChanged += new EventHandler(Variable_ValueChanged);
116      foreach (var oldVar in e.OldItems)
117        oldVar.ValueChanged -= new EventHandler(Variable_ValueChanged);
118      UpdateContent();
119    }
120
121    private void VariableNames_Removed(object sender, CollectionItemsChangedEventArgs<StringValue> e) {
122      foreach (var newVar in e.Items)
123        newVar.ValueChanged -= new EventHandler(Variable_ValueChanged);
124      UpdateContent();
125    }
126
127    private void Variable_ValueChanged(object sender, EventArgs e) {
128      UpdateContent();
129    }
130
131    private void UpdateContent() {
132      if (Content != null) {
133        Content.Fixed = true;
134        DeregisterContentEvents();
135        Content.VariableNames = variableNamesView.Content.CheckedItems.Select(x => x.Value);
136        Content.AllVariableNames = variableNamesView.Content.Select(x => x.Value);
137        RegisterContentEvents();
138      }
139    }
140
141    private void WeightMuTextBox_TextChanged(object sender, EventArgs e) {
142      double nu;
143      if (double.TryParse(weightInitializationMuTextBox.Text, out nu)) {
144        Content.WeightMu = nu;
145        errorProvider.SetError(weightInitializationMuTextBox, string.Empty);
146      } else {
147        errorProvider.SetError(weightInitializationMuTextBox, "Invalid value");
148      }
149    }
150    private void WeightSigmaTextBox_TextChanged(object sender, EventArgs e) {
151      double sigma;
152      if (double.TryParse(weightInitializationSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
153        Content.WeightSigma = sigma;
154        errorProvider.SetError(weightInitializationSigmaTextBox, string.Empty);
155      } else {
156        errorProvider.SetError(weightInitializationSigmaTextBox, "Invalid value");
157      }
158    }
159
160    private void AdditiveWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
161      double sigma;
162      if (double.TryParse(additiveWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
163        Content.WeightManipulatorSigma = sigma;
164        errorProvider.SetError(additiveWeightChangeSigmaTextBox, string.Empty);
165      } else {
166        errorProvider.SetError(additiveWeightChangeSigmaTextBox, "Invalid value");
167      }
168    }
169    private void MultiplicativeWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
170      double sigma;
171      if (double.TryParse(multiplicativeWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
172        Content.MultiplicativeWeightManipulatorSigma = sigma;
173        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, string.Empty);
174      } else {
175        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, "Invalid value");
176      }
177    }
178
179    private void VarProbTextBox_TextChanged(object sender, EventArgs e) {
180      double prob;
181      if (double.TryParse(varChangeProbTextBox.Text, out prob) && prob >= 0.0 && prob <= 1.0) {
182        Content.VariableChangeProbability = prob;
183        errorProvider.SetError(varChangeProbTextBox, string.Empty);
184      } else {
185        errorProvider.SetError(varChangeProbTextBox, "Invalid value");
186      }
187    }
188    #endregion
189
190    #region helpers
191    private void UpdateControl() {
192      if (Content == null) {
193        weightInitializationMuTextBox.Text = string.Empty;
194        weightInitializationSigmaTextBox.Text = string.Empty;
195        additiveWeightChangeSigmaTextBox.Text = string.Empty;
196        multiplicativeWeightChangeSigmaTextBox.Text = string.Empty;
197        varChangeProbTextBox.Text = string.Empty;
198        // temporarily deregister to prevent circular calling of events
199        DeregisterVariableNamesViewContentEvents();
200        variableNamesView.Content.Clear();
201        RegisterVariableNamesViewContentEvents();
202      } else {
203        // temporarily deregister to prevent circular calling of events
204        DeregisterVariableNamesViewContentEvents();
205        variableNamesView.Content.Clear();
206        foreach (var variableName in Content.AllVariableNames) {
207          variableNamesView.Content.Add(new StringValue(variableName), Content.VariableNames.Contains(variableName));
208        }
209        RegisterVariableNamesViewContentEvents();
210
211        weightInitializationMuTextBox.Text = Content.WeightMu.ToString();
212        weightInitializationSigmaTextBox.Text = Content.WeightSigma.ToString();
213        additiveWeightChangeSigmaTextBox.Text = Content.WeightManipulatorSigma.ToString();
214        multiplicativeWeightChangeSigmaTextBox.Text = Content.MultiplicativeWeightManipulatorSigma.ToString();
215        varChangeProbTextBox.Text = Content.VariableChangeProbability.ToString();
216      }
217      SetEnabledStateOfControls();
218    }
219    #endregion
220
221  }
222}
Note: See TracBrowser for help on using the repository browser.