Skip to content

Commit 451fd86

Browse files
committed
minor bug fixes
1 parent 5e42fbc commit 451fd86

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

mapel-elections/src/mapel/elections/cultures/group_separable.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
def gs_mask(num_voters=None,
55
num_candidates=None,
66
tree_sampler=None,
7+
seed=None,
78
**kwargs):
89

910
if type(tree_sampler) is str:
@@ -18,4 +19,4 @@ def gs_mask(num_voters=None,
1819
return pref_ordinal.group_separable(num_voters=num_voters,
1920
num_candidates=num_candidates,
2021
tree_sampler=tree_sampler,
21-
**kwargs)
22+
seed=seed)

mapel-elections/src/mapel/elections/cultures/guardians_plus.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# Auxiliary functions
99

10+
1011
def distribute_in_matrix(n, m):
1112
if m == 0:
1213
return []
@@ -75,13 +76,13 @@ def generate_un_from_list(num_voters=None, num_candidates=None):
7576

7677

7778
def generate_un_from_matrix_votes(num_voters=None, num_candidates=None):
78-
""" Generate real elections that have UN positionwise matrix """
79+
""" Generate real election that have UN positionwise matrix """
7980
matrix = distribute_in_matrix(num_voters, num_candidates)
8081
return draw_election(matrix)
8182

8283

8384
def generate_idan_part_votes(num_voters=None, num_candidates=None, part_share=None, **kwargs):
84-
""" Generate real elections between (ID) and (AN) """
85+
""" Generate real election between (ID) and (AN) """
8586
if part_share is None:
8687
print("IDAN_part generation : params None : random param generated")
8788
part_size = np.random.choice(range(num_voters))
@@ -253,34 +254,33 @@ def generate_unst_mallows_votes(num_voters=None, num_candidates=None, params=Non
253254
return votes
254255

255256

256-
def generate_unst_topsize_votes(num_voters=None, num_candidates=None, params=None):
257+
def generate_unst_topsize_votes(num_voters=None, num_candidates=None, top_share=None, **kwargs):
257258
""" Generate kind of real elections between (UN) and (ST) """
258-
if params is None or not ('top_share' in params):
259+
if top_share is None:
259260
print("UNST_topsize generation : params None : random param generated")
260261
top_share = np.random.random()
261262
else:
262-
top_share = params['top_share']
263+
top_share = top_share
263264
top_size = int(round(top_share * num_candidates))
264265
better = top_size
265266
worse = num_candidates - top_size
266267
matrix = distribute_in_block_matrix(num_voters, [better, worse])
267268
return draw_election(matrix)
268269

269270

270-
def generate_idst_blocks_votes(num_voters=None, num_candidates=None, params=None):
271+
def generate_idst_blocks_votes(num_voters=None, num_candidates=None, no_blocks=None, **kwargs):
271272
""" Generate kind of real elections between (ID) and (UN) """
272-
if params is None or not ('no_blocks' in params):
273+
if no_blocks is None:
273274
print("IDST_blocks generation : params None : random param generated")
274275
no_blocks = np.random.choice(range(num_candidates + 1))
275276
else:
276-
no_blocks = params['no_blocks']
277+
no_blocks = no_blocks
277278
no_blocks = int(round(no_blocks))
278279
k = num_candidates // no_blocks
279280
r = num_candidates - k * no_blocks
280281
blocks = [k for _ in range(no_blocks)]
281282
with_one_more = list(np.random.choice(range(no_blocks), r, replace=False))
282283
for i in with_one_more:
283284
blocks[i] = blocks[i] + 1
284-
print(blocks)
285285
matrix = distribute_in_block_matrix(num_voters, blocks)
286286
return draw_election(matrix)

mapel-elections/src/mapel/elections/distances/main_ordinal_distances.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def compute_spearman_distance(election_1: OrdinalElection,
121121

122122

123123
def compute_spearman_distance_fastmap(
124-
election_1: OrdinalElection, election_2: OrdinalElection, method: str = "bf"
124+
election_1: OrdinalElection, election_2: OrdinalElection, method: str = "aa"
125125
) -> tuple[int, Union[list, None]]:
126126
"""Computes Isomorphic Spearman distance between elections using `fastmap` library.
127127
@@ -169,7 +169,7 @@ def compute_spearman_distance_fastmap(
169169
except ImportError as e:
170170
raise ImportError("`fastmap` library module not found") from e
171171

172-
U, V = election_1.votes, election_2.votes
172+
U, V = np.array(election_1.votes), np.array(election_2.votes)
173173
d = fastmap.spearman(U=U, V=V, method=method)
174174

175175
return d, None

mapel-elections/src/mapel/elections/distances_.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
'swap': mod.compute_swap_distance,
3636
'spearman': mod.compute_spearman_distance,
37+
'spearman_aa': mod.compute_spearman_distance_fastmap,
3738

3839
'blank': mod.compute_blank_distance,
3940

mapel-elections/src/mapel/elections/features/scores.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ def highest_borda_score(election) -> dict:
4141
"""
4242
if election.culture_id in LIST_OF_FAKE_MODELS:
4343
return {'value': None}
44-
c = election.num_candidates
44+
n = election.num_voters
45+
m = election.num_candidates
4546
vectors = election.get_vectors()
46-
borda = [sum([vectors[i][pos] * (c - pos - 1) for pos in range(c)])
47-
for i in range(c)]
48-
return {'value': max(borda) * election.num_voters}
47+
scores = [0 for _ in range(m)]
48+
49+
for i in range(n):
50+
for j in range(m):
51+
scores[vectors[i][j]] += m - j - 1
52+
53+
return {'value': max(scores) * election.num_voters}
4954

5055

5156
def highest_plurality_score(election) -> dict:

0 commit comments

Comments
 (0)