Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Various Functions and Refactor #4

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

Quantalabs
Copy link

  1. Fix linting errors and format with black.
  2. Uses a bidirectional BFS instead of a standard BFS, meaning the algorithm visits less nodes and completes faster.
  3. Optimize margins matrix calculation in core by using vectorized operations as opposed to the previous nested for loop.
  4. Refactor the impartial culture and Euclidean model, with slight efficiency updates (less nested for loops and more vectorized operations).

I'll try and get some benchmarks to see how much of a performance improvement there is.

@Quantalabs
Copy link
Author

Quantalabs commented Jul 23, 2024

I added in the tests with 986a806, and the following is the output (running pytest --benchmark-min-rounds=100). I have:

  • pytest
  • pytest-benchmarks
  • Python 3.12.4

BFS and DFS now have similar performance. I also reverted by core module to use the previous single-direction BFS implementation, which is the second image. The old BFS implementation performs worse on all measures.
New:
Benchmarks for DFS vs. Bi-directional BFS
Old:
Benchmarks for DFS vs. Old BFS

@Quantalabs
Copy link
Author

Comparing the previous margins_from_ballots function to the new one, there is a significant increase in performance.
Old Margins vs New Margins

@Quantalabs
Copy link
Author

I made a couple fixes but they don't seem to have affected the speed.

Old margins v New Margins

@Quantalabs
Copy link
Author

Quantalabs commented Jul 24, 2024

It seems as though on large amounts of ballots, as in ba56f14, the difference between DFS and BFS is much clearer, and the bi-directional BFS is outperforming DFS.
BFS vs. DFS on 1,000,000 ballots

@Quantalabs
Copy link
Author

Finally, the tests for the improved models in b25eceb. All 3 perform better than their old counterparts.
Old IC and Euclidean vs New IC and Euclidean

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.

1 participant