1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022014 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 HeuristicLab.Common;


23  using HeuristicLab.Core;


24  using HeuristicLab.Data;


25  using HeuristicLab.Parameters;


26  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


27 


28  namespace 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  /// <returns>The newly created binary vector.</returns>


69  public static BinaryVector Apply(IRandom random, int length, double trueProbability) {


70  BinaryVector result;


71 


72  //Backwards compatiblity code to ensure the same behavior for existing algorithm runs


73  //remove with HL 3.4


74  if (trueProbability.IsAlmost(0.5))


75  result = new BinaryVector(length, random);


76  else {


77  var values = new bool[length];


78  for (int i = 0; i < length; i++)


79  values[i] = random.NextDouble() < trueProbability;


80  result = new BinaryVector(values);


81  }


82  return result;


83  }


84 


85  protected override BinaryVector Create(IRandom random, IntValue length) {


86  return Apply(random, length.Value, TrueProbability);


87  }


88  }


89  }

