Skip to main content

A brief description of your package

Project description

Library for Distortion in Utilitarian Social Choice Theory

The purpose of this project is to provide a way to easily access and experiment with distortion in social choice theory. This library contains ways to parse and generate ranking data, experiment with deterministic and randomized voting rules, and to calculate distortion from those rules.

Description

This library provides tools for:

  • Parsing and generating ranking data
  • Experimenting with deterministic and randomized voting rules
  • Calculating distortion from those voting rules

Installation

pip install tools_for_distortion

Cloning the Repository

# Clone the repository
git clone https://github.com/AshleyDong34/Masters-Project.git

Setting up a virtual environment

Navigate to the project directory

cd Masters-Project

Create a virtual environment

python -m venv venv

Activate the virtual environment

On Windows

.\venv\Scripts\activate

On macOS/Linux

source venv/bin/activate

Installing dependencies

run the following code on to install the dependencies on the virtual environment

pip install -r requirements.txt

Data Setup

You need to create folders for all your data that you download. The data is found on the Preflib website(https://preflib.simonrey.fr/format#soc). There are 4 files you need to download to use the library offline. Each of them needs to be in their own folder named soc_data, soi_data, toc_data, and toi_data.

Example structure

implementation/
├── soc_data/
│   ├── 00004-00000001.soc
│   └── ...
├── soi_data/
│   ├── ...
├── toc_data/
│   ├── ...
├── toi_data/
│   └── ...

Usage

This library provides several ways to get ranking data with generated utilities

1. Use the downloaded preflib data and parse them into a usable data type

import numpy as np
from src.tools_for_distortion.data_parser import DataParser
import src.tools_for_distortion.voting_rules as vr

def custom_distribution():
    return np.random.uniform(0, 1)

parser = DataParser('soc_data/00004-00000001.soc', custom_distribution)
metadata, ranking_data, utilities_data = parser.parse()
num_alternatives = int(metadata['number_alternatives'])
winner, _ = vr.harmonic_scoring_rule(ranking_data, num_alternatives)
print(f"The winner is: {winner}")

2. Using URL parsing that Preflib provides, offsetting the need to download data

import numpy as np
from preflibtools.instances import OrdinalInstance
from src.tools_for_distortion.data_parser import DataParser
import src.tools_for_distortion.voting_rules as vr

# Custom distribution function for utility generation
def custom_distribution():
    return np.random.uniform(0, 1)

instance = OrdinalInstance()
instance.parse_url("https://www.preflib.org/static/data/irish/00001-00000001.soi")
parser = DataParser()
metadata, ranking_data, utilities_data = parser.parse_data(instance, custom_distribution)

# Use the ranking data and metadata to get the winner of a voting rule
num_alternatives = int(metadata['number_alternatives'])
winner, _ = vr.harmonic_scoring_rule(ranking_data, num_alternatives)
print(f"The winner is: {winner}")

3. Generate Ranking Data using Markov's model or single peaked preferences

Single Peaked Preferences:

from src.tools_for_distortion.ranking_generator import generate_single_peaked_preferences

candidates = [1, 2, 3, 4, 5]  # List of candidates
num_voters = 10
parser = generate_single_peaked_preferences(candidates, num_voters)
print(parser.ranking_data)
print(parser.utilities)

Markov's Model(RIM)

import numpy as np
from src.tools_for_distortion.ranking_generator import generate_rim_rankings
import src.tools_for_distortion.voting_rules as vr

num_voters = 10
num_candidates = 4
phi = 0.8  # A dispersion parameter; smaller values indicate closer to the reference ranking

parser = generate_rim_rankings(num_voters, num_candidates, phi)
parser.generate_utilities()
print(parser.utilities_data)
num_alternatives = int(parser.metadata['number_alternatives'])
winner, _ = vr.harmonic_scoring_rule(parser.ranking_data, num_alternatives)
print(f"The winner is: {winner}")

Calculating Deterministic Distortion

To use the deterministic distortion, there are two options: calculating either the average distortion over a number of iterations or the worst-case distortion over a number of iterations. It is set to average by default.

import src.tools_for_distortion.distortion as dist

distortion_value = dist.det_distortion(winner, parser, num_iteres=10)
print(f"Distortion: {distortion_value}")

Features

  • Parsing ranking data from local files or URLs
  • Generating utility data
  • Implementing and experimenting with various voting rules
  • Calculating distortion metrics

License

This project is licensed under the MIT License - see the LICENSE file for details.

Authors and Acknowledgements

  • Ashley Dong - initial work
  • Aris Filos-Ratsikas - supervisor

Project Status

This project is completed and no longer maintained. Future development is not currently planned.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tools_for_distortion-0.2.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tools_for_distortion-0.2.0-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file tools_for_distortion-0.2.0.tar.gz.

File metadata

  • Download URL: tools_for_distortion-0.2.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for tools_for_distortion-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3b30eeff810a57b2212228f6a42956b63e71460d1498224e4605f73c756c3a4a
MD5 500d2414cf3c2d6565d239db321a32b0
BLAKE2b-256 8ca3f9af2e1f3763d5d10142918069e144ddaf66e903c3b9329e34a170b150ab

See more details on using hashes here.

File details

Details for the file tools_for_distortion-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tools_for_distortion-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44e2494b2b7d8521d2378dafc0cb3d0c0268946e82d87be7e91c258be2cfad43
MD5 d10cfcd09ae17ef7213a398db54984e1
BLAKE2b-256 133583273f0f7d3fe97c48884e843ff64bbd7971f5f12b7c4df6fc2ce010af25

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page