Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/DoubleValueRange.cs @ 5184

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

#1215

  • enhanced combinations generator (now with batchruns!)
  • fixed ActualNames for metaopt-alg
  • added penalty for invalid solution-candidates (algs which throw exceptions)
  • migrated to .NET 4.0
File size: 2.0 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 DoubleValueRange : Range<DoubleValue> {
13
14    public DoubleValueRange(DoubleValue lowerBound, DoubleValue upperBound, DoubleValue stepSize) : base(lowerBound, upperBound, stepSize) { }
15    public DoubleValueRange() { }
16    [StorableConstructor]
17    protected DoubleValueRange(bool deserializing) : base(deserializing) { }
18    protected DoubleValueRange(DoubleValueRange original, Cloner cloner) : base(original, cloner) { }
19    public override IDeepCloneable Clone(Cloner cloner) {
20      return new DoubleValueRange(this, cloner);
21    }
22
23    protected override DoubleValue 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 (val < LowerBound.Value || val > UpperBound.Value);
28      return new DoubleValue(val);
29    }
30
31    public void Fix(DoubleValue value) {
32      // apply stepsize
33      value.Value = ((int)Math.Round(value.Value / this.StepSize.Value, 0)) * this.StepSize.Value;
34
35      // repair bounds
36      if (value.Value > this.UpperBound.Value) value.Value = this.UpperBound.Value;
37      if (value.Value < this.LowerBound.Value) value.Value = this.LowerBound.Value;
38    }
39
40    public override IEnumerable<DoubleValue> GetCombinations() {
41      var solutions = new List<DoubleValue>();
42      double value = ((int)Math.Round(LowerBound.Value / StepSize.Value, 0)) * StepSize.Value;
43      if (value < LowerBound.Value) value += StepSize.Value;
44
45      while (value <= UpperBound.Value) {
46        solutions.Add(new DoubleValue(value));
47        value += StepSize.Value;
48      }
49      return solutions;
50    }
51  }
52}
Note: See TracBrowser for help on using the repository browser.