Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/PercentValueRange.cs @ 5207

Last change on this file since 5207 was 5207, checked in by cneumuel, 13 years ago

#1215

  • lots of memory-consumption improvements
  • validValues -> validTypes (this saves memory!)
  • changed manipulators; modifications are less significant, out-of-bound-values are resampled instead of set to lower or upper bound
  • changed the way a base-level algorithm gets executed -> introduced AlgorithmExecutor
File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Data;
6using HeuristicLab.Common;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.Core;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  [StorableClass]
12  public class PercentValueRange : Range<PercentValue> {
13
14    public PercentValueRange(PercentValue lowerBound, PercentValue upperBound, PercentValue stepSize) : base(lowerBound, upperBound, stepSize) { }
15    public PercentValueRange() { }
16    [StorableConstructor]
17    protected PercentValueRange(bool deserializing) : base(deserializing) { }
18    protected PercentValueRange(PercentValueRange original, Cloner cloner) : base(original, cloner) { }
19    public override IDeepCloneable Clone(Cloner cloner) {
20      return new PercentValueRange(this, cloner);
21    }
22
23    protected override PercentValue GetRandomSample(IRandom random) {
24      double val;
25      do {
26        val = Math.Round((random.NextDouble() * (UpperBound.Value - LowerBound.Value) + LowerBound.Value) / StepSize.Value, 0) * StepSize.Value;
27      } while (!IsInRange(val));
28      return new PercentValue(val);
29    }
30
31    public DoubleValueRange AsDoubleValueRange() {
32      return new DoubleValueRange(LowerBound, UpperBound, StepSize);
33    }
34
35    public void ApplyStepSize(PercentValue value) {
36      value.Value = ((int)Math.Round(value.Value / this.StepSize.Value, 0)) * this.StepSize.Value;
37    }
38
39    public bool IsInRange(double value) {
40      return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
41    }
42
43    public override IEnumerable<PercentValue> GetCombinations() {
44      var solutions = new List<PercentValue>();
45      double value = LowerBound.Value;
46
47      while (value <= UpperBound.Value) {
48        solutions.Add(new PercentValue(value));
49        value += StepSize.Value;
50      }
51      return solutions;
52    }
53  }
54}
Note: See TracBrowser for help on using the repository browser.