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

ABM language #1354

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
self.unique_id = unique_id
self.model = model
self.pos: Optional[Position] = None
self.heading = 90

def step(self) -> None:
"""A single step of the agent."""
Expand All @@ -36,6 +37,81 @@
def advance(self) -> None:
pass

def move_forward_or_backward(self, amount, sign):
"""Does the calculation to find the agent's next move and is used within the forward and backward functions"""
new_x = float(self.pos[0]) + sign * math.cos(self.heading * math.pi / 180) * amount
new_y = float(self.pos[1]) + sign * math.sin(self.heading * math.pi / -180) * amount
next_pos = (new_x, new_y)
try:
self.model.space.move_agent(self, next_pos)
except:
print("agent.py (forward_backwards): could not move agent within self.model.space")

Check warning on line 48 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L42-L48

Added lines #L42 - L48 were not covered by tests

def move_forward(self, amount):
"""Moves the agent forward by the amount given"""
self.move_forward_or_backward(amount, 1)

Check warning on line 52 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L52

Added line #L52 was not covered by tests

def move_backward(self, amount):
"""Moves the agent backwards from where its facing by the given amount"""
self.move_forward_or_backward(amount, -1)

Check warning on line 56 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L56

Added line #L56 was not covered by tests

def turn_right(self, degree):
"""Turns the agent right by the given degree"""
self.heading = (self.heading - degree) % 360

Check warning on line 60 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L60

Added line #L60 was not covered by tests

def turn_left(self, degree):
"""Turns the agent left by the given degree"""
self.heading = (self.heading + degree) % 360

Check warning on line 64 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L64

Added line #L64 was not covered by tests

def setxy(self, x, y):
"""Sets the current position to the specified x,y parameters"""
self.pos = (x, y)

Check warning on line 68 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L68

Added line #L68 was not covered by tests

def set_pos(self, apos):
"""Sets the current position to the specified pos parameter"""
self.pos = apos

Check warning on line 72 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L72

Added line #L72 was not covered by tests

def distancexy(self, x, y):
"""Gives you the distance of the agent and the given coordinate"""
return math.dist(self.pos, (x, y))

Check warning on line 76 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L76

Added line #L76 was not covered by tests

def distance(self, another_agent):
"""Gives you the distance between the agent and another agent"""
return self.distancexy(another_agent.pos[0], another_agent.pos[1])

Check warning on line 80 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L80

Added line #L80 was not covered by tests

def die(self):
"""Removes the agent from the schedule and the grid """
try:
self.model.schedule.remove(self)
except:
print("agent.py (die): could not remove agent from self.model.schedule")
try:
self.model.space.remove_agent(self)
except:
print("agent.py (die): could not remove agent from self.model.space")

Check warning on line 91 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L84-L91

Added lines #L84 - L91 were not covered by tests

def towardsxy(self, x, y):
"""Calculates angle between a given coordinate and horizon as if the current position is the origin"""
dx = x - float(self.pos[0])
dy = float(self.pos[1]) - y

Check warning on line 96 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L95-L96

Added lines #L95 - L96 were not covered by tests
if dx == 0:
return 90 if dy > 0 else 270

Check warning on line 98 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L98

Added line #L98 was not covered by tests
else:
return math.degrees(math.atan2(dy, dx))

Check warning on line 100 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L100

Added line #L100 was not covered by tests

def towards(self, another_agent):
"""Calculates angle between an agent and horizon as if the current position is the origin"""
return self.towardsxy(*another_agent.pos)

Check warning on line 104 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L104

Added line #L104 was not covered by tests

def facexy(self, x, y):
"""Makes agent face a given coordinate"""
self.heading = self.towardsxy(x, y)

Check warning on line 108 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L108

Added line #L108 was not covered by tests

def face(self, another_agent):
"""Makes agent face another agent"""
x, y = another_agent.pos
self.facexy(x, y)

Check warning on line 113 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L112-L113

Added lines #L112 - L113 were not covered by tests

@property
def random(self) -> Random:
return self.model.random