Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Creators/UniformRandomIntegerVectorCreator.cs @ 3269

Last change on this file since 3269 was 3160, checked in by swagner, 14 years ago

Removed Creatable test attribute (#935).

File size: 3.0 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 System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.IntegerVectorEncoding {
30  /// <summary>
31  /// Generates a new random integer vector with each element uniformly distributed in a specified range.
32  /// </summary>
33  [Item("UniformRandomIntegerVectorCreator", "An operator which creates a new random int vector with each element uniformly distributed in a specified range.")]
34  [StorableClass]
35  public class UniformRandomIntegerVectorCreator : IntegerVectorCreator {
36    /// <summary>
37    /// Generates a new random integer vector with the given <paramref name="length"/>.
38    /// </summary>
39    /// <param name="random">The random number generator.</param>
40    /// <param name="length">The length of the int vector.</param>
41    /// <param name="min">The minimum value of the sampling range for each vector element (inclusive).</param>
42    /// <param name="max">The maximum value of the sampling range for each vector element (exclusive).</param>
43    /// <returns>The newly created integer vector.</returns>
44    public static IntegerVector Apply(IRandom random, int length, int min, int max) {
45      int[] result = new int[length];
46      for (int i = 0; i < length; i++)
47        result[i] = random.Next(min, max);
48      return new IntegerVector(result);
49    }
50
51    /// <summary>
52    /// Forwards the call to <see cref="Apply(IRandom, int, int, int)"/>.
53    /// </summary>
54    /// <param name="random">The pseudo random number generator to use.</param>
55    /// <param name="length">The length of the int vector.</param>
56    /// <param name="minimum">The minimum value of the sampling range for each vector element (inclusive).</param>
57    /// <param name="maximum">The maximum value of the sampling range for each vector element (exclusive).</param>
58    /// <returns>The newly created int vector.</returns>
59    protected override IntegerVector Create(IRandom random, IntValue length, IntValue minimum, IntValue maximum) {
60      return Apply(random, length.Value, minimum.Value, maximum.Value);
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.