#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 RangeValueConfiguration : ValueConfiguration { [Storable] protected IRange rangeConstraint; public IRange RangeConstraint { get { return rangeConstraint; } private set { if (rangeConstraint != value) { DeregisterRangeConstraintEvents(); rangeConstraint = value; RegisterRangeConstraintEvents(); } } } #region Constructor and Cloning [StorableConstructor] protected RangeValueConfiguration(bool deserializing) : base(deserializing) { } public RangeValueConfiguration() { } protected RangeValueConfiguration(RangeValueConfiguration original, Cloner cloner) : base(original, cloner) { this.rangeConstraint = cloner.Clone(original.RangeConstraint); RegisterRangeConstraintEvents(); } public RangeValueConfiguration(IItem value, Type valueDataType) : base(value, valueDataType) { if (actualValue.ValueDataType == typeof(IntValue)) { RangeConstraint = new IntValueRange(new IntValue(0), (IntValue)value.Clone(), new IntValue(1)); } else if (actualValue.ValueDataType == typeof(DoubleValue)) { RangeConstraint = new DoubleValueRange(new DoubleValue(0), (DoubleValue)value.Clone(), new DoubleValue(0.01)); } else if (actualValue.ValueDataType == typeof(PercentValue)) { RangeConstraint = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.01)); } else if (actualValue.ValueDataType == typeof(BoolValue)) { this.IsOptimizable = false; // there is nothing to configure for bools } else { RangeConstraint = null; } } public override IDeepCloneable Clone(Cloner cloner) { return new RangeValueConfiguration(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterRangeConstraintEvents(); } #endregion private void RegisterRangeConstraintEvents() { if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged); } private void DeregisterRangeConstraintEvents() { if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged -= new EventHandler(RangeConstraint_ToStringChanged); } public override string ToString() { if (ActualValue != null && ActualValue.Value != null) { if (Optimize) { return string.Format("{0} (Optimize: {1})", NumberedName, RangeConstraint); } else { return string.Format("{0}: {1}", NumberedName, ActualValue.Value); } } else { return base.ToString(); } } public override void Randomize(IRandom random) { this.actualValue.Value = rangeConstraint.GetRandomValue(random); } public override double CalculateSimilarity(IOptimizable optimizable) { var other = (IValueConfiguration)optimizable; return this.RangeConstraint.CalculateSimilarity(this.ActualValue.Value, other.ActualValue.Value); } public override string ParameterInfoString { get { return string.Empty; } } public override void CollectOptimizedParameterNames(List parameterNames, string prefix) { } public override List GetAllOptimizables() { return new List(); } #region Events private void RangeConstraint_ToStringChanged(object sender, EventArgs e) { OnToStringChanged(); } #endregion } }