using System; namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Boolean { using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; [StorableClass] public class BooleanRandomErc : WeightedErcItem { private const string AllowTrueParameterName = "Allow true"; private const string AllowFalseParameterName = "Allow false"; public BooleanRandomErc() : this(true, true, true) { } public BooleanRandomErc(bool isEnabled, bool allowTrue, bool allowFalse, double weight = 1d) : base(isEnabled, weight) { if (!allowTrue && allowFalse) { throw new InvalidOperationException("true and false values are disabled"); } Name = "Boolean random"; Parameters.Add(new FixedValueParameter(AllowTrueParameterName, new BoolValue(allowTrue))); Parameters.Add(new FixedValueParameter(AllowFalseParameterName, new BoolValue(allowFalse))); } [StorableConstructor] public BooleanRandomErc(bool deserializing) : base(deserializing) { } public BooleanRandomErc(BooleanRandomErc origin, Cloner cloner) : base(origin, cloner) { AllowFalse = origin.AllowFalse; AllowTrue = origin.AllowTrue; } public IValueParameter AllowFalseParameter { get { return (IValueParameter)Parameters[AllowFalseParameterName]; } } public bool AllowFalse { get { return AllowFalseParameter.Value.Value; } set { AllowFalseParameter.Value.Value = value; } } public IValueParameter AllowTrueParameter { get { return (IValueParameter)Parameters[AllowTrueParameterName]; } } public bool AllowTrue { get { return AllowTrueParameter.Value.Value; } set { AllowTrueParameter.Value.Value = value; } } public override IDeepCloneable Clone(Cloner cloner) { return new BooleanRandomErc(this, cloner); } public override bool GetErcValue(IRandom random) { if (!AllowTrue && !AllowFalse) throw new InvalidOperationException(); if (!AllowFalse) return true; if (!AllowTrue) return false; return random.NextDouble() >= 0.5; } } }