From 5105ddecd29e48f4440ac3268a6522c09c34e96d Mon Sep 17 00:00:00 2001 From: GaelLucero Date: Fri, 27 May 2022 17:34:47 -0400 Subject: [PATCH 1/5] adding heading information and created move_forward_or_backward function, which calculates the next position the agent will move. move_forward function, which moves the agent forward by specified amount. move_backward function, which moves the agent backwards by specified amount. turn_right function, turns the agent right by specified degree. turn_left function, turns the agent left by specified degree. --- mesa/agent.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mesa/agent.py b/mesa/agent.py index 6d3d67919c6..bcbb03e70a3 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -28,6 +28,7 @@ def __init__(self, unique_id: int, model: "Model") -> None: 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.""" @@ -36,6 +37,33 @@ def step(self) -> None: 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") + + def move_forward(self, amount): + """Moves the agent forward by the amount given""" + self.move_forward_or_backward(amount, 1) + + def move_backward(self, amount): + """Moves the agent backwards from where its facing by the given amount""" + self.move_forward_or_backward(amount, -1) + + def turn_right(self, degree): + """Turns the agent right by the given degree""" + self.heading = (self.heading - degree) % 360 + + def turn_left(self, degree): + """Turns the agent left by the given degree""" + self.heading = (self.heading + degree) % 360 + + @property def random(self) -> Random: return self.model.random From a6fd559bdc35e90bcd90f4f8aee496cb0de629ce Mon Sep 17 00:00:00 2001 From: GaelLucero Date: Fri, 27 May 2022 17:47:03 -0400 Subject: [PATCH 2/5] Created setxy function which sets the agents position to the specified parameters. set_pos function, sets the current position to the specified pos parameter. distancexy function, gives you the distance of the agent and the given coordinate. distance function, gives you the distance between the agent and another agent. --- mesa/agent.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mesa/agent.py b/mesa/agent.py index bcbb03e70a3..c151269f944 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -63,6 +63,22 @@ def turn_left(self, degree): """Turns the agent left by the given degree""" self.heading = (self.heading + degree) % 360 + def setxy(self, x, y): + """Sets the current position to the specified x,y parameters""" + self.pos = (x, y) + + def set_pos(self, apos): + """Sets the current position to the specified pos parameter""" + self.pos = apos + + def distancexy(self, x, y): + """Gives you the distance of the agent and the given coordinate""" + return math.dist(self.pos, (x, y)) + + 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]) + @property def random(self) -> Random: From 89d628638aaa8d85c66a5cb37c507f254a1dfdd9 Mon Sep 17 00:00:00 2001 From: GaelLucero Date: Fri, 27 May 2022 17:55:52 -0400 Subject: [PATCH 3/5] Created die function which, removes the agent from the schedule and the grid. towardsxy function, calculates angle between a given coordinate and horizon as if the current position is the origin. towards function, calculates angle between an agent and horizon as if the current position is the origin. facexy function, makes agent face a given coordinate. face function, makes agent face another agent --- mesa/agent.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/mesa/agent.py b/mesa/agent.py index c151269f944..08937bd112c 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -79,6 +79,38 @@ 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]) + 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") + + 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 + if dx == 0: + return 90 if dy > 0 else 270 + else: + return math.degrees(math.atan2(dy, dx)) + + 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) + + def facexy(self, x, y): + """Makes agent face a given coordinate""" + self.heading = self.towardsxy(x, y) + + def face(self, another_agent): + """Makes agent face another agent""" + x, y = another_agent.pos + self.facexy(x, y) @property def random(self) -> Random: From 5bb8f256701af120cae69b936550bcd754b683bd Mon Sep 17 00:00:00 2001 From: Catherine Devlin Date: Sun, 26 May 2024 17:10:28 -0400 Subject: [PATCH 4/5] Remove spatial methods Those are included in https://github.com/projectmesa/mesa/pull/2149 --- mesa/agent.py | 64 --------------------------------------------------- 1 file changed, 64 deletions(-) diff --git a/mesa/agent.py b/mesa/agent.py index 02adf9eae4d..46b00d35ad3 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -76,48 +76,6 @@ def step(self) -> None: 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") - - def move_forward(self, amount): - """Moves the agent forward by the amount given""" - self.move_forward_or_backward(amount, 1) - - def move_backward(self, amount): - """Moves the agent backwards from where its facing by the given amount""" - self.move_forward_or_backward(amount, -1) - - def turn_right(self, degree): - """Turns the agent right by the given degree""" - self.heading = (self.heading - degree) % 360 - - def turn_left(self, degree): - """Turns the agent left by the given degree""" - self.heading = (self.heading + degree) % 360 - - def setxy(self, x, y): - """Sets the current position to the specified x,y parameters""" - self.pos = (x, y) - - def set_pos(self, apos): - """Sets the current position to the specified pos parameter""" - self.pos = apos - - def distancexy(self, x, y): - """Gives you the distance of the agent and the given coordinate""" - return math.dist(self.pos, (x, y)) - - 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]) - def die(self): """Removes the agent from the schedule and the grid """ try: @@ -129,28 +87,6 @@ def die(self): except: print("agent.py (die): could not remove agent from self.model.space") - 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 - if dx == 0: - return 90 if dy > 0 else 270 - else: - return math.degrees(math.atan2(dy, dx)) - - 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) - - def facexy(self, x, y): - """Makes agent face a given coordinate""" - self.heading = self.towardsxy(x, y) - - def face(self, another_agent): - """Makes agent face another agent""" - x, y = another_agent.pos - self.facexy(x, y) - @property def random(self) -> Random: return self.model.random From 7fe412e4a5ca58a376013b81f952dceebf8e7aab Mon Sep 17 00:00:00 2001 From: Catherine Devlin Date: Sun, 26 May 2024 17:29:45 -0400 Subject: [PATCH 5/5] Informative warnings rather than print upon .die failure --- mesa/agent.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mesa/agent.py b/mesa/agent.py index 46b00d35ad3..7ec94196bea 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -80,12 +80,15 @@ 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") + except Exception as exc: + warnings.warn(f"agent.py (die): could not remove agent {self.id} " + f"from self.model.schedule. Not removing from space.\n{exc}") + return try: self.model.space.remove_agent(self) - except: - print("agent.py (die): could not remove agent from self.model.space") + except Exception as exc: + warnings.warn(f"agent.py (die): could not remove agent {self.id} " + . f"from self.model.space.\n{exc}") @property def random(self) -> Random: