Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Creators/UniformRandomRealVectorCreator.cs @ 3017

Last change on this file since 3017 was 3017, checked in by epitzer, 14 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

File size: 3.8 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.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.RealVector {
31  /// <summary>
32  /// An operator which creates a new random real vector with each element uniformly distributed in a specified range.
33  /// </summary>
34  [Item("UniformRandomRealVectorCreator", "An operator which creates a new random real vector with each element uniformly distributed in a specified range.")]
35  [StorableClass]
36  [Creatable("Test")]
37  public class UniformRandomRealVectorCreator : RealVectorCreator {
38    /// <summary>
39    /// Generates a new random real vector with the given <paramref name="length"/> and in the interval [min,max).
40    /// </summary>
41    /// <exception cref="ArgumentException">
42    /// Thrown when <paramref name="length"/> is smaller or equal to 0.<br />
43    /// Thrown when <paramref name="min"/> is greater than <paramref name="max"/>.
44    /// </exception>
45    /// <remarks>
46    /// Note that if <paramref name="min"/> is equal to <paramref name="max"/>, all elements of the vector will be equal to <paramref name="min"/>.
47    /// </remarks>
48    /// <param name="random">The random number generator.</param>
49    /// <param name="length">The length of the real vector.</param>
50    /// <param name="min">The minimum value of the sampling range for each vector element (inclusive).</param>
51    /// <param name="max">The maximum value of the sampling range for each vector element (exclusive).</param>
52    /// <returns>The newly created real vector.</returns>
53    public static DoubleArrayData Apply(IRandom random, int length, double min, double max) {
54      if (length <= 0) throw new ArgumentException("UniformRandomRealVectorCreator: Length is smaller or equal to 0.", "length");
55      if (min > max) throw new ArgumentException("UniformRandomRealVectorCreator: Minimum is greater than Maximum.", "min");
56      DoubleArrayData result = new DoubleArrayData(length);
57      for (int i = 0; i < length; i++)
58        result[i] = min + random.NextDouble() * (max - min);
59      return result;
60    }
61
62    /// <summary>
63    /// Forwards the call to <see cref="Apply(IRandom, int, double, double)"/>.
64    /// </summary>
65    /// <param name="random">The pseudo random number generator to use.</param>
66    /// <param name="length">The length of the real vector.</param>
67    /// <param name="minimum">The minimum value of the sampling range for each vector element (inclusive).</param>
68    /// <param name="maximum">The maximum value of the sampling range for each vector element (exclusive).</param>
69    /// <returns>The newly created real vector.</returns>
70    protected override DoubleArrayData Create(IRandom random, IntData length, DoubleData minimum, DoubleData maximum) {
71      return Apply(random, length.Value, minimum.Value, maximum.Value);
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.