Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12012 was 12012, checked in by ascheibe, 9 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.5 KB
RevLine 
[3062]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3062]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
[11909]22using System;
[4722]23using HeuristicLab.Common;
[3062]24using HeuristicLab.Core;
25using HeuristicLab.Data;
[11410]26using HeuristicLab.Parameters;
[3062]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  [StorableClass]
[4722]35  public sealed class RandomBinaryVectorCreator : BinaryVectorCreator {
[11410]36    private const string TrueProbabilityParameterName = "TruePropability";
37
[11909]38    private IValueLookupParameter<DoubleValue> TrueProbabilityParameter {
39      get { return (IValueLookupParameter<DoubleValue>)Parameters[TrueProbabilityParameterName]; }
[11410]40    }
41
[11909]42    public DoubleValue TrueProbability {
43      get { return TrueProbabilityParameter.Value; }
44      set { TrueProbabilityParameter.Value = value; }
[11410]45    }
46
[4722]47    [StorableConstructor]
48    private RandomBinaryVectorCreator(bool deserializing) : base(deserializing) { }
49    private RandomBinaryVectorCreator(RandomBinaryVectorCreator original, Cloner cloner) : base(original, cloner) { }
[11410]50    public override IDeepCloneable Clone(Cloner cloner) { return new RandomBinaryVectorCreator(this, cloner); }
51    public RandomBinaryVectorCreator()
52      : base() {
[11909]53      Parameters.Add(new ValueLookupParameter<DoubleValue>(TrueProbabilityParameterName, "Probability of true value", new DoubleValue(0.5)));
[4722]54    }
55
[11410]56    [StorableHook(HookType.AfterDeserialization)]
57    private void AfterDeserialization() {
[11909]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      }
[11410]65      if (!Parameters.ContainsKey(TrueProbabilityParameterName))
[11909]66        Parameters.Add(new ValueLookupParameter<DoubleValue>(TrueProbabilityParameterName, "Probability of true value", new DoubleValue(defaultValue)));
67      #endregion
[11410]68    }
69
[3062]70    /// <summary>
71    /// Generates a new random binary vector with the given <paramref name="length"/>.
72    /// </summary>
73    /// <param name="random">The random number generator.</param>
74    /// <param name="length">The length of the binary vector.</param>
[11411]75    /// <param name="trueProbability">The propability for true to occur at a certain position in the binary vector</param>
[3062]76    /// <returns>The newly created binary vector.</returns>
[11411]77    public static BinaryVector Apply(IRandom random, int length, double trueProbability = 0.5) {
[11410]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))
[11909]83        result = new BinaryVector(length, random);
[11410]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      }
[3062]90      return result;
91    }
92
93    protected override BinaryVector Create(IRandom random, IntValue length) {
[11909]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);
[3062]96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.