Skip to content

Commit 03c27ee

Browse files
committed
docs(vehicle-routing): remove pre-computed distance mode references
- Remove distance calculation mode selector from features table - Simplify Location class to show only Haversine formula - Remove distanceMode API parameter documentation - Remove UI dropdown and distance mode testing sections - Update constraint names to match actual code (camelCase) - Add Vehicle.name field to class definition - Fix ConstraintVerifier import path
1 parent 7aecdfb commit 03c27ee

File tree

1 file changed

+11
-90
lines changed

1 file changed

+11
-90
lines changed

content/en/docs/getting-started/vehicle-routing.md

Lines changed: 11 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ This implementation includes several enhancements over the standard Timefold qui
7171

7272
| Feature | Benefit |
7373
|---------|---------|
74-
| **Distance calculation mode selector** | Choose between on-demand Haversine (memory-efficient) or pre-computed matrix (faster solving) directly from the UI |
7574
| **Adaptive time windows** | Time windows dynamically scale based on problem area and visit count, ensuring feasible solutions |
7675
| **Haversine formula** | Realistic great-circle distances without external API dependencies |
7776

@@ -185,65 +184,22 @@ class Location:
185184

186185
def driving_time_to(self, other: "Location") -> int:
187186
"""
188-
Get driving time in seconds to another location.
189-
190-
If a pre-computed matrix is available, uses O(1) lookup.
191-
Otherwise, calculates on-demand using Haversine formula.
187+
Get driving time in seconds to another location using Haversine formula.
192188
"""
193-
# Check pre-computed matrix first
194-
key = _get_matrix_key(self, other)
195-
if key in _DRIVING_TIME_MATRIX:
196-
return _DRIVING_TIME_MATRIX[key]
197-
198-
# Fall back to on-demand Haversine calculation
199189
return self._calculate_driving_time_haversine(other)
200190
```
201191

202192
**What it represents:** A geographic coordinate (latitude/longitude).
203193

204194
**Key method:**
205-
- `driving_time_to()`: Gets driving time using either:
206-
1. **Pre-computed matrix lookup** (O(1)) — if matrix is initialized
207-
2. **On-demand Haversine calculation** — fallback for flexibility
208-
209-
#### Distance Calculation Modes
210-
211-
SolverForge offers two distance calculation strategies, selectable from the UI:
212-
213-
| Mode | Memory | Speed | Best For |
214-
|------|--------|-------|----------|
215-
| **On-demand** | O(1) | Moderate | Development, small problems, memory-constrained |
216-
| **Pre-computed** | O(n²) | Fast | Production, large problems, performance-critical |
217-
218-
**On-demand mode** (default):
219-
- Calculates Haversine distance on each `driving_time_to()` call
220-
- Zero memory overhead for distance storage
221-
- Good for development and testing
222-
223-
**Pre-computed mode**:
224-
- Builds distance matrix once before solving: `init_driving_time_matrix(all_locations)`
225-
- All subsequent lookups are O(1) dictionary access
226-
- For FIRENZE (77 visits + 6 depots = 83 locations): only 6,889 entries (~55 KB)
227-
- Significantly faster solving due to millions of distance lookups per solve
228-
229-
```python
230-
# Pre-compute distances for all locations
231-
from vehicle_routing.domain import init_driving_time_matrix, clear_driving_time_matrix
232-
233-
all_locations = [v.home_location for v in vehicles] + [v.location for v in visits]
234-
init_driving_time_matrix(all_locations) # O(n²) one-time cost
235-
236-
# ... solve ...
237-
238-
clear_driving_time_matrix() # Clean up when done
239-
```
195+
- `driving_time_to()`: Calculates driving time using the Haversine formula
240196

241197
**Haversine formula details:**
242198
- Accounts for Earth's curvature using great-circle distance
243199
- Assumes 50 km/h average driving speed
244200
- Example: Philadelphia to New York (~130 km) → ~9,400 seconds (~2.6 hours)
245201

246-
**Optimization concept:** The Haversine formula provides realistic geographic distances without external API dependencies. For production with real road networks, you can replace the distance calculation with a pre-loaded matrix from a routing API (Google Maps, OSRM, etc.) — the solver doesn't care how distances are calculated, only that they're fast to retrieve.
202+
**Optimization concept:** The Haversine formula provides realistic geographic distances without external API dependencies. For production with real road networks, you can replace the distance calculation with a routing API (Google Maps, OSRM, etc.).
247203

248204
### The Visit Class (Planning Entity)
249205

@@ -777,13 +733,10 @@ Returns a specific demo dataset:
777733

778734
**Parameters:**
779735
- `demo_name`: Name of the demo dataset (PHILADELPHIA, HARTFORT, FIRENZE)
780-
- `distanceMode` (query, optional): Distance calculation mode
781-
- `ON_DEMAND` (default): Calculate distances using Haversine formula on each call
782-
- `PRECOMPUTED`: Pre-compute distance matrix for O(1) lookups (faster solving)
783736

784737
**Request:**
785738
```
786-
GET /demo-data/PHILADELPHIA?distanceMode=PRECOMPUTED
739+
GET /demo-data/PHILADELPHIA
787740
```
788741

789742
**Response:**
@@ -1591,11 +1544,9 @@ pytest tests/test_feasible.py -v
15911544
- Click "Stop" after 10 seconds
15921545
- Verify you get a partial solution (may be infeasible)
15931546

1594-
8. **Test distance calculation modes:**
1595-
- Use the calculator dropdown (header) to switch to "Pre-computed matrix"
1596-
- Load FIRENZE dataset again
1597-
- Solve and compare performance with on-demand mode
1598-
- Pre-computed mode should solve faster (O(1) lookups vs repeated Haversine calculations)
1547+
8. **Test with different datasets:**
1548+
- Try PHILADELPHIA (55 visits), HARTFORT (50 visits), and FIRENZE (77 visits)
1549+
- Larger datasets take longer to solve but demonstrate scalability
15991550

16001551
---
16011552

@@ -1646,52 +1597,22 @@ def good_constraint(constraint_factory: ConstraintFactory):
16461597

16471598
SolverForge includes a **built-in distance mode selector** — no custom code required. Choose between:
16481599

1649-
| Mode | When to Use |
1650-
|------|-------------|
1651-
| **On-demand** (default) | Development, small problems (< 50 locations), memory-constrained |
1652-
| **Pre-computed** | Production, large problems, performance-critical deployments |
1653-
1654-
**From the UI:** Use the calculator dropdown in the header to switch modes.
1655-
1656-
**Programmatically:**
1657-
1658-
```python
1659-
from vehicle_routing.domain import init_driving_time_matrix, clear_driving_time_matrix
1660-
from vehicle_routing.demo_data import generate_demo_data, DemoData
1661-
1662-
# Option 1: Generate demo data with pre-computed matrix
1663-
plan = generate_demo_data(DemoData.PHILADELPHIA, use_precomputed_matrix=True)
1664-
1665-
# Option 2: Initialize matrix manually for custom data
1666-
all_locations = [v.home_location for v in vehicles] + [v.location for v in visits]
1667-
init_driving_time_matrix(all_locations) # Pre-computes n² driving times
1668-
1669-
# To switch back to on-demand
1670-
clear_driving_time_matrix()
1671-
```
1672-
1673-
**Why this matters:**
1674-
1675-
The solver evaluates distances **millions of times** during optimization. With 77 locations (FIRENZE dataset), pre-computing stores only 5,929 entries but eliminates repeated trigonometric calculations.
1600+
This quickstart uses the Haversine formula for distance calculations, which provides realistic great-circle distances without external dependencies.
16761601

16771602
### Real Road Network Data
16781603

1679-
For production deployments requiring actual road distances (not straight-line approximations), pre-compute using a routing API **before** solving:
1604+
For production deployments requiring actual road distances (not straight-line approximations), you can replace the distance calculation in `Location.driving_time_to()` with a pre-computed matrix from a routing API:
16801605

16811606
```python
16821607
def build_real_distance_matrix(locations):
1683-
"""Fetch actual driving times from routing API (run once)."""
1608+
"""Fetch actual driving times from routing API (run once, before solving)."""
16841609
matrix = {}
16851610
for loc1 in locations:
16861611
for loc2 in locations:
16871612
if loc1 != loc2:
16881613
# Call Google Maps / Mapbox / OSRM once per pair
1689-
matrix[(loc1.lat, loc1.lng, loc2.lat, loc2.lng)] = call_routing_api(loc1, loc2)
1614+
matrix[(loc1, loc2)] = call_routing_api(loc1, loc2)
16901615
return matrix
1691-
1692-
# Then inject into SolverForge's matrix
1693-
from vehicle_routing.domain import _DRIVING_TIME_MATRIX
1694-
_DRIVING_TIME_MATRIX.update(build_real_distance_matrix(all_locations))
16951616
```
16961617

16971618
**Never** call external APIs during solving — pre-compute everything.

0 commit comments

Comments
 (0)