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 BooleanRandomErc : WeightedErcItem<bool> {
|
---|
12 |
|
---|
13 | private const string AllowTrueParameterName = "Allow true";
|
---|
14 | private const string AllowFalseParameterName = "Allow false";
|
---|
15 |
|
---|
16 | public BooleanRandomErc() : this(true, true, true) { }
|
---|
17 |
|
---|
18 | public BooleanRandomErc(bool isEnabled, bool allowTrue, bool allowFalse, double weight = 1d) : base(isEnabled, weight) {
|
---|
19 | if (!allowTrue && allowFalse) {
|
---|
20 | throw new InvalidOperationException("true and false values are disabled");
|
---|
21 | }
|
---|
22 |
|
---|
23 | Name = "Boolean random";
|
---|
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 BooleanRandomErc(bool deserializing) : base(deserializing) { }
|
---|
30 |
|
---|
31 | public BooleanRandomErc(BooleanRandomErc origin, Cloner cloner) : base(origin, cloner) {
|
---|
32 | AllowFalse = origin.AllowFalse;
|
---|
33 | 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 BooleanRandomErc(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override bool GetErcValue(IRandom random) {
|
---|
63 | if (!AllowTrue && !AllowFalse) throw new InvalidOperationException();
|
---|
64 | if (!AllowFalse) return true;
|
---|
65 | if (!AllowTrue) return false;
|
---|
66 |
|
---|
67 | return random.NextDouble() >= 0.5;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|