Metadata-Version: 2.4
Name: pdptw-lns-solver
Version: 0.1.1
Summary: Fast LNS solver for online PDPTW / on-demand transit: insert ride requests into a live fleet schedule.
Author: Danushka Edirimanna
License: MIT
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: numpy>=1.20
Requires-Dist: highspy>=1.5.3
Requires-Dist: requests>=2.25

# pdptw-lns-solver

A fast solver for the **Pickup and Delivery Problem with Time Windows (PDPTW)**,
built for **online / on-demand transit dispatch**. Given a fleet of vehicles with
existing schedules, it inserts new ride requests into those schedules using Large
Neighborhood Search (LNS) — respecting capacities, time windows, and
pickup-before-dropoff order — and tells you which requests it could serve.

- **Insert new requests into a live fleet** without disrupting already-scheduled
  trips.
- **Never drops a committed ride:** if a new request can't be served, only that
  new request is rejected; everything already on a vehicle stays.
- **No external solver license:** works out of the box after `pip install`.
- **Parallel-safe:** each solver instance is independent, so you can run many
  concurrently (e.g. one per worker) with no shared state.

## Install

```bash
pip install pdptw-lns-solver
```

The Linux wheel ships a self-contained engine — no compiler, no optimization-solver
license, and no routing server required to install.

```python
from online_lns_solver import OnlineLNSSolver
```

## Quickstart

```python
from online_lns_solver import OnlineLNSSolver

solver = OnlineLNSSolver(K=3)

payload = {
    "depot":        {"pt": {"lat": 35.72, "lon": -77.91, "node_id": 0}},
    "driver_runs":  [...],   # current vehicle schedules (see format below)
    "requests":     [...],   # new requests to insert
    "travel_time_matrix": [[...]],   # optional; N×N seconds, indexed by node_id
}

updated_runs, unserved = solver.solve_pdptw_lns(payload)
# updated_runs -> the fleet schedules with the new requests inserted
# unserved     -> booking_ids of new requests that could not be served
```

If your payload includes a `travel_time_matrix`, no routing server is needed.
Otherwise, point the solver at an [OSRM](https://project-osrm.org/) server and it
will build the matrix for you:

```python
solver = OnlineLNSSolver("http://127.0.0.1:50000/", K=3)
```

### Check which time slots are feasible

Offer a request several candidate pickup windows and get back the feasible ones,
each with a cost ratio (insertion cost ÷ direct travel time; `1.0` = no detour):

```python
feasible = solver.check_feasibility(payload)   # requests[0] carries "time_windows"
# -> [(time_window_dict, cost_ratio), ...]  (only the feasible windows)
```

### Score a schedule

```python
feasible, stats = solver.get_stats(payload["depot"], updated_runs, matrix=matrix)
# stats -> vmt, pmt, vmt/pmt, serviced, average_wait_time, average_detour, ...
```

## Payload format

**Request** (a new ride to insert):

```python
{
    "booking_id": int,
    "am": int, "wc": int,                 # ambulatory / wheelchair seats needed
    "pickup_pt":  {"lat": float, "lon": float, "node_id": int},
    "dropoff_pt": {"lat": float, "lon": float, "node_id": int},
    "pickup_time_window_start": int, "pickup_time_window_end": int,
    "dropoff_time_window_start": int, "dropoff_time_window_end": int,
}
```

**Driver run** (one vehicle's schedule): a `state` (capacities, start/end time,
current location, how many stops are already done) plus a `manifest` of stops.
All times are **seconds from midnight**. The full field-by-field spec, along with
every method's signature and return shape, is in **[API.md](API.md)**.

## Common options

```python
OnlineLNSSolver(
    server_url=None,          # OSRM URL; optional if payloads carry travel_time_matrix
    K=3,                      # search neighborhood size (requests reconsidered per move)
    RV_K=5,                   # candidate vehicles considered per request
    enable_tail_swap=True,    # allow moving trip tails between vehicles
    ilp_time_limit=30.0,      # assignment time limit (seconds)
)
```

Any of these can also be overridden per call, e.g. `solver.solve_pdptw_lns(payload, K=5)`.

## What you can do with it

| Method | Purpose |
|---|---|
| `solve_pdptw_lns` | Insert new requests into the fleet (main entry point) |
| `resolve_pdptw_lns` | Same, but all-or-nothing (keep changes only if all served) |
| `solve_pdptw_heuristic` | Fast greedy insertion (no full optimization) |
| `serve_asap` | Insert each request at its earliest feasible pickup time |
| `check_feasibility` | Which candidate pickup windows are feasible, and at what cost |
| `simulate_manifest` | Advance vehicle positions/state to a given clock time |
| `get_stats` | Validate a schedule and compute VMT/PMT, wait, detour |

See **[API.md](API.md)** for full signatures, arguments, and return shapes.

## License

MIT.
