A Repository for an ML based solution to Trash Truck routing for real world applications. A Custom model defined on Tensorflow for accurate prediction of Trash Truck's routing for a city, depending on parameters of how fast the trashcans at each street fill up and traffic, such that the operating costs for the company is minimal.
The software is built to be ready to deploy with API endpoints built using FastAPI and telemetry implemented using PostgreSQL.
The repository also contains some sample input data for testing the system
- Predicts individual trashcan fill levels using an LSTM model trained on simulated time-series data.
- Window size is fully configurable to adapt to different prediction horizons or sensor sampling rates.
- Simulates dynamic urban environments with customizable parameters:
- Number of streets and bins
- Trash generation patterns
- Designed to evaluate route optimization strategies in varied scenarios.
- Implements a cost-vs-reward decision tree for route generation.
- Step-by-step:
- Identify mandatory pickups (bins close to full).
- Generate a baseline route.
- For each nearby bin, predict future fill state.
- If the projected reward exceeds the detour cost, include it in the final route.
- Balances fuel/time cost with overflow prevention and efficiency.
- Provides RESTful endpoints for control and operations.
- Easily integrates with visualizations or external dashboards.
- Automatically detects GPU availability and offloads training if possible.
- Supports parallel training of multiple models in configurable batches.
- Designed for rapid experimentation and ensemble model training.
- Tested on RTX 3050 Ti Mobile 4GB with concurrent training of up to 20 LSTM models.
- In case of no GPU, System automatically resorts to sequential model training to not overload the system.
- Modular codebase with clear separation of concerns (simulation, model, API, routing).
- Centralized configuration for model parameters, path planner settings, and system behavior.
- Easy to extend with new models, routing strategies, or visualization layers.
- Togglable extended debug logging for Dev support.
- Designed for iterative experimentation with routing heuristics and prediction models.
- Outputs include raw logs, simulation state dumps, and performance metrics.
- Python 3.10.x (otherwise Tensorflow will break)
- Tensorflow 2.12
- FastAPI
- Numpy/Pandas
- PostgreSQL for database (Optional)
- Postman (for pinging)
git clone <repository_link>
cd TrashPanda
python -m venv venv
venv\Scripts\activate
-- Replace <repository_link> with the correct link
-- use deactivate command to deactivate Virtual envirnment
pip install -r requirements.txt
This will install all the required dependancies
python main.py
We have provided some sample city simulations that you can utilise to test the system, visit tests and you will see some folders with test cases
Each testcase has 3 parts, city_map.json is the city map defined by the user, simulation_script has parameters like TRASHCAN_NUMBER and SIMULATION_DAYS as configurations, the script will automatically analyse the city map and generate a dataset for it depending on parameters and it will ping the given host address to TrashPanda, and simulate its responses.
The Simulation script is universal and will work with any valid city_map provided to it, thus it also serves as a Template for testcases
Upload the city map (.json) and trashcan data (.csv) files. These are validated and stored for model training and predictions.
- Content Type:
multipart/form-data - Fields:
-
city_map: JSON file describing city structure -trashcan_data: CSV file mapping trashcan to edges, and storing time series data for training across coloumns
city_map.json
{
"nodes": [
{"id": "Node1"}, {"id": "Node2"}, {"id": "Node3"},
{"id": "Node4"}, {"id": "Node5"}, {"id": "Node6"},
{"id": "Node7"}, {"id": "Node8"}
],
"edges": [
{"source": "Node1", "target": "Node2", "weight": 1, "id": "edge1"},
{"source": "Node2", "target": "Node3", "weight": 2, "id": "edge2"},
{"source": "Node3", "target": "Node4", "weight": 3, "id": "edge3"},
{"source": "Node4", "target": "Node5", "weight": 4, "id": "edge4"},
{"source": "Node5", "target": "Node6", "weight": 5, "id": "edge5"},
{"source": "Node6", "target": "Node7", "weight": 6, "id": "edge6"},
{"source": "Node7", "target": "Node8", "weight": 7, "id": "edge7"}
]
}
trashcan.csv
EdgeID,TrashcanID,Day 1, Day 2, Day 3
edge1,can1,30,40,50
edge2,can2,60,70,80
edge3,can3,10,20,30
- File types:
city_mapmust be a JSON file.trashcan_datamust be a CSV file.
- Data Integrity:
city_map: Must be a valid map withweightandidas attributes for edges, and edges must be valid.trashcan_data: All trashcans should exist on valid edgeID in the corrospondingcity_map
{
"INFO": "Files uploaded Successfully"
}
Requests the System to train the models based on latest provided data, will return error if data is not provided
NOTE: The system will take decent amount of time to train depending on dataset size, HTTP 200 is sent after successful training, expect long request time without timeout
- None, empty request
{
"INFO": "Models Trained Successfully",
"Time_taken": time_in_seconds
}
Predicts trashcan fill levels and returns an optimized path for collection. For latest data to be provided alongside
- Content Type:
multipart/form-data - Fields:
start_node: Starting node for Path Planner.day_name: Title for column to be appended in datasetlatest_data_file: JSON file with key value pairs for new values of trash for each trashcan
{
"can1": 70,
"can2": 69,
"can3": 68
}
latest_data_filemust be a JSON file.- For each ID:value pair in file, ID should corrospond to a valid trashcan defined in masterdata set during upload, for any missing IDs, error will be returned
System will process the data and make a wise decision on path planning (Described in next topics)
{
"route": ["Node1", "Node2", "Node3", "Node4"]
}
This system is designed to process city infrastructure and real-time trashcan data to predict trashcan fill levels and generate optimized collection routes for large scale use.
The system is composed of the following key modules:
Working:
- Setups, initializes models for each trashcan in dataset, and trains a model for each trashcan using parallel training if GPU is present using tensorflow.
- Divides all trashcans into three categories namely
must visit (MV),visit if worth it (VIWI), andno urgency (NU)based on two informations.Latest trashcan fillsfor today, given by user.Predicted trashcan fillspredicted individually for each trashcan with its own model, these trashcans are not full now but will get full soon hence can be visited if worht it
- Trashcans that will Overflow soon are alwas added to
MVcategory, reducing chances of overflowing. - Trashcans that are not full now but predicted to be full soon are added to
VIWIcategory. - Dynamically selects and returns
Edge ListandEdge Rewardsfor the map, mapping trashcans onto edges.Edge_listis dict ofedgeID : 0/1/2, describing edges intomust visit (MV),visit if worth it (VIWI), andno urgency (NU)categoriesEdge_rewardsis a dict of Reward values for visiting each edge inVIWIcategory
Working:
- Uses
Edge_listandEdge__rewardsfor finding optimal route using optimized Dijkstra's. - We initilize the
city_mapand since its fairly static, we leverage hashing for all route, hence we create a hash of all best possible route from nodeutovin the map at minimum cost. - During prediction, our algorithm uses preprocessed shortest paths instead of calculating it everytime, thus speeding up response time.
- We use a
startposition and create a cyclic route by adding allMVedges to route at minimum cost. - For each
VIWIedge, we calculate cost of taking detour from our shortest route to add that edge and compare against the reward we get for it, this comparison can be tuned by altering scalers defined inconfig.yamlfile. Depending on that the system chooses to add the detour at minimum cost or ignore theVIWIedge. - Finally we return a cyclic path such that all
MWedges are present andVIWIedges added if the detour is worth it
Responsible for:
- Houses model methods for one single trashcan
- We use a LSTM Based neural network for prediction of trashcan values on basis of last
Xdays (configurable by user) - Automates data handling and stuff for each trashcan for itself
- Used internally by
EdgeSelectorduring training and prediction.
- Custom logging wrapper for consistent logs across the system.
- Logs debug, info, warning, and error messages.
- Used by all core modules and the FastAPI app.
- Log files are recorded and stored in logging directory
- Has a debug mode for Extended logging
- Takes complete care of anything related to database
- Handles telemetry like database logging, performance metrics, and analytics.
- Tracks uploads and model training stats.
- Currently commented out but structured for integration with database.
- Supports in data validation and handles file updation for datasets.
- Helps in preprocessing maps and datasets for ease of use.