Skip to content

Make spatial matching compatible with PuLP 4 - #963

Open
MahnoorAhmed999 wants to merge 2 commits into
pysal:mainfrom
MahnoorAhmed999:pulp-v4-compat
Open

MahnoorAhmed999 wants to merge 2 commits into
pysal:mainfrom
MahnoorAhmed999:pulp-v4-compat

Conversation

@MahnoorAhmed999

Copy link
Copy Markdown

Closes #944

The spatial matching in Graph.build_spatial_matches / _spatial_matching did not work with PuLP 4. This makes it work with PuLP 4 while staying backwards compatible with PuLP 3.

Changes in libpysal/graph/_matching.py

  • Variables are created with LpProblem.add_variable_dicts when available (PuLP 4 removed LpVariable.dicts), falling back to LpVariable.dicts on PuLP 3.
  • Constraints are written as >= / == / <= comparisons instead of LpConstraint(..., sense=..., rhs=...), which works on both versions.
  • solve() returns a solve report in PuLP 4 instead of an integer, so the status is read from either form, and the warning uses status_str or LpStatus.
  • PuLP 4 no longer bundles CBC and can have no default solver. When solver=None and PuLP has no default, the first available solver is used, with a clear ImportError if none is installed.
  • Solvers such as HiGHS return no solution for infeasible problems, which crashed on unpacking an empty edge list. It now returns empty arrays after the existing "Problem is Infeasible" warning.

Other changes

  • pulp<4 pin removed from the plus extra, with highspy added so PuLP 4 has a solver. The latest and dev CI environments use PuLP 4 with highspy, while 312-oldest keeps pulp<4 so PuLP 3 stays tested.
  • Docstrings for solver updated for PuLP 4.
  • test_returns_mip no longer checks LpProblem.sol_status, which PuLP 4 removed; the infeasible case is still checked through the warning.
  • New tests: test_default_solver (no solver passed) and test_infeasible_without_solution.

Testing

  • libpysal/graph/tests/test_matching.py passes with PuLP 4.3.1 (HiGHS) and PuLP 3.3.2 (CBC). test_stores was not run locally because it needs to download the GeoDa liquor stores dataset.
  • The matched edges and weights are identical to the current code on PuLP 3 for within, partial and between matching, and identical between PuLP 3 and PuLP 4.
  • The rest of libpysal/graph/tests shows no new failures.

@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.0%. Comparing base (d947be0) to head (33db5a6).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
libpysal/graph/_matching.py 95.2% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main    #963   +/-   ##
=====================================
  Coverage   86.0%   86.0%           
=====================================
  Files        151     151           
  Lines      16645   16677   +32     
=====================================
+ Hits       14318   14349   +31     
- Misses      2327    2328    +1     
Files with missing lines Coverage Δ
libpysal/graph/base.py 96.0% <ø> (ø)
libpysal/graph/tests/test_matching.py 98.7% <100.0%> (+0.4%) ⬆️
libpysal/graph/_matching.py 95.5% <95.2%> (-0.5%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pulp v4 not compatible

1 participant