perf: use numpy array lookup for solution unpacking#619
Open
MaykThewessen wants to merge 2 commits intoPyPSA:masterfrom
Open
perf: use numpy array lookup for solution unpacking#619MaykThewessen wants to merge 2 commits intoPyPSA:masterfrom
MaykThewessen wants to merge 2 commits intoPyPSA:masterfrom
Conversation
Convert the primal/dual pandas Series to a dense numpy lookup array before the per-variable/per-constraint unpacking loop. This replaces pandas indexing (sol[idx].values) with direct numpy array indexing (sol_arr[idx]), avoiding pandas overhead per variable type. The loop over variable/constraint types still exists (needed to set each variable's .solution xr.DataArray), but the inner indexing operation is now pure numpy instead of pandas Series.__getitem__. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
for more information, see https://pre-commit.ci
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.
Summary
Replace pandas Series indexing with numpy array lookup in the solution unpacking loop (
Model.solve(), lines 1570-1594).Before:
After:
Same pattern applied to the dual values unpacking loop.
Motivation
After HiGHS solves, the solution is a pandas Series with integer labels. The unpacking loop accesses this Series once per variable type (~20 types in a typical PyPSA model). Each
sol[idx].valuescall involves pandas'__getitem__with index alignment overhead. Converting to a numpy array first and using direct array indexing eliminates this overhead.Context
See #198 (comment) — item 5 in the priority list.
Test plan
test_optimization.pyhighs-direct — 24/25 pass (one pre-existing failure intest_modified_model)test_default_setting_sol_and_dual_accessortest_default_setting_expression_sol_accessor🤖 Generated with Claude Code