Skip to content

highspy: expose basis status as numpy arrays - #3794

Closed
galabovaa wants to merge 2 commits into
latestfrom
issue-3373
Closed

galabovaa wants to merge 2 commits into
latestfrom
issue-3373

Conversation

@galabovaa

@galabovaa galabovaa commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds HighsBasis.col_status_array and row_status_array, numpy uint8
views of the basis status that are fast at scale and assignable from
numpy. Reading returns a copy, so a kept array stays valid and cannot
write invalid statuses into the basis. Assignment checks every value is
a valid HighsBasisStatus, as the C API's Highs_setBasis does.

The existing col_status / row_status lists are unchanged, since
code compares their entries to HighsBasisStatus and passes them to
basisStatusToString.

Checklist

  • I have read the contributing guidelines
  • This PR targets the latest branch
  • Tests are passing
  • Documentation was updated where relevant
  • This PR is not primarily AI-generated (per the AI contributions policy in CONTRIBUTING.md)

Related issue

Closes #3373

@galabovaa galabovaa self-assigned this Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.06%. Comparing base (29ebfb4) to head (86c7680).
⚠️ Report is 107 commits behind head on latest.

Additional details and impacted files
@@            Coverage Diff             @@
##           latest    #3794      +/-   ##
==========================================
+ Coverage   76.03%   76.06%   +0.03%     
==========================================
  Files         467      468       +1     
  Lines      116750   116985     +235     
  Branches    18176    18195      +19     
==========================================
+ Hits        88768    88990     +222     
- Misses      27742    27755      +13     
  Partials      240      240              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mathgeekcoder mathgeekcoder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to #3109, and it is on my short TODO list to replace all arrays with direct numpy accessors. That said, I have not investigated this since we have considered moving to nanobind.

In the meantime, it's perfectly reasonable to make this change for these basis arrays and change the others later.

Regardless, the current make_readonly_ptr and make_setter_ptr will also work with enums (I've previously tested this with HighsVarType). BTW: I also want to rename make_readonly_ptr to make_getter_ptr soon.

So, the make_basis_status_[setter/getter] methods are not necessary, with one minor caveat. Consider the following code:

b1 = h.getBasis()
b1.col_status = [1,6]
b1.col_status[0] = 6

The code in PR prevents the first set, whereas make_setter_ptr allows it. However, I believe both approaches allow the 2nd set, which directly modifies the underlying data.

Note: the 2nd set is fast and shouldn't cause issues, since getBasis() returns a copy due to pybind default for const. So we're still returning a copy of the basis arrays, but only once instead of every access.

We might want to check invalid values setBasis() or highs_setBasis() instead.

@galabovaa

Copy link
Copy Markdown
Contributor Author

Is this sort of what you mean? Looking simpler and more like the methods that are already there.

I see that the value checking is missing, I thought I would try a more "Python" approach initially, but I guess it is better to make this change simple for the patch, and do the checks separately. Thank you!

Let me know if this look OK

@mathgeekcoder mathgeekcoder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking much better! I'd not seen that pattern using a for loop on member pointers in highs_setBasis before, quite concise.

Sorry I didn't mention earlier, one minor suggestion is to simply use col_status instead of col_status_array (similarly with row_status).

It shouldn't cause any issues to replace the old members, and anyone using them will automatically get a performance boost. See HighsLp.col_cost_ for an example, it's simply been replaced by the numpy properties.

@mathgeekcoder

Copy link
Copy Markdown
Collaborator

I see that the value checking is missing, I thought I would try a more "Python" approach initially, but I guess it is better to make this change simple for the patch, and do the checks separately. Thank you!

I think the value checks you've already done in highs_setBasis is great. It's much more efficient by only checking when it really matters, and it covers both direct edits and assignment.

@galabovaa

Copy link
Copy Markdown
Contributor Author

Looking much better! I'd not seen that pattern using a for loop on member pointers in highs_setBasis before, quite concise.

Sorry I didn't mention earlier, one minor suggestion is to simply use col_status instead of col_status_array (similarly with row_status).

It shouldn't cause any issues to replace the old members, and anyone using them will automatically get a performance boost. See HighsLp.col_cost_ for an example, it's simply been replaced by the numpy properties.

Thanks! I tried replacing col_status / row_status with the numpy properties, and it would fix the #3109 problem for the basis (basis.col_status[i] = ... currently does nothing). But it causes a different failure that also gives no error:

  • An entry becomes numpy.uint8, so basis.col_status[i] == HighsBasisStatus.kBasic returns False. It still returns False when I add py::arithmetic() to the enum.

  • h.basisStatusToString(basis.col_status[i]) raises TypeError.
    Three of the Python examples and test_read_basis use both patterns. lp.integrality_ (HighsVarType) will hit the same thing when it's converted for Member function in highspy has no effect #3109.

Do you want me to do the rename here and update the examples to wrap entries in HighsBasisStatus(...)? Or should we keep both names until there's a plan for enum arrays in general?

@mathgeekcoder

Copy link
Copy Markdown
Collaborator
  • An entry becomes numpy.uint8, so basis.col_status[i] == HighsBasisStatus.kBasic returns False. It still returns False when I add py::arithmetic() to the enum.
  • h.basisStatusToString(basis.col_status[i]) raises TypeError.
    Three of the Python examples and test_read_basis use both patterns. lp.integrality_ (HighsVarType) will hit the same thing when it's converted for Member function in highspy has no effect #3109.

Ah! Good point. I believe these are issues that will go away by using py::native_enum instead of py::enum_, which has been deprecated due to PEP 435 incompatibility.

That said, py::native_enum requires pybind11 v3 and it might become a larger change than a small patch fix. At this point we could also consider the nanobind upgrade.

Do you want me to do the rename here and update the examples to wrap entries in HighsBasisStatus(...)? Or should we keep both names until there's a plan for enum arrays in general?

With this in mind, we have an elegant plan for enum arrays in general. I'm hesitant to make the larger change if we're wanting to push a release this week, but I'm hesitant to add extra [col/row]_status_array members that will be redundant in the next release.

Is it reasonable to hold off on this change until we can make the larger one?

@galabovaa

Copy link
Copy Markdown
Contributor Author

Sure, OK. Closing this for now and I will add a note on the issue.

@galabovaa galabovaa closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants