Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/IntValueRange.cs @ 12417

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

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

File size: 1.9 KB
RevLine 
[5112]1using System;
2using System.Collections.Generic;
[8087]3using HeuristicLab.Common;
4using HeuristicLab.Core;
[5112]5using HeuristicLab.Data;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.Problems.MetaOptimization {
9  [StorableClass]
10  public class IntValueRange : Range<IntValue> {
11
12    public IntValueRange(IntValue lowerBound, IntValue upperBound, IntValue stepSize) : base(lowerBound, upperBound, stepSize) { }
13    [StorableConstructor]
14    protected IntValueRange(bool deserializing) : base(deserializing) { }
15    protected IntValueRange(IntValueRange original, Cloner cloner) : base(original, cloner) { }
16    public override IDeepCloneable Clone(Cloner cloner) {
17      return new IntValueRange(this, cloner);
18    }
19
20    protected override IntValue GetRandomSample(IRandom random) {
21      int val;
22      do {
23        val = random.Next(LowerBound.Value / StepSize.Value, UpperBound.Value / StepSize.Value + 1) * StepSize.Value;
[5207]24      } while (!IsInRange(val));
[5112]25      return new IntValue(val);
26    }
27
[5337]28    public int ApplyStepSize(int value) {
29      return ((int)Math.Round(value / (double)this.StepSize.Value, 0)) * this.StepSize.Value;
[5207]30    }
[5112]31
[5207]32    public bool IsInRange(int value) {
33      return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
[5112]34    }
[5144]35
36    public override IEnumerable<IntValue> GetCombinations() {
37      var solutions = new List<IntValue>();
[5207]38      int value = this.LowerBound.Value;
[5144]39
40      while (value <= this.UpperBound.Value) {
41        solutions.Add(new IntValue(value));
42        value += this.StepSize.Value;
43      }
44      return solutions;
45    }
[5522]46
47    protected override double CalculateSimilarityValue(IntValue a, IntValue b) {
48      double range = UpperBound.Value - LowerBound.Value;
49      double diff = Math.Abs(a.Value - b.Value);
50      return Math.Max(0, (range - (diff * 2)) / range);
51    }
[5112]52  }
53}
Note: See TracBrowser for help on using the repository browser.