Changeset 1153 for trunk/sources/HeuristicLab.Random/UniformRandomizer.cs
- Timestamp:
- 01/16/09 11:24:03 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Random/UniformRandomizer.cs
r763 r1153 28 28 29 29 namespace HeuristicLab.Random { 30 /// <summary> 31 /// Uniformly distributed random number generator. 32 /// </summary> 30 33 public class UniformRandomizer : OperatorBase { 31 34 private static int MAX_NUMBER_OF_TRIES = 100; 35 /// <inheritdoc select="summary"/> 32 36 public override string Description { 33 37 get { return "Initializes the value of variable 'Value' to a random value uniformly distributed between 'Min' and 'Max' (exclusive)"; } 34 38 } 35 39 40 /// <summary> 41 /// Gets or sets the maximum value of the random number generator (exclusive). 42 /// </summary> 43 /// <remarks>Gets or sets the variable with name <c>Max</c> through the 44 /// <see cref="OperatorBase.GetVariable"/> method of class <see cref="OperatorBase"/>.</remarks> 36 45 public double Max { 37 46 get { return ((DoubleData)GetVariable("Max").Value).Data; } 38 47 set { ((DoubleData)GetVariable("Max").Value).Data = value; } 39 48 } 49 /// <summary> 50 /// Gets or sets the minimum value of the random number generator. 51 /// </summary> 52 /// <remarks>Gets or sets the variable with name <c>Min</c> through the 53 /// <see cref="OperatorBase.GetVariable"/> method of class <see cref="OperatorBase"/>.</remarks> 40 54 public double Min { 41 55 get { return ((DoubleData)GetVariable("Min").Value).Data; } … … 43 57 } 44 58 59 /// <summary> 60 /// Initializes a new instance of <see cref="UniformRandomizer"/> with four variable infos 61 /// (<c>Value</c>, <c>Random</c>, <c>Max</c> and <c>Min</c>), being a random number generator 62 /// between 0.0 and 1.0. 63 /// </summary> 45 64 public UniformRandomizer() { 46 65 AddVariableInfo(new VariableInfo("Value", "The value to manipulate (type is one of: IntData, ConstrainedIntData, DoubleData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In)); … … 55 74 } 56 75 76 /// <summary> 77 /// Generates a new uniformly distributed random variable. 78 /// </summary> 79 /// <param name="scope">The scope where to apply the random number generator.</param> 80 /// <returns>null.</returns> 57 81 public override IOperation Apply(IScope scope) { 58 82 IObjectData value = GetVariableValue<IObjectData>("Value", scope, false); … … 65 89 } 66 90 91 /// <summary> 92 /// Generates a new random number depending on the type of the <paramref name="value"/>. 93 /// </summary> 94 /// <exception cref="ArgumentException">Thrown when an unhandleable type appears.</exception> 95 /// <param name="value">The object whose data should be a new randomly generated number.</param> 96 /// <param name="mt">The MersenneTwister to generate a new random number.</param> 97 /// <param name="min">The left border of the interval in which the next random number has to lie.</param> 98 /// <param name="max">The right border (exclusive) of the interval in which the next random number 99 /// has to lie.</param> 67 100 private void RandomizeUniform(IObjectData value, MersenneTwister mt, double min, double max) { 68 101 // Dispatch manually based on dynamic type, … … 79 112 } 80 113 81 114 /// <summary> 115 /// Generates a new double random number. 116 /// </summary> 117 /// <param name="data">The double object which the new value is assigned to.</param> 118 /// <param name="mt">The random number generator.</param> 119 /// <param name="min">The left border of the interval in which the next random number has to lie.</param> 120 /// <param name="max">The right border (exclusive) of the interval in which the next random number 121 /// has to lie.</param> 82 122 public void RandomizeUniform(DoubleData data, MersenneTwister mt, double min, double max) { 83 123 data.Data = mt.NextDouble() * (max - min) + min; 84 124 } 85 125 126 /// <summary> 127 /// Generates a new int random number. 128 /// </summary> 129 /// <param name="data">The int object which the new value is assigned to.</param> 130 /// <param name="mt">The random number generator.</param> 131 /// <param name="min">The left border of the interval in which the next random number has to lie.</param> 132 /// <param name="max">The right border (exclusive) of the interval in which the next random number 133 /// has to lie.</param> 86 134 public void RandomizeUniform(IntData data, MersenneTwister mt, double min, double max) { 87 135 data.Data = (int)Math.Floor(mt.NextDouble() * (max - min) + min); 88 136 } 89 137 138 /// <summary> 139 /// Generates a new double random number, being restricted to some constraints. 140 /// </summary> 141 /// <exception cref="InvalidOperationException">Thrown when no valid value could be found.</exception> 142 /// <param name="data">The double object which the new value is assigned to and whose constraints 143 /// must be fulfilled.</param> 144 /// <param name="mt">The random number generator.</param> 145 /// <param name="min">The left border of the interval in which the next random number has to lie.</param> 146 /// <param name="max">The right border (exclusive) of the interval in which the next random number 147 /// has to lie.</param> 90 148 public void RandomizeUniform(ConstrainedDoubleData data, MersenneTwister mt, double min, double max) { 91 149 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { … … 100 158 throw new InvalidOperationException("Couldn't find a valid value"); 101 159 } 102 160 /// <summary> 161 /// Generates a new int random number, being restricted to some constraints. 162 /// </summary> 163 /// <exception cref="InvalidOperationException">Thrown when no valid value could be found.</exception> 164 /// <param name="data">The int object which the new value is assigned to and whose constraints 165 /// must be fulfilled.</param> 166 /// <param name="mt">The random number generator.</param> 167 /// <param name="min">The left border of the interval in which the next random number has to lie.</param> 168 /// <param name="max">The right border (exclusive) of the interval in which the next random number 169 /// has to lie.</param> 103 170 public void RandomizeUniform(ConstrainedIntData data, MersenneTwister mt, double min, double max) { 104 171 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
Note: See TracChangeset
for help on using the changeset viewer.