Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5576 was 5522, checked in by cneumuel, 14 years ago

#1215

  • implemented population diversity analysis
File size: 2.1 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 (!IsInRange(val));
28      return new DoubleValue(val);
29    }
30
31    public double ApplyStepSize(double value) {
32      return (Math.Round(value / this.StepSize.Value, 0)) * this.StepSize.Value;
33    }
34
35    public bool IsInRange(double value) {
36      return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
37    }
38
39    public override IEnumerable<DoubleValue> GetCombinations() {
40      var solutions = new List<DoubleValue>();
41      double value = LowerBound.Value;
42
43      while (value <= UpperBound.Value) {
44        solutions.Add(new DoubleValue(value));
45        value += StepSize.Value;
46        value = ApplyStepSize(value);
47      }
48      return solutions;
49    }
50
51    protected override double CalculateSimilarityValue(DoubleValue a, DoubleValue b) {
52      double range = UpperBound.Value - LowerBound.Value;
53      double diff = Math.Abs(a.Value - b.Value);
54      return Math.Max(0, (range - (diff*2)) / range);
55    }
56  }
57}
Note: See TracBrowser for help on using the repository browser.