using System; using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { [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 void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { mutate(random, this, intValueManipulator, doubleValueManipulator); } public override void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) { cross(random, this, other, intValueCrossover, doubleValueCrossover); } 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 } }