Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/RangeConstraints/Range.cs @ 14049

Last change on this file since 14049 was 7840, checked in by spimming, 13 years ago

#1853:

  • included files from metaopt branch
  • ignored mutation and crossover
  • updated plugin frame
File size: 7.9 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.ParameterConfigurationTreeEncoding {
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    public Range() { }
110    [StorableConstructor]
111    protected Range(bool deserializing) : base(deserializing) { }
112    protected Range(Range<T> original, Cloner cloner)
113      : base(original, cloner) {
114      this.LowerBound = cloner.Clone(original.LowerBound);
115      this.UpperBound = cloner.Clone(original.UpperBound);
116      this.StepSize = cloner.Clone(original.StepSize);
117    }
118
119    [StorableHook(HookType.AfterDeserialization)]
120    private void AfterDeserialization() {
121      if (lowerBound != null) {
122        lowerBound.ValueChanged += new EventHandler(lowerBound_ToStringChanged);
123      }
124      if (upperBound != null) {
125        upperBound.ValueChanged += new EventHandler(upperBound_ToStringChanged);
126      }
127      if (stepSize != null) {
128        stepSize.ValueChanged += new EventHandler(stepSize_ToStringChanged);
129      }
130    }
131    #endregion
132
133    #region Events
134    private void lowerBound_ToStringChanged(object sender, EventArgs e) {
135      OnToStringChanged();
136    }
137    private void upperBound_ToStringChanged(object sender, EventArgs e) {
138      OnToStringChanged();
139    }
140    private void stepSize_ToStringChanged(object sender, EventArgs e) {
141      OnToStringChanged();
142    }
143    #endregion
144
145    #region Eventhandler
146    public event EventHandler LowerBoundChanged;
147    private void OnLowerBoundChanged() {
148      var handler = LowerBoundChanged;
149      if (handler != null) handler(this, EventArgs.Empty);
150    }
151    public event EventHandler UpperBoundChanged;
152    private void OnUpperBoundChanged() {
153      var handler = UpperBoundChanged;
154      if (handler != null) handler(this, EventArgs.Empty);
155    }
156    public event EventHandler StepSizeChanged;
157    private void OnStepSizeChanged() {
158      var handler = StepSizeChanged;
159      if (handler != null) handler(this, EventArgs.Empty);
160    }
161    public event EventHandler ValueChanged;
162    private void OnValueChanged() {
163      var handler = ValueChanged;
164      if (handler != null) handler(this, EventArgs.Empty);
165    }
166    #endregion
167
168    public override string ToString() {
169      return string.Format("{0},{1}:{2}", LowerBound.ToString(), UpperBound.ToString(), StepSize.ToString());
170    }
171
172    #region IStringConvertibleValue Members
173
174    public string GetValue() {
175      return this.ToString();
176    }
177    public bool ReadOnly {
178      get { return false; }
179    }
180    public bool SetValue(string value) {
181      var parts1 = value.Split(':');
182      if (parts1.Length != 2) return false;
183      var parts2 = parts1[0].Split(',');
184      if (parts2.Length != 2) return false;
185
186      return
187        lowerBound.SetValue(parts2[0]) &&
188        upperBound.SetValue(parts2[1]) &&
189        stepSize.SetValue(parts1[1]);
190    }
191    public bool Validate(string value, out string errorMessage) {
192      // TODO: check that upper is larger than lower and that stepsize < upper-lower
193      T lower = (T)lowerBound.Clone();
194      T upper = (T)upperBound.Clone();
195      T step = (T)stepSize.Clone();
196
197      var parts1 = value.Split(':');
198      if (parts1.Length != 2) {
199        errorMessage = "Could not parse range."; return false;
200      }
201      var parts2 = parts1[0].Split(',');
202      if (parts2.Length != 2) {
203        errorMessage = "Could not parse range."; return false;
204      }
205
206      if (lowerBound.SetValue(parts2[0]) &&
207          upperBound.SetValue(parts2[1]) &&
208          stepSize.SetValue(parts1[1])) {
209        errorMessage = string.Empty; return true;
210      } else {
211        errorMessage = "Could not parse range."; return false;
212      }
213    }
214
215    #endregion
216
217    public T GetRandomValue(IRandom random) {
218      // by a chance return the extreme values of this range to intensify search in those regions
219      if (random.NextDouble() < 0.1) {
220        if (random.NextDouble() < 0.5) {
221          return (T)LowerBound.Clone();
222        } else {
223          return (T)UpperBound.Clone();
224        }
225      }
226
227      // otherwise sample a random value from the range
228      return GetRandomSample(random);
229    }
230
231    protected abstract T GetRandomSample(IRandom random);
232
233    IItem IRange.GetRandomValue(IRandom random) {
234      return GetRandomValue(random);
235    }
236
237    public abstract IEnumerable<T> GetCombinations();
238    IEnumerable<IItem> IRange.GetCombinations() {
239      return GetCombinations().Cast<IItem>().ToArray();
240    }
241
242    public virtual double CalculateSimilarity(IItem a, IItem b) {
243      return CalculateSimilarityValue((T)a, (T)b);
244    }
245
246    protected abstract double CalculateSimilarityValue(T a, T b);
247  }
248}
Note: See TracBrowser for help on using the repository browser.