Changeset 1254 for cpp/frams/genetics/genooperators.cpp
- Timestamp:
- 06/22/23 03:29:05 (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/genetics/genooperators.cpp
r1247 r1254 5 5 #include <ctype.h> //isupper() 6 6 #include <algorithm> // std::min, std::max 7 #include <cmath> // std::floor() 7 8 #include "genooperators.h" 8 9 #include <common/log.h> … … 203 204 if (result > mx) result = mx - (result - mx); else 204 205 if (result < mn) result = mn + (mn - result); 205 //wrap (just in case 'result' exceeded the allowed range so much that after reflection above it exceeded the other boundary):206 //wrap (just in case 'result' exceeded the allowed range so much that after the reflection above it exceeded the other boundary): 206 207 if (result > mx) result = mn + fmod(result - mx, mx - mn); else 207 208 if (result < mn) result = mn + fmod(mn - result, mx - mn); … … 210 211 //reflect and wrap above may have changed the (limited) precision, so try to round again (maybe unnecessarily, because we don't know if reflect+wrap above were triggered) 211 212 double result_try = round(result, 3); 212 if (mn <= result_try && result_try <= mx) result = result_try; //after rounding still witin allowed range, so keep rounded value 213 } 214 } 213 if (mn <= result_try && result_try <= mx) result = result_try; //after rounding still within allowed range, so keep rounded value 214 } 215 } 216 clipNegativeZeroIfNeeded(result, mn); //so we don't get -0.0 when minimum is 0.0 215 217 return result; 216 218 } … … 222 224 } 223 225 224 void GenoOperators::setIntFromDoubleWithProbabilisticDithering(ParamInterface &p, int index, double value) //TODO 225 { 226 p.setInt(index, (paInt)(value + 0.5)); //TODO value=2.499 will result in 2 and 2.5 will result in 3, but we want these cases to be 2 or 3 with almost equal probability. value=2.1 should be mostly 2, rarely 3. Careful with negative values (test it!) 226 void GenoOperators::setIntFromDoubleWithProbabilisticDithering(ParamInterface &p, int index, double value) 227 { 228 // Deterministic rounding to the closest integer: 229 //value += 0.5; // value==2.499 will become int 2 and value==2.5 will become int 3, but we want these cases to be 2 or 3 with almost equal probability (stochastic rounding). 230 231 //stochastic rounding (value==2.1 should turn in most cases to int 2, rarely to int 3; value==-2.1 should become mostly int -2, rarely int -3): 232 double lower = std::floor(value); 233 value = rndDouble(1) < (value - lower) ? lower + 1 : lower; 234 235 p.setInt(index, (paInt)value); 227 236 } 228 237
Note: See TracChangeset
for help on using the changeset viewer.