using System; using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HEAL.Attic; namespace HeuristicLab.Problems.MetaOptimization { [StorableType("4D296D1B-020F-49AF-A36A-D4948C4EC0B0")] public class DoubleValueRange : Range { public override DoubleValue LowerBound { get { return base.LowerBound; } set { base.LowerBound = value; } } public DoubleValueRange(DoubleValue lowerBound, DoubleValue upperBound, DoubleValue stepSize) : base(lowerBound, upperBound, stepSize) { } [StorableConstructor] protected DoubleValueRange(StorableConstructorFlag _) : base(_) { } protected DoubleValueRange(DoubleValueRange original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new DoubleValueRange(this, cloner); } protected override DoubleValue GetRandomSample(IRandom random) { double val; do { val = Math.Round((random.NextDouble() * (UpperBound.Value - LowerBound.Value) + LowerBound.Value) / StepSize.Value, 0) * StepSize.Value; } while (!IsInRange(val)); return new DoubleValue(val); } public double ApplyStepSize(double value) { return (Math.Round(value / this.StepSize.Value, 0)) * this.StepSize.Value; } public bool IsInRange(double value) { return value <= this.UpperBound.Value && value >= this.LowerBound.Value; } public override IEnumerable GetCombinations() { var solutions = new List(); double value = LowerBound.Value; while (value <= UpperBound.Value) { solutions.Add(new DoubleValue(value)); value += StepSize.Value; value = ApplyStepSize(value); } return solutions; } protected override double CalculateSimilarityValue(DoubleValue a, DoubleValue b) { double range = UpperBound.Value - LowerBound.Value; double diff = Math.Abs(a.Value - b.Value); return Math.Max(0, (range - (diff * 2)) / range); } } }