#region License Information /* HeuristicLab * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HEAL.Attic; namespace HeuristicLab.Random { /// /// Normally distributed random number generator using the Marsaglia's polar method. /// [Item("NormalRandomizerPolar", "Initializes the value of variable 'Value' to a random value normally distributed with parameters 'Mu' and 'Sigma'")] [StorableType("24927018-F729-449C-97BB-D41184F64A61")] public class NormalRandomizerPolar : SingleSuccessorOperator { #region Parameter Properties public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public IValueLookupParameter MuParameter { get { return (IValueLookupParameter)Parameters["Mu"]; } } public IValueLookupParameter SigmaParameter { get { return (IValueLookupParameter)Parameters["Sigma"]; } } public ILookupParameter ValueParameter { get { return (ILookupParameter)Parameters["Value"]; } } #endregion #region Properties public DoubleValue Mu { get { return MuParameter.ActualValue; } set { MuParameter.ActualValue = value; } } public DoubleValue Max { get { return SigmaParameter.ActualValue; } set { SigmaParameter.ActualValue = value; } } #endregion [StorableConstructor] protected NormalRandomizerPolar(StorableConstructorFlag _) : base(_) { } protected NormalRandomizerPolar(NormalRandomizerPolar original, Cloner cloner) : base(original, cloner) { } /// /// Initializes a new instance of with four variable infos /// (Mu, Sigma, Value and Random). /// public NormalRandomizerPolar() { Parameters.Add(new LookupParameter("Random", "A random generator that supplies uniformly distributed values.")); Parameters.Add(new ValueLookupParameter("Mu", "Mu parameter of the normal distribution (N(mu,sigma)).")); Parameters.Add(new ValueLookupParameter("Sigma", "Sigma parameter of the normal distribution (N(mu,sigma)).")); Parameters.Add(new LookupParameter("Value", "The value that should be set to a random value.")); } public override IDeepCloneable Clone(Cloner cloner) { return new NormalRandomizerPolar(this, cloner); } /// /// Generates a new normally distributed random variable and assigns it to the specified variable. /// public override IOperation Apply() { IRandom random = RandomParameter.ActualValue; double mu = MuParameter.ActualValue.Value; double sigma = SigmaParameter.ActualValue.Value; NormalDistributedRandomPolar normalRandom = new NormalDistributedRandomPolar(random, mu, sigma); ValueParameter.ActualValue = new DoubleValue(normalRandom.NextDouble()); return base.Apply(); } } }