Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Creators/RandomBinaryVectorCreator.cs @ 16724

Last change on this file since 16724 was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.BinaryVectorEncoding {
30  /// <summary>
31  /// Generates a new random binary vector with each element randomly initialized.
32  /// </summary>
33  [Item("RandomBinaryVectorCreator", "An operator which creates a new random binary vector with each element randomly initialized.")]
34  [StorableType("6633A89D-B65E-4F8B-A0E9-E00DC81E7420")]
35  public sealed class RandomBinaryVectorCreator : BinaryVectorCreator {
36    private const string TrueProbabilityParameterName = "TrueProbability";
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
47    [StorableConstructor]
48    private RandomBinaryVectorCreator(bool deserializing) : base(deserializing) { }
49    private RandomBinaryVectorCreator(RandomBinaryVectorCreator original, Cloner cloner) : base(original, cloner) { }
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    }
55
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      const string parameterNameWithTypo = "TruePropability";
62      if (Parameters.ContainsKey(parameterNameWithTypo) && Parameters[parameterNameWithTypo] is IValueParameter<DoubleValue>) {
63        defaultValue = ((IValueParameter<DoubleValue>)Parameters[parameterNameWithTypo]).Value.Value;
64        Parameters.Remove(parameterNameWithTypo);
65      }
66      if (!Parameters.ContainsKey(TrueProbabilityParameterName))
67        Parameters.Add(new ValueLookupParameter<DoubleValue>(TrueProbabilityParameterName, "Probability of true value", new DoubleValue(defaultValue)));
68      #endregion
69    }
70
71    /// <summary>
72    /// Generates a new random binary vector with the given <paramref name="length"/>.
73    /// </summary>
74    /// <param name="random">The random number generator.</param>
75    /// <param name="length">The length of the binary vector.</param>
76    /// <param name="trueProbability">The propability for true to occur at a certain position in the binary vector</param>
77    /// <returns>The newly created binary vector.</returns>
78    public static BinaryVector Apply(IRandom random, int length, double trueProbability = 0.5) {
79      BinaryVector result;
80
81      //Backwards compatiblity code to ensure the same behavior for existing algorithm runs
82      //remove with HL 3.4
83      if (trueProbability.IsAlmost(0.5))
84        result = new BinaryVector(length, random);
85      else {
86        var values = new bool[length];
87        for (int i = 0; i < length; i++)
88          values[i] = random.NextDouble() < trueProbability;
89        result = new BinaryVector(values);
90      }
91      return result;
92    }
93
94    protected override BinaryVector Create(IRandom random, IntValue length) {
95      if (TrueProbabilityParameter.ActualValue == null) throw new InvalidOperationException("RandomBinaryVectorCreator: Parameter " + TrueProbabilityParameter.ActualName + " could not be found.");
96      return Apply(random, length.Value, TrueProbabilityParameter.ActualValue.Value);
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.