Spaces:
Sleeping
Sleeping
File size: 15,434 Bytes
14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 bc31a5a 14c0e52 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import math
from copy import deepcopy
from itertools import permutations, product
from collections import defaultdict
# Initialize session state
if "voters" not in st.session_state:
st.session_state.voters = []
if "scoring_history" not in st.session_state:
st.session_state.scoring_history = [] # List of tuples: (label, vector, manipulability %)
st.title("π― Strategy-Proof Ranked Voting System")
# 1. Alternatives and scoring vector
num_alts = st.number_input("Number of alternatives:", min_value=2, value=3)
alternatives = [f"M{i+1}" for i in range(num_alts)]
scoring_input = st.text_input("Enter scoring vector (comma-separated):", value="2,1,0")
try:
scoring_vector = list(map(int, scoring_input.strip().split(',')))
assert len(scoring_vector) == num_alts
except:
st.error("Scoring vector must contain exactly one value per alternative.")
st.stop()
# 2. Add voter
st.subheader("π³οΈ Add Voter")
with st.form("add_voter_form"):
voter_name = st.text_input("Voter Name:", f"Voter{len(st.session_state.voters)+1}")
ranking = st.multiselect("Rank alternatives (top to bottom):", alternatives, default=alternatives)
submit = st.form_submit_button("Add Voter")
if submit:
if len(ranking) != num_alts:
st.warning("Rank all alternatives uniquely.")
else:
preferences = {ranking[i]: scoring_vector[i] for i in range(num_alts)}
st.session_state.voters.append({
"name": voter_name,
"ranking": ranking,
"preferences": preferences
})
st.success(f"{voter_name} added.")
# 3. Show all voter preferences
st.subheader("π₯ Voter Preferences")
if st.session_state.voters:
df_votes = pd.DataFrame([{**{"Voter": v["name"]}, **v["preferences"]} for v in st.session_state.voters])
st.dataframe(df_votes)
# --- Function to detect manipulation ---
import math
def detect_manipulation(voters, alternatives, scoring_vector):
total_profiles = math.factorial(len(alternatives)) ** len(voters)
manipulable_cases = 0
manipulable_info = []
def get_rank(score_dict):
sorted_alts = sorted(score_dict.items(), key=lambda x: -x[1])
return {alt: rank + 1 for rank, (alt, _) in enumerate(sorted_alts)}
# Original overall ranking
original_scores = {alt: 0 for alt in alternatives}
for v in voters:
for i, alt in enumerate(v["ranking"]):
original_scores[alt] += scoring_vector[i]
original_ranks = get_rank(original_scores)
original_winner = max(original_scores, key=original_scores.get)
for voter in voters:
original_ranking = voter["ranking"]
# Skip if voter's top choice is already the winner
if original_ranking[0] == original_winner:
continue
for alt in alternatives:
if alt == original_ranking[0]:
continue # skip if same as current top
new_ranking = original_ranking.copy()
if alt in new_ranking:
new_ranking.remove(alt)
new_ranking.insert(0, alt)
simulated_scores = {a: 0 for a in alternatives}
for v in voters:
rank_list = new_ranking if v == voter else v["ranking"]
for i, a in enumerate(rank_list):
simulated_scores[a] += scoring_vector[i]
simulated_ranks = get_rank(simulated_scores)
new_winner = max(simulated_scores, key=simulated_scores.get)
# Manipulation is beneficial ONLY IF new winner is ranked *higher* than original winner
if original_ranking.index(new_winner) < original_ranking.index(original_winner):
rank_improvements = []
for a in alternatives:
old_rank = original_ranks[a]
new_rank = simulated_ranks[a]
if new_rank < old_rank:
rank_improvements.append(f"{a}: {old_rank} β {new_rank}")
manipulable_cases += 1
manipulable_info.append({
"voter": voter["name"],
"original_ranking": original_ranking,
"manipulated_ranking": new_ranking,
"original_winner": original_winner,
"new_winner": new_winner,
"rank_improvements": rank_improvements,
"simulated_ranks": simulated_ranks # β
Add this for your DataFrame logic
})
break
return manipulable_cases, len(voters), manipulable_info
def get_winners(scores):
max_score = max(scores.values())
return [alt for alt, score in scores.items() if score == max_score]
# --- Function to count manipulable profile ---
from itertools import permutations, product
from collections import defaultdict
def count_manipulable_profiles(m, n, scoring_vector):
alternatives = [f"M{i+1}" for i in range(m)]
all_rankings = list(permutations(alternatives))
total_profiles = len(all_rankings) ** n
manipulable_count = 0
def get_rank(scores):
# Returns a ranking dictionary based on scores (highest score first)
sorted_items = sorted(scores.items(), key=lambda x: -x[1])
return {alt: i + 1 for i, (alt, _) in enumerate(sorted_items)}
# Iterate over all possible profiles
for profile in product(all_rankings, repeat=n):
# Calculate the original scores for this profile
original_scores = defaultdict(int)
for ranking in profile:
for i, alt in enumerate(ranking):
original_scores[alt] += scoring_vector[i]
# Identify the original winner (the alternative with the highest score)
original_winner = max(original_scores, key=original_scores.get)
# Calculate the original rank order
original_ranks = get_rank(original_scores)
# Check if the profile can be manipulated by at least one voter
profile_manipulated = False
for voter_idx, ranking in enumerate(profile):
# If the original winner is already the first choice for this voter, no manipulation is possible
if ranking[0] == original_winner:
continue
# Find the alternatives this voter prefers over the original winner
preferred_alts = []
for alt in ranking:
if alt == original_winner:
break
preferred_alts.append(alt)
# Try manipulating the vote by promoting each preferred alternative
for alt in preferred_alts:
# Create a manipulated vote where 'alt' is ranked first
manipulated = list(ranking)
manipulated.remove(alt)
manipulated.insert(0, alt)
# Recalculate the scores with the manipulated vote
new_scores = original_scores.copy()
# Subtract the original vote's scores
for pos, a in enumerate(ranking):
new_scores[a] -= scoring_vector[pos]
# Add the manipulated vote's scores
for pos, a in enumerate(manipulated):
new_scores[a] += scoring_vector[pos]
# Determine the new winner after manipulation
new_winner = max(new_scores, key=new_scores.get)
# Check if the new winner is one of the preferred alternatives and is better than the original winner
if new_winner in preferred_alts and new_scores[new_winner] > new_scores[original_winner]:
manipulable_count += 1
profile_manipulated = True
break
if profile_manipulated:
break
# Count the profile if at least one voter can manipulate it
if profile_manipulated:
manipulable_count += 1
# Return manipulable count, total profiles, and manipulability percentage
manipulability = (manipulable_count / total_profiles) * 100
return manipulable_count, total_profiles, manipulability
# 4. Compute results
if st.button("π Compute Result") and st.session_state.voters:
scores = {alt: 0 for alt in alternatives}
for v in st.session_state.voters:
for alt, pts in v["preferences"].items():
scores[alt] += pts
st.subheader("π Result Table")
score_df = pd.DataFrame(scores.items(), columns=["Alternative", "Score"]).sort_values("Score", ascending=False)
st.dataframe(score_df)
# Bar chart
st.subheader("π Score Distribution")
fig, ax = plt.subplots()
sns.barplot(data=score_df, x="Alternative", y="Score", palette="Blues_d", ax=ax)
ax.set_title("Total Points per Alternative")
st.pyplot(fig)
# Heatmap of votes
st.subheader("π₯ Voter Preferences Heatmap")
heatmap_df = pd.DataFrame(
[{alt: v["preferences"][alt] for alt in alternatives} for v in st.session_state.voters],
index=[v["name"] for v in st.session_state.voters]
)
fig2, ax2 = plt.subplots(figsize=(8, 4))
sns.heatmap(heatmap_df, annot=True, cmap="YlGnBu", ax=ax2)
st.pyplot(fig2)
# Tie-breaking logic
top_score = score_df["Score"].max()
top_alts = score_df[score_df["Score"] == top_score]["Alternative"].tolist()
if len(top_alts) > 1:
st.warning(f"Tie detected between: {', '.join(top_alts)}")
# Top-rank frequency
first_rank = {alt: 0 for alt in top_alts}
for v in st.session_state.voters:
top = v["ranking"][0]
if top in first_rank:
first_rank[top] += 1
max_first = max(first_rank.values())
tied_top = [a for a in top_alts if first_rank[a] == max_first]
if len(tied_top) == 1:
st.success(f"π
Winner by top-rank frequency: {tied_top[0]}")
else:
final = sorted(tied_top)[0]
st.success(f"π
Still tied after top-rank votes. Tie-breaker by alphabetical order: {final}")
else:
st.success(f"π
Winner: {top_alts[0]}")
# 5. Manipulation Detection
st.subheader("π΅οΈ Manipulation Detection")
m_cases, t_cases, m_profiles = detect_manipulation(
st.session_state.voters, alternatives, scoring_vector
)
st.markdown(f"π **Voters who can potentially improve their outcome:** `{m_cases} / {t_cases}`")
if m_profiles:
for item in m_profiles:
# Corrected rank improvements with accurate old and new positions
rank_changes = []
original_ranking = item['original_ranking']
manipulated_ranking = item['manipulated_ranking']
for imp in item['rank_improvements']:
alt_name, _ = imp.split(':') # Only keep the alternative name, ignore the rank change part
alt_name = alt_name.strip()
# Get original and new rank positions (1-based)
old = original_ranking.index(alt_name) + 1
new = manipulated_ranking.index(alt_name) + 1
benefit = old - new
if benefit > 0:
rank_changes.append(f"β
{alt_name}: Rank {old} β {new} (β {benefit})")
elif benefit < 0:
rank_changes.append(f"β οΈ {alt_name}: Rank {old} β {new} (β {abs(benefit)})")
else:
rank_changes.append(f"β {alt_name}: Rank {old} β {new} (no change)")
# Simulated overall rank table
sim_ranks_df = pd.DataFrame.from_dict(item["simulated_ranks"], orient="index", columns=["Rank"])
sim_ranks_df.index.name = "Alternative"
sim_ranks_df = sim_ranks_df.sort_values("Rank")
# Display voter manipulation info
st.markdown(f"""
<div style='margin-bottom:15px; border-left: 3px solid #444; padding-left: 10px;'>
π <b>{item['voter']}</b> can manipulate:
<ul>
<li><b>Original Ranking:</b> {', '.join(original_ranking)}</li>
<li><b>Manipulated Ranking:</b> {', '.join(manipulated_ranking)}</li>
<li><b>Original Winner:</b> {item['original_winner']}</li>
<li><b>New Winner after manipulation:</b> {item['new_winner']}</li>
<li><b>Rank Improvements:</b>
<ul>
{''.join(f"<li>{rc}</li>" for rc in rank_changes)}
</ul>
</li>
</ul>
</div>
""", unsafe_allow_html=True)
st.markdown("**π Simulated Ranks after Manipulation:**")
st.dataframe(sim_ranks_df)
else:
st.success("β
No manipulable profiles detected.")
# 6. Global Manipulability (All Possible Profiles)
st.subheader("π Global Profile Manipulability")
m = len(alternatives)
n = len(st.session_state.voters)
with st.spinner("Computing over all possible profiles..."):
manipulable, total,manipulability_perc = count_manipulable_profiles(m, n, scoring_vector)
st.success(f"Out of {total} total profiles, at least one voter can manipulate in {manipulable} profiles.")
st.metric("Manipulable Profiles", f"{manipulable} / {total}", delta=f"{round((manipulable/total)*100, 2)}%")
current_label = f"Custom {scoring_vector}"
# After detect_manipulation()
#m_cases, t_cases, m_profiles = detect_manipulation(st.session_state.voters, alternatives, scoring_vector)
# Accurate manipulability % for the current scoring vector
global_m, global_t, manipulability = count_manipulable_profiles(len(alternatives), len(st.session_state.voters), scoring_vector)
#manipulability = (global_m / global_t) * 100
# Avoid duplicates
if not any(vector == scoring_vector for _, vector, _, _, _ in st.session_state.scoring_history):
st.session_state.scoring_history.append((f"Custom {scoring_vector}", scoring_vector.copy(), manipulability, len(alternatives), len(st.session_state.voters)))
# 6. Accumulated Scoring Vector Comparison
if st.session_state.scoring_history:
df_hist = pd.DataFrame(
[(label, m, n, percent) for label, _, percent, m, n in st.session_state.scoring_history],
columns=["Scoring Vector", "Alternatives", "Voters", "Manipulable %"]
)
st.subheader("π Manipulability of All Used Scoring Vectors")
fig_hist, ax_hist = plt.subplots(figsize=(10, 6))
sns.barplot(data=df_hist, x="Manipulable %", y="Scoring Vector", palette="Spectral", ax=ax_hist)
ax_hist.set_xlim(0, 100)
ax_hist.set_xlabel("Percentage of Manipulable Profiles")
ax_hist.set_title("Manipulability by Scoring Vector")
# Annotate each bar with m, n and manipulability %
for i, row in df_hist.iterrows():
label = f"m={row['Alternatives']}, n={row['Voters']} | {row['Manipulable %']:.2f}%"
ax_hist.text(row['Manipulable %'] + 1, i, label, va='center', fontsize=9, color='black')
st.pyplot(fig_hist)
# Reset
if st.button("π Reset Voters"):
st.session_state.voters = []
st.session_state.scoring_history = []
st.rerun()
|