Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • implemented population diversity analysis
File size: 2.2 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 double ApplyStepSize(double value) {
36      return ((int)Math.Round(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        value = ApplyStepSize(value);
51      }
52      return solutions;
53    }
54
55    protected override double CalculateSimilarityValue(PercentValue a, PercentValue b) {
56      double range = UpperBound.Value - LowerBound.Value;
57      double diff = Math.Abs(a.Value - b.Value);
58      return Math.Max(0, (range - (diff * 2)) / range);
59    }
60  }
61}
Note: See TracBrowser for help on using the repository browser.