Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/PercentValueRange.cs @ 15740

Last change on this file since 15740 was 8087, checked in by mkommend, 13 years ago

#1877: Corrected plugin dependencies and ToString methods in MetaOpt.

File size: 2.1 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 PercentValueRange : Range<PercentValue> {
11
12    public PercentValueRange(PercentValue lowerBound, PercentValue upperBound, PercentValue stepSize) : base(lowerBound, upperBound, stepSize) { }
13    [StorableConstructor]
14    protected PercentValueRange(bool deserializing) : base(deserializing) { }
15    protected PercentValueRange(PercentValueRange original, Cloner cloner) : base(original, cloner) { }
16    public override IDeepCloneable Clone(Cloner cloner) {
17      return new PercentValueRange(this, cloner);
18    }
19
20    protected override PercentValue GetRandomSample(IRandom random) {
21      double val;
22      do {
23        val = Math.Round((random.NextDouble() * (UpperBound.Value - LowerBound.Value) + LowerBound.Value) / StepSize.Value, 0) * StepSize.Value;
24      } while (!IsInRange(val));
25      return new PercentValue(val);
26    }
27
28    public DoubleValueRange AsDoubleValueRange() {
29      return new DoubleValueRange(LowerBound, UpperBound, StepSize);
30    }
31
32    public double ApplyStepSize(double value) {
33      return ((int)Math.Round(value / this.StepSize.Value, 0)) * this.StepSize.Value;
34    }
35
36    public bool IsInRange(double value) {
37      return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
38    }
39
40    public override IEnumerable<PercentValue> GetCombinations() {
41      var solutions = new List<PercentValue>();
42      double value = LowerBound.Value;
43
44      while (value <= UpperBound.Value) {
45        solutions.Add(new PercentValue(value));
46        value += StepSize.Value;
47        value = ApplyStepSize(value);
48      }
49      return solutions;
50    }
51
52    protected override double CalculateSimilarityValue(PercentValue a, PercentValue b) {
53      double range = UpperBound.Value - LowerBound.Value;
54      double diff = Math.Abs(a.Value - b.Value);
55      return Math.Max(0, (range - (diff * 2)) / range);
56    }
57  }
58}
Note: See TracBrowser for help on using the repository browser.