This project simulates a train network with multiple stations, connected with tracks, packages and trains. It calculates the optimal routes for train to pickup and deliver package based on the network configuration provided in a JSON file.
- Clone the repository
git clone https://github.com/mirzanorazman/train-network.git
cd train-network
- Install dependencies
npm install
- Compile TypeScript files
npx tsc
-
Create a network configuration file. This repo has two examples of a configuration file,
network-simple.json
andnetwork-complex.json
-
Run the simulation
node dist/main.js ./src/data/network-complex.json
- View the results
- At the end of the simulation, the console will display the result which consist of train movements and the total time taken for the simulation to complete (in
journeyTimeInMinutes
unit)
npm test
- Network input is a JSON file.
- Console log output of the train operations.
Optimization:
- The simulation uses a path finding algorithm called Dijkstra's algorithm to determine the shortest time taken for a train to travel between stations. Since the train network is a type of weighted graph, using Dijkstra's is more optimal in terms finding the shortest route (in time) than using the regular Breadth First Search algorithm.
- For the train-package assignments, I use a greedy approach by taking the closest train to a certain package for pickup.