Skip to content

Commit

Permalink
update tests and criteria with refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdfish committed Feb 5, 2023
1 parent 1629a1e commit 7adc2f5
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,14 @@ function tied_ranks(a::Array{T,1}) where {T<:Real}
return r
end

function Base.show(io::IO, system::VotingSystem)
println("counts ranks")
max_spaces = 12
for (c,r) zip(system.counts,system.uranks)
n_spaces = max_spaces - length(digits(c))
println(c, " "^n_spaces, r)
end
end

get_counts(system) = system.counts
get_uranks(system) = system.uranks
3 changes: 2 additions & 1 deletion src/criteria/condorcet_loser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function satisfies(::Fails, system::VotingSystem{T,I}, criterion::CondorcetLoser
(;counts,uranks) = system
candidates = uranks[1]
winner = evaluate_winner(system)
length(winner) 1 ? (return true) : nothing
pairs = combinations(candidates, 2) |> collect
condorcet_losers = T[]
for c candidates
Expand All @@ -31,7 +32,7 @@ function satisfies(::Fails, system::VotingSystem{T,I}, criterion::CondorcetLoser
break
end
end
return (winner condorcet_losers) || isempty(condorcet_losers)
return (winner[1] condorcet_losers) || isempty(condorcet_losers)
end

"""
Expand Down
3 changes: 2 additions & 1 deletion src/criteria/condorcet_winner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function satisfies(::Fails, system::VotingSystem{T,I}, criterion::CondorcetWinne
(;counts,uranks) = system
candidates = uranks[1]
winner = evaluate_winner(system)
length(winner) 1 ? (return true) : nothing
pairs = combinations(candidates, 2) |> collect
condorcet_winners = T[]
for c candidates
Expand All @@ -31,7 +32,7 @@ function satisfies(::Fails, system::VotingSystem{T,I}, criterion::CondorcetWinne
break
end
end
return (winner condorcet_winners) || isempty(condorcet_winners)
return (winner[1] condorcet_winners) || isempty(condorcet_winners)
end

"""
Expand Down
4 changes: 2 additions & 2 deletions src/criteria/independence_irrelevant_alternatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rank order changes. The current method is less strict compared to alternative me
"""
function satisfies(::Fails, system::VotingSystem, criterion::Independence; _...)
winner = evaluate_winner(system)
losers = setdiff(system.uranks[1], [winner])
losers = setdiff(system.uranks[1], winner)
for i 1:(length(losers) - 1)
for comb combinations(losers, i)
_system = deepcopy(system)
Expand Down Expand Up @@ -58,7 +58,7 @@ rank order changes. The current method is less strict compared to alternative me
"""
function count_violations(::Fails, system::VotingSystem, criterion::Independence; _...)
winner = evaluate_winner(system)
losers = setdiff(system.uranks[1], [winner])
losers = setdiff(system.uranks[1], winner)
cnt = 0
for i 1:(length(losers) - 1)
for comb combinations(losers, i)
Expand Down
3 changes: 2 additions & 1 deletion src/criteria/majority.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ Tests whether a voting system satisfies the majority criterion.
function satisfies(::Fails, system::VotingSystem, criterion::Majority; _...)
winner_id = evaluate_winner(system)
majority_id = get_majority_id(system)
length(winner_id) 1 ? (return false) : nothing
isempty(majority_id) ? (return true) : nothing
return winner_id majority_id ? true : false
return winner_id[1] majority_id ? true : false
end

"""
Expand Down
6 changes: 4 additions & 2 deletions src/criteria/monotonicity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function satisfies(::Fails, system::VotingSystem, criteria::Monotonicity; max_re
system = deepcopy(system)
add_zero_counts!(system)
winner = evaluate_winner(system)
win_ind = map(x -> x[1] == winner, system.uranks)
length(winner) 1 ? (return true) : nothing
win_ind = map(x -> x[1] == winner[1], system.uranks)
for _ 1:max_reps
_system = deepcopy(system)
redistribute!(_system, win_ind)
Expand Down Expand Up @@ -57,7 +58,8 @@ function count_violations(::Fails, system::VotingSystem, criteria::Monotonicity;
system = deepcopy(system)
add_zero_counts!(system)
winner = evaluate_winner(system)
win_ind = map(x -> x[1] == winner, system.uranks)
length(winner) 1 ? (return true) : nothing
win_ind = map(x -> x[1] == winner[1], system.uranks)
cnt = 0
for _ 1:n_reps
_system = deepcopy(system)
Expand Down
3 changes: 2 additions & 1 deletion src/criteria/mutual_majority.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Tests whether a voting system satisfies the mutual majority criterion.
function satisfies(::Fails, system::VotingSystem, criterion::MutualMajority; _...)
winner_id = evaluate_winner(system)
majority_set = get_majority_set(system)
return winner_id majority_set
length(winner_id) 1 ? (return true) : nothing
return winner_id[1] majority_set
end

"""
Expand Down
10 changes: 0 additions & 10 deletions test/independence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@
using Test
using Random

[:a, :b, :c]
[:a, :c, :b]
[:b, :a, :c]
[:c, :a, :b]

rankings = Vector{Vector{Symbol}}()
push!(rankings, [[:a, :b, :c] for _ 1:5]...)
push!(rankings, [[:a, :c, :b] for _ 1:4]...)
Expand All @@ -94,11 +89,6 @@
using Test
using Random

[:a, :b, :c]
[:b, :c, :a]
[:b, :a, :c]
[:c, :a, :b]

rankings = Vector{Vector{Symbol}}()
push!(rankings, [[:a, :b, :c] for _ 1:3]...)
push!(rankings, [[:b, :c, :a] for _ 1:2]...)
Expand Down
2 changes: 1 addition & 1 deletion test/instant_runoff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
_,ranks = compute_ranks(system)
winner = evaluate_winner(system)

@test winner == []:d]
@test winner == [:d]
@test ranks == [:d,:b,:c,:e,:a]
end
end

0 comments on commit 7adc2f5

Please sign in to comment.