Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Random/3.3/NormalRandomizer.cs @ 4410

Last change on this file since 4410 was 4258, checked in by swagner, 14 years ago

Worked in integration of FastRandom PRNG (#1114)

File size: 3.6 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Operators;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Random {
29  /// <summary>
30  /// Normally distributed random number generator.
31  /// </summary>
32  [StorableClass]
33  [Item("NormalRandomizer", "Initializes the value of variable 'Value' to a random value normally distributed with parameters 'Mu' and 'Sigma'")]
34  public class NormalRandomizer : SingleSuccessorOperator {
35    #region parameter properties
36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
38    }
39    public IValueLookupParameter<DoubleValue> MuParameter {
40      get { return (IValueLookupParameter<DoubleValue>)Parameters["Mu"]; }
41    }
42    public IValueLookupParameter<DoubleValue> SigmaParameter {
43      get { return (IValueLookupParameter<DoubleValue>)Parameters["Sigma"]; }
44    }
45    public ILookupParameter<DoubleValue> ValueParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["Value"]; }
47    }
48    #endregion
49    #region Properties
50    public DoubleValue Mu {
51      get { return MuParameter.ActualValue; }
52      set { MuParameter.ActualValue = value; }
53    }
54    public DoubleValue Max {
55      get { return SigmaParameter.ActualValue; }
56      set { SigmaParameter.ActualValue = value; }
57    }
58    #endregion
59    /// <summary>
60    /// Initializes a new instance of <see cref="NormalRandomizer"/> with four variable infos
61    /// (<c>Mu</c>, <c>Sigma</c>, <c>Value</c> and <c>Random</c>).
62    /// </summary>
63    public NormalRandomizer() {
64      Parameters.Add(new LookupParameter<IRandom>("Random", "A random generator that supplies uniformly distributed values."));
65      Parameters.Add(new ValueLookupParameter<DoubleValue>("Mu", "Mu parameter of the normal distribution (N(mu,sigma))."));
66      Parameters.Add(new ValueLookupParameter<DoubleValue>("Sigma", "Sigma parameter of the normal distribution (N(mu,sigma))."));
67      Parameters.Add(new LookupParameter<DoubleValue>("Value", "The value that should be set to a random value."));
68    }
69    [StorableConstructor]
70    protected NormalRandomizer(bool deserializing) : base(deserializing) { }
71
72    /// <summary>
73    /// Generates a new normally distributed random variable and assigns it to the specified variable.
74    /// </summary>
75    public override IOperation Apply() {
76      IRandom random = RandomParameter.ActualValue;
77      double mu = MuParameter.ActualValue.Value;
78      double sigma = SigmaParameter.ActualValue.Value;
79
80      NormalDistributedRandom normalRandom = new NormalDistributedRandom(random, mu, sigma);
81      ValueParameter.ActualValue = new DoubleValue(normalRandom.NextDouble());
82      return null;
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.