Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/UniformOnePositionManipulator.cs @ 12726

Last change on this file since 12726 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.IntegerVectorEncoding {
30  /// <summary>
31  /// Uniformly distributed change of a single position of an integer vector.
32  /// </summary>
33  /// <remarks>
34  /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.
35  /// </remarks>
36  [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.")]
37  [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    }
51
52    [StorableConstructor]
53    protected UniformOnePositionManipulator(bool deserializing) : base(deserializing) { }
54    protected UniformOnePositionManipulator(UniformOnePositionManipulator original, Cloner cloner) : base(original, cloner) { }
55    /// <summary>
56    /// Initializes a new instance of <see cref="UniformOnePositionManipulator"/> with two parameters
57    /// (<c>Minimum</c> and <c>Maximum</c>).
58    /// </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    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new UniformOnePositionManipulator(this, cloner);
67    }
68
69    /// <summary>
70    /// Changes randomly a single position in the given integer <paramref name="vector"/>.
71    /// </summary>
72    /// <param name="random">A random number generator.</param>
73    /// <param name="vector">The integer vector to manipulate.</param>
74    /// <param name="min">The minimum value of the sampling range for
75    /// the vector element to change (inclusive).</param>
76    /// <param name="max">The maximum value of the sampling range for
77    /// 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);
81    }
82
83    /// <summary>
84    /// Changes randomly a single position in the given integer <paramref name="vector"/>.
85    /// </summary>
86    /// <remarks>Calls <see cref="Apply"/>.</remarks>
87    /// <param name="random">A random number generator.</param>
88    /// <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);
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.