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 tools_for_distortion.data_parser import DataParser
import 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 tools_for_distortion.data_parser import DataParser
import 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 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 tools_for_distortion.ranking_generator import generate_rim_rankings
import 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.5.0.tar.gz (16.5 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.5.0-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tools_for_distortion-0.5.0.tar.gz
Algorithm Hash digest
SHA256 60da7294ce20197b82189ef2ce110327fae6da49b5bda4bc0b474f03e359118c
MD5 16e94a85c048d9152786e560de9cc9f5
BLAKE2b-256 69aa3c4d9989e498f6dffdebc5260b60611650c12e36520f3855b563f47ca95e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tools_for_distortion-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea4470e53b8240e565e6e4cad54ee94b42a52149fdef6d9bd1d3376354f850fd
MD5 6f4f2455aa3d8c7abf79bada211119ae
BLAKE2b-256 8fcfd356f4004366f4c770902122fee81e8ddc174c7fe0cbf9e8a7dee2043a22

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