[3032] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.IntVector {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Uniformly distributed change of a single position of an integer vector.
|
---|
| 33 | /// </summary>
|
---|
| 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 | /// 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 | /// </remarks>
|
---|
| 38 | [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.")]
|
---|
| 39 | [StorableClass]
|
---|
| 40 | public class UniformOnePositionManipulator : IntVectorManipulator {
|
---|
| 41 | /// <summary>
|
---|
| 42 | /// The lower bound of the values in the int vector.
|
---|
| 43 | /// </summary>
|
---|
[3048] | 44 | public ValueLookupParameter<IntValue> MinimumParameter {
|
---|
| 45 | get { return (ValueLookupParameter<IntValue>)Parameters["Minimum"]; }
|
---|
[3032] | 46 | }
|
---|
| 47 | /// <summary>
|
---|
| 48 | /// The upper bound of the values in the int vector.
|
---|
| 49 | /// </summary>
|
---|
[3048] | 50 | public ValueLookupParameter<IntValue> MaximumParameter {
|
---|
| 51 | get { return (ValueLookupParameter<IntValue>)Parameters["Maximum"]; }
|
---|
[3032] | 52 | }
|
---|
| 53 |
|
---|
| 54 | /// <summary>
|
---|
| 55 | /// Initializes a new instance of <see cref="UniformOnePositionManipulator"/> with two parameters
|
---|
| 56 | /// (<c>Minimum</c> and <c>Maximum</c>).
|
---|
| 57 | /// </summary>
|
---|
| 58 | public UniformOnePositionManipulator() {
|
---|
[3048] | 59 | Parameters.Add(new ValueLookupParameter<IntValue>("Minimum", "Minimum of the sampling range for the vector element (included)"));
|
---|
| 60 | Parameters.Add(new ValueLookupParameter<IntValue>("Maximum", "Maximum of the sampling range for the vector element (excluded)"));
|
---|
[3032] | 61 | }
|
---|
| 62 |
|
---|
| 63 | /// <summary>
|
---|
| 64 | /// Changes randomly a single position in the given integer <paramref name="vector"/>.
|
---|
| 65 | /// </summary>
|
---|
| 66 | /// <param name="random">A random number generator.</param>
|
---|
| 67 | /// <param name="vector">The integer vector to manipulate.</param>
|
---|
| 68 | /// <param name="min">The minimum value of the sampling range for
|
---|
| 69 | /// the vector element to change (inclusive).</param>
|
---|
| 70 | /// <param name="max">The maximum value of the sampling range for
|
---|
| 71 | /// the vector element to change (exclusive).</param>
|
---|
[3048] | 72 | public static void Apply(IRandom random, IntArray vector, IntValue min, IntValue max) {
|
---|
[3032] | 73 | int index = random.Next(vector.Length);
|
---|
| 74 | vector[index] = random.Next(min.Value, max.Value);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /// <summary>
|
---|
| 78 | /// Changes randomly a single position in the given integer <paramref name="vector"/>.
|
---|
| 79 | /// </summary>
|
---|
| 80 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
| 81 | /// <param name="random">A random number generator.</param>
|
---|
| 82 | /// <param name="vector">The integer vector to manipulate.</param>
|
---|
[3048] | 83 | protected override void Manipulate(IRandom random, IntArray vector) {
|
---|
[3032] | 84 | if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MinimumParameter.ActualName + " could not be found.");
|
---|
| 85 | if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MaximumParameter.ActualName + " could not be found.");
|
---|
| 86 | Apply(random, vector, MinimumParameter.ActualValue, MaximumParameter.ActualValue);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|