using System; using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HEAL.Attic; namespace HeuristicLab.Problems.MetaOptimization { [StorableType("0A899E4E-9BC6-4A29-B015-F529C94BF752")] public class PercentValueRange : Range { public PercentValueRange(PercentValue lowerBound, PercentValue upperBound, PercentValue stepSize) : base(lowerBound, upperBound, stepSize) { } [StorableConstructor] protected PercentValueRange(StorableConstructorFlag _) : base(_) { } protected PercentValueRange(PercentValueRange original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new PercentValueRange(this, cloner); } protected override PercentValue 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 PercentValue(val); } public DoubleValueRange AsDoubleValueRange() { return new DoubleValueRange(LowerBound, UpperBound, StepSize); } public double ApplyStepSize(double value) { return ((int)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 PercentValue(value)); value += StepSize.Value; value = ApplyStepSize(value); } return solutions; } protected override double CalculateSimilarityValue(PercentValue a, PercentValue b) { double range = UpperBound.Value - LowerBound.Value; double diff = Math.Abs(a.Value - b.Value); return Math.Max(0, (range - (diff * 2)) / range); } } }