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

Make agent move to actual random closest position #2119

Merged
merged 2 commits into from
Apr 23, 2024

Conversation

EwoutH
Copy link
Member

@EwoutH EwoutH commented Apr 23, 2024

This fixes a bug in which move_agent_to_one_of is deterministic when pos has multiple closest choices and selection="closest".

Closes #2117.

This fixes a bug in which move_agent_to_one_of is deterministic when pos has multiple closest choices and selection="closest".
@EwoutH EwoutH requested review from rht and tpike3 April 23, 2024 18:58
@EwoutH
Copy link
Member Author

EwoutH commented Apr 23, 2024

I don't understand this error:

        # Only move agent if there are positions given (non-empty list)
        if pos:
            if selection == "random":
                chosen_pos = agent.random.choice(pos)
            elif selection == "closest":
                current_pos = agent.pos
                # Find the closest position without sorting all positions
                closest_pos = None
                min_distance = float("inf")
>               for p in agent.random.shuffle(pos):
E               TypeError: 'NoneType' object is not iterable

The if pos: should filter out None values.

The function `random.shuffle` from Python's random module does not return any value; it modifies the list `pos` in-place. Therefore, `agent.random.shuffle(pos)` results in `None`, and trying to iterate over `None` leads to the error you're seeing. This commit fixes this error.
@EwoutH
Copy link
Member Author

EwoutH commented Apr 23, 2024

Right, random.shuffle does not return any value; it modifies the list pos in-place. Therefore, agent.random.shuffle(pos) results in None, and trying to iterate over None leads to the error above.

By putting it on a separate line above it now works as expected.

@EwoutH EwoutH added the bug Release notes label label Apr 23, 2024
@EwoutH EwoutH merged commit 2e282e1 into main Apr 23, 2024
10 of 11 checks passed
@rht rht deleted the fix-non-random-closest-movement branch April 23, 2024 23:45
@rht
Copy link
Contributor

rht commented Apr 23, 2024

Make sure to delete the branch after the PR has been merged.

vitorfrois pushed a commit to vitorfrois/mesa that referenced this pull request Jul 15, 2024
This fixes a bug in which move_agent_to_one_of is deterministic when pos has multiple closest choices and selection="closest".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Release notes label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

move_agent_to_one_of is deterministic when pos has multiple closest choices and selection="closest"
3 participants