namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc { using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Base.Weighted; [StorableClass] public abstract class ErcOptionConvertible : ItemCollection>, IErcValueItem { private readonly Converter converter; protected ErcOptionConvertible(Converter converter) : this(converter, false) { } protected ErcOptionConvertible(Converter converter, bool isEnabled, params IWeightedErcValueItem[] values) { this.converter = converter; IsEnabled = isEnabled; AddRange(values); } [StorableConstructor] protected ErcOptionConvertible(bool deserializing) : base(deserializing) { } protected ErcOptionConvertible(ErcOptionConvertible origin, Cloner cloner) : base(origin, cloner) { } public bool IsEnabled { get; set; } public TValue GetErcValue(IRandom random) { if (Count == 0 || !IsEnabled) return default(TValue); var ercValueItem = list.RandomWeightedOrDefault(random); return ercValueItem == null ? default(TValue) : converter(ercValueItem.GetErcValue(random)); } } }