-
Notifications
You must be signed in to change notification settings - Fork 2k
intfactor in phenotype.js formula error #1487
Comments
Why not creating a pr if you already got some code to fix the issue? :-) |
I barely function with git as is. I'm not confident with I wouldn't push my API keys :) |
Can anyone help me understand why there is a 50% chance of returning 0 if v.min == 0? This doesn't seem right to me. It seems like the chance of returning 0 should be equal to (v.max - v.min) / v.factor. else if (v.type === 'intfactor') {
// possible 0 value by providing min 0
if (v.min == 0 && Math.random() <= 0.5) r[k] = 0
else r[k] = Math.round(((Math.random() * (v.max - v.min) + 1) / v.factor) * v.factor)
} |
@DeviaVir @Shawn8901 I am still having problems with RangeFactor returning out of range values with a current pull. I am testing this function which seems rock solid so far. Please consider using this one as I believe its a vast improvement over the current one. It works with floating points and floating point factors and gets rid of any funny repeating decimals that show up sometimes with the current solution. Getting this right is very important to darwin as it can help prevent so many other errors that come up such as the stalling issues i was experiencing due to parameter values being off. else if (v.type === 'intfactor') {
r[k] = randfactor(v.min, v.max, v.factor)
}
...
function randfactor(min, max, factor) {
let factorString = factor.toString(),
decimalIdx = factorString.indexOf('.') + 1,
decimals = decimalIdx === -1 ? 0 : factorString.length - decimalIdx
return (Math.floor(Math.random() * (max - min) / factor) * factor + min).toFixed(decimals)
} |
I'll look into that when I get a chance. Thanks for the suggestion. |
Where are you placing the function at? I'm not a programmer but I still want to help test it. |
I just put the function at the bottom of the file. |
Here it is without the function call: else if (v.type === 'intfactor') {
let factorString = v.factor.toString(),
decimalIdx = factorString.indexOf('.') + 1,
decimals = decimalIdx === -1 ? 0 : factorString.length - decimalIdx
r[k] = (Math.floor(Math.random() * (v.max - v.min) / v.factor) * v.factor + v.min).toFixed(decimals)
} |
Oops, off by 1 error causing integer factors to return a decimal of '.0'. Other than this cosmetic bug, I've been testing with this for 2 days now and seen no issues. Here is fixed version: else if (v.type === 'intfactor') {
let factorString = v.factor.toString(),
decimalIdx = factorString.indexOf('.') + 1,
decimals = decimalIdx === 0 ? 0 : factorString.length - decimalIdx
r[k] = (Math.floor(Math.random() * (v.max - v.min) / v.factor) * v.factor + v.min).toFixed(decimals)
} |
One last (I hope) revision necessary as this was never returning v.max. Now running RangeFactor(0,1,0.5) results in an even distribution of 0, 0.5 and 1 results else if (v.type === 'intfactor') {
let factorString = v.factor.toString(),
decimalIdx = factorString.indexOf('.') + 1,
decimals = decimalIdx === 0 ? 0 : factorString.length - decimalIdx
r[k] = (Math.floor(Math.random() * (v.max - v.min + v.factor) / v.factor) * v.factor + v.min).toFixed(decimals)
} |
trying it out now |
Are you running this in /lib/phenotype.js? Are you changing it in both range function and create function? |
Yes, its lib/phenotype.js line 22. r and k should be defined there. |
do you also modify the range function as well? Line 62 |
No, I just got rid of that function and put it inline . |
Sorry, were you referring to my earlier randfactor function? Thats what I got rid of. I never modified any of the Range functions. |
disclaimer* Not a programmer. |
The range function. I never touched that one. Haven't thought much about it but things seem to be working for me without changing it. |
This has been working for me. |
System information
Describe the problem
Currently it is possible for the formula for intfactor in phenotype.js to evaluate to zero even if its outside the designated range.
Source code / Error logs
Here is the offending code.
} else if (v.type === 'intfactor') { // possible 0 value by providing min 0 if (v.min == 0 && Math.random() <= 0.5) r[k] = 0 else r[k] = Math.round((Math.random() * (v.max - v.min + 1)/v.factor)*v.factor) }
Here is a possible fix by Shawn
} else if (v.type === 'intfactor') { // possible 0 value by providing min 0 if (v.min == 0 && Math.random() <= 0.5) r[k] = 0 else r[k] = Math.round(((Math.random() * (v.max - v.min) + 1) /v.factor)*v.factor) }
The text was updated successfully, but these errors were encountered: