using System; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { [StorableClass] public class SingleValuedParameterConfiguration : ParameterConfiguration { #region Constructors and Cloning [StorableConstructor] protected SingleValuedParameterConfiguration(bool deserializing) : base(deserializing) { } public SingleValuedParameterConfiguration(string parameterName, IValueParameter valueParameter) : base(parameterName, valueParameter, false) { this.Optimize = true; base.PopulateValueConfigurations(); this.ValueConfigurations.Single().Optimize = true; } public SingleValuedParameterConfiguration(string parameterName, IValueConfiguration valueConfiguration) : base() { this.ParameterName = parameterName; this.parameterDataType = valueConfiguration.ActualValue.ValueDataType; this.valueDataType = valueConfiguration.ActualValue.ValueDataType; this.discoverValidValues = false; this.validValues = new ItemSet { valueConfiguration.ActualValue.Value }; this.validTypes = new Type[] { valueConfiguration.ActualValue.ValueDataType }; this.isNullable = false; this.itemImage = valueConfiguration.ItemImage; this.ValueConfigurations = new CheckedValueConfigurationList { valueConfiguration }; this.ValueConfigurations.Single().Optimize = true; valueConfiguration.ToStringChanged += new EventHandler(valueConfiguration_ToStringChanged); this.ActualValue = new ConstrainedValue( valueConfiguration.ActualValue.Value, valueConfiguration.ActualValue.ValueDataType, this.validValues, this.IsNullable); } protected SingleValuedParameterConfiguration(SingleValuedParameterConfiguration original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new SingleValuedParameterConfiguration(this, cloner); } #endregion protected override void PopulateValueConfigurations() { // don't change valueconfigurations, since it only contains the one element specified in constructor } protected override void ClearValueConfigurations() { // do nothing } public override string ToString() { if (this.valueConfigurations.Single() is SymbolValueConfiguration) { var syVc = (SymbolValueConfiguration)this.valueConfigurations.Single(); if (this.Optimize) { return syVc.ToString() + " (Optimize)"; } else { return ActualValue.ToString(); } } else { return base.ToString(); } } protected virtual void valueConfiguration_ToStringChanged(object sender, EventArgs e) { OnToStringChanged(); } public override string ParameterInfoString { get { if (this.Optimize) { return string.Format("{0} ({1})", this.Name, this.ValueConfigurations.Single().ParameterInfoString); } else { return string.Empty; } } } } }