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.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
32 | /// <summary>
|
---|
33 | /// An operator which creates a new random real vector with each element normally distributed in a specified range.
|
---|
34 | /// </summary>
|
---|
35 | [Item("NormalDistributedRealVectorCreator", "An operator which creates a new random real vector with each element normally distributed in a specified range.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class NormalDistributedRealVectorCreator : RealVectorCreator, IStrategyParameterCreator {
|
---|
38 |
|
---|
39 | public IValueLookupParameter<RealVector> MeanParameter {
|
---|
40 | get { return (IValueLookupParameter<RealVector>)Parameters["Mean"]; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public IValueLookupParameter<DoubleArray> SigmaParameter {
|
---|
44 | get { return (IValueLookupParameter<DoubleArray>)Parameters["Sigma"]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IValueParameter<IntValue> MaximumTriesParameter {
|
---|
48 | get { return (IValueParameter<IntValue>)Parameters["MaximumTries"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | protected NormalDistributedRealVectorCreator(bool deserializing) : base(deserializing) { }
|
---|
53 | protected NormalDistributedRealVectorCreator(NormalDistributedRealVectorCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
54 | public NormalDistributedRealVectorCreator()
|
---|
55 | : base() {
|
---|
56 | Parameters.Add(new ValueLookupParameter<RealVector>("Mean", "The mean vector around which the points will be sampled."));
|
---|
57 | Parameters.Add(new ValueLookupParameter<DoubleArray>("Sigma", "The standard deviations for all or for each dimension."));
|
---|
58 | Parameters.Add(new ValueParameter<IntValue>("MaximumTries", "The maximum number of tries to sample within the specified bounds.", new IntValue(1000)));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
62 | return new NormalDistributedRealVectorCreator(this, cloner);
|
---|
63 | }
|
---|
64 |
|
---|
65 | [StorableHook(HookType.AfterDeserialization)]
|
---|
66 | private void AfterDeserialization() {
|
---|
67 | if (!Parameters.ContainsKey("MaximumTries"))
|
---|
68 | Parameters.Add(new ValueParameter<IntValue>("MaximumTries", "The maximum number of tries to sample within the specified bounds.", new IntValue(1000)));
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Generates a new random real vector normally distributed around the given mean with the given <paramref name="length"/> and in the interval [min,max).
|
---|
73 | /// </summary>
|
---|
74 | /// <exception cref="ArgumentException">
|
---|
75 | /// Thrown when <paramref name="random"/> is null.<br />
|
---|
76 | /// Thrown when <paramref name="mean"/> is null or of length 0.<br />
|
---|
77 | /// Thrown when <paramref name="sigma"/> is null or of length 0.<br />
|
---|
78 | /// </exception>
|
---|
79 | /// <remarks>
|
---|
80 | /// If no bounds are given the bounds will be set to (double.MinValue;double.MaxValue).
|
---|
81 | ///
|
---|
82 | /// If dimensions of the mean do not lie within the given bounds they're set to either to the min or max of the bounds depending on whether the given dimension
|
---|
83 | /// for the mean is smaller or larger than the bounds. If min and max for a certain dimension are almost the same the resulting value will be set to min.
|
---|
84 | ///
|
---|
85 | /// However, please consider that such static bounds are not really meaningful to optimize.
|
---|
86 | ///
|
---|
87 | /// The sigma vector can contain 0 values in which case the dimension will be exactly the same as the given mean.
|
---|
88 | /// </remarks>
|
---|
89 | /// <param name="random">The random number generator.</param>
|
---|
90 | /// <param name="means">The mean vector around which the resulting vector is sampled.</param>
|
---|
91 | /// <param name="sigmas">The vector of standard deviations, must have at least one row.</param>
|
---|
92 | /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param>
|
---|
93 | /// <param name="maximumTries">The maximum number of tries to sample a value inside the bounds for each dimension. If a valid value cannot be obtained, the mean will be used.</param>
|
---|
94 | /// <returns>The newly created real vector.</returns>
|
---|
95 | public static RealVector Apply(IntValue lengthValue, IRandom random, RealVector means, DoubleArray sigmas, DoubleMatrix bounds, int maximumTries = 1000) {
|
---|
96 | if (lengthValue == null || lengthValue.Value == 0) throw new ArgumentException("Length is not defined or zero");
|
---|
97 | if (random == null) throw new ArgumentNullException("Random is not defined", "random");
|
---|
98 | if (means == null || means.Length == 0) throw new ArgumentNullException("Mean is not defined", "mean");
|
---|
99 | if (sigmas == null || sigmas.Length == 0) throw new ArgumentNullException("Sigma is not defined.", "sigma");
|
---|
100 | if (bounds == null || bounds.Rows == 0) bounds = new DoubleMatrix(new[,] { { double.MinValue, double.MaxValue } });
|
---|
101 | var length = lengthValue.Value;
|
---|
102 | var nd = new NormalDistributedRandom(random, 0, 1);
|
---|
103 | var result = new RealVector(length);
|
---|
104 | for (int i = 0; i < result.Length; i++) {
|
---|
105 | var min = bounds[i % bounds.Rows, 0];
|
---|
106 | var max = bounds[i % bounds.Rows, 1];
|
---|
107 | var mean = means[i % means.Length];
|
---|
108 | var sigma = sigmas[i % sigmas.Length];
|
---|
109 | if (min.IsAlmost(max) || mean < min) result[i] = min;
|
---|
110 | else if (mean > max) result[i] = max;
|
---|
111 | else {
|
---|
112 | int count = 0;
|
---|
113 | bool inRange;
|
---|
114 | do {
|
---|
115 | result[i] = mean + sigma * nd.NextDouble();
|
---|
116 | inRange = result[i] >= min && result[i] < max;
|
---|
117 | count++;
|
---|
118 | } while (count < maximumTries && !inRange);
|
---|
119 | if (count == maximumTries && !inRange)
|
---|
120 | result[i] = mean;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return result;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /// <summary>
|
---|
127 | /// Forwards the call to <see cref="Apply(IRandom, RealVector, DoubleArray, DoubleMatrix)"/>.
|
---|
128 | /// </summary>
|
---|
129 | /// <param name="random">The pseudo random number generator to use.</param>
|
---|
130 | /// <param name="length">The length of the real vector.</param>
|
---|
131 | /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param>
|
---|
132 | /// <returns>The newly created real vector.</returns>
|
---|
133 | protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) {
|
---|
134 | return Apply(length, random, MeanParameter.ActualValue, SigmaParameter.ActualValue, bounds, MaximumTriesParameter.Value.Value);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|