- Timestamp:
- 11/23/09 15:17:08 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Random/3.3/NormalRandomizer.cs
r1853 r2524 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Constraints;28 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 28 … … 98 97 if (value is IntData) 99 98 RandomizeNormal((IntData)value, n); 100 else if (value is ConstrainedIntData)101 RandomizeNormal((ConstrainedIntData)value, n);102 99 else if (value is DoubleData) 103 100 RandomizeNormal((DoubleData)value, n); 104 else if (value is ConstrainedDoubleData)105 RandomizeNormal((ConstrainedDoubleData)value, n);106 101 else throw new InvalidOperationException("Can't handle type " + value.GetType().Name); 107 }108 109 /// <summary>110 /// Generates a new double random variable based on a continuous, normally distributed random number generator111 /// <paramref name="normal"/> and checks some contraints.112 /// </summary>113 /// <exception cref="InvalidOperationException">Thrown when with the given settings no valid value in114 /// 100 tries could be found.115 /// </exception>116 /// <param name="data">The double object where to assign the new number to and whose constraints117 /// must be fulfilled.</param>118 /// <param name="normal">The continuous, normally distributed random variable.</param>119 public void RandomizeNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) {120 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {121 double r = normal.NextDouble();122 if (IsIntegerConstrained(data)) {123 r = Math.Round(r);124 }125 if (data.TrySetData(r)) {126 return;127 }128 }129 throw new InvalidOperationException("Couldn't find a valid value in 100 tries with mu=" + normal.Mu + " sigma=" + normal.Sigma);130 }131 132 /// <summary>133 /// Generates a new int random variable based on a continuous, normally distributed random number134 /// generator <paramref name="normal"/> and checks some constraints.135 /// </summary>136 /// <exception cref="InvalidOperationException">Thrown when with the given settings no valid137 /// value could be found.</exception>138 /// <param name="data">The int object where to assign the new value to and whose constraints must139 /// be fulfilled.</param>140 /// <param name="normal">The continuous, normally distributed random variable.</param>141 public void RandomizeNormal(ConstrainedIntData data, NormalDistributedRandom normal) {142 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {143 double r = normal.NextDouble();144 if (data.TrySetData((int)Math.Round(r))) // since r is a continuous, normally distributed random variable rounding should be OK145 return;146 }147 throw new InvalidOperationException("Couldn't find a valid value");148 102 } 149 103 … … 167 121 data.Data = (int)Math.Round(normal.NextDouble()); 168 122 } 169 170 171 private bool IsIntegerConstrained(ConstrainedDoubleData data) {172 foreach (IConstraint constraint in data.Constraints) {173 if (constraint is IsIntegerConstraint) {174 return true;175 }176 }177 return false;178 }179 123 } 180 124 }
Note: See TracChangeset
for help on using the changeset viewer.