Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2954 was 2524, checked in by swagner, 15 years ago

Removed plugin HeuristicLab.Constraints (#804)

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Random {
30  /// <summary>
31  /// Normally distributed random number generator.
32  /// </summary>
33  [EmptyStorableClass]
34  public class NormalRandomizer : OperatorBase {
35    private static int MAX_NUMBER_OF_TRIES = 100;
36
37    /// <inheritdoc select="summary"/>
38    public override string Description {
39      get { return "Initializes the value of variable 'Value' to a random value normally distributed with 'Mu' and 'Sigma'."; }
40    }
41
42    /// <summary>
43    /// Gets or sets the value for µ.
44    /// </summary>
45    /// <remarks>Gets or sets the variable with the name <c>Mu</c> through the method
46    /// <see cref="OperatorBase.GetVariable"/> of class <see cref="OperatorBase"/>.</remarks>
47    public double Mu {
48      get { return ((DoubleData)GetVariable("Mu").Value).Data; }
49      set { ((DoubleData)GetVariable("Mu").Value).Data = value; }
50    }
51    /// <summary>
52    /// Gets or sets the value for sigma.
53    /// </summary>
54    /// <remarks>Gets or sets the variable with the name <c>Sigma</c> through the method
55    /// <see cref="OperatorBase.GetVariable"/> of class <see cref="OperatorBase"/>.</remarks>
56    public double Sigma {
57      get { return ((DoubleData)GetVariable("Sigma").Value).Data; }
58      set { ((DoubleData)GetVariable("Sigma").Value).Data = value; }
59    }
60
61    /// <summary>
62    /// Initializes a new instance of <see cref="NormalRandomizer"/> with four variable infos
63    /// (<c>Mu</c>, <c>Sigma</c>, <c>Value</c> and <c>Random</c>).
64    /// </summary>
65    public NormalRandomizer() {
66      AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.None));
67      GetVariableInfo("Mu").Local = true;
68      AddVariable(new Variable("Mu", new DoubleData(0.0)));
69
70      AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.None));
71      GetVariableInfo("Sigma").Local = true;
72      AddVariable(new Variable("Sigma", new DoubleData(1.0)));
73
74      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData, ConstrainedIntData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
75      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
76    }
77
78    /// <summary>
79    /// Generates a new normally distributed random variable and assigns it to the specified variable
80    /// in the given <paramref name="scope"/>.
81    /// </summary>
82    /// <param name="scope">The scope where to assign the new random value to.</param>
83    /// <returns><c>null</c>.</returns>
84    public override IOperation Apply(IScope scope) {
85      IObjectData value = GetVariableValue<IObjectData>("Value", scope, false);
86      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
87      double mu = GetVariableValue<DoubleData>("Mu", scope, true).Data;
88      double sigma = GetVariableValue<DoubleData>("Sigma", scope, true).Data;
89
90      NormalDistributedRandom n = new NormalDistributedRandom(mt, mu, sigma);
91      RandomizeNormal(value, n);
92      return null;
93    }
94
95    private void RandomizeNormal(IObjectData value, NormalDistributedRandom n) {
96      // dispatch manually based on dynamic type
97      if (value is IntData)
98        RandomizeNormal((IntData)value, n);
99      else if (value is DoubleData)
100        RandomizeNormal((DoubleData)value, n);
101      else throw new InvalidOperationException("Can't handle type " + value.GetType().Name);
102    }
103
104    /// <summary>
105    /// Generates a new double random number based on a continuous, normally distributed random number
106    /// generator <paramref name="normal"/>.
107    /// </summary>
108    /// <param name="data">The double object where to assign the new value to.</param>
109    /// <param name="normal">The continuous, normally distributed random variable.</param>
110    public void RandomizeNormal(DoubleData data, NormalDistributedRandom normal) {
111      data.Data = normal.NextDouble();
112    }
113
114    /// <summary>
115    /// Generates a new int random number based on a continuous, normally distributed random number
116    /// generator <paramref name="normal"/>.
117    /// </summary>
118    /// <param name="data">The int object where to assign the new value to.</param>
119    /// <param name="normal">The continuous, normally distributed random variable.</param>
120    public void RandomizeNormal(IntData data, NormalDistributedRandom normal) {
121      data.Data = (int)Math.Round(normal.NextDouble());
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.