- Timestamp:
- 10/02/14 13:40:05 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Creators/RandomBinaryVectorCreator.cs
r11171 r11410 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Data; 25 using HeuristicLab.Parameters; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 27 … … 32 33 [StorableClass] 33 34 public sealed class RandomBinaryVectorCreator : BinaryVectorCreator { 35 private const string TrueProbabilityParameterName = "TruePropability"; 36 37 private IFixedValueParameter<DoubleValue> TrueProbabilityParameter { 38 get { return (IFixedValueParameter<DoubleValue>)Parameters[TrueProbabilityParameterName]; } 39 } 40 41 public double TrueProbability { 42 get { return TrueProbabilityParameter.Value.Value; } 43 set { TrueProbabilityParameter.Value.Value = value; } 44 } 45 34 46 [StorableConstructor] 35 47 private RandomBinaryVectorCreator(bool deserializing) : base(deserializing) { } 48 36 49 private RandomBinaryVectorCreator(RandomBinaryVectorCreator original, Cloner cloner) : base(original, cloner) { } 37 public RandomBinaryVectorCreator() : base() {}50 public override IDeepCloneable Clone(Cloner cloner) { return new RandomBinaryVectorCreator(this, cloner); } 38 51 39 public override IDeepCloneable Clone(Cloner cloner) { 40 return new RandomBinaryVectorCreator(this, cloner); 52 public RandomBinaryVectorCreator() 53 : base() { 54 Parameters.Add(new FixedValueParameter<DoubleValue>(TrueProbabilityParameterName, "Probability of true value", new DoubleValue(0.5))); 55 } 56 57 [StorableHook(HookType.AfterDeserialization)] 58 private void AfterDeserialization() { 59 if (!Parameters.ContainsKey(TrueProbabilityParameterName)) 60 Parameters.Add(new FixedValueParameter<DoubleValue>(TrueProbabilityParameterName, "Probability of true value", new DoubleValue(0.5))); 41 61 } 42 62 … … 47 67 /// <param name="length">The length of the binary vector.</param> 48 68 /// <returns>The newly created binary vector.</returns> 49 public static BinaryVector Apply(IRandom random, int length) { 50 BinaryVector result = new BinaryVector(length, random); 69 public static BinaryVector Apply(IRandom random, int length, double trueProbability) { 70 BinaryVector result; 71 72 //Backwards compatiblity code to ensure the same behavior for existing algorithm runs 73 //remove with HL 3.4 74 if (trueProbability.IsAlmost(0.5)) 75 result = new BinaryVector(length, random); 76 else { 77 var values = new bool[length]; 78 for (int i = 0; i < length; i++) 79 values[i] = random.NextDouble() < trueProbability; 80 result = new BinaryVector(values); 81 } 51 82 return result; 52 83 } 53 84 54 85 protected override BinaryVector Create(IRandom random, IntValue length) { 55 return Apply(random, length.Value );86 return Apply(random, length.Value, TrueProbability); 56 87 } 57 88 }
Note: See TracChangeset
for help on using the changeset viewer.