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