Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weighted random selector/sequence #206

Merged
merged 7 commits into from
Aug 21, 2023

Conversation

lostptr
Copy link
Contributor

@lostptr lostptr commented Aug 16, 2023

Description

To better encapsulate the random manipulation logic for composites, I created a RandomizedComposite node that should never be used as is; it is only so that logic can be shared for SelectorRandom and SequenceRandom (or else, I would have to copy and paste a hundred lines on both scripts)

This new class contains the seed settings, option to specify weights for each child and the shuffle logic.

⚠️ Thorough testing needed
The editor for weights works fine but I noticed it is sometimes finicky. I had to reopen the scene for it to work again. Maybe this happened because I was constantly changing the code but I would like to be sure that it is all working properly.

Addressed issues

Screenshots

Weights editor:

ezgif-5-345c281904

Proof of concept

I made a little test to make sure that the weights are actually affecting the random sampling by recording all of the times a node was picked as the first child.
After 36698 times:

  • Action "First" with weight 40 was picked 39.69% of the times
  • Action "Second" with weight 30 was picked 30.03% of the times
  • Action "Third" with weight 15 was picked 15.12% of the times
  • Action "Forth" with weight 15 was picked 15.16% of the times
    image

@bitbrain
Copy link
Owner

Looking great - don't forget to also add some tests for this.

@lostptr lostptr marked this pull request as ready for review August 17, 2023 20:53
@bitbrain bitbrain removed the 🧪 unit-tests-required Related to writing unit tests. label Aug 18, 2023
@bitbrain
Copy link
Owner

@lostptr a couple of findings:

  • renaming a child node affected by a weight resets the weight
  • If I set the weight of Node A to 50 but the weight of node B to 10, currently node B gets invoked more often which is rather confusing - is this intentional? As your description suggestes that a higher weight equals more likelihood to be executed. e.g. if I set weight of Node A to 30 and of node B to 70, A is called way more often which seems to be wrong

weight-issue.zip

bitbrain and others added 2 commits August 19, 2023 22:20
- renaming will not reset the weight
- selector composite was not reversing the children bag (weights were
having the opposite effect)
@lostptr
Copy link
Contributor Author

lostptr commented Aug 20, 2023

renaming a child node affected by a weight resets the weight

Yeah I noticed that too but I had no ideia how to deal with that and thought it wasn't such I big deal. However, I can see how that can be annoying and cause a bunch of silly bugs because people would not notice. I found a way to fix that, albeit not very elegant 😆.

If I set the weight of Node A to 50 but the weight of node B to 10, currently node B gets invoked more often which is rather confusing - is this intentional? As your description suggestes that a higher weight equals more likelihood to be executed. e.g. if I set weight of Node A to 30 and of node B to 70, A is called way more often which seems to be wrong

Oops!
I made a little change in the SequenceRandomComposite and forgot to do the same with the SelectorRandomComposite. The weights were having the exact opposite effect, it should work now.

@bitbrain
Copy link
Owner

renaming a child node affected by a weight resets the weight

Yeah I noticed that too but I had no ideia how to deal with that and thought it wasn't such I big deal. However, I can see how that can be annoying and cause a bunch of silly bugs because people would not notice. I found a way to fix that, albeit not very elegant 😆.

If I set the weight of Node A to 50 but the weight of node B to 10, currently node B gets invoked more often which is rather confusing - is this intentional? As your description suggestes that a higher weight equals more likelihood to be executed. e.g. if I set weight of Node A to 30 and of node B to 70, A is called way more often which seems to be wrong

Oops! I made a little change in the SequenceRandomComposite and forgot to do the same with the SelectorRandomComposite. The weights were having the exact opposite effect, it should work now.

@lostptr Why didn't the test fail? Or was the test broken too?

@lostptr
Copy link
Contributor Author

lostptr commented Aug 21, 2023

Why didn't the test fail? Or was the test broken too?

There where no tests for the selector thats why. I have added them in the latest commit.

@bitbrain bitbrain merged commit f07d169 into bitbrain:godot-4.x Aug 21, 2023
3 checks passed
@lostptr lostptr deleted the feat/weighted-random branch August 24, 2023 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weighted random selectors/sequences
2 participants