Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/17/12 15:05:11 (12 years ago)
Author:
abeham
Message:

#1775: reintegrated branch

Location:
trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding

  • trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/UniformOnePositionManipulator.cs

    r7259 r8019  
    3636  [Item("UniformOnePositionManipulator", " Uniformly distributed change of a single position of an integer vector. It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
    3737  [StorableClass]
    38   public class UniformOnePositionManipulator : IntegerVectorManipulator {
    39     /// <summary>
    40     /// The lower bound of the values in the int vector.
    41     /// </summary>
    42     public ValueLookupParameter<IntValue> MinimumParameter {
    43       get { return (ValueLookupParameter<IntValue>)Parameters["Minimum"]; }
    44     }
    45     /// <summary>
    46     /// The upper bound of the values in the int vector.
    47     /// </summary>
    48     public ValueLookupParameter<IntValue> MaximumParameter {
    49       get { return (ValueLookupParameter<IntValue>)Parameters["Maximum"]; }
    50     }
     38  public class UniformOnePositionManipulator : BoundedIntegerVectorManipulator {
    5139
    5240    [StorableConstructor]
     
    5745    /// (<c>Minimum</c> and <c>Maximum</c>).
    5846    /// </summary>
    59     public UniformOnePositionManipulator()
    60       : base() {
    61       Parameters.Add(new ValueLookupParameter<IntValue>("Minimum", "Minimum of the sampling range for the vector element (included)"));
    62       Parameters.Add(new ValueLookupParameter<IntValue>("Maximum", "Maximum of the sampling range for the vector element (excluded)"));
    63     }
     47    public UniformOnePositionManipulator() : base() { }
    6448
    6549    public override IDeepCloneable Clone(Cloner cloner) {
    6650      return new UniformOnePositionManipulator(this, cloner);
    6751    }
     52
     53    // BackwardsCompatibility3.3
     54    #region Backwards compatible code, remove with 3.4
     55    [StorableHook(HookType.AfterDeserialization)]
     56    private void AfterDeserialization() {
     57      if (!Parameters.ContainsKey("Bounds")) {
     58        var min = ((IValueLookupParameter<IntValue>)Parameters["Minimum"]).Value as IntValue;
     59        var max = ((IValueLookupParameter<IntValue>)Parameters["Maximum"]).Value as IntValue;
     60        Parameters.Remove("Minimum");
     61        Parameters.Remove("Maximum");
     62        Parameters.Add(new ValueLookupParameter<IntMatrix>("Bounds", "The bounds matrix can contain one row for each dimension with three columns specifying minimum (inclusive), maximum (exclusive), and step size. If less rows are given the matrix is cycled."));
     63        if (min != null && max != null) {
     64          BoundsParameter.Value = new IntMatrix(new int[,] { { min.Value, max.Value, 1 } });
     65        }
     66      }
     67    }
     68    #endregion
    6869
    6970    /// <summary>
     
    7677    /// <param name="max">The maximum value of the sampling range for
    7778    /// the vector element to change (exclusive).</param>
    78     public static void Apply(IRandom random, IntegerVector vector, IntValue min, IntValue max) {
    79       int index = random.Next(vector.Length);
    80       vector[index] = random.Next(min.Value, max.Value);
     79    /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
     80    public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds) {
     81      Manipulate(random, vector, bounds, random.Next(vector.Length));
     82    }
     83
     84    public static void Manipulate(IRandom random, IntegerVector vector, IntMatrix bounds, int index) {
     85      if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("UniformOnePositionManipulator: Invalid bounds specified", "bounds");
     86      int min = bounds[index % bounds.Rows, 0], max = bounds[index % bounds.Rows, 1], step = 1;
     87      if (bounds.Columns > 2) step = bounds[index % bounds.Rows, 2];
     88      vector[index] = RoundFeasible(min, max, step, random.Next(min, max + 1));
    8189    }
    8290
     
    8795    /// <param name="random">A random number generator.</param>
    8896    /// <param name="vector">The integer vector to manipulate.</param>
    89     protected override void Manipulate(IRandom random, IntegerVector vector) {
    90       if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MinimumParameter.ActualName + " could not be found.");
    91       if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MaximumParameter.ActualName + " could not be found.");
    92       Apply(random, vector, MinimumParameter.ActualValue, MaximumParameter.ActualValue);
     97    /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
     98    protected override void ManipulateBounded(IRandom random, IntegerVector vector, IntMatrix bounds) {
     99      if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + BoundsParameter.ActualName + " could not be found.");
     100      Apply(random, vector, bounds);
    93101    }
    94102  }
Note: See TracChangeset for help on using the changeset viewer.