source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Symbols/VariableView.cs @ 14238

Last change on this file since 14238 was 14238, checked in by gkronber, 3 years ago

#2650:

  • added weight for FactorVariable (necessary for LR)
  • introduced VariableBase and VariableTreeNodeBase and IVariableSymbol
  • support for factors in LR
  • extended variable impacts in solution view
  • fixed ERC view for regression
  • support for FactorVariable in simplifier
  • improved support for FactorVariable in constants optimizer
  • multiple related changes and small fixes
File size: 8.9 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.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;
31using HeuristicLab.MainForm.WindowsForms;
32
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
35  [View("Variable View")]
36  [Content(typeof(Variable), true)]
37  [Content(typeof(FactorVariable), true)]
38  public partial class VariableView : SymbolView {
39    private CheckedItemCollectionView<StringValue> variableNamesView;
40
41    public new VariableBase Content {
42      get { return (VariableBase)base.Content; }
43      set { base.Content = value; }
44    }
45
46    public VariableView() {
47      InitializeComponent();
48      variableNamesView = new CheckedItemCollectionView<StringValue>();
49      variableNamesView.Dock = DockStyle.Fill;
50      variableNamesTabPage.Controls.Add(variableNamesView);
51      variableNamesView.Content = new CheckedItemCollection<StringValue>();
52
53      RegisterVariableNamesViewContentEvents();
54    }
55
56    private void RegisterVariableNamesViewContentEvents() {
57      variableNamesView.Content.ItemsAdded += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
58      variableNamesView.Content.ItemsRemoved += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Removed);
59      variableNamesView.Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
60      variableNamesView.Content.CollectionReset += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
61      foreach (var variable in variableNamesView.Content) {
62        variable.ValueChanged += new EventHandler(Variable_ValueChanged);
63      }
64    }
65
66
67    private void DeregisterVariableNamesViewContentEvents() {
68      variableNamesView.Content.ItemsAdded -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
69      variableNamesView.Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Removed);
70      variableNamesView.Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
71      variableNamesView.Content.CollectionReset -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
72      foreach (var variable in variableNamesView.Content) {
73        variable.ValueChanged -= new EventHandler(Variable_ValueChanged);
74      }
75    }
76
77
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.Changed += new EventHandler(Content_Changed);
81    }
82
83    protected override void DeregisterContentEvents() {
84      base.DeregisterContentEvents();
85      Content.Changed -= new EventHandler(Content_Changed);
86    }
87
88    protected override void OnContentChanged() {
89      base.OnContentChanged();
90      UpdateControl();
91    }
92
93    protected override void SetEnabledStateOfControls() {
94      base.SetEnabledStateOfControls();
95      enabledCheckBox.Enabled = Content != null && Content.VariableNames.Any() && !Locked && !ReadOnly;
96      weightInitializationMuTextBox.Enabled = Content != null;
97      weightInitializationMuTextBox.ReadOnly = ReadOnly;
98      weightInitializationSigmaTextBox.Enabled = Content != null;
99      weightInitializationSigmaTextBox.ReadOnly = ReadOnly;
100      additiveWeightChangeSigmaTextBox.Enabled = Content != null;
101      additiveWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
102      multiplicativeWeightChangeSigmaTextBox.Enabled = Content != null;
103      multiplicativeWeightChangeSigmaTextBox.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    #endregion
179
180    #region helpers
181    private void UpdateControl() {
182      if (Content == null) {
183        weightInitializationMuTextBox.Text = string.Empty;
184        weightInitializationSigmaTextBox.Text = string.Empty;
185        additiveWeightChangeSigmaTextBox.Text = string.Empty;
186        multiplicativeWeightChangeSigmaTextBox.Text = string.Empty;
187        // temporarily deregister to prevent circular calling of events
188        DeregisterVariableNamesViewContentEvents();
189        variableNamesView.Content.Clear();
190        RegisterVariableNamesViewContentEvents();
191      } else {
192        // temporarily deregister to prevent circular calling of events
193        DeregisterVariableNamesViewContentEvents();
194        variableNamesView.Content.Clear();
195        foreach (var variableName in Content.AllVariableNames) {
196          variableNamesView.Content.Add(new StringValue(variableName), Content.VariableNames.Contains(variableName));
197        }
198        RegisterVariableNamesViewContentEvents();
199
200        weightInitializationMuTextBox.Text = Content.WeightMu.ToString();
201        weightInitializationSigmaTextBox.Text = Content.WeightSigma.ToString();
202        additiveWeightChangeSigmaTextBox.Text = Content.WeightManipulatorSigma.ToString();
203        multiplicativeWeightChangeSigmaTextBox.Text = Content.MultiplicativeWeightManipulatorSigma.ToString();
204      }
205      SetEnabledStateOfControls();
206    }
207    #endregion
208  }
209}
Note: See TracBrowser for help on using the repository browser.