[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12018] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
[4722] | 22 | using HeuristicLab.Common;
|
---|
[2] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
[3269] | 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[4068] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Random {
|
---|
[1153] | 30 | /// <summary>
|
---|
| 31 | /// Normally distributed random number generator.
|
---|
| 32 | /// </summary>
|
---|
[3269] | 33 | [StorableClass]
|
---|
| 34 | [Item("NormalRandomizer", "Initializes the value of variable 'Value' to a random value normally distributed with parameters 'Mu' and 'Sigma'")]
|
---|
| 35 | public class NormalRandomizer : SingleSuccessorOperator {
|
---|
[4722] | 36 | #region Parameter Properties
|
---|
[3269] | 37 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 38 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
[2] | 39 | }
|
---|
[3269] | 40 | public IValueLookupParameter<DoubleValue> MuParameter {
|
---|
| 41 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Mu"]; }
|
---|
[426] | 42 | }
|
---|
[3269] | 43 | public IValueLookupParameter<DoubleValue> SigmaParameter {
|
---|
| 44 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Sigma"]; }
|
---|
[426] | 45 | }
|
---|
[3269] | 46 | public ILookupParameter<DoubleValue> ValueParameter {
|
---|
| 47 | get { return (ILookupParameter<DoubleValue>)Parameters["Value"]; }
|
---|
| 48 | }
|
---|
| 49 | #endregion
|
---|
[4722] | 50 |
|
---|
[3269] | 51 | #region Properties
|
---|
| 52 | public DoubleValue Mu {
|
---|
| 53 | get { return MuParameter.ActualValue; }
|
---|
| 54 | set { MuParameter.ActualValue = value; }
|
---|
| 55 | }
|
---|
| 56 | public DoubleValue Max {
|
---|
| 57 | get { return SigmaParameter.ActualValue; }
|
---|
| 58 | set { SigmaParameter.ActualValue = value; }
|
---|
| 59 | }
|
---|
| 60 | #endregion
|
---|
[4722] | 61 |
|
---|
| 62 | [StorableConstructor]
|
---|
| 63 | protected NormalRandomizer(bool deserializing) : base(deserializing) { }
|
---|
| 64 | protected NormalRandomizer(NormalRandomizer original, Cloner cloner) : base(original, cloner) { }
|
---|
[1153] | 65 | /// <summary>
|
---|
| 66 | /// Initializes a new instance of <see cref="NormalRandomizer"/> with four variable infos
|
---|
| 67 | /// (<c>Mu</c>, <c>Sigma</c>, <c>Value</c> and <c>Random</c>).
|
---|
| 68 | /// </summary>
|
---|
[2] | 69 | public NormalRandomizer() {
|
---|
[3269] | 70 | Parameters.Add(new LookupParameter<IRandom>("Random", "A random generator that supplies uniformly distributed values."));
|
---|
| 71 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Mu", "Mu parameter of the normal distribution (N(mu,sigma))."));
|
---|
| 72 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Sigma", "Sigma parameter of the normal distribution (N(mu,sigma))."));
|
---|
| 73 | Parameters.Add(new LookupParameter<DoubleValue>("Value", "The value that should be set to a random value."));
|
---|
[2] | 74 | }
|
---|
| 75 |
|
---|
[4722] | 76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 77 | return new NormalRandomizer(this, cloner);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[1153] | 80 | /// <summary>
|
---|
[3269] | 81 | /// Generates a new normally distributed random variable and assigns it to the specified variable.
|
---|
[1153] | 82 | /// </summary>
|
---|
[3269] | 83 | public override IOperation Apply() {
|
---|
| 84 | IRandom random = RandomParameter.ActualValue;
|
---|
| 85 | double mu = MuParameter.ActualValue.Value;
|
---|
| 86 | double sigma = SigmaParameter.ActualValue.Value;
|
---|
[2] | 87 |
|
---|
[3269] | 88 | NormalDistributedRandom normalRandom = new NormalDistributedRandom(random, mu, sigma);
|
---|
| 89 | ValueParameter.ActualValue = new DoubleValue(normalRandom.NextDouble());
|
---|
[7644] | 90 | return base.Apply();
|
---|
[2] | 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|