Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/IntValueRange.cs @ 16574

Last change on this file since 16574 was 16574, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.MetaOptimization addon to compile with new HL.Persistence

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;
7using HEAL.Attic;
8
9namespace HeuristicLab.Problems.MetaOptimization {
10  [StorableType("124C6B55-A78D-4D82-810F-B5FD0C23EBB6")]
11  public class IntValueRange : Range<IntValue> {
12
13    public IntValueRange(IntValue lowerBound, IntValue upperBound, IntValue stepSize) : base(lowerBound, upperBound, stepSize) { }
14    [StorableConstructor]
15    protected IntValueRange(StorableConstructorFlag _) : base(_) { }
16    protected IntValueRange(IntValueRange original, Cloner cloner) : base(original, cloner) { }
17    public override IDeepCloneable Clone(Cloner cloner) {
18      return new IntValueRange(this, cloner);
19    }
20
21    protected override IntValue GetRandomSample(IRandom random) {
22      int val;
23      do {
24        val = random.Next(LowerBound.Value / StepSize.Value, UpperBound.Value / StepSize.Value + 1) * StepSize.Value;
25      } while (!IsInRange(val));
26      return new IntValue(val);
27    }
28
29    public int ApplyStepSize(int value) {
30      return ((int)Math.Round(value / (double)this.StepSize.Value, 0)) * this.StepSize.Value;
31    }
32
33    public bool IsInRange(int value) {
34      return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
35    }
36
37    public override IEnumerable<IntValue> GetCombinations() {
38      var solutions = new List<IntValue>();
39      int value = this.LowerBound.Value;
40
41      while (value <= this.UpperBound.Value) {
42        solutions.Add(new IntValue(value));
43        value += this.StepSize.Value;
44      }
45      return solutions;
46    }
47
48    protected override double CalculateSimilarityValue(IntValue a, IntValue b) {
49      double range = UpperBound.Value - LowerBound.Value;
50      double diff = Math.Abs(a.Value - b.Value);
51      return Math.Max(0, (range - (diff * 2)) / range);
52    }
53  }
54}
Note: See TracBrowser for help on using the repository browser.