Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/NumericRange.cs @ 4682

Last change on this file since 4682 was 4525, checked in by cneumuel, 14 years ago

implemented basic crossover operator for ParameterSets. MetaOptimization is now functional on a basic level (Configuration and Crossing only works for IntValue Parameters) (#1215)

File size: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Problems.MetaOptimization {
10  [StorableClass]
11  public class NumericRange : Item, INumericRange {
12    [Storable]
13    private IntValue lowerBound;
14    public IntValue LowerBound {
15      get { return lowerBound; }
16      set {
17        if (lowerBound != value) {
18          if (lowerBound != null) {
19            lowerBound.ValueChanged -= new EventHandler(lowerBound_ValueChanged);
20          }
21          lowerBound = value;
22          if (lowerBound != null) {
23            lowerBound.ValueChanged += new EventHandler(lowerBound_ValueChanged);
24          }
25          OnLowerBoundChanged();
26        }
27      }
28    }
29
30    [Storable]
31    private IntValue upperBound;
32    public IntValue UpperBound {
33      get { return upperBound; }
34      set {
35        if (upperBound != value) {
36          if (upperBound != null) {
37            upperBound.ValueChanged -= new EventHandler(upperBound_ValueChanged);
38          }
39          upperBound = value;
40          if (upperBound != null) {
41            upperBound.ValueChanged += new EventHandler(upperBound_ValueChanged);
42          }
43          OnUpperBoundChanged();
44        }
45      }
46    }
47
48    [Storable]
49    private IntValue stepSize;
50    public IntValue StepSize {
51      get { return stepSize; }
52      set {
53        if (stepSize != value) {
54          if (stepSize != null) {
55            stepSize.ValueChanged -= new EventHandler(upperBound_ValueChanged);
56          }
57          stepSize = value;
58          if (stepSize != null) {
59            stepSize.ValueChanged += new EventHandler(stepSize_ValueChanged);
60          }
61          OnStepSizeChanged();
62        }
63      }
64    }
65
66    public NumericRange() {
67      LowerBound = new IntValue(0);
68      UpperBound = new IntValue(0);
69      StepSize = new IntValue(1);
70    }
71
72    [StorableConstructor]
73    protected NumericRange(bool deserializing) : base(deserializing) { }
74
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      if (lowerBound != null) {
78        lowerBound.ValueChanged += new EventHandler(lowerBound_ValueChanged);
79      }
80      if (upperBound != null) {
81        upperBound.ValueChanged += new EventHandler(upperBound_ValueChanged);
82      }
83      if (stepSize != null) {
84        stepSize.ValueChanged += new EventHandler(stepSize_ValueChanged);
85      }
86    }
87
88    void lowerBound_ValueChanged(object sender, EventArgs e) {
89      OnToStringChanged();
90    }
91    void upperBound_ValueChanged(object sender, EventArgs e) {
92      OnToStringChanged();
93    }
94    void stepSize_ValueChanged(object sender, EventArgs e) {
95      OnToStringChanged();
96    }
97
98    public event EventHandler LowerBoundChanged;
99    private void OnLowerBoundChanged() {
100      var handler = LowerBoundChanged;
101      if (handler != null) handler(this, EventArgs.Empty);
102    }
103
104    public event EventHandler UpperBoundChanged;
105    private void OnUpperBoundChanged() {
106      var handler = UpperBoundChanged;
107      if (handler != null) handler(this, EventArgs.Empty);
108    }
109    public event EventHandler StepSizeChanged;
110    private void OnStepSizeChanged() {
111      var handler = StepSizeChanged;
112      if (handler != null) handler(this, EventArgs.Empty);
113    }
114
115    public override string ToString() {
116      return string.Format("[{0},{1}:{2}]", LowerBound.ToString(), UpperBound.ToString(), StepSize.ToString());
117    }
118
119    #region Cloning
120    public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
121      NumericRange clone = (NumericRange)base.Clone(cloner);
122      clone.LowerBound = (IntValue)this.LowerBound.Clone();
123      clone.UpperBound = (IntValue)this.UpperBound.Clone();
124      clone.StepSize = (IntValue)this.StepSize.Clone();
125      return base.Clone(cloner);
126    }
127    #endregion
128  }
129}
Note: See TracBrowser for help on using the repository browser.