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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace 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]
|
---|
35 | 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 |
|
---|
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 | 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
|
---|
68 | }
|
---|
69 |
|
---|
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>
|
---|
75 | /// <param name="trueProbability">The propability for true to occur at a certain position in the binary vector</param>
|
---|
76 | /// <returns>The newly created binary vector.</returns>
|
---|
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 | }
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected override BinaryVector Create(IRandom random, IntValue length) {
|
---|
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);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|