perf(vrp): vectorize hot paths in _utils, milp, and data generation - #6
Open
nathan-ferchtandiker wants to merge 4 commits into
Open
perf(vrp): vectorize hot paths in _utils, milp, and data generation#6nathan-ferchtandiker wants to merge 4 commits into
nathan-ferchtandiker wants to merge 4 commits into
Conversation
_utils.py:
- _dist_matrix: replace per-pair geopy.geodesic() loop with a single
pyproj.Geod.inv() call over all upper-triangle pairs at once (C-level
PROJ). Results agree with geopy to <2e-12 miles (~3 nm). ~130-255x faster.
- extract_routes: precompute successor array with argmax(axis=1) in one
vectorized pass instead of calling np.where per route step. ~13x faster.
milp.py:
- __init__: enable Gurobi Presolve=2 and Cuts=2 for stronger LP relaxations.
- _add_vars: replace O(n) per-element self-loop ub=0 assignments with a
single setAttr("UB", ...) batch call, consistent with _preprocess_arcs.
- _preprocess_arcs: batch setAttr for infeasible arc elimination.
- _add_constraints / _add_objective: cache self._var['x'], travel_time_matrix,
delta etc. as locals before O(n^2) generator loops to eliminate repeated
dict/attribute lookups; rewrite big-M expressions from 4 terms to 3.
- _gel_sol: replace n^2 per-element round() loop with np.rint() over a
contiguous list, then reshape — eliminates n^2 Python function calls.
_data_generation.py:
- Replace np.random.choice(range(...)) with np.random.randint (avoids
constructing a Python range object before sampling).
- Replace list-comprehension l_t with np.minimum(e_t_arr + duration,
day_end).tolist() (vectorized).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
_utils._dist_matrix: replace per-pairgeopy.geodesic()loop with a singlepyproj.Geod.inv()call over all upper-triangle pairs (C-level PROJ library). Results agree with geopy to <2e-12 miles. ~130-255x faster depending on n._utils.extract_routes: precompute successor array withargmax(axis=1)in one vectorized pass instead of onenp.wherescan per route step. ~13x faster.milp.__init__: enablePresolve=2andCuts=2for stronger LP relaxations at the Gurobi level.milp._add_vars: replace O(n) per-element self-loopub=0assignments with a singlesetAttr("UB", ...)batch call.milp._preprocess_arcs: batchsetAttrfor infeasible arc elimination (depot and customer-to-customer).milp._add_constraints/_add_objective: cacheself._var['x'],travel_time_matrix,deltaas locals before O(n²) generator loops; rewrite big-M expressions from 4 terms to 3.milp._gel_sol: replace n² per-elementround()loop withnp.rint()over a contiguous list + reshape._data_generation:np.random.randintinstead ofnp.random.choice(range(...)), vectorizedl_twithnp.minimum.Test plan
_dist_matrixresults agree with the geopy baseline to <2e-12 miles (verified by benchmark assertions).extract_routesoutput matches per-stepnp.wherebaseline (verified by benchmark assertions).🤖 Generated with Claude Code