[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[1853] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Random {
|
---|
[1153] | 30 | /// <summary>
|
---|
| 31 | /// Normally distributed random number generator.
|
---|
| 32 | /// </summary>
|
---|
[1853] | 33 | [EmptyStorableClass]
|
---|
[2] | 34 | public class NormalRandomizer : OperatorBase {
|
---|
| 35 | private static int MAX_NUMBER_OF_TRIES = 100;
|
---|
| 36 |
|
---|
[1153] | 37 | /// <inheritdoc select="summary"/>
|
---|
[2] | 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 |
|
---|
[1153] | 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>
|
---|
[426] | 47 | public double Mu {
|
---|
| 48 | get { return ((DoubleData)GetVariable("Mu").Value).Data; }
|
---|
| 49 | set { ((DoubleData)GetVariable("Mu").Value).Data = value; }
|
---|
| 50 | }
|
---|
[1153] | 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>
|
---|
[426] | 56 | public double Sigma {
|
---|
| 57 | get { return ((DoubleData)GetVariable("Sigma").Value).Data; }
|
---|
| 58 | set { ((DoubleData)GetVariable("Sigma").Value).Data = value; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[1153] | 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>
|
---|
[2] | 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;
|
---|
[183] | 72 | AddVariable(new Variable("Sigma", new DoubleData(1.0)));
|
---|
[2] | 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 |
|
---|
[1153] | 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>
|
---|
[1157] | 83 | /// <returns><c>null</c>.</returns>
|
---|
[2] | 84 | public override IOperation Apply(IScope scope) {
|
---|
| 85 | IObjectData value = GetVariableValue<IObjectData>("Value", scope, false);
|
---|
| 86 | MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
|
---|
[719] | 87 | double mu = GetVariableValue<DoubleData>("Mu", scope, true).Data;
|
---|
| 88 | double sigma = GetVariableValue<DoubleData>("Sigma", scope, true).Data;
|
---|
[2] | 89 |
|
---|
[763] | 90 | NormalDistributedRandom n = new NormalDistributedRandom(mt, mu, sigma);
|
---|
| 91 | RandomizeNormal(value, n);
|
---|
[2] | 92 | return null;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[763] | 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 | }
|
---|
[2] | 103 |
|
---|
[1153] | 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>
|
---|
[763] | 110 | public void RandomizeNormal(DoubleData data, NormalDistributedRandom normal) {
|
---|
| 111 | data.Data = normal.NextDouble();
|
---|
| 112 | }
|
---|
[2] | 113 |
|
---|
[1153] | 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>
|
---|
[763] | 120 | public void RandomizeNormal(IntData data, NormalDistributedRandom normal) {
|
---|
| 121 | data.Data = (int)Math.Round(normal.NextDouble());
|
---|
| 122 | }
|
---|
[2] | 123 | }
|
---|
| 124 | }
|
---|