Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/IntValueRange.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: 1.9 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 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;
24      } while (!IsInRange(val));
25      return new IntValue(val);
26    }
27
28    public int ApplyStepSize(int value) {
29      return ((int)Math.Round(value / (double)this.StepSize.Value, 0)) * this.StepSize.Value;
30    }
31
32    public bool IsInRange(int value) {
33      return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
34    }
35
36    public override IEnumerable<IntValue> GetCombinations() {
37      var solutions = new List<IntValue>();
38      int value = this.LowerBound.Value;
39
40      while (value <= this.UpperBound.Value) {
41        solutions.Add(new IntValue(value));
42        value += this.StepSize.Value;
43      }
44      return solutions;
45    }
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    }
52  }
53}
Note: See TracBrowser for help on using the repository browser.