This project is a pathfinding visualization tool for MRT systems, inspired by heuristic search algorithms such as A* and Dijkstra. The tool uses the MyRapid KL MRT system as its base and allows users to visualize stations, lines, and optimal paths between them.
- Introduction
- Features
- Technologies Used
- Setup and Installation
- Graph Construction
- Pathfinding Algorithms
- Acknowledgements
- License
This project visualizes the MRT stations of MyRapid KL, demonstrating pathfinding algorithms with accurate representations of station interconnections. The idea was inspired by a similar project utilizing MRT stations in Singapore and focuses on manually constructing nodes and edges to replicate the MyRapid KL system.
Link to Project - https://zuhu162.github.io/AStar-MRT-PathFinder/
References -
https://www.youtube.com/watch?v=QXQ4iD4UXOE&t=1s
https://www.youtube.com/watch?v=aKYlikFAV4k&t=789s
https://www.youtube.com/watch?v=i0x5fj4PqP4
- Node and Edge Visualization: Dynamically generated nodes (stations) and edges (connections) using JSON data.
- Pathfinding Algorithm: Implementation of A* algorithm to find the fastest path between two stations based on travel time.
- Interactive Interface: A map-like UI resembling the real MRT system.
- Dynamic Edge Creation: Automated edge generation based on station connections, minimizing manual effort.
- React.js: Frontend framework for building the user interface.
- React Flow: Library for rendering nodes and edges.
- JavaScript/TypeScript: Programming languages for the logic and algorithms.
- Image Editing Tools: Used to grid and map station positions.
- Clone the repository:
git clone https://github.com/Zuhu162/AStar-MRT-PathFinder.git cd AStar-MRT-PathFinder
- Install dependencies:
npm install
- Start the development server:
npm run dev
- Open the application in your browser at
http://localhost:5173
.
-
Plotted each MRT station manually:
- Used an MRT map image, reduced its opacity in an image editing tool, and overlaid a grid.
- Extracted each station's data (coordinates, connections) into a JSON format compatible with React Flow.
Example node data:
{ "id": "titiwangsa", "connectedStations": ["batuCaves", "station2"], "data": { "label": "Titiwangsa", "lines": [ { "color": "yellow", "stationNumber": 17 }, { "color": "brown", "stationNumber": 4 }, { "color": "orange", "stationNumber": 3 }, { "color": "chartreuse", "stationNumber": 11 } ], "connectedStations": [ { "id": "sentulBarat", "time": 2 }, { "id": "hospitalKualaLumpur", "time": 2 }, { "id": "sentul-SentulTimur", "time": 2 }, { "id": "pwtc", "time": 2 }, { "id": "chowKit", "time": 3 } ] }, "position": { "x": 6900, "y": 3000 } }
-
Automated edge creation using a function:
- Each station lists its connected stations.
- Edges are generated dynamically, avoiding duplicates:
if (node.id < connectedStation) { edges.push({ id: `${node.id}-${connectedStation}`, source: node.id, target: connectedStation, type: "line", }); }
-
Edge Duplication:
- Edges were created twice (e.g., A-B and B-A).
- Fixed using an
if
condition to compare IDs.
-
Interchange Stations:
- Stations connected by multiple lines caused overlapping edges.
- Ensured correct line and handle connections using station IDs and line-specific rules.
-
Path Visualization:
- Ensured that lines followed their respective colors and avoided overlapping edges.
The A* algorithm determines the shortest path by combining:
- g(n): Cost to travel from the start node to the current node.
- h(n): Estimated cost to travel from the current node to the target node (heuristic).
Formula:
f(n) = g(n) + h(n)
The heuristic uses the Euclidean distance between the current node and the target node:
const calculateHeuristic = (currentNode, targetNode) => {
const dx = currentNode.position.x - targetNode.position.x;
const dy = currentNode.position.y - targetNode.position.y;
return Math.sqrt(dx * dx + dy * dy);
};
- Open Set: Nodes yet to be fully explored.
- Closed Set: Nodes already evaluated.
- Inspired by the pathfinding project for Singapore MRT.
- Special thanks to online tutorials and resources on A* and graph algorithms.
This project is licensed under the MIT License. See the LICENSE
file for details.
Feel free to contribute or raise issues in the Issues section!