Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Manipulators/UniformOnePositionManipulator.cs @ 2913

Last change on this file since 2913 was 2913, checked in by abeham, 15 years ago

checked and corrected (where necessary) BLX-a, BLX-a-b, as well the SinglePointCrossover
checked the UniformOnePositionManipulator
fixed a copy-paste oversight in RealVectorManipulator
#890

File size: 4.2 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.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.RealVector {
29  /// <summary>
30  /// Uniformly distributed change of a single position in a real vector.
31  /// </summary>
32  /// <remarks>
33  /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.
34  /// </remarks>
35  [Item("UniformOnePositionManipulator", "Changes a single position in the vector by sampling uniformly from the interval [Minimum, Maximum). It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
36  [EmptyStorableClass]
37  public class UniformOnePositionManipulator : RealVectorManipulator {
38    public ValueLookupParameter<DoubleData> MinimumParameter {
39      get { return (ValueLookupParameter<DoubleData>)Parameters["Minimum"]; }
40    }
41
42    public ValueLookupParameter<DoubleData> MaximumParameter {
43      get { return (ValueLookupParameter<DoubleData>)Parameters["Maximum"]; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="UniformOnePositionManipulator"/> with two parameters
48    /// (<c>Minimum</c> and <c>Maximum</c>).
49    /// </summary>
50    public UniformOnePositionManipulator() {
51      Parameters.Add(new ValueLookupParameter<DoubleData>("Minimum", "Minimum of the sampling range for the vector element (included)"));
52      Parameters.Add(new ValueLookupParameter<DoubleData>("Maximum", "Maximum of the sampling range for the vector element (excluded)"));
53    }
54
55    /// <summary>
56    /// Changes randomly a single position in the given real <paramref name="vector"/>.
57    /// </summary>
58    /// <param name="random">A random number generator.</param>
59    /// <param name="vector">The real vector to manipulate.</param>
60    /// <param name="min">The minimum value of the sampling range for
61    /// the vector element to change (inclusive).</param>
62    /// <param name="max">The maximum value of the sampling range for
63    /// the vector element to change (exclusive).</param>
64    public static void Apply(IRandom random, DoubleArrayData vector, DoubleData min, DoubleData max) {
65      int index = random.Next(vector.Length);
66      vector[index] = min.Value + random.NextDouble() * (max.Value - min.Value);
67    }
68
69    /// <summary>
70    /// Checks if the minimum and maximum parameters are available and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleData, DoubleData)"/>.
71    /// </summary>
72    /// <param name="random">The random number generator to use.</param>
73    /// <param name="realVector">The real vector to manipulate.</param>
74    protected override void Manipulate(IRandom random, DoubleArrayData realVector) {
75      if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MinimumParameter.ActualName + " could not be found.");
76      if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MaximumParameter.ActualName + " could not be found.");
77      Apply(random, realVector, MinimumParameter.ActualValue, MaximumParameter.ActualValue);
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.