[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[3269] | 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
[3376] | 25 | using HeuristicLab.Common;
|
---|
[2] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[1853] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3269] | 29 | using HeuristicLab.Operators;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
[2] | 31 |
|
---|
| 32 | namespace HeuristicLab.Random {
|
---|
[1153] | 33 | /// <summary>
|
---|
| 34 | /// Uniformly distributed random number generator.
|
---|
| 35 | /// </summary>
|
---|
[3269] | 36 | [StorableClass]
|
---|
| 37 | [Item("UniformRandomizer", "Initializes the value of variable 'Value' to a random value uniformly distributed between 'Min' and 'Max'")]
|
---|
| 38 | public class UniformRandomizer : SingleSuccessorOperator {
|
---|
| 39 | #region parameter properties
|
---|
| 40 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 41 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
[2] | 42 | }
|
---|
[3269] | 43 | public IValueLookupParameter<DoubleValue> MinParameter {
|
---|
| 44 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Min"]; }
|
---|
[426] | 45 | }
|
---|
[3269] | 46 | public IValueLookupParameter<DoubleValue> MaxParameter {
|
---|
| 47 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Max"]; }
|
---|
[426] | 48 | }
|
---|
[3269] | 49 | public ILookupParameter<DoubleValue> ValueParameter {
|
---|
| 50 | get { return (ILookupParameter<DoubleValue>)Parameters["Value"]; }
|
---|
| 51 | }
|
---|
| 52 | #endregion
|
---|
| 53 | #region Properties
|
---|
| 54 | public DoubleValue Min {
|
---|
| 55 | get { return MinParameter.ActualValue; }
|
---|
| 56 | set { MinParameter.ActualValue = value; }
|
---|
| 57 | }
|
---|
| 58 | public DoubleValue Max {
|
---|
| 59 | get { return MaxParameter.ActualValue; }
|
---|
| 60 | set { MaxParameter.ActualValue = value; }
|
---|
| 61 | }
|
---|
| 62 | #endregion
|
---|
[426] | 63 |
|
---|
[1153] | 64 | /// <summary>
|
---|
| 65 | /// Initializes a new instance of <see cref="UniformRandomizer"/> with four variable infos
|
---|
| 66 | /// (<c>Value</c>, <c>Random</c>, <c>Max</c> and <c>Min</c>), being a random number generator
|
---|
| 67 | /// between 0.0 and 1.0.
|
---|
| 68 | /// </summary>
|
---|
[3269] | 69 | public UniformRandomizer()
|
---|
| 70 | : base() {
|
---|
| 71 | Parameters.Add(new LookupParameter<IRandom>("Random", "A random generator that supplies uniformly distributed values."));
|
---|
| 72 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Min", "The minimal allowed value (inclusive)"));
|
---|
| 73 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Max", "The maximal allowed value (exclusive)"));
|
---|
| 74 | Parameters.Add(new LookupParameter<DoubleValue>("Value", "The value that should be set to a random value."));
|
---|
[2] | 75 | }
|
---|
| 76 |
|
---|
[1153] | 77 | /// <summary>
|
---|
| 78 | /// Generates a new uniformly distributed random variable.
|
---|
| 79 | /// </summary>
|
---|
[3269] | 80 | public override IOperation Apply() {
|
---|
| 81 | IRandom random = RandomParameter.ActualValue;
|
---|
| 82 | double min = MinParameter.ActualValue.Value;
|
---|
| 83 | double max = MaxParameter.ActualValue.Value;
|
---|
[2] | 84 |
|
---|
[3269] | 85 | ValueParameter.ActualValue = new DoubleValue(random.NextDouble() * (max - min) + min);
|
---|
[2] | 86 | return null;
|
---|
| 87 | }
|
---|
[3269] | 88 | }
|
---|
[2] | 89 | }
|
---|