1 | using System;
|
---|
2 |
|
---|
3 | namespace 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 | Name = "Boolean random";
|
---|
24 | IsEnabled = isEnabled;
|
---|
25 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowTrueParameterName, new BoolValue(allowTrue)));
|
---|
26 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowFalseParameterName, new BoolValue(allowFalse)));
|
---|
27 | }
|
---|
28 |
|
---|
29 | [StorableConstructor]
|
---|
30 | public BooleanRandomErcValue(bool deserializing) : base(deserializing) { }
|
---|
31 |
|
---|
32 | public BooleanRandomErcValue(BooleanRandomErcValue origin, Cloner cloner) : base(origin, cloner) {
|
---|
33 | this.AllowFalse = origin.AllowFalse;
|
---|
34 | this.AllowTrue = origin.AllowTrue;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public IValueParameter<BoolValue> AllowFalseParameter
|
---|
38 | {
|
---|
39 | get { return (IValueParameter<BoolValue>)Parameters[AllowFalseParameterName]; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public bool AllowFalse
|
---|
43 | {
|
---|
44 | get { return AllowFalseParameter.Value.Value; }
|
---|
45 | set { AllowFalseParameter.Value.Value = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public IValueParameter<BoolValue> AllowTrueParameter
|
---|
49 | {
|
---|
50 | get { return (IValueParameter<BoolValue>)Parameters[AllowTrueParameterName]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public bool AllowTrue
|
---|
54 | {
|
---|
55 | get { return AllowTrueParameter.Value.Value; }
|
---|
56 | set { AllowTrueParameter.Value.Value = value; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new BooleanRandomErcValue(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public override bool GetErcValue(IRandom random) {
|
---|
64 | if (!this.AllowTrue && !this.AllowFalse) throw new InvalidOperationException();
|
---|
65 | if (!this.AllowFalse) return true;
|
---|
66 | if (!this.AllowTrue) return false;
|
---|
67 |
|
---|
68 | return random.NextDouble() >= 0.5;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|