sim_compact_com.mp4
A delivery route optimization and visualization dashboard for an Instacart-like business model. This system simulates and optimizes delivery operations using real-world data from San Francisco. Also supports returns and return paths. All modeling is probablistic based on demand rates (changeable parameters). Assumes orders are received at start of time period (i.e. at start of day, hr, week, etc)
Category | Description |
---|---|
Simulation Engine | Real-time computational simulation tracking driver positions, package states, house and retailer statuses |
Interactive UI | Frontend displaying driver paths, package heatmaps, and key metrics ($/package, total cost, driver time) |
VRP Solver | Google OR-Tools implementation for complex routing with pickup/delivery constraints |
Optimization | Multiple modes (time, cost, distance) for route optimization |
Data | San Francisco housing and retail locations with optional traffic simulation |
Architecture | Object-oriented design for all system components (drivers, retailers, packages, houses) |
# Clone the repository
git clone https://github.com/yourusername/DeliverySim.git
cd DeliverySim
# Install dependencies
pip install -r requirements.txt
python app/app.py
Navigate to the provided local URL to access the interactive dashboard interface where you can watch the simulation unfold in real-time.
# Run a basic simulation with default parameters
python sim/BasicSim.py
NOTE: it is useful to adjust the solver parameters. The solver max time determines the accuracy of the solution. Increase it for a better solution. For example,
OR_TOOLS_TIME_LIMIT_SECONDS = 60*5 # five minute solve-time per day
You can also adjust the mean number of packages by adjusting this parameter:
# Package Delivery Rates
MEAN_PACKAGE_PER_DAY_PER_HOUSE = -1 # mean num packages, normal dist, std=1. I recommend values from -1 to 1.
You may need to increase solve times as the number of packages increases (otherwise a valid solution may not be found).
app/
: Web dashboard and visualization components using Flask and SocketIOconsts/
: Configuration constants, distance matrices, and world variablesobjects/
: Core object models (drivers, packages, retailers, homes, locations)sim/
: Simulation logic including OR-Tools VRP implementationutils/
: Helper utilitiesdata/
: Simulation run history and results
The uploaded (to this repo) distance matrices are only for SF. You can create your own for any city by modifying the following:
-
Change the city name in
consts/world_variables.py
: -
Update the retailer and home coordinates in
consts/consts.py
to match locations in your chosen city. The current lists (list_of_retailer_coords
andlist_of_home_coords
) contain San Francisco coordinates only. -
Configure traffic and cost settings in
consts/consts.py
:# Traffic Settings ADD_TRAFFIC_DELAYS = True # Set to False if you don't want traffic delays STOPSIGN_EXPECTED_DELAY = 5 # seconds STOPLIGHT_EXPECTED_DELAY = 10 # seconds # Cost type for distance matrix DISTANCE_MATRIX_COST_TYPE = 'money' # Options: 'length', 'time', 'money'. If money, uses a calculated rate of min_wage * time + mile_rate * miles driven
-
Generate a new distance matrix by starting the sim. The sim will take longer than normal and eventually generate a distance matrix.:
python app/app.py
-
The new distance matrix will be saved as
distance_matrix.json
in the root directory. You can move this to theconsts/distance_matrices/
directory if you want to keep multiple matrices. -
If you want to generate matrices with different settings (with/without traffic, cost vs time vs distance, etc), you can:
- Change the
ADD_TRAFFIC_DELAYS
setting - Run the generation command again
- Save each matrix with a descriptive name in the
consts/distance_matrices/
directory
- Change the
Note: Generating a distance matrix can take significant time depending on the number of locations and the size of the city's road network. The system uses OSMnx to download the street network data for your specified city.
The simulation is highly configurable through parameters in consts/consts.py
. Key parameters include:
RUN_HEADLESS
: Controls whether to run in browser (visual) or headless mode (faster)SHOW_ANY_EDGES
: Toggle for displaying map edgesSHOW_DETAILED_EDGES
: Show detailed edge information when edges are displayed
ADD_TRAFFIC_DELAYS
: Enable realistic traffic delay simulationSTOPSIGN_EXPECTED_DELAY
: Average delay at stop signs (seconds)STOPLIGHT_EXPECTED_DELAY
: Average delay at traffic lights (seconds)
TIMESTEP_RATE
: Time between state updates (seconds)SCALING_FACTOR
: Simulation time per second of real time (accelerates simulation)SIMULATION_LENGTH_DAYS
: Total simulation duration in days
MEAN_PACKAGE_PER_DAY_PER_HOUSE
: Average number of packages per house per dayDRIVER_PICKUP_TOLERANCE
: Tolerance (meters) for drivers to pick up/drop off packagesVEHICLE_MAX_CAPACITY
: Maximum number of packages a vehicle can carryNUM_DRIVERS
: Number of drivers in the simulationPICKUP_DELAY_SECONDS
: Time spent at each pickup locationDROPOFF_DELAY_SECONDS
: Time spent at each dropoff location
MINIMUM_WAGE
: Base hourly wage for drivers (dollars)MINIMUM_WAGE_MULTIPLIER
: Multiplier for minimum wageMILE_RATE
: Compensation per mile driven (dollars)
DISTANCE_MATRIX_COST_TYPE
: Type of cost for optimization ('length', 'time', 'money')OR_TOOLS_TIME_LIMIT_SECONDS
: Time limit for OR-Tools solverOR_TOOLS_MAX_SOLUTION_LIMIT
: Maximum number of solutions for OR-Tools to try
The project uses real-world data sampled from San Francisco:
- Housing zones for delivery destinations
- Retail locations for package origins
- Distance matrices for accurate routing
- Cost calculations based on industry models
DeliverySim is not just a visualization tool but a complete computational simulation that:
- Computes in Real-Time: All driver movements, package pickups/deliveries, and routing decisions are calculated as the simulation runs
- Accelerated Timeline: Runs faster than real-time (configurable via
SCALING_FACTOR
) while maintaining computational accuracy - Live Metrics: Displays driver statistics, package states, and business costs as they evolve
- Dynamic Routing: Solves the Vehicle Routing Problem at the start of each day to optimize delivery routes
- Interactive Controls: Toggle path visibility, clear paths, and observe day transitions as they happen
- Packages are generated daily for delivery from retailers to homes
- The system uses OR-Tools to solve the Vehicle Routing Problem with pickup and delivery constraints
- Drivers follow optimized routes to pick up and deliver packages in real-time
- The dashboard visualizes the entire operation as it happens, showing driver movements, package states, and locations
- Cost metrics and performance analytics are tracked and displayed live as the simulation progresses