[5112] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[6090] | 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
[5112] | 5 | using HeuristicLab.Data;
|
---|
[16574] | 6 | using HEAL.Attic;
|
---|
[5112] | 7 |
|
---|
| 8 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
[16574] | 9 | [StorableType("4D296D1B-020F-49AF-A36A-D4948C4EC0B0")]
|
---|
[5112] | 10 | public class DoubleValueRange : Range<DoubleValue> {
|
---|
| 11 |
|
---|
[6090] | 12 | public override DoubleValue LowerBound {
|
---|
| 13 | get {
|
---|
| 14 | return base.LowerBound;
|
---|
| 15 | }
|
---|
| 16 | set {
|
---|
| 17 | base.LowerBound = value;
|
---|
| 18 | }
|
---|
| 19 | }
|
---|
[8087] | 20 |
|
---|
[5112] | 21 | public DoubleValueRange(DoubleValue lowerBound, DoubleValue upperBound, DoubleValue stepSize) : base(lowerBound, upperBound, stepSize) { }
|
---|
| 22 | [StorableConstructor]
|
---|
[16574] | 23 | protected DoubleValueRange(StorableConstructorFlag _) : base(_) { }
|
---|
[5112] | 24 | protected DoubleValueRange(DoubleValueRange original, Cloner cloner) : base(original, cloner) { }
|
---|
| 25 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 26 | return new DoubleValueRange(this, cloner);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | protected override DoubleValue GetRandomSample(IRandom random) {
|
---|
| 30 | double val;
|
---|
| 31 | do {
|
---|
| 32 | val = Math.Round((random.NextDouble() * (UpperBound.Value - LowerBound.Value) + LowerBound.Value) / StepSize.Value, 0) * StepSize.Value;
|
---|
[5207] | 33 | } while (!IsInRange(val));
|
---|
[5112] | 34 | return new DoubleValue(val);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[5337] | 37 | public double ApplyStepSize(double value) {
|
---|
| 38 | return (Math.Round(value / this.StepSize.Value, 0)) * this.StepSize.Value;
|
---|
[5207] | 39 | }
|
---|
[5112] | 40 |
|
---|
[5207] | 41 | public bool IsInRange(double value) {
|
---|
| 42 | return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
|
---|
[5112] | 43 | }
|
---|
[5144] | 44 |
|
---|
| 45 | public override IEnumerable<DoubleValue> GetCombinations() {
|
---|
| 46 | var solutions = new List<DoubleValue>();
|
---|
[5207] | 47 | double value = LowerBound.Value;
|
---|
[5144] | 48 |
|
---|
| 49 | while (value <= UpperBound.Value) {
|
---|
| 50 | solutions.Add(new DoubleValue(value));
|
---|
| 51 | value += StepSize.Value;
|
---|
[5337] | 52 | value = ApplyStepSize(value);
|
---|
[5144] | 53 | }
|
---|
| 54 | return solutions;
|
---|
| 55 | }
|
---|
[5522] | 56 |
|
---|
| 57 | protected override double CalculateSimilarityValue(DoubleValue a, DoubleValue b) {
|
---|
| 58 | double range = UpperBound.Value - LowerBound.Value;
|
---|
| 59 | double diff = Math.Abs(a.Value - b.Value);
|
---|
[8087] | 60 | return Math.Max(0, (range - (diff * 2)) / range);
|
---|
[5522] | 61 | }
|
---|
[5112] | 62 | }
|
---|
| 63 | }
|
---|