From 34d80798c12f8a165a91fd418034230b08a5e8a6 Mon Sep 17 00:00:00 2001 From: PRIYANSHU TIWARI Date: Wed, 28 Aug 2024 11:22:44 +0530 Subject: [PATCH 1/4] Example Project :-Dijkstra Journey --- examples/dijkstra_journey/README.md | 18 +++ examples/dijkstra_journey/app.py | 25 ++++ .../dijkstra_journey/__init__.py | 0 .../dijkstra_journey/model.py | 110 ++++++++++++++++++ examples/dijkstra_journey/setup.py | 11 ++ 5 files changed, 164 insertions(+) create mode 100644 examples/dijkstra_journey/README.md create mode 100644 examples/dijkstra_journey/app.py create mode 100644 examples/dijkstra_journey/dijkstra_journey/__init__.py create mode 100644 examples/dijkstra_journey/dijkstra_journey/model.py create mode 100644 examples/dijkstra_journey/setup.py diff --git a/examples/dijkstra_journey/README.md b/examples/dijkstra_journey/README.md new file mode 100644 index 00000000..c399826f --- /dev/null +++ b/examples/dijkstra_journey/README.md @@ -0,0 +1,18 @@ +Dijkstra_journey +======================== + +Dijkstra's algorithm, developed by Edsger W. Dijkstra in 1956, is a classic algorithm used to find the shortest paths between nodes in a graph. Its applications span network routing, geographical mapping, and various optimization problems. + +## How to run +To launch the interactive visualization, run `solara run app.py` in this directory. Tune the $\alpha$ and $\beta$ parameters to modify how much the pheromone and city proximity influence the ants' decisions, respectively. See the Algorithm details section for more. + + +## Algorithm details + formula used in Dijkstra's algorithm: + +[ d[v] = \min(d[v], d[u] + w(u, v)) \] + +where: +- \( d[v] \) is the current shortest distance from the start node to node \( v \). +- \( d[u] \) is the shortest distance from the start node to node \( u \). +- \( w(u, v) \) is the weight of the edge between nodes \( u \) and \( v \). diff --git a/examples/dijkstra_journey/app.py b/examples/dijkstra_journey/app.py new file mode 100644 index 00000000..700d5acd --- /dev/null +++ b/examples/dijkstra_journey/app.py @@ -0,0 +1,25 @@ +from mesa.visualization import SolaraViz +from dijkstra_journey.model import Dijkstra_JourneyModel + +def circle_portrayal_example(agent): + return { + "size": 40, + "color": "tab:red", + "Layer": 1, + "Shape": "circle", + } + +model_params = { + "num_agents": 10, + "width": 10, + "height": 10, +} + +page = SolaraViz( + model_class=Dijkstra_JourneyModel, + model_params=model_params, + agent_portrayal=circle_portrayal_example, + measures=["num_agents"] +) + +page diff --git a/examples/dijkstra_journey/dijkstra_journey/__init__.py b/examples/dijkstra_journey/dijkstra_journey/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/examples/dijkstra_journey/dijkstra_journey/model.py b/examples/dijkstra_journey/dijkstra_journey/model.py new file mode 100644 index 00000000..fd374263 --- /dev/null +++ b/examples/dijkstra_journey/dijkstra_journey/model.py @@ -0,0 +1,110 @@ +import mesa +import heapq + +class Dijkstra_JourneyAgent(mesa.Agent): + """ + An agent that performs pathfinding using Dijkstra's algorithm. + """ + + def __init__(self, unique_id, model, start_pos, goal_pos): + """ + Initialize the agent with a start position and a goal position. + """ + super().__init__(unique_id, model) + self.position = start_pos + self.goal = goal_pos + self.path = [] # To store the shortest path + self.current_step = 0 + + def step(self): + """ + Move the agent along the computed path towards the goal. + """ + if self.current_step < len(self.path): + self.move_to(self.path[self.current_step]) + self.current_step += 1 + + def move_to(self, new_position): + """ + Move the agent to the specified position and update its grid location. + """ + self.model.grid.move_agent(self, new_position) + self.position = new_position + + def find_shortest_path(self, start, end): + """ + Compute the shortest path using Dijkstra's algorithm. + """ + width = self.model.grid.width + height = self.model.grid.height + + def get_neighbors(position): + x, y = position + neighbors = [(x + dx, y + dy) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]] + neighbors = [(nx, ny) for nx, ny in neighbors if 0 <= nx < width and 0 <= ny < height] + return neighbors + + graph = { (x, y): get_neighbors((x, y)) for x in range(width) for y in range(height) } + + distances = {node: float('inf') for node in graph} + previous_nodes = {node: None for node in graph} + distances[start] = 0 + + queue = [(0, start)] + heapq.heapify(queue) + + while queue: + current_distance, current_node = heapq.heappop(queue) + + if current_node == end: + break + + for neighbor in graph[current_node]: + distance = current_distance + 1 # All edges have weight 1 + if distance < distances[neighbor]: + distances[neighbor] = distance + previous_nodes[neighbor] = current_node + heapq.heappush(queue, (distance, neighbor)) + + path = [] + step = end + while step is not None: + path.append(step) + step = previous_nodes[step] + path.reverse() + + return path + + def get_path(self): + """ + Returns the computed path. + """ + return self.path + +class Dijkstra_JourneyModel(mesa.Model): + """ + A model with agents that use Dijkstra's algorithm to find paths. + """ + + def __init__(self, num_agents, width, height): + super().__init__() + self.num_agents = num_agents + self.schedule = mesa.time.RandomActivation(self) + self.grid = mesa.space.MultiGrid(width, height, True) + + for i in range(self.num_agents): + start_pos = (self.random.randrange(width), self.random.randrange(height)) + goal_pos = (self.random.randrange(width), self.random.randrange(height)) + agent = Dijkstra_JourneyAgent(i, self, start_pos, goal_pos) + self.schedule.add(agent) + self.grid.place_agent(agent, start_pos) + agent.path = agent.find_shortest_path(start_pos, goal_pos) # Compute the path + + self.datacollector = mesa.datacollection.DataCollector({"num_agents": "num_agents"}) + + def step(self): + """ + Advance the model by one step. + """ + self.datacollector.collect(self) + self.schedule.step() diff --git a/examples/dijkstra_journey/setup.py b/examples/dijkstra_journey/setup.py new file mode 100644 index 00000000..ad3b9e98 --- /dev/null +++ b/examples/dijkstra_journey/setup.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +from setuptools import find_packages, setup + +requires = ["mesa"] + +setup( + name="dijkstra_journey", + version="0.0.1", + packages=find_packages(), + install_requires=requires, +) From f1fb6e5b9244e92442b65e70e075b087e08722ae Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Aug 2024 06:01:50 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/dijkstra_journey/app.py | 4 +++- .../dijkstra_journey/model.py | 24 ++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/examples/dijkstra_journey/app.py b/examples/dijkstra_journey/app.py index 700d5acd..0bdc72bd 100644 --- a/examples/dijkstra_journey/app.py +++ b/examples/dijkstra_journey/app.py @@ -1,6 +1,7 @@ from mesa.visualization import SolaraViz from dijkstra_journey.model import Dijkstra_JourneyModel + def circle_portrayal_example(agent): return { "size": 40, @@ -9,6 +10,7 @@ def circle_portrayal_example(agent): "Shape": "circle", } + model_params = { "num_agents": 10, "width": 10, @@ -19,7 +21,7 @@ def circle_portrayal_example(agent): model_class=Dijkstra_JourneyModel, model_params=model_params, agent_portrayal=circle_portrayal_example, - measures=["num_agents"] + measures=["num_agents"], ) page diff --git a/examples/dijkstra_journey/dijkstra_journey/model.py b/examples/dijkstra_journey/dijkstra_journey/model.py index fd374263..1fdf2501 100644 --- a/examples/dijkstra_journey/dijkstra_journey/model.py +++ b/examples/dijkstra_journey/dijkstra_journey/model.py @@ -1,6 +1,7 @@ import mesa import heapq + class Dijkstra_JourneyAgent(mesa.Agent): """ An agent that performs pathfinding using Dijkstra's algorithm. @@ -40,13 +41,19 @@ def find_shortest_path(self, start, end): def get_neighbors(position): x, y = position - neighbors = [(x + dx, y + dy) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]] - neighbors = [(nx, ny) for nx, ny in neighbors if 0 <= nx < width and 0 <= ny < height] + neighbors = [ + (x + dx, y + dy) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)] + ] + neighbors = [ + (nx, ny) for nx, ny in neighbors if 0 <= nx < width and 0 <= ny < height + ] return neighbors - graph = { (x, y): get_neighbors((x, y)) for x in range(width) for y in range(height) } + graph = { + (x, y): get_neighbors((x, y)) for x in range(width) for y in range(height) + } - distances = {node: float('inf') for node in graph} + distances = {node: float("inf") for node in graph} previous_nodes = {node: None for node in graph} distances[start] = 0 @@ -81,6 +88,7 @@ def get_path(self): """ return self.path + class Dijkstra_JourneyModel(mesa.Model): """ A model with agents that use Dijkstra's algorithm to find paths. @@ -98,9 +106,13 @@ def __init__(self, num_agents, width, height): agent = Dijkstra_JourneyAgent(i, self, start_pos, goal_pos) self.schedule.add(agent) self.grid.place_agent(agent, start_pos) - agent.path = agent.find_shortest_path(start_pos, goal_pos) # Compute the path + agent.path = agent.find_shortest_path( + start_pos, goal_pos + ) # Compute the path - self.datacollector = mesa.datacollection.DataCollector({"num_agents": "num_agents"}) + self.datacollector = mesa.datacollection.DataCollector( + {"num_agents": "num_agents"} + ) def step(self): """ From da67d8c4ac815d733736ed91abb4b72845ece5f4 Mon Sep 17 00:00:00 2001 From: PRIYANSHU TIWARI <153541511+PRIYANSHU2026@users.noreply.github.com> Date: Wed, 28 Aug 2024 11:38:20 +0530 Subject: [PATCH 3/4] Update README.md --- examples/dijkstra_journey/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dijkstra_journey/README.md b/examples/dijkstra_journey/README.md index c399826f..4a15e71c 100644 --- a/examples/dijkstra_journey/README.md +++ b/examples/dijkstra_journey/README.md @@ -4,7 +4,7 @@ Dijkstra_journey Dijkstra's algorithm, developed by Edsger W. Dijkstra in 1956, is a classic algorithm used to find the shortest paths between nodes in a graph. Its applications span network routing, geographical mapping, and various optimization problems. ## How to run -To launch the interactive visualization, run `solara run app.py` in this directory. Tune the $\alpha$ and $\beta$ parameters to modify how much the pheromone and city proximity influence the ants' decisions, respectively. See the Algorithm details section for more. +To launch the interactive visualization, run `solara run app.py` in this directory. ## Algorithm details From 96d87e28ac5cc7fd261a46867f1ecbf661ec538c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Aug 2024 06:08:26 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/dijkstra_journey/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dijkstra_journey/README.md b/examples/dijkstra_journey/README.md index 4a15e71c..4fa3cad9 100644 --- a/examples/dijkstra_journey/README.md +++ b/examples/dijkstra_journey/README.md @@ -4,7 +4,7 @@ Dijkstra_journey Dijkstra's algorithm, developed by Edsger W. Dijkstra in 1956, is a classic algorithm used to find the shortest paths between nodes in a graph. Its applications span network routing, geographical mapping, and various optimization problems. ## How to run -To launch the interactive visualization, run `solara run app.py` in this directory. +To launch the interactive visualization, run `solara run app.py` in this directory. ## Algorithm details