Changeset 2524 for trunk/sources/HeuristicLab.Random
- Timestamp:
- 11/23/09 15:17:08 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Random/3.3
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Random/3.3/HeuristicLab.Random-3.3.csproj
r1691 r2524 96 96 </ItemGroup> 97 97 <ItemGroup> 98 <ProjectReference Include="..\..\HeuristicLab.Constraints\3.3\HeuristicLab.Constraints-3.3.csproj">99 <Project>{19C1E42A-4B48-4EFD-B697-899016F1C198}</Project>100 <Name>HeuristicLab.Constraints-3.3</Name>101 </ProjectReference>102 98 <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj"> 103 99 <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project> -
trunk/sources/HeuristicLab.Random/3.3/NormalRandomAdder.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 … … 106 105 if (value is IntData) 107 106 AddNormal((IntData)value, normal); 108 else if (value is ConstrainedIntData)109 AddNormal((ConstrainedIntData)value, normal);110 else if (value is ConstrainedDoubleData)111 AddNormal((ConstrainedDoubleData)value, normal);112 107 else if (value is DoubleData) 113 108 AddNormal((DoubleData)value, normal); … … 125 120 126 121 /// <summary> 127 /// Generates a new double random number and adds it to the value of the given <paramref name="data"/>128 /// checking its constraints.129 /// </summary>130 /// <exception cref="InvalidProgramException">Thrown when with the current settings no valid value131 /// could be found.</exception>132 /// <param name="data">The double object where to add the random number and whose constraints133 /// to fulfill.</param>134 /// <param name="normal">The continuous, normally distributed random variable.</param>135 public void AddNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) {136 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {137 double newValue = data.Data + normal.NextDouble();138 if (IsIntegerConstrained(data)) {139 newValue = Math.Round(newValue);140 }141 if (data.TrySetData(newValue)) {142 return;143 }144 }145 throw new InvalidProgramException("Coudn't find a valid value");146 }147 148 /// <summary>149 122 /// Generates a new int random number and adds it to value of the given <paramref name="data"/>. 150 123 /// </summary> … … 154 127 data.Data = (int)Math.Round(data.Data + normal.NextDouble()); 155 128 } 156 157 /// <summary>158 /// Generates a new int random number and adds it to the value of the given <paramref name="data"/>159 /// checking its constraints.160 /// </summary>161 /// <exception cref="InvalidProgramException">Thrown when with the current settings no valid value162 /// could be found.</exception>163 /// <param name="data">The int object where to add the generated value and whose contraints to check.</param>164 /// <param name="normal">The continuous, normally distributed random variable.</param>165 public void AddNormal(ConstrainedIntData data, NormalDistributedRandom normal) {166 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {167 if (data.TrySetData((int)Math.Round(data.Data + normal.NextDouble())))168 return;169 }170 throw new InvalidProgramException("Couldn't find a valid value.");171 }172 173 private bool IsIntegerConstrained(ConstrainedDoubleData data) {174 foreach (IConstraint constraint in data.Constraints) {175 if (constraint is IsIntegerConstraint) {176 return true;177 }178 }179 return false;180 }181 129 } 182 130 } -
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 } -
trunk/sources/HeuristicLab.Random/3.3/UniformRandomAdder.cs
r1530 r2524 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Constraints;28 27 29 28 namespace HeuristicLab.Random { … … 91 90 if (value is IntData) 92 91 AddUniform((IntData)value, mt, min, max); 93 else if (value is ConstrainedIntData)94 AddUniform((ConstrainedIntData)value, mt, min, max);95 92 else if (value is DoubleData) 96 93 AddUniform((DoubleData)value, mt, min, max); 97 else if (value is ConstrainedDoubleData)98 AddUniform((ConstrainedDoubleData)value, mt, min, max);99 94 else throw new InvalidOperationException("Can't handle type " + value.GetType().Name); 100 }101 102 /// <summary>103 /// Adds a new double random variable being restricted to some constraints to the value of the given104 /// <paramref name="data"/>.105 /// </summary>106 /// <exception cref="InvalidProgramException">Thrown when no valid value can be found.</exception>107 /// <param name="data">The data where to add the new variable and where to assign the new value to.</param>108 /// <param name="mt">The random number generator.</param>109 /// <param name="min">The left border of the interval in which the next random number has to lie.</param>110 /// <param name="max">The right border (exclusive) of the interval in which the next random number111 /// has to lie.</param>112 public void AddUniform(ConstrainedDoubleData data, MersenneTwister mt, double min, double max) {113 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {114 double newValue = data.Data + mt.NextDouble() * (max - min) + min;115 if (IsIntegerConstrained(data)) {116 newValue = Math.Floor(newValue);117 }118 if (data.TrySetData(newValue)) {119 return;120 }121 }122 throw new InvalidProgramException("Couldn't find a valid value");123 }124 125 /// <summary>126 /// Adds a new int random variable being restricted to some constraints to the value of the given127 /// <paramref name="data"/>.128 /// </summary>129 /// <exception cref="InvalidProgramException">Thrown when no valid value could be found.</exception>130 /// <param name="data">The data where to add the random value and where to assign the new value to.</param>131 /// <param name="mt">The random number generator.</param>132 /// <param name="min">The left border of the interval in which the next random number has to lie.</param>133 /// <param name="max">The right border (exclusive) of the interval in which the next random number134 /// has to lie.</param>135 public void AddUniform(ConstrainedIntData data, MersenneTwister mt, double min, double max) {136 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {137 int newValue = (int)Math.Floor(data.Data + mt.NextDouble() * (max - min) + min);138 if (data.TrySetData(newValue)) {139 return;140 }141 }142 throw new InvalidProgramException("Couldn't find a valid value");143 95 } 144 96 … … 168 120 data.Data = (int)Math.Floor(data.Data + mt.NextDouble() * (max - min) + min); 169 121 } 170 private bool IsIntegerConstrained(ConstrainedDoubleData data) {171 foreach (IConstraint constraint in data.Constraints) {172 if (constraint is IsIntegerConstraint) {173 return true;174 }175 }176 return false;177 }178 122 } 179 123 } -
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.