Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Range.cs @ 4839

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

#1215 worked on MetaOptimization

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