1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.IntegerVectorEncoding {
|
---|
30 | /// <summary>
|
---|
31 | /// Uniformly distributed change of several, but at least one, positions 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("UniformSomePositionsManipulator", "Uniformly distributed change of several, but at least one, positions 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 UniformSomePositionsManipulator : BoundedIntegerVectorManipulator {
|
---|
39 |
|
---|
40 | public IValueLookupParameter<DoubleValue> ProbabilityParameter {
|
---|
41 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Probability"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | protected UniformSomePositionsManipulator(bool deserializing) : base(deserializing) { }
|
---|
46 | protected UniformSomePositionsManipulator(UniformSomePositionsManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
47 | public UniformSomePositionsManipulator()
|
---|
48 | : base() {
|
---|
49 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Probability", "The probability for each dimension to be manipulated.", new DoubleValue(0.5)));
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
53 | return new UniformSomePositionsManipulator(this, cloner);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Changes randomly several, but at least one, positions in the given integer <paramref name="vector"/>, according to the given probabilities.
|
---|
58 | /// </summary>
|
---|
59 | /// <param name="random">A random number generator.</param>
|
---|
60 | /// <param name="vector">The integer vector to manipulate.</param>
|
---|
61 | /// <param name="bounds"> Contains the minimum value (inclusive), maximum value (exclusive), and step size of the sampling range for
|
---|
62 | /// the vector element to change.</param>
|
---|
63 | /// <param name="probability">The probability for each dimension to be manipulated..</param>
|
---|
64 | public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, double probability) {
|
---|
65 | if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("UniformSomePositionsManipulator: Invalid bounds specified", "bounds");
|
---|
66 | bool atLeastOneManipulated = false;
|
---|
67 | for (int index = 0; index < vector.Length; index++) {
|
---|
68 | if (random.NextDouble() < probability) {
|
---|
69 | atLeastOneManipulated = true;
|
---|
70 | UniformOnePositionManipulator.Manipulate(random, vector, bounds, index);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (!atLeastOneManipulated) {
|
---|
75 | UniformOnePositionManipulator.Manipulate(random, vector, bounds, random.Next(vector.Length));
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Changes randomly several, but at least one, positions in the given integer <paramref name="vector"/>.
|
---|
81 | /// </summary>
|
---|
82 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
83 | /// <param name="random">A random number generator.</param>
|
---|
84 | /// <param name="vector">The integer vector to manipulate.</param>
|
---|
85 | protected override void ManipulateBounded(IRandom random, IntegerVector vector, IntMatrix bounds) {
|
---|
86 | Apply(random, vector, bounds, ProbabilityParameter.ActualValue.Value);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|