#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.ParameterConfigurationEncoding { [StorableClass] public class IntValueRange : Range { #region Constructors and Cloning [StorableConstructor] protected IntValueRange(bool deserializing) : base(deserializing) { } protected IntValueRange(IntValueRange original, Cloner cloner) : base(original, cloner) { } public IntValueRange(IntValue lowerBound, IntValue upperBound, IntValue stepSize) : base(lowerBound, upperBound, stepSize) { } public override IDeepCloneable Clone(Cloner cloner) { return new IntValueRange(this, cloner); } #endregion protected override IntValue GetRandomSample(IRandom random) { int val; do { val = random.Next(LowerBound.Value / StepSize.Value, UpperBound.Value / StepSize.Value + 1) * StepSize.Value; } while (!IsInRange(val)); return new IntValue(val); } public int ApplyStepSize(int value) { return ((int)Math.Round(value / (double)this.StepSize.Value, 0)) * this.StepSize.Value; } public bool IsInRange(int value) { return value <= this.UpperBound.Value && value >= this.LowerBound.Value; } public override IEnumerable GetCombinations() { var solutions = new List(); int value = this.LowerBound.Value; while (value <= this.UpperBound.Value) { solutions.Add(new IntValue(value)); value += this.StepSize.Value; } return solutions; } protected override double CalculateSimilarityValue(IntValue a, IntValue b) { double range = UpperBound.Value - LowerBound.Value; double diff = Math.Abs(a.Value - b.Value); return Math.Max(0, (range - (diff * 2)) / range); } } }