Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CMAES/HeuristicLab.Encodings.RealVectorEncoding/3.3/Creators/NormalDistributedRealVectorCreator.cs @ 9148

Last change on this file since 9148 was 9148, checked in by abeham, 11 years ago

#1961:

  • Added Logarithmic flag also to AxisRatio
  • Fixed some bugs regarding initial iterations
  • Removed the PredefinedRealVectorCreator and instead added a NormalDistributedRealVectorCreator
    • When sigma is 0 this is the same as the PredefinedRealVectorCreator
File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Random;
29using System;
30
31namespace 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    [StorableConstructor]
48    protected NormalDistributedRealVectorCreator(bool deserializing) : base(deserializing) { }
49    protected NormalDistributedRealVectorCreator(NormalDistributedRealVectorCreator original, Cloner cloner) : base(original, cloner) { }
50    public NormalDistributedRealVectorCreator()
51      : base() {
52      Parameters.Add(new ValueLookupParameter<RealVector>("Mean", "The mean vector around which the points will be sampled."));
53      Parameters.Add(new ValueLookupParameter<DoubleArray>("Sigma", "The standard deviations for all or for each dimension."));
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new NormalDistributedRealVectorCreator(this, cloner);
58    }
59
60    /// <summary>
61    /// Generates a new random real vector normally distributed around the given mean with the given <paramref name="length"/> and in the interval [min,max).
62    /// </summary>
63    /// <exception cref="ArgumentException">
64    /// Thrown when <paramref name="random"/> is null.<br />
65    /// Thrown when <paramref name="mean"/> is null or of length 0.<br />
66    /// Thrown when <paramref name="sigma"/> is null or of length 0.<br />
67    /// </exception>
68    /// <remarks>
69    /// If no bounds are given the bounds will be set to (double.MinValue;double.MaxValue).
70    ///
71    /// 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
72    /// 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.
73    ///
74    /// However, please consider that such static bounds are not really meaningful to optimize.
75    ///
76    /// The sigma vector can contain 0 values in which case the dimension will be exactly the same as the given mean.
77    /// </remarks>
78    /// <param name="random">The random number generator.</param>
79    /// <param name="mean">The mean vector around which the resulting vector is sampled.</param>
80    /// <param name="sigma">The vector of standard deviations, must have at least one row.</param>
81    /// <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>
82    /// <returns>The newly created real vector.</returns>
83    public static RealVector Apply(IRandom random, RealVector mean, DoubleArray sigma, DoubleMatrix bounds) {
84      if (random == null) throw new ArgumentException("Random is not defined", "random");
85      if (mean == null || mean.Length == 0) throw new ArgumentException("Mean is not defined", "mean");
86      if (sigma == null || sigma.Length == 0) throw new ArgumentException("Sigma is not defined.", "sigma");
87      if (bounds == null || bounds.Rows == 0) bounds = new DoubleMatrix(new[,] { { double.MinValue, double.MaxValue } });
88      var nd = new NormalDistributedRandom(random, 0, 1);
89      var result = (RealVector)mean.Clone();
90      for (int i = 0; i < result.Length; i++) {
91        var min = bounds[i % bounds.Rows, 0];
92        var max = bounds[i % bounds.Rows, 1];
93        if (min.IsAlmost(max) || mean[i] < min) result[i] = min;
94        else if (mean[i] > max) result[i] = max;
95        else {
96          do {
97            result[i] = mean[i] + sigma[i % sigma.Length] * nd.NextDouble();
98          } while (result[i] < bounds[i % sigma.Length, 0] || result[i] > bounds[i % sigma.Length, 1]);
99        }
100      }
101      return result;
102    }
103
104    /// <summary>
105    /// Forwards the call to <see cref="Apply(IRandom, RealVector, DoubleArray, DoubleMatrix)"/>.
106    /// </summary>
107    /// <param name="random">The pseudo random number generator to use.</param>
108    /// <param name="length">The length of the real vector.</param>
109    /// <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>
110    /// <returns>The newly created real vector.</returns>
111    protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) {
112      return Apply(random, MeanParameter.ActualValue, SigmaParameter.ActualValue, bounds);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.