Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/RangeConstraints/Range.cs @ 8517

Last change on this file since 8517 was 8517, checked in by jkarder, 12 years ago

#1853:

  • created branch for ParameterConfigurationEncoding
  • added CreateExperimentDialog that uses the extracted encoding
File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
31  [StorableClass]
32  public abstract class Range<T> : Item, IRange<T> where T : class, IItem, IStringConvertibleValue, IDeepCloneable {
33    [Storable]
34    private T lowerBound;
35    public virtual T LowerBound {
36      get { return lowerBound; }
37      set {
38        if (lowerBound != value) {
39          if (lowerBound != null) {
40            lowerBound.ValueChanged -= new EventHandler(lowerBound_ToStringChanged);
41          }
42          lowerBound = value;
43          if (lowerBound != null) {
44            lowerBound.ValueChanged += new EventHandler(lowerBound_ToStringChanged);
45          }
46          OnLowerBoundChanged();
47        }
48      }
49    }
50
51    [Storable]
52    private T upperBound;
53    public virtual T UpperBound {
54      get { return upperBound; }
55      set {
56        if (upperBound != value) {
57          if (upperBound != null) {
58            upperBound.ValueChanged -= new EventHandler(upperBound_ToStringChanged);
59          }
60          upperBound = value;
61          if (upperBound != null) {
62            upperBound.ValueChanged += new EventHandler(upperBound_ToStringChanged);
63          }
64          OnUpperBoundChanged();
65        }
66      }
67    }
68
69    [Storable]
70    private T stepSize;
71    public virtual T StepSize {
72      get { return stepSize; }
73      set {
74        if (stepSize != value) {
75          if (stepSize != null) {
76            stepSize.ValueChanged -= new EventHandler(stepSize_ToStringChanged);
77          }
78          stepSize = value;
79          if (stepSize != null) {
80            stepSize.ValueChanged += new EventHandler(stepSize_ToStringChanged);
81          }
82          OnStepSizeChanged();
83        }
84      }
85    }
86
87    #region IRange Members
88    object IRange.LowerBound {
89      get { return lowerBound; }
90      set { lowerBound = (T)value; }
91    }
92    object IRange.UpperBound {
93      get { return upperBound; }
94      set { upperBound = (T)value; }
95    }
96    object IRange.StepSize {
97      get { return stepSize; }
98      set { stepSize = (T)value; }
99    }
100    #endregion
101
102    #region Constructors and Cloning
103    public Range(T lowerBound, T upperBound, T stepSize) {
104      this.LowerBound = lowerBound;
105      this.UpperBound = upperBound;
106      this.StepSize = stepSize;
107    }
108
109    [StorableConstructor]
110    protected Range(bool deserializing) : base(deserializing) { }
111    protected Range(Range<T> original, Cloner cloner)
112      : base(original, cloner) {
113      this.LowerBound = cloner.Clone(original.LowerBound);
114      this.UpperBound = cloner.Clone(original.UpperBound);
115      this.StepSize = cloner.Clone(original.StepSize);
116    }
117
118    [StorableHook(HookType.AfterDeserialization)]
119    private void AfterDeserialization() {
120      if (lowerBound != null) {
121        lowerBound.ValueChanged += new EventHandler(lowerBound_ToStringChanged);
122      }
123      if (upperBound != null) {
124        upperBound.ValueChanged += new EventHandler(upperBound_ToStringChanged);
125      }
126      if (stepSize != null) {
127        stepSize.ValueChanged += new EventHandler(stepSize_ToStringChanged);
128      }
129    }
130    #endregion
131
132    #region Events
133    private void lowerBound_ToStringChanged(object sender, EventArgs e) {
134      OnToStringChanged();
135    }
136    private void upperBound_ToStringChanged(object sender, EventArgs e) {
137      OnToStringChanged();
138    }
139    private void stepSize_ToStringChanged(object sender, EventArgs e) {
140      OnToStringChanged();
141    }
142    #endregion
143
144    #region Eventhandler
145    public event EventHandler LowerBoundChanged;
146    private void OnLowerBoundChanged() {
147      var handler = LowerBoundChanged;
148      if (handler != null) handler(this, EventArgs.Empty);
149    }
150    public event EventHandler UpperBoundChanged;
151    private void OnUpperBoundChanged() {
152      var handler = UpperBoundChanged;
153      if (handler != null) handler(this, EventArgs.Empty);
154    }
155    public event EventHandler StepSizeChanged;
156    private void OnStepSizeChanged() {
157      var handler = StepSizeChanged;
158      if (handler != null) handler(this, EventArgs.Empty);
159    }
160    public event EventHandler ValueChanged;
161    private void OnValueChanged() {
162      var handler = ValueChanged;
163      if (handler != null) handler(this, EventArgs.Empty);
164    }
165    #endregion
166
167    public override string ToString() {
168      return string.Format("{0},{1}:{2}", LowerBound.ToString(), UpperBound.ToString(), StepSize.ToString());
169    }
170
171    #region IStringConvertibleValue Members
172
173    public string GetValue() {
174      return this.ToString();
175    }
176    public bool ReadOnly {
177      get { return false; }
178    }
179    public bool SetValue(string value) {
180      var parts1 = value.Split(':');
181      if (parts1.Length != 2) return false;
182      var parts2 = parts1[0].Split(',');
183      if (parts2.Length != 2) return false;
184
185      return
186        lowerBound.SetValue(parts2[0]) &&
187        upperBound.SetValue(parts2[1]) &&
188        stepSize.SetValue(parts1[1]);
189    }
190    public bool Validate(string value, out string errorMessage) {
191      // TODO: check that upper is larger than lower and that stepsize < upper-lower
192      T lower = (T)lowerBound.Clone();
193      T upper = (T)upperBound.Clone();
194      T step = (T)stepSize.Clone();
195
196      var parts1 = value.Split(':');
197      if (parts1.Length != 2) {
198        errorMessage = "Could not parse range."; return false;
199      }
200      var parts2 = parts1[0].Split(',');
201      if (parts2.Length != 2) {
202        errorMessage = "Could not parse range."; return false;
203      }
204
205      if (lowerBound.SetValue(parts2[0]) &&
206          upperBound.SetValue(parts2[1]) &&
207          stepSize.SetValue(parts1[1])) {
208        errorMessage = string.Empty; return true;
209      } else {
210        errorMessage = "Could not parse range."; return false;
211      }
212    }
213
214    #endregion
215
216    public T GetRandomValue(IRandom random) {
217      // by a chance return the extreme values of this range to intensify search in those regions
218      if (random.NextDouble() < 0.1) {
219        if (random.NextDouble() < 0.5) {
220          return (T)LowerBound.Clone();
221        } else {
222          return (T)UpperBound.Clone();
223        }
224      }
225
226      // otherwise sample a random value from the range
227      return GetRandomSample(random);
228    }
229
230    protected abstract T GetRandomSample(IRandom random);
231
232    IItem IRange.GetRandomValue(IRandom random) {
233      return GetRandomValue(random);
234    }
235
236    public abstract IEnumerable<T> GetCombinations();
237    IEnumerable<IItem> IRange.GetCombinations() {
238      return GetCombinations().Cast<IItem>().ToArray();
239    }
240
241    public virtual double CalculateSimilarity(IItem a, IItem b) {
242      return CalculateSimilarityValue((T)a, (T)b);
243    }
244
245    protected abstract double CalculateSimilarityValue(T a, T b);
246  }
247}
Note: See TracBrowser for help on using the repository browser.