Changeset 763 for trunk/sources/HeuristicLab.Random
- Timestamp:
- 11/15/08 16:47:45 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Random
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Random/NormalRandomAdder.cs
r719 r763 71 71 NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma * factor); 72 72 73 value.Accept(new RandomAdderVisitor(normal)); 74 73 AddNormal(value, normal); 75 74 return null; 76 75 } 77 76 77 private void AddNormal(IObjectData value, NormalDistributedRandom normal) { 78 // dispatch manually based on dynamic type 79 if (value is IntData) 80 AddNormal((IntData)value, normal); 81 else if (value is ConstrainedIntData) 82 AddNormal((ConstrainedIntData)value, normal); 83 else if (value is ConstrainedDoubleData) 84 AddNormal((ConstrainedDoubleData)value, normal); 85 else if (value is DoubleData) 86 AddNormal((DoubleData)value, normal); 87 else throw new InvalidOperationException("Can't handle type " + value.GetType().Name); 88 } 89 public void AddNormal(DoubleData data, NormalDistributedRandom normal) { 90 data.Data += normal.NextDouble(); 91 } 78 92 79 private class RandomAdderVisitor : ObjectDataVisitorBase { 80 private NormalDistributedRandom normal; 81 public RandomAdderVisitor(NormalDistributedRandom normal) { 82 this.normal = normal; 93 public void AddNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) { 94 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 95 double newValue = data.Data + normal.NextDouble(); 96 if (IsIntegerConstrained(data)) { 97 newValue = Math.Round(newValue); 98 } 99 if (data.TrySetData(newValue)) { 100 return; 101 } 83 102 } 103 throw new InvalidProgramException("Coudn't find a valid value"); 104 } 84 105 85 public override void Visit(DoubleData data) { 86 data.Data += normal.NextDouble(); 106 public void AddNormal(IntData data, NormalDistributedRandom normal) { 107 data.Data = (int)Math.Round(data.Data + normal.NextDouble()); 108 } 109 110 public void AddNormal(ConstrainedIntData data, NormalDistributedRandom normal) { 111 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 112 if (data.TrySetData((int)Math.Round(data.Data + normal.NextDouble()))) 113 return; 87 114 } 115 throw new InvalidProgramException("Couldn't find a valid value."); 116 } 88 117 89 public override void Visit(ConstrainedDoubleData data) { 90 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 91 double newValue = data.Data + normal.NextDouble(); 92 if(IsIntegerConstrained(data)) { 93 newValue = Math.Round(newValue); 94 } 95 if(data.TrySetData(newValue)) { 96 return; 97 } 118 private bool IsIntegerConstrained(ConstrainedDoubleData data) { 119 foreach (IConstraint constraint in data.Constraints) { 120 if (constraint is IsIntegerConstraint) { 121 return true; 98 122 } 99 throw new InvalidProgramException("Coudn't find a valid value");100 123 } 101 102 public override void Visit(IntData data) { 103 data.Data = (int)Math.Round(data.Data + normal.NextDouble()); 104 } 105 106 public override void Visit(ConstrainedIntData data) { 107 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 108 if(data.TrySetData((int)Math.Round(data.Data + normal.NextDouble()))) 109 return; 110 } 111 throw new InvalidProgramException("Couldn't find a valid value."); 112 } 113 114 private bool IsIntegerConstrained(ConstrainedDoubleData data) { 115 foreach(IConstraint constraint in data.Constraints) { 116 if(constraint is IsIntegerConstraint) { 117 return true; 118 } 119 } 120 return false; 121 } 124 return false; 122 125 } 123 126 } -
trunk/sources/HeuristicLab.Random/NormalRandomizer.cs
r719 r763 62 62 double mu = GetVariableValue<DoubleData>("Mu", scope, true).Data; 63 63 double sigma = GetVariableValue<DoubleData>("Sigma", scope, true).Data; 64 NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma);65 64 66 value.Accept(new RandomizerVisitor(normal));67 65 NormalDistributedRandom n = new NormalDistributedRandom(mt, mu, sigma); 66 RandomizeNormal(value, n); 68 67 return null; 69 68 } 70 69 71 private class RandomizerVisitor : ObjectDataVisitorBase { 72 private NormalDistributedRandom normal; 70 private void RandomizeNormal(IObjectData value, NormalDistributedRandom n) { 71 // dispatch manually based on dynamic type 72 if (value is IntData) 73 RandomizeNormal((IntData)value, n); 74 else if (value is ConstrainedIntData) 75 RandomizeNormal((ConstrainedIntData)value, n); 76 else if (value is DoubleData) 77 RandomizeNormal((DoubleData)value, n); 78 else if (value is ConstrainedDoubleData) 79 RandomizeNormal((ConstrainedDoubleData)value, n); 80 else throw new InvalidOperationException("Can't handle type " + value.GetType().Name); 81 } 73 82 74 public RandomizerVisitor(NormalDistributedRandom normal) { 75 this.normal = normal; 83 public void RandomizeNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) { 84 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 85 double r = normal.NextDouble(); 86 if (IsIntegerConstrained(data)) { 87 r = Math.Round(r); 88 } 89 if (data.TrySetData(r)) { 90 return; 91 } 76 92 } 93 throw new InvalidOperationException("Couldn't find a valid value in 100 tries with mu=" + normal.Mu + " sigma=" + normal.Sigma); 94 } 77 95 78 public override void Visit(ConstrainedDoubleData data) { 79 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 80 double r = normal.NextDouble(); 81 if(IsIntegerConstrained(data)) { 82 r = Math.Round(r); 83 } 84 if(data.TrySetData(r)) { 85 return; 86 } 87 } 88 throw new InvalidProgramException("Couldn't find a valid value in 100 tries with mu=" + normal.Mu + " sigma=" + normal.Sigma); 96 public void RandomizeNormal(ConstrainedIntData data, NormalDistributedRandom normal) { 97 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 98 double r = normal.NextDouble(); 99 if (data.TrySetData((int)Math.Round(r))) // since r is a continuous normally distributed random variable rounding should be OK 100 return; 89 101 } 102 throw new InvalidOperationException("Couldn't find a valid value"); 103 } 90 104 91 public override void Visit(ConstrainedIntData data) { 92 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 93 double r = normal.NextDouble(); 94 if(data.TrySetData((int)Math.Round(r))) // since r is a continuous normally distributed random variable rounding should be OK 95 return; 96 } 97 throw new InvalidProgramException("Couldn't find a valid value"); 98 } 105 public void RandomizeNormal(DoubleData data, NormalDistributedRandom normal) { 106 data.Data = normal.NextDouble(); 107 } 99 108 100 public override void Visit(DoubleData data) { 101 data.Data = normal.NextDouble(); 102 } 103 104 public override void Visit(IntData data) { 105 data.Data = (int)Math.Round(normal.NextDouble()); 106 } 109 public void RandomizeNormal(IntData data, NormalDistributedRandom normal) { 110 data.Data = (int)Math.Round(normal.NextDouble()); 111 } 107 112 108 113 109 private bool IsIntegerConstrained(ConstrainedDoubleData data) { 110 foreach(IConstraint constraint in data.Constraints) { 111 if(constraint is IsIntegerConstraint) { 112 return true; 113 } 114 private bool IsIntegerConstrained(ConstrainedDoubleData data) { 115 foreach (IConstraint constraint in data.Constraints) { 116 if (constraint is IsIntegerConstraint) { 117 return true; 114 118 } 115 return false;116 119 } 120 return false; 117 121 } 118 122 } -
trunk/sources/HeuristicLab.Random/UniformRandomAdder.cs
r719 r763 33 33 34 34 public override string Description { 35 get { return @"Samples a uniformly distributed random variable 'U' with range = [min,max] and E(u) = (max-min)/2 35 get { 36 return @"Samples a uniformly distributed random variable 'U' with range = [min,max] and E(u) = (max-min)/2 36 37 and adds the result to the variable 'Value'. ShakingFactor influences the effective range of U. 37 38 If r=(max-min) then the effective range of U is [E(u) - shakingFactor * r/2, E(u) + shakingFactor * r/2]. 38 39 39 40 If a constraint for the allowed range of 'Value' is defined and the result of the operation would be smaller then 40 the smallest allowed value then 'Value' is set to the lower bound and vice versa for the upper bound."; } 41 the smallest allowed value then 'Value' is set to the lower bound and vice versa for the upper bound."; 42 } 41 43 } 42 44 … … 66 68 max = ex + newRange / 2; 67 69 68 value.Accept(new RandomAdderVisitor(mt, min, max)); 69 70 AddUniform(value, mt, min, max); 70 71 return null; 71 72 } 72 73 74 private void AddUniform(IObjectData value, MersenneTwister mt, double min, double max) { 75 // dispatch manually on dynamic type 76 if (value is IntData) 77 AddUniform((IntData)value, mt, min, max); 78 else if (value is ConstrainedIntData) 79 AddUniform((ConstrainedIntData)value, mt, min, max); 80 else if (value is DoubleData) 81 AddUniform((DoubleData)value, mt, min, max); 82 else if (value is ConstrainedDoubleData) 83 AddUniform((ConstrainedDoubleData)value, mt, min, max); 84 else throw new InvalidOperationException("Can't handle type " + value.GetType().Name); 85 } 73 86 74 private class RandomAdderVisitor : ObjectDataVisitorBase {75 private double min;76 private double max;77 private MersenneTwister mt;78 87 79 public RandomAdderVisitor(MersenneTwister mt, double min, double max) { 80 this.mt = mt; 81 this.min = min; 82 this.max = max; 88 public void AddUniform(ConstrainedDoubleData data, MersenneTwister mt, double min, double max) { 89 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 90 double newValue = data.Data + mt.NextDouble() * (max - min) + min; 91 if (IsIntegerConstrained(data)) { 92 newValue = Math.Floor(newValue); 93 } 94 if (data.TrySetData(newValue)) { 95 return; 96 } 83 97 } 98 throw new InvalidProgramException("Couldn't find a valid value"); 99 } 84 100 85 public override void Visit(ConstrainedDoubleData data) { 86 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 87 double newValue = data.Data + mt.NextDouble() * (max - min) + min; 88 if(IsIntegerConstrained(data)) { 89 newValue = Math.Floor(newValue); 90 } 91 if(data.TrySetData(newValue)) { 92 return; 93 } 101 public void AddUniform(ConstrainedIntData data, MersenneTwister mt, double min, double max) { 102 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 103 int newValue = (int)Math.Floor(data.Data + mt.NextDouble() * (max - min) + min); 104 if (data.TrySetData(newValue)) { 105 return; 94 106 } 95 throw new InvalidProgramException("Couldn't find a valid value");96 107 } 108 throw new InvalidProgramException("Couldn't find a valid value"); 109 } 97 110 98 public override void Visit(ConstrainedIntData data) { 99 for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 100 int newValue = (int)Math.Floor(data.Data + mt.NextDouble() * (max - min) + min); 101 if(data.TrySetData(newValue)) { 102 return; 103 } 111 public void AddUniform(DoubleData data, MersenneTwister mt, double min, double max) { 112 data.Data = data.Data + mt.NextDouble() * (max - min) + min; 113 } 114 115 public void AddUniform(IntData data, MersenneTwister mt, double min, double max) { 116 data.Data = (int)Math.Floor(data.Data + mt.NextDouble() * (max - min) + min); 117 } 118 private bool IsIntegerConstrained(ConstrainedDoubleData data) { 119 foreach (IConstraint constraint in data.Constraints) { 120 if (constraint is IsIntegerConstraint) { 121 return true; 104 122 } 105 throw new InvalidProgramException("Couldn't find a valid value");106 123 } 107 108 public override void Visit(DoubleData data) { 109 data.Data = data.Data + mt.NextDouble() * (max - min) + min; 110 } 111 112 public override void Visit(IntData data) { 113 data.Data = (int)Math.Floor(data.Data + mt.NextDouble() * (max - min) + min); 114 } 115 private bool IsIntegerConstrained(ConstrainedDoubleData data) { 116 foreach(IConstraint constraint in data.Constraints) { 117 if(constraint is IsIntegerConstraint) { 118 return true; 119 } 120 } 121 return false; 122 } 124 return false; 123 125 } 124 126 } -
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.