- Timestamp:
- 11/23/09 15:17:08 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Random/3.3/UniformRandomizer.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 … … 105 104 if (value is DoubleData) 106 105 RandomizeUniform((DoubleData)value, mt, min, max); 107 else if (value is ConstrainedDoubleData)108 RandomizeUniform((ConstrainedDoubleData)value, mt, min, max);109 106 else if (value is IntData) 110 107 RandomizeUniform((IntData)value, mt, min, max); 111 else if (value is ConstrainedIntData)112 RandomizeUniform((ConstrainedIntData)value, mt, min, max);113 108 else throw new ArgumentException("Can't handle type " + value.GetType().Name); 114 109 } … … 137 132 data.Data = (int)Math.Floor(mt.NextDouble() * (max - min) + min); 138 133 } 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 constraints145 /// 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 number149 /// 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 constraints167 /// 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 number171 /// 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 134 } 191 135 }
Note: See TracChangeset
for help on using the changeset viewer.