Changeset 11929 for stable


Ignore:
Timestamp:
02/06/15 08:30:19 (6 years ago)
Author:
abeham
Message:

#2253: merged to stable

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Creators/RandomBinaryVectorCreator.cs

    r11170 r11929  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Data;
     26using HeuristicLab.Parameters;
    2527using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2628
     
    3234  [StorableClass]
    3335  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
    3447    [StorableConstructor]
    3548    private RandomBinaryVectorCreator(bool deserializing) : base(deserializing) { }
    3649    private RandomBinaryVectorCreator(RandomBinaryVectorCreator original, Cloner cloner) : base(original, cloner) { }
    37     public RandomBinaryVectorCreator() : base() { }
     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    }
    3855
    39     public override IDeepCloneable Clone(Cloner cloner) {
    40       return new RandomBinaryVectorCreator(this, cloner);
     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
    4168    }
    4269
     
    4673    /// <param name="random">The random number generator.</param>
    4774    /// <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>
    4876    /// <returns>The newly created binary vector.</returns>
    49     public static BinaryVector Apply(IRandom random, int length) {
    50       BinaryVector result = new BinaryVector(length, random);
     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      }
    5190      return result;
    5291    }
    5392
    5493    protected override BinaryVector Create(IRandom random, IntValue length) {
    55       return Apply(random, length.Value);
     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);
    5696    }
    5797  }
  • stable/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Crossovers/NPointCrossover.cs

    r11170 r11929  
    4141    /// Number of crossover points.
    4242    /// </summary>
    43     public ValueLookupParameter<IntValue> NParameter {
    44       get { return (ValueLookupParameter<IntValue>)Parameters["N"]; }
     43    public IValueLookupParameter<IntValue> NParameter {
     44      get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
    4545    }
    4646
     
    137137      if (NParameter.ActualValue == null) throw new InvalidOperationException("NPointCrossover: Parameter " + NParameter.ActualName + " could not be found.");
    138138
    139       return Apply(random, parents[0], parents[1], NParameter.Value);
     139      return Apply(random, parents[0], parents[1], NParameter.ActualValue);
    140140    }
    141141  }
  • stable/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Manipulators/SomePositionsBitflipManipulator.cs

    r11170 r11929  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    3940    /// Mmutation probability for each position.
    4041    /// </summary>
    41     public ValueLookupParameter<DoubleValue> MutationProbabilityParameter {
    42       get { return (ValueLookupParameter<DoubleValue>)Parameters["MutationProbability"]; }
     42    public IValueLookupParameter<DoubleValue> MutationProbabilityParameter {
     43      get { return (IValueLookupParameter<DoubleValue>)Parameters["MutationProbability"]; }
    4344    }
    4445
     
    7879    /// <param name="realVector">The vector of binary values to manipulate.</param>
    7980    protected override void Manipulate(IRandom random, BinaryVector binaryVector) {
    80       Apply(random, binaryVector, MutationProbabilityParameter.Value);
     81      if (MutationProbabilityParameter.ActualValue == null) throw new InvalidOperationException("SomePositionsBitflipManipulator: Parameter " + MutationProbabilityParameter.ActualName + " could not be found.");
     82      Apply(random, binaryVector, MutationProbabilityParameter.ActualValue);
    8183    }
    8284  }
  • stable/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Moves/OneBitflipMove/StochasticOneBitflipMultiMoveGenerator.cs

    r11170 r11929  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    3031  [Item("StochasticOneBitflipMultiMoveGenerator", "Randomly samples n from all possible one bitflip moves from a given BinaryVector.")]
    3132  [StorableClass]
    32   public class StochasticOneBitflipMultiMoveGenerator : OneBitflipMoveGenerator, IMultiMoveGenerator {
     33  public class StochasticOneBitflipMultiMoveGenerator : OneBitflipMoveGenerator, IStochasticOperator, IMultiMoveGenerator {
    3334    public ILookupParameter<IRandom> RandomParameter {
    3435      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     
    6667    protected override OneBitflipMove[] GenerateMoves(BinaryVector binaryVector) {
    6768      IRandom random = RandomParameter.ActualValue;
     69      if (SampleSizeParameter.ActualValue == null) throw new InvalidOperationException("StochasticOneBitflipMultiMoveGenerator: Parameter " + SampleSizeParameter.ActualName + " could not be found.");
    6870      return Apply(binaryVector, random, SampleSizeParameter.ActualValue.Value);
    6971    }
Note: See TracChangeset for help on using the changeset viewer.