Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Random/3.3/UniformRandomizer.cs @ 3026

Last change on this file since 3026 was 1853, checked in by epitzer, 16 years ago

Fix EmptyStorableClass attributes. (#603)

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Constraints;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Random {
31  /// <summary>
32  /// Uniformly distributed random number generator.
33  /// </summary>
34  [EmptyStorableClass]
35  public class UniformRandomizer : OperatorBase {
36    private static int MAX_NUMBER_OF_TRIES = 100;
37    /// <inheritdoc select="summary"/>
38    public override string Description {
39      get { return "Initializes the value of variable 'Value' to a random value uniformly distributed between 'Min' and 'Max' (exclusive)"; }
40    }
41
42    /// <summary>
43    /// Gets or sets the maximum value of the random number generator (exclusive).
44    /// </summary>
45    /// <remarks>Gets or sets the variable with name <c>Max</c> through the
46    /// <see cref="OperatorBase.GetVariable"/> method of class <see cref="OperatorBase"/>.</remarks>
47    public double Max {
48      get { return ((DoubleData)GetVariable("Max").Value).Data; }
49      set { ((DoubleData)GetVariable("Max").Value).Data = value; }
50    }
51    /// <summary>
52    /// Gets or sets the minimum value of the random number generator.
53    /// </summary>
54    /// <remarks>Gets or sets the variable with name <c>Min</c> through the
55    /// <see cref="OperatorBase.GetVariable"/> method of class <see cref="OperatorBase"/>.</remarks>
56    public double Min {
57      get { return ((DoubleData)GetVariable("Min").Value).Data; }
58      set { ((DoubleData)GetVariable("Min").Value).Data = value; }
59    }
60
61    /// <summary>
62    /// Initializes a new instance of <see cref="UniformRandomizer"/> with four variable infos
63    /// (<c>Value</c>, <c>Random</c>, <c>Max</c> and <c>Min</c>), being a random number generator
64    /// between 0.0 and 1.0.
65    /// </summary>
66    public UniformRandomizer() {
67      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (type is one of: IntData, ConstrainedIntData, DoubleData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
68      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
69      AddVariableInfo(new VariableInfo("Min", "Lower bound of the uniform distribution (inclusive)", typeof(DoubleData), VariableKind.None));
70      GetVariableInfo("Min").Local = true;
71      AddVariable(new Variable("Min", new DoubleData(0.0)));
72
73      AddVariableInfo(new VariableInfo("Max", "Upper bound of the uniform distribution (exclusive)", typeof(DoubleData), VariableKind.None));
74      GetVariableInfo("Max").Local = true;
75      AddVariable(new Variable("Max", new DoubleData(1.0)));
76    }
77
78    /// <summary>
79    /// Generates a new uniformly distributed random variable.
80    /// </summary>
81    /// <param name="scope">The scope where to apply the random number generator.</param>
82    /// <returns><c>null</c>.</returns>
83    public override IOperation Apply(IScope scope) {
84      IObjectData value = GetVariableValue<IObjectData>("Value", scope, false);
85      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
86      double min = GetVariableValue<DoubleData>("Min", scope, true).Data;
87      double max = GetVariableValue<DoubleData>("Max", scope, true).Data;
88
89      RandomizeUniform(value, mt, min, max);
90      return null;
91    }
92
93    /// <summary>
94    /// Generates a new random number depending on the type of the <paramref name="value"/>.
95    /// </summary>
96    /// <exception cref="ArgumentException">Thrown when an unhandleable type appears.</exception>
97    /// <param name="value">The object whose data should be a new randomly generated number.</param>
98    /// <param name="mt">The MersenneTwister to generate a new random number.</param>
99    /// <param name="min">The left border of the interval in which the next random number has to lie.</param>
100    /// <param name="max">The right border (exclusive) of the interval in which the next random number
101    /// has to lie.</param>
102    private void RandomizeUniform(IObjectData value, MersenneTwister mt, double min, double max) {
103      // Dispatch manually based on dynamic type,
104      // a bit awkward but necessary until we create a better type hierarchy for numeric types (gkronber 15.11.2008).
105      if (value is DoubleData)
106        RandomizeUniform((DoubleData)value, mt, min, max);
107      else if (value is ConstrainedDoubleData)
108        RandomizeUniform((ConstrainedDoubleData)value, mt, min, max);
109      else if (value is IntData)
110        RandomizeUniform((IntData)value, mt, min, max);
111      else if (value is ConstrainedIntData)
112        RandomizeUniform((ConstrainedIntData)value, mt, min, max);
113      else throw new ArgumentException("Can't handle type " + value.GetType().Name);
114    }
115
116      /// <summary>
117      /// Generates a new double random number.
118      /// </summary>
119      /// <param name="data">The double object which the new value is assigned to.</param>
120      /// <param name="mt">The random number generator.</param>
121      /// <param name="min">The left border of the interval in which the next random number has to lie.</param>
122      /// <param name="max">The right border (exclusive) of the interval in which the next random number
123      /// has to lie.</param>
124      public void RandomizeUniform(DoubleData data, MersenneTwister mt, double min, double max) {
125        data.Data = mt.NextDouble() * (max - min) + min;
126      }
127
128      /// <summary>
129      /// Generates a new int random number.
130      /// </summary>
131      /// <param name="data">The int object which the new value is assigned to.</param>
132      /// <param name="mt">The random number generator.</param>
133      /// <param name="min">The left border of the interval in which the next random number has to lie.</param>
134      /// <param name="max">The right border (exclusive) of the interval in which the next random number
135      /// has to lie.</param>
136      public void RandomizeUniform(IntData data, MersenneTwister mt, double min, double max) {
137        data.Data = (int)Math.Floor(mt.NextDouble() * (max - min) + min);
138      }
139
140      /// <summary>
141      /// Generates a new double random number, being restricted to some constraints.
142      /// </summary>
143      /// <exception cref="InvalidOperationException">Thrown when no valid value could be found.</exception>
144      /// <param name="data">The double object which the new value is assigned to and whose constraints
145      /// must be fulfilled.</param>
146      /// <param name="mt">The random number generator.</param>
147      /// <param name="min">The left border of the interval in which the next random number has to lie.</param>
148      /// <param name="max">The right border (exclusive) of the interval in which the next random number
149      /// has to lie.</param>
150      public void RandomizeUniform(ConstrainedDoubleData data, MersenneTwister mt, double min, double max) {
151        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
152          double r = mt.NextDouble() * (max - min) + min;
153          if(IsIntegerConstrained(data)) {
154            r = Math.Floor(r);
155          }
156          if(data.TrySetData(r)) {
157            return;
158          }
159        }
160        throw new InvalidOperationException("Couldn't find a valid value");
161      }
162      /// <summary>
163      /// Generates a new int random number, being restricted to some constraints.
164      /// </summary>
165      /// <exception cref="InvalidOperationException">Thrown when no valid value could be found.</exception>
166      /// <param name="data">The int object which the new value is assigned to and whose constraints
167      /// must be fulfilled.</param>
168      /// <param name="mt">The random number generator.</param>
169      /// <param name="min">The left border of the interval in which the next random number has to lie.</param>
170      /// <param name="max">The right border (exclusive) of the interval in which the next random number
171      /// has to lie.</param>
172      public void RandomizeUniform(ConstrainedIntData data, MersenneTwister mt, double min, double max) {
173        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
174          int r = (int)Math.Floor(mt.NextDouble() * (max - min) + min);
175          if(data.TrySetData(r)) {
176            return;
177          }
178        }
179        throw new InvalidOperationException("Couldn't find a valid value");
180      }
181
182      private bool IsIntegerConstrained(ConstrainedDoubleData data) {
183        foreach(IConstraint constraint in data.Constraints) {
184          if(constraint is IsIntegerConstraint) {
185            return true;
186          }
187        }
188        return false;
189      }
190    }
191}
Note: See TracBrowser for help on using the repository browser.