Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Random/3.3/UniformRandomizer.cs @ 8471

Last change on this file since 8471 was 7644, checked in by ascheibe, 13 years ago

#1803 fixed bug where Randomizers stopped execution of algorithms

File size: 3.8 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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]22using HeuristicLab.Common;
[2]23using HeuristicLab.Core;
24using HeuristicLab.Data;
[3269]25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
[4068]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]28
29namespace HeuristicLab.Random {
[1153]30  /// <summary>
31  /// Uniformly distributed random number generator.
32  /// </summary>
[3269]33  [StorableClass]
34  [Item("UniformRandomizer", "Initializes the value of variable 'Value' to a random value uniformly distributed between 'Min' and 'Max'")]
35  public class UniformRandomizer : 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> MinParameter {
41      get { return (IValueLookupParameter<DoubleValue>)Parameters["Min"]; }
[426]42    }
[3269]43    public IValueLookupParameter<DoubleValue> MaxParameter {
44      get { return (IValueLookupParameter<DoubleValue>)Parameters["Max"]; }
[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 Min {
53      get { return MinParameter.ActualValue; }
54      set { MinParameter.ActualValue = value; }
55    }
56    public DoubleValue Max {
57      get { return MaxParameter.ActualValue; }
58      set { MaxParameter.ActualValue = value; }
59    }
60    #endregion
[426]61
[4722]62    [StorableConstructor]
63    protected UniformRandomizer(bool deserializing) : base(deserializing) { }
64    protected UniformRandomizer(UniformRandomizer original, Cloner cloner) : base(original, cloner) { }
[1153]65    /// <summary>
66    /// Initializes a new instance of <see cref="UniformRandomizer"/> with four variable infos
67    /// (<c>Value</c>, <c>Random</c>, <c>Max</c> and <c>Min</c>), being a random number generator
68    /// between 0.0 and 1.0.
69    /// </summary>
[3269]70    public UniformRandomizer()
71      : base() {
72      Parameters.Add(new LookupParameter<IRandom>("Random", "A random generator that supplies uniformly distributed values."));
73      Parameters.Add(new ValueLookupParameter<DoubleValue>("Min", "The minimal allowed value (inclusive)"));
74      Parameters.Add(new ValueLookupParameter<DoubleValue>("Max", "The maximal allowed value (exclusive)"));
75      Parameters.Add(new LookupParameter<DoubleValue>("Value", "The value that should be set to a random value."));
[2]76    }
77
[4722]78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new UniformRandomizer(this, cloner);
80    }
81
[1153]82    /// <summary>
83    /// Generates a new uniformly distributed random variable.
84    /// </summary>
[3269]85    public override IOperation Apply() {
86      IRandom random = RandomParameter.ActualValue;
87      double min = MinParameter.ActualValue.Value;
88      double max = MaxParameter.ActualValue.Value;
[2]89
[3269]90      ValueParameter.ActualValue = new DoubleValue(random.NextDouble() * (max - min) + min);
[7644]91      return base.Apply();
[2]92    }
[3269]93  }
[2]94}
Note: See TracBrowser for help on using the repository browser.