Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/06/15 08:30:19 (9 years ago)
Author:
abeham
Message:

#2253: merged to stable

Location:
stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Creators/RandomBinaryVectorCreator.cs

    r11170 r11929  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Data;
     26using HeuristicLab.Parameters;
    2527using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2628
     
    3234  [StorableClass]
    3335  public sealed class RandomBinaryVectorCreator : BinaryVectorCreator {
     36    private const string TrueProbabilityParameterName = "TruePropability";
     37
     38    private IValueLookupParameter<DoubleValue> TrueProbabilityParameter {
     39      get { return (IValueLookupParameter<DoubleValue>)Parameters[TrueProbabilityParameterName]; }
     40    }
     41
     42    public DoubleValue TrueProbability {
     43      get { return TrueProbabilityParameter.Value; }
     44      set { TrueProbabilityParameter.Value = value; }
     45    }
     46
    3447    [StorableConstructor]
    3548    private RandomBinaryVectorCreator(bool deserializing) : base(deserializing) { }
    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); }
     51    public RandomBinaryVectorCreator()
     52      : base() {
     53      Parameters.Add(new ValueLookupParameter<DoubleValue>(TrueProbabilityParameterName, "Probability of true value", new DoubleValue(0.5)));
     54    }
    3855
    39     public override IDeepCloneable Clone(Cloner cloner) {
    40       return new RandomBinaryVectorCreator(this, cloner);
     56    [StorableHook(HookType.AfterDeserialization)]
     57    private void AfterDeserialization() {
     58      // BackwardsCompatibility3.3
     59      #region Backwards compatible code, remove with 3.4
     60      var defaultValue = 0.5;
     61      if (Parameters.ContainsKey(TrueProbabilityParameterName) && Parameters[TrueProbabilityParameterName] is IFixedValueParameter<DoubleValue>) {
     62        defaultValue = ((IFixedValueParameter<DoubleValue>)Parameters[TrueProbabilityParameterName]).Value.Value;
     63        Parameters.Remove(TrueProbabilityParameterName);
     64      }
     65      if (!Parameters.ContainsKey(TrueProbabilityParameterName))
     66        Parameters.Add(new ValueLookupParameter<DoubleValue>(TrueProbabilityParameterName, "Probability of true value", new DoubleValue(defaultValue)));
     67      #endregion
    4168    }
    4269
     
    4673    /// <param name="random">The random number generator.</param>
    4774    /// <param name="length">The length of the binary vector.</param>
     75    /// <param name="trueProbability">The propability for true to occur at a certain position in the binary vector</param>
    4876    /// <returns>The newly created binary vector.</returns>
    49     public static BinaryVector Apply(IRandom random, int length) {
    50       BinaryVector result = new BinaryVector(length, random);
     77    public static BinaryVector Apply(IRandom random, int length, double trueProbability = 0.5) {
     78      BinaryVector result;
     79
     80      //Backwards compatiblity code to ensure the same behavior for existing algorithm runs
     81      //remove with HL 3.4
     82      if (trueProbability.IsAlmost(0.5))
     83        result = new BinaryVector(length, random);
     84      else {
     85        var values = new bool[length];
     86        for (int i = 0; i < length; i++)
     87          values[i] = random.NextDouble() < trueProbability;
     88        result = new BinaryVector(values);
     89      }
    5190      return result;
    5291    }
    5392
    5493    protected override BinaryVector Create(IRandom random, IntValue length) {
    55       return Apply(random, length.Value);
     94      if (TrueProbabilityParameter.ActualValue == null) throw new InvalidOperationException("RandomBinaryVectorCreator: Parameter " + TrueProbabilityParameter.ActualName + " could not be found.");
     95      return Apply(random, length.Value, TrueProbabilityParameter.ActualValue.Value);
    5696    }
    5797  }
Note: See TracChangeset for help on using the changeset viewer.