Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • added weight parameters for quality, stddev and evaluated solutions
  • lots of fixes
File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.Problems.MetaOptimization {
9  [StorableClass]
10  public class DoubleValueRange : Range<DoubleValue> {
11
12    public override DoubleValue LowerBound {
13      get {
14        return base.LowerBound;
15      }
16      set {
17        base.LowerBound = value;
18      }
19    }
20   
21    public DoubleValueRange(DoubleValue lowerBound, DoubleValue upperBound, DoubleValue stepSize) : base(lowerBound, upperBound, stepSize) { }
22    public DoubleValueRange() { }
23    [StorableConstructor]
24    protected DoubleValueRange(bool deserializing) : base(deserializing) { }
25    protected DoubleValueRange(DoubleValueRange original, Cloner cloner) : base(original, cloner) { }
26    public override IDeepCloneable Clone(Cloner cloner) {
27      return new DoubleValueRange(this, cloner);
28    }
29
30    protected override DoubleValue GetRandomSample(IRandom random) {
31      double val;
32      do {
33        val = Math.Round((random.NextDouble() * (UpperBound.Value - LowerBound.Value) + LowerBound.Value) / StepSize.Value, 0) * StepSize.Value;
34      } while (!IsInRange(val));
35      return new DoubleValue(val);
36    }
37
38    public double ApplyStepSize(double value) {
39      return (Math.Round(value / this.StepSize.Value, 0)) * this.StepSize.Value;
40    }
41
42    public bool IsInRange(double value) {
43      return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
44    }
45
46    public override IEnumerable<DoubleValue> GetCombinations() {
47      var solutions = new List<DoubleValue>();
48      double value = LowerBound.Value;
49
50      while (value <= UpperBound.Value) {
51        solutions.Add(new DoubleValue(value));
52        value += StepSize.Value;
53        value = ApplyStepSize(value);
54      }
55      return solutions;
56    }
57
58    protected override double CalculateSimilarityValue(DoubleValue a, DoubleValue b) {
59      double range = UpperBound.Value - LowerBound.Value;
60      double diff = Math.Abs(a.Value - b.Value);
61      return Math.Max(0, (range - (diff*2)) / range);
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.