source: trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Creators/RandomBinaryVectorCreator.cs @ 11411

Last change on this file since 11411 was 11411, checked in by mkommend, 6 years ago

#2253: Added default value for trueProbability and missing XML parameter comment in RandomBinaryVectorCreator.

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.BinaryVectorEncoding {
29  /// <summary>
30  /// Generates a new random binary vector with each element randomly initialized.
31  /// </summary>
32  [Item("RandomBinaryVectorCreator", "An operator which creates a new random binary vector with each element randomly initialized.")]
33  [StorableClass]
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
46    [StorableConstructor]
47    private RandomBinaryVectorCreator(bool deserializing) : base(deserializing) { }
48
49    private RandomBinaryVectorCreator(RandomBinaryVectorCreator original, Cloner cloner) : base(original, cloner) { }
50    public override IDeepCloneable Clone(Cloner cloner) { return new RandomBinaryVectorCreator(this, cloner); }
51
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)));
61    }
62
63    /// <summary>
64    /// Generates a new random binary vector with the given <paramref name="length"/>.
65    /// </summary>
66    /// <param name="random">The random number generator.</param>
67    /// <param name="length">The length of the binary vector.</param>
68    /// <param name="trueProbability">The propability for true to occur at a certain position in the binary vector</param>
69    /// <returns>The newly created binary vector.</returns>
70    public static BinaryVector Apply(IRandom random, int length, double trueProbability = 0.5) {
71      BinaryVector result;
72
73      //Backwards compatiblity code to ensure the same behavior for existing algorithm runs
74      //remove with HL 3.4
75      if (trueProbability.IsAlmost(0.5))
76      result = new BinaryVector(length, random);
77      else {
78        var values = new bool[length];
79        for (int i = 0; i < length; i++)
80          values[i] = random.NextDouble() < trueProbability;
81        result = new BinaryVector(values);
82      }
83      return result;
84    }
85
86    protected override BinaryVector Create(IRandom random, IntValue length) {
87      return Apply(random, length.Value, TrueProbability);
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.