using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Data; using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Core; namespace HeuristicLab.Problems.MetaOptimization { [StorableClass] public class IntValueRange : Range { public IntValueRange(IntValue lowerBound, IntValue upperBound, IntValue stepSize) : base(lowerBound, upperBound, stepSize) { } public IntValueRange() { } [StorableConstructor] protected IntValueRange(bool deserializing) : base(deserializing) { } protected IntValueRange(IntValueRange original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new IntValueRange(this, cloner); } protected override IntValue GetRandomSample(IRandom random) { int val; do { val = random.Next(LowerBound.Value / StepSize.Value, UpperBound.Value / StepSize.Value + 1) * StepSize.Value; } while (val < LowerBound.Value || val > UpperBound.Value); return new IntValue(val); } public void Fix(IntValue value) { // apply stepsize value.Value = ((int)Math.Round(value.Value / (double)this.StepSize.Value, 0)) * this.StepSize.Value; // repair bounds if (value.Value > this.UpperBound.Value) value.Value = this.UpperBound.Value; if (value.Value < this.LowerBound.Value) value.Value = this.LowerBound.Value; } public override IEnumerable GetCombinations() { var solutions = new List(); int value = (this.LowerBound.Value / StepSize.Value) * StepSize.Value; if (value < this.LowerBound.Value) value += StepSize.Value; while (value <= this.UpperBound.Value) { solutions.Add(new IntValue(value)); value += this.StepSize.Value; } return solutions; } } }