Skip to content

Commit e59103f

Browse files
committed
cache for elections
1 parent d4fefaf commit e59103f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/votekit/elections/election_types.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import itertools as it
33
import numpy as np
44
from typing import Callable, Optional
5+
from functools import lru_cache
56

67
from ..models import Election
78
from ..election_state import ElectionState
@@ -18,6 +19,9 @@
1819
first_place_votes,
1920
)
2021

22+
# add ballots attribute // remove preference profile so the original profile is
23+
# not modified in place everytime?
24+
2125

2226
class STV(Election):
2327
"""
@@ -164,6 +168,7 @@ def run_step(self) -> ElectionState:
164168
)
165169
return self.state
166170

171+
@lru_cache
167172
def run_election(self) -> ElectionState:
168173
"""
169174
Runs complete STV election
@@ -278,6 +283,7 @@ def run_step(self) -> ElectionState:
278283
self.state = new_state
279284
return self.state
280285

286+
@lru_cache
281287
def run_election(self) -> ElectionState:
282288
"""
283289
Simulates a complete Limited election
@@ -340,6 +346,7 @@ def run_step(self) -> ElectionState:
340346
self.state = outcome
341347
return outcome
342348

349+
@lru_cache
343350
def run_election(self) -> ElectionState:
344351
"""
345352
Runs complete Bloc election
@@ -398,6 +405,7 @@ def run_step(self) -> ElectionState:
398405
self.state = outcome
399406
return outcome
400407

408+
@lru_cache
401409
def run_election(self) -> ElectionState:
402410
"""
403411
Runs complete SNTV election
@@ -513,6 +521,7 @@ def run_step(self, stage: str) -> ElectionState:
513521
self.state = new_state # type: ignore
514522
return new_state # type: ignore
515523

524+
@lru_cache
516525
def run_election(self) -> ElectionState:
517526
"""
518527
Runs complete SNTV_STV election
@@ -574,6 +583,7 @@ def run_step(self) -> ElectionState:
574583
self.state = outcome
575584
return outcome
576585

586+
@lru_cache
577587
def run_election(self) -> ElectionState:
578588
"""
579589
Simulates a complete TopTwo election
@@ -637,6 +647,7 @@ def run_step(self) -> ElectionState:
637647
self.state = new_state
638648
return new_state
639649

650+
@lru_cache
640651
def run_election(self) -> ElectionState:
641652
"""
642653
Simulates a complete DominatingSets election
@@ -709,6 +720,7 @@ def run_step(self) -> ElectionState:
709720
self.state = new_state
710721
return new_state
711722

723+
@lru_cache
712724
def run_election(self) -> ElectionState:
713725
"""
714726
Simulates a complete Conda-Borda election
@@ -784,6 +796,7 @@ def run_step(self, old_profile: PreferenceProfile) -> ElectionState:
784796
)
785797
return self.state
786798

799+
@lru_cache
787800
def run_election(self) -> ElectionState:
788801
"""
789802
Simulates a complete sequential RCV contest.
@@ -873,6 +886,7 @@ def run_step(self) -> ElectionState:
873886
self.state = new_state
874887
return new_state
875888

889+
@lru_cache
876890
def run_election(self) -> ElectionState:
877891
"""
878892
Simulates a complete Borda contest

0 commit comments

Comments
 (0)