Changeset 763 for trunk/sources/HeuristicLab.Random/UniformRandomizer.cs
- Timestamp:
- 11/15/08 16:47:45 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Random/UniformRandomizer.cs
r719 r763 61 61 double max = GetVariableValue<DoubleData>("Max", scope, true).Data; 62 62 63 value.Accept(new RandomVisitor(mt, min, max)); 64 63 RandomizeUniform(value, mt, min, max); 65 64 return null; 66 65 } 67 66 68 private class RandomVisitor : ObjectDataVisitorBase { 69 private MersenneTwister mt; 70 private double min; 71 private double max; 67 private void RandomizeUniform(IObjectData value, MersenneTwister mt, double min, double max) { 68 // Dispatch manually based on dynamic type, 69 // a bit awkward but necessary until we create a better type hierarchy for numeric types (gkronber 15.11.2008). 70 if (value is DoubleData) 71 RandomizeUniform((DoubleData)value, mt, min, max); 72 else if (value is ConstrainedDoubleData) 73 RandomizeUniform((ConstrainedDoubleData)value, mt, min, max); 74 else if (value is IntData) 75 RandomizeUniform((IntData)value, mt, min, max); 76 else if (value is ConstrainedIntData) 77 RandomizeUniform((ConstrainedIntData)value, mt, min, max); 78 else throw new ArgumentException("Can't handle type " + value.GetType().Name); 79 } 72 80 73 public RandomVisitor(MersenneTwister mt, double min, double max) {74 this.mt = mt;75 this.min = min;76 this.max = max;77 }78 81 79 public override void Visit(DoubleData data) {82 public void RandomizeUniform(DoubleData data, MersenneTwister mt, double min, double max) { 80 83 data.Data = mt.NextDouble() * (max - min) + min; 81 84 } 82 85 83 public override void Visit(IntData data) {86 public void RandomizeUniform(IntData data, MersenneTwister mt, double min, double max) { 84 87 data.Data = (int)Math.Floor(mt.NextDouble() * (max - min) + min); 85 88 } 86 89 87 public override void Visit(ConstrainedDoubleData data) {90 public void RandomizeUniform(ConstrainedDoubleData data, MersenneTwister mt, double min, double max) { 88 91 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 89 92 double r = mt.NextDouble() * (max - min) + min; … … 95 98 } 96 99 } 97 throw new Invalid ProgramException("Couldn't find a valid value");100 throw new InvalidOperationException("Couldn't find a valid value"); 98 101 } 99 102 100 public override void Visit(ConstrainedIntData data) {103 public void RandomizeUniform(ConstrainedIntData data, MersenneTwister mt, double min, double max) { 101 104 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 102 105 int r = (int)Math.Floor(mt.NextDouble() * (max - min) + min); … … 105 108 } 106 109 } 107 throw new Invalid ProgramException("Couldn't find a valid value");110 throw new InvalidOperationException("Couldn't find a valid value"); 108 111 } 109 112 … … 117 120 } 118 121 } 119 }120 122 }
Note: See TracChangeset
for help on using the changeset viewer.