Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

intfactor in phenotype.js formula error #1487

Closed
LoneWolf345 opened this issue Mar 12, 2018 · 19 comments
Closed

intfactor in phenotype.js formula error #1487

LoneWolf345 opened this issue Mar 12, 2018 · 19 comments
Labels

Comments

@LoneWolf345
Copy link
Contributor

System information

  • Have I written custom code (as opposed to using zenbot vanilla): no
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04
  • Zenbot version (commit ref, or version): current
  • Zenbot branch: unstable

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) }

@Shawn8901
Copy link
Contributor

Why not creating a pr if you already got some code to fix the issue? :-)

@LoneWolf345
Copy link
Contributor Author

I barely function with git as is. I'm not confident with I wouldn't push my API keys :)

@travisstaloch
Copy link
Contributor

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)
      }

@travisstaloch
Copy link
Contributor

travisstaloch commented Mar 15, 2018

@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)
}

@LoneWolf345
Copy link
Contributor Author

I'll look into that when I get a chance. Thanks for the suggestion.

@LoneWolf345
Copy link
Contributor Author

Where are you placing the function at? I'm not a programmer but I still want to help test it.

@travisstaloch
Copy link
Contributor

I just put the function at the bottom of the file.

@travisstaloch
Copy link
Contributor

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)
}

@travisstaloch
Copy link
Contributor

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)
}

@travisstaloch
Copy link
Contributor

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)
      }

@LoneWolf345
Copy link
Contributor Author

trying it out now

@LoneWolf345
Copy link
Contributor Author

LoneWolf345 commented Mar 18, 2018

Are you running this in /lib/phenotype.js? Are you changing it in both range function and create function?

@travisstaloch
Copy link
Contributor

Yes, its lib/phenotype.js line 22. r and k should be defined there.

@LoneWolf345
Copy link
Contributor Author

LoneWolf345 commented Mar 18, 2018

do you also modify the range function as well? Line 62

@travisstaloch
Copy link
Contributor

No, I just got rid of that function and put it inline .

@travisstaloch
Copy link
Contributor

Sorry, were you referring to my earlier randfactor function? Thats what I got rid of. I never modified any of the Range functions.

@LoneWolf345
Copy link
Contributor Author

disclaimer* Not a programmer.
Are you sure that line 62 does not need to be changed too?

@travisstaloch
Copy link
Contributor

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.

@LoneWolf345
Copy link
Contributor Author

This has been working for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants