Vehicle routing¶
Derived from the Timefold Quickstarts vehicle-routing example (Apache-2.0), translated
and modified; see the repository NOTICE.
Domain model, constraints and API details: python.md and optimaru/vehicle_routing/cpp/.
Usage¶
OPTIMARU_USE_CASES=vehicle_routing uv sync # builds only this use case's C++ extension
uv run pytest -q tests/vehicle_routing
from optimaru import vehicle_routing as vrp
locations = [vrp.Location(37.5665, 126.9780), # index 0: the depot
vrp.Location(37.5700, 126.9820)] # index 1: a customer
vehicles = [vrp.Vehicle("V1", capacity=10, start_location_index=0, departure_time=7 * 3600)]
visits = [vrp.Visit("C1", "Customer 1", location_index=1, demand=3,
min_start_time=8 * 3600, max_end_time=18 * 3600, service_duration=600)]
plan = vrp.VehicleRoutePlan("demo", locations, vehicles, visits)
solved = vrp.solve(plan, time_limit_seconds=5.0, random_seed=42) # solves in place and returns the plan
print(solved.score, solved.routes()) # 0hard/0medium/-38soft {'V1': ['C1']}
solve takes time_limit_seconds, random_seed, parallel_solver_count, stop_at_first_feasible, full_assert, on_new_best, a full config (vrp.SolverConfig, whose fields include spent_limit_seconds, random_seed, parallel_solver_count and full_assert; the keywords override a copy of it) and constraints (see below). Solver(config).solve(plan) and Solver(config).solve_with_constraints(plan, picks) are the same solves one level down, on the engine objects; for JSON in/out use optimaru.vehicle_routing.models.solve_request(...), which also takes constraints=. The C++ side builds and tests with:
cmake -B build -DOPTIMARU_USE_CASES=vehicle_routing && cmake --build build -j
ctest --test-dir build # vrp_test, vrp_plugin_test (assembly == monolith,
# raw costs by hand, catalog, medium level),
# vrp_group_moves_test (visit groups)
Constraints¶
| Constraint (Python name) | C++ component | Default level | Default weight | Raw cost (0 = no violation) |
|---|---|---|---|---|
vehicle_capacity_cost |
VehicleCapacityCost |
hard | 1 | per vehicle: summed visit demand above the capacity |
max_end_time_cost |
MaxEndTimeCost |
hard | 1 | per vehicle: time-window lateness (arrival + service past the visit's max_end_time), in whole minutes rounded up |
travel_time_cost |
TravelTimeCost |
soft | 1 | per vehicle: driving seconds — start depot → visits, plus the leg to the end depot when the vehicle has one (end_depot) |
travel_distance_cost |
TravelDistanceCost |
soft | 0 (opt-in) | per vehicle: driving meters over the same legs, from the distances of the vehicle's travel profile |
max_travel_time_cost |
MaxTravelTimeCost |
hard | 1 | per vehicle: driving seconds above max_travel_time (0 = no limit) |
max_route_duration_cost |
MaxRouteDurationCost |
hard | 1 | per vehicle: route duration seconds above max_route_duration (0 = no limit) |
vehicle_max_end_time_cost |
VehicleMaxEndTimeCost |
hard | 1 | per vehicle: seconds the route ends after the vehicle's max_end_time (0 = no limit) |
max_stops_cost |
MaxStopsCost |
hard | 1 | per vehicle: visits above max_stops (0 = no cap) |
max_route_radius_cost |
MaxRouteRadiusCost |
hard | 1 | per vehicle: meters the farthest visit lies beyond max_route_radius_meters from the start depot (0 = no limit) |
enclosing_circle_radius_cost |
EnclosingCircleRadiusCost |
soft | 0 (opt-in) | per vehicle: radius in meters of the smallest circle around its visits (0 with fewer than two) |
min_capacity_utilization_cost |
MinCapacityUtilizationCost |
soft | 1 | per used vehicle: demand short of capacity * min_load_ratio (0 = no target) |
route_duration_cost |
RouteDurationCost |
soft | 0 (opt-in) | per used vehicle: seconds from departure to the route end |
latency_cost |
LatencyCost |
soft | 0 (opt-in) | per vehicle: seconds from departure to each visit's arrival, summed |
vehicle_fixed_cost |
VehicleFixedCost |
soft | 1 | per used vehicle: its fixed_cost |
visit_order_cost |
VisitOrderCost |
hard | 1000 | per vehicle: pairs of its visits served the wrong way round, a visit before one its after names (see Visit groups) |
profile_forbidden_visit_cost |
ProfileForbiddenVisitCost |
hard | 1000 | per vehicle: its visits that its travel profile lists in forbidden_visits; always scored (see Profile visits) |
profile_preferred_visit_cost |
ProfilePreferredVisitCost |
soft | 600 | per vehicle: its visits that some vehicle's profile lists in preferred_visits and its own does not |
visit_group_cost |
VisitGroupCost |
hard | 1000 | grouped visits that are not where most of their group is: on another vehicle, or unassigned while the others ride |
unassigned_visits_cost |
UnassignedVisitsCost |
medium | 1 | penalty minutes of the visits left unrouted (unassigned_penalty_minutes, by default the service time in whole minutes) |
That is the list_constraints() order: the seventeen per-vehicle unit constraints
(each vehicle; constraint_costs() lists the unassigned pool as one more cell, always
0, which no route constraint evaluates), then the two globals, visit_group_cost and
unassigned_visits_cost. optimaru.vehicle_routing.constraint_info() reports exactly this
table from the C++ components (kDefaultLevel / kDefaultWeight). Every
constraint is flippable onto any of the three levels (level="hard", "medium" or
"soft", or a Level), none is a reward, and every one at a weight above 0 is scored
by default (default_enabled); the four opt-in objectives (weight 0) are measured
but move no score until given a weight. The route-policy constraints cost 0 while their
vehicle field is 0. The four core constraints
(vehicle_capacity_cost, max_end_time_cost, unassigned_visits_cost,
travel_time_cost) are also the monolithic ScoreCalculator and the incremental
calculator, which read the same constants.
The medium level. unassigned_visits_cost is the domain's medium level. The
domain declares its fold once (kMediumFold = intoSoft(10^9) in domain.hpp):
the engine compares Score, so medium lands in soft as
medium * 10^9 + soft, which keeps the 3-level Score lexicographic. Its weight
is in medium units (default 1). level="hard" charges one hard unit per penalty
minute; level="soft" one soft unit per penalty minute; an explicit weight is in units
of the level in effect. Any other constraint may move onto medium the same way
(TravelTimeCost(level="medium") charges one medium unit per driving second), and a
repeated pick carries its own level. constraint_costs() carries
"medium_fold": {"kind": "into_soft", "unit": 1e9} on every entry, so
optimaru.vehicle_routing.constraints.score() folds exactly like the engine. A plugin-path
solve (constraints=) reports plan.score and every on_new_best score from
the calculator's three levels (levelScores()), not by decoding the folded soft,
so a soft total of 10^9 or more stays on soft in both.
Exponent. It shapes each unit's raw cost before the weight: per vehicle for
the unit constraints, on the single total for unassigned_visits_cost.
Offset, clip and repeats. Every constraint takes the knobs weight, level,
exponent, offset and clip (offset and clip keyword-only), and may be picked
several times. A raw cost x (per vehicle, or the unassigned total) is
shaped as sign(x) * max(0, |x| - offset) ** exponent (clip=True, the default) or
(x - offset) ** exponent, sign-preserving (clip=False, which lowers the penalty of
every vehicle below the offset, an idle one included; the unassigned pool is no vehicle
and is never charged); offset=0 is the plain power. Both apply per vehicle, not to the
total. clip=False with an offset is refused on hard (by default, every constraint the
table lists as hard), because hard 0 must mean no violation; medium folds into soft here,
so it takes the literal formula. A negative offset with clip=True, such as
TravelTimeCost(offset=-60), is a fixed charge of 60 per vehicle that drives; the engine
recomputes it for every route a move changes, so the solve takes about half as many steps
per second. Each pick is its own instance with its own knobs, and the instances add up:
from optimaru.vehicle_routing.constraints import TravelTimeCost, UnassignedVisitsCost, VehicleCapacityCost
vrp.solve(plan, time_limit_seconds=5.0, random_seed=42, constraints=[
VehicleCapacityCost(),
UnassignedVisitsCost(),
TravelTimeCost(), # driving seconds
TravelTimeCost(weight=1e-9, exponent=3, offset=7200), # + 1e-9 x (seconds beyond 2 h)^3
])
vrp.constraint_costs(plan, picks) keys the second pick travel_time_cost#2; its
per-vehicle entries list the vehicles they evaluate as "units".
Names. Every constraint name ends in _cost (C++ struct, header file, registry
name, Python spec class, module-level default instance and catalog name); in
vehicle_fixed_cost that ending is the rule's own subject (the vehicle's fixed cost),
not an added suffix. max_end_time_cost is the visit deadline and
vehicle_max_end_time_cost the vehicle's own end bound. The model FIELDS keep their plain
names (max_travel_time, max_route_duration, max_end_time, max_stops,
max_route_radius_meters, min_load_ratio, fixed_cost, unassigned_penalty_minutes).
unassigned_visits_cost defaults to weight 1 in medium units: level="soft" demotes it to
soft and level="hard" charges 1 hard unit per penalty minute. enclosing_circle_radius_cost
defaults to weight 0 (opt-in).
Visit groups¶
Orders the input has already merged into one job get the same group: a name on
VisitModel (None or an empty name is no group), Visit.group_index on a plan
(-1 for none). The visits of a group ride one vehicle together, or all stay
unassigned. A group holds any number of visits and a visit belongs to at most
one. Other visits may be served between them.
after puts a partial order on a group: it lists the visits of the same
group that are served before this one on the route (Visit.after_visit_indices
on a plan). Visits the order does not relate may be served either way round.
VisitModel(id="load", group="job-4", location=...)
VisitModel(id="unload-a", group="job-4", after=["load"], location=...)
VisitModel(id="unload-b", group="job-4", after=["load"], location=...) # a and b in either order
VisitModel(id="sign-off", group="job-4", after=["unload-a", "unload-b"], location=...)
A visit may only come after visits of its own group, the order must have no
cycle, and a visit is named once; anything else is refused (ValueError).
Two hard constraints score it, both at weight 1000, well above a typical lateness or overload, so a broken group is mended before windows are polished:
visit_group_costcounts the grouped visits that are not where most of their group is, on a vehicle or on none. A group of three with one visit on another vehicle costs 1, and so does one with a visit left unassigned while two ride; spread over three vehicles it costs 2. Every visit that joins the others takes 1 off, which is the slope the engine's one-visit moves follow back to a whole group.visit_order_costcounts, per vehicle, the pairs of its visits that are served the wrong way round. A pair whose visits ride different vehicles is left tovisit_group_cost.
The search moves a group as one unit. Before the engine's construction runs,
each group is placed whole on the vehicle that takes all of its visits for the
least added driving, every visit at its cheapest slot behind the ones it comes
after (constructGroups); a group no vehicle takes is parked unassigned, whole.
A group partly on a caller's initial_route is completed on that vehicle; when
the rest does not fit there, it waits and the group starts split, for the search
to mend.
A domain move operator, groupRuinRecreate (vrp/search/moves/group_moves.hpp), then takes a
group out of its route, alone or with up to group_ruin_max_visits more visits
(the groups and single visits of nearby stops, or other groups), and puts each
unit back on one vehicle, the larger units first. That is how a group changes
vehicle, how groups trade places, and how a waiting group gets routed; the
engine's own moves reorder a group inside its route and are priced by the two
constraints when they would split one. The operator is registered only for a
plan with groups, so a plan without them is solved exactly as before.
The slots it offers are the ones the plan allows: the vehicle's capacity, every
visit's window, and the vehicle limits that are set (max_end_time,
max_route_duration, max_travel_time, max_stops), whatever level those
constraints are scored on. The visits of a group go onto a vehicle in an order
that respects after, each at its cheapest slot once the earlier ones are in;
one such offer costs a walk of the route per visit, so groups are meant to be a
handful of visits, not dozens.
What the operator is worth, on generated instances of 90 visits around one depot with morning or afternoon windows, visits in groups of three picked at random (4 s, 5 seeds, mean driving seconds; every group whole and in order in all 60 results):
| Instances | with groupRuinRecreate (default) |
the engine's moves alone | nothing grouped |
|---|---|---|---|
| half the visits grouped | 8,986 | 10,760 | 6,256 |
| half grouped, each group a chain | 9,769 | 11,182 | 6,256 |
| every visit grouped, each group a chain | 12,647 | 15,709 | 6,256 |
| half grouped in chains, a tighter fleet | 10,185 | 11,277 | 6,447 |
The operator drives 10% to 20% less and is better in 19 of the 20 pairs. The last
column is what such groups cost: three visits picked at random across town and
tied to one vehicle pull every route across town, so these plans drive 44% to
102% more than the same visits left free. Groups whose visits lie near each
other cost far less. A grouped plan also settles early: on two of these instances
a 60 s solve ends within 2% of a 4 s one, while nine seeds end 3% and 6% apart
(10 s), so another seed buys more than a longer run. parallel_solver_count runs such seeds side by side: with four starts the same
4 s end 0.8% to 2.4% shorter on the four classes above (5 seeds each: shorter in
17 of the 20 pairs and longer in none; under a step limit the first start is the
single-start solve, so more starts cannot do worse, while under a time limit the
starts share the machine's cores).
Every entry point scores a grouped plan with the default constraints, since the
four core constraints include neither of these two: solve_request, vrp.solve(plan),
Solver(config).solve(plan) and the C++ Solver::solve(plan), whenever some
group holds two visits or more (a visit alone in its group changes nothing). That is a solve on the
constraint assembly, as when a vehicle limit is set: the whole default set is
scored. parallel_solver_count runs that many starts there as
on the core constraints, each start on a set of its own.
calculate_score(plan) (C++ calculateScore) reads such a plan the same way,
so it returns the score the solve stored and prices a split or reversed group
on a plan built by hand. Both
constraints are engine-only: the CP-SAT reference has no plugin for them and
reads neither group nor after.
Profile visits¶
A travel profile also says which visits its vehicles take. forbidden_visits lists
the visits a vehicle on the profile may not serve (a job that needs a skill the
technician lacks, a street the truck does not fit); preferred_visits lists the
visits that should go to a vehicle on the profile (the customer's usual technician,
a job the profile does best). A profile may consist of these lists alone and drive
on the travel of another profile (same_travel_as), so one profile per skill set
shares one set of travel dicts:
from optimaru.vehicle_routing.models import TravelProfile
profiles = {
"road": TravelProfile(durations=seconds), # the shared travel
"basic": TravelProfile(same_travel_as="road", forbidden_visits=["boiler-3", "gas-7"]),
"gas": TravelProfile(same_travel_as="road", preferred_visits=["gas-7"]),
}
# VehicleModel(..., profile="basic"), VehicleModel(..., profile="gas")
profile_forbidden_visit_cost(hard, weight 1000) counts, per vehicle, the visits it serves that its profile forbids. It is always on: on a plan with forbidden visits, a constraint list without a hard instance of it at a weight above 0 gets one at its defaults, so leaving it out, or picking it soft or at weight 0 (which then scores beside it as picked), never lifts the rule;constraint_costs(plan, picks)reports that instance. On a plan without forbidden visits nothing is added. A visit that no vehicle may serve stays unassigned, since that costs only medium.profile_preferred_visit_cost(soft, weight 600) counts, per vehicle, the visits that some vehicle's profile prefers and its own does not. A preference of a profile no vehicle drives on counts nowhere, and an unassigned visit is left tounassigned_visits_cost. Weight 600 againsttravel_time_cost's 1 per driving second: a vehicle that does not prefer a visit takes it only when that saves more than ten minutes of driving. A request whose vehicles drive on a profile with preferred visits needs this constraint among itsconstraints(ValueErrorotherwise: the lists would be ignored); the default set has it. A profile no vehicle drives on prefers nothing, so its list asks for nothing either. A forbidden visit in a vehicle'sinitial_routeis accepted: the route is only a start, and the search moves the visit off.- A visit id must be one of the visits, at most once per list, and not in both lists
of one profile (
ValueError).same_travel_asnames another profile that gives its own travel; the profile that names it gives nodurations,distancesormissing_value. Profiles that drive alike share one matrix in the engine, and so do all profiles that give no travel at all (straight lines). - Every entry point scores a plan with such lists on the default constraints, as it does a plan with visit groups (the four core constraints read neither). That is the constraint assembly, which takes fewer steps per second than the core path: on a 120-visit plan, about a third as many. The group operators never place a visit on a vehicle whose profile forbids it; the engine's own moves are judged by the hard cost. On a plan without lists both constraints cost 0 and the search is the one it was before.
- On a compiled plan the lists are
vrp.ProfileVisits(forbidden_visit_indices, preferred_visit_indices)entries (visit indices, strictly increasing) inVehicleRoutePlan(..., profile_visits=[...]), which a vehicle names byprofile_visits_index(-1 for none). Both constraints are engine-only: the CP-SAT reference has no plugin for them.
Starting from an existing solution¶
from optimaru.vehicle_routing.models import solve_request, with_initial_solution
solution = solve_request(request) # request: a RoutePlanRequest
next_request = with_initial_solution(request, solution) # or solution.model_dump(mode="json")
better = solve_request(next_request)
with_initial_solution returns a new request whose vehicles carry initial_route, a list of visit ids. The solve keeps those routes as its starting point and construction places only the visits in no route, so a partial solution works and the unassigned visits get another try. With time_limit_seconds=0 a complete solution comes back as it went in. An unknown id or a visit routed twice raises ValueError.
Extending¶
Engine contracts and code skeletons: docs/extending.md.
The solution has the sequence shape; the domain solver that configures the
engine is optimaru/vehicle_routing/cpp/vrp/solver.hpp + optimaru/vehicle_routing/cpp/vrp/solver.cpp.
Adding a move. The engine already mixes relocate / swap / 2-opt / Or-opt / 2-opt* tail swap moves
(weights in Solver::Config). The one domain move is groupRuinRecreate in optimaru/vehicle_routing/cpp/vrp/search/moves/group_moves.cpp (on the insertion scan of visit_insertion.cpp), registered for plans with visit groups — use it as a template. A new move is a
optimaru::MoveOperator<…> registered with engine.add_move_operator(...) next
to the engine construction in optimaru/vehicle_routing/cpp/vrp/solver.cpp; a ruin for LNS is a
RuinOperator registered with engine.add_ruin_operator(...).
Changing the construction heuristic. difficulty is already set in optimaru/vehicle_routing/cpp/vrp/solver.cpp (tightest time window and largest demand first). Set
engineConfig.construction.difficulty for First Fit
Decreasing ordering, or pre-assign part of the solution in the domain
solver's solve before the engine runs — the engine only fills what is still unassigned.
Adding a hard or soft cost. Adding a constraint, step by step takes one new constraint through this use case's files in six steps: changed from Python, written as a component, tested, made cheaper per move, moved to the default fast path and measured.
Benchmark¶
benchmarks/vehicle-routing compares this use case with
the OR-Tools Routing solver on the same instances, rescored by the same evaluator.