Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis.Base/Erc/Boolean/BooleanRandomErcValue.cs @ 14897

Last change on this file since 14897 was 14897, checked in by pkimmesw, 7 years ago

#2665 Dynamic ErcValues, Separate Push from BenchmarkSuite Push

File size: 2.3 KB
Line 
1using System;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Boolean {
4  using HeuristicLab.Common;
5  using HeuristicLab.Core;
6  using HeuristicLab.Data;
7  using HeuristicLab.Parameters;
8  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10  [StorableClass]
11  public class BooleanRandomErcValue : WeightedErcValueItem<bool> {
12
13    private const string AllowTrueParameterName = "Allow true";
14    private const string AllowFalseParameterName = "Allow false";
15
16    public BooleanRandomErcValue() : this(false, true, true) { }
17
18    public BooleanRandomErcValue(bool isEnabled, bool allowTrue, bool allowFalse) {
19      if (!allowTrue && allowFalse) {
20        throw new InvalidOperationException("true and false values are disabled");
21      }
22
23      IsEnabled = isEnabled;
24      Parameters.Add(new FixedValueParameter<BoolValue>(AllowTrueParameterName, new BoolValue(allowTrue)));
25      Parameters.Add(new FixedValueParameter<BoolValue>(AllowFalseParameterName, new BoolValue(allowFalse)));
26    }
27
28    [StorableConstructor]
29    public BooleanRandomErcValue(bool deserializing) : base(deserializing) { }
30
31    public BooleanRandomErcValue(BooleanRandomErcValue origin, Cloner cloner) : base(origin, cloner) {
32      this.AllowFalse = origin.AllowFalse;
33      this.AllowTrue = origin.AllowTrue;
34    }
35
36    public IValueParameter<BoolValue> AllowFalseParameter
37    {
38      get { return (IValueParameter<BoolValue>)Parameters[AllowFalseParameterName]; }
39    }
40
41    public bool AllowFalse
42    {
43      get { return AllowFalseParameter.Value.Value; }
44      set { AllowFalseParameter.Value.Value = value; }
45    }
46
47    public IValueParameter<BoolValue> AllowTrueParameter
48    {
49      get { return (IValueParameter<BoolValue>)Parameters[AllowTrueParameterName]; }
50    }
51
52    public bool AllowTrue
53    {
54      get { return AllowTrueParameter.Value.Value; }
55      set { AllowTrueParameter.Value.Value = value; }
56    }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new BooleanRandomErcValue(this, cloner);
60    }
61
62    public override bool GetErcValue(IRandom random) {
63      if (!this.AllowTrue && !this.AllowFalse) throw new InvalidOperationException();
64      if (!this.AllowFalse) return true;
65      if (!this.AllowTrue) return false;
66
67      return random.NextDouble() >= 0.5;
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.