[5112] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[8087] | 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
[5112] | 5 | using HeuristicLab.Data;
|
---|
| 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 7 |
|
---|
| 8 | namespace 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 | }
|
---|