Changeset 11410


Ignore:
Timestamp:
10/02/14 13:40:05 (5 years ago)
Author:
mkommend
Message:

#2253: Added true probability parameter to RandomBinaryVectorCreator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Creators/RandomBinaryVectorCreator.cs

    r11171 r11410  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
    2526using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2627
     
    3233  [StorableClass]
    3334  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
    3446    [StorableConstructor]
    3547    private RandomBinaryVectorCreator(bool deserializing) : base(deserializing) { }
     48
    3649    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); }
    3851
    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)));
    4161    }
    4262
     
    4767    /// <param name="length">The length of the binary vector.</param>
    4868    /// <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      }
    5182      return result;
    5283    }
    5384
    5485    protected override BinaryVector Create(IRandom random, IntValue length) {
    55       return Apply(random, length.Value);
     86      return Apply(random, length.Value, TrueProbability);
    5687    }
    5788  }
Note: See TracChangeset for help on using the changeset viewer.