Skip to content

Commit

Permalink
Python - package PyPi - files upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth Reddy committed Mar 21, 2024
1 parent 306657b commit 9376bc1
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 1 deletion.
7 changes: 7 additions & 0 deletions build/lib/auction_systems/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .auction_models import (
first_price_sealed_bid,
second_price_sealed_bid,
open_ascending_bid,
open_descending_bid,
)
from .auction_sim import Auction
15 changes: 15 additions & 0 deletions build/lib/auction_systems/auction_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def first_price_sealed_bid(bids):
return max(bids)


def second_price_sealed_bid(bids):
sorted_bids = sorted(bids, reverse=True)
return sorted_bids[1] if len(sorted_bids) > 1 else sorted_bids[0]


def open_ascending_bid(bids):
return max(bids)


def open_descending_bid(bids):
return min(bids)
24 changes: 24 additions & 0 deletions build/lib/auction_systems/auction_sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from auction_models import *


class Auction:
def __init__(self, auction_type):
self.auction_type = auction_type
self.bids = []

def submit_bid(self, bid):
self.bids.append(bid)

def conduct_auction(self):
if self.auction_type == "First price sealed bid":
winner_bid = first_price_sealed_bid(self.bids)
elif self.auction_type == "Second price sealed bid":
winner_bid = second_price_sealed_bid(self.bids)
elif self.auction_type == "Open ascending bid":
winner_bid = open_ascending_bid(self.bids)
elif self.auction_type == "Open descending bid":
winner_bid = open_descending_bid(self.bids)
else:
raise ValueError("Invalid auction type")

return winner_bid
2 changes: 2 additions & 0 deletions build/lib/prisoners_dilemma/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .game import PrisonersDilemma
from .strategies import tit_for_tat, susp_tit_for_tat, always_cooperate, always_defect, random_strategy, probable_coop, grim, pavlov, n_pavlov, reactive, memory_one
79 changes: 79 additions & 0 deletions build/lib/prisoners_dilemma/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import random

class PrisonersDilemma:
def __init__(self):
# Initialize variables
self.player1_name = None
self.player2_name = None
self.player1_score = 0
self.player2_score = 0
self.player1_strategy = None
self.player2_strategy = None
self.history = []
self.current_player = 0 # 0 for player 1, 1 for player 2

# Strategy-specific parameters
self.grim = 0
self.pav_prob = 1
self.reactive = 0
self.reactive_c = 0
self.reactive_d = 0
self.memory_one = [0, 0, 0, 0]

def set_players(self, name1, name2):
# Set player names
self.player1_name = name1
self.player2_name = name2

def set_payoffs(self, weight1: int, weight2: int, weight3: list, weight4: list):
# Set payoff weights
self.cooperate = weight1
self.defect = weight2
self.coop_def = weight3
self.def_coop = weight4

def set_strategies(self, strategy1, strategy2):
# Set player strategies
self.player1_strategy = strategy1
self.player2_strategy = strategy2

def set_reactive(self, y, p, q):
# Set reactive strategy parameters
self.reactive = y
self.reactive_c = p
self.reactive_d = q

def set_memory_one(self, a, b, c, d):
# Set memory-one strategy parameters
self.memory_one[0] = a
self.memory_one[1] = b
self.memory_one[2] = c
self.memory_one[3] = d

def play(self, iterations):
# Play the game for a certain number of iterations
for i in range(iterations):
move1 = self.player1_strategy(self)
move2 = self.player2_strategy(self)

self.history.append((move1, move2))

# Update scores based on moves
if move1 == 'C' and move2 == 'C':
self.player1_score += self.cooperate
self.player2_score += self.cooperate
elif move1 == 'C' and move2 == 'D':
self.player1_score += self.coop_def[0]
self.player2_score += self.coop_def[1]
elif move1 == 'D' and move2 == 'C':
self.player1_score += self.def_coop[0]
self.player2_score += self.def_coop[1]
elif move1 == 'D' and move2 == 'D':
self.player1_score += self.defect
self.player2_score += self.defect

self.current_player = 1 - self.current_player

def display_scores(self):
# Return scores of both players
return (self.player1_score, self.player2_score)
105 changes: 105 additions & 0 deletions build/lib/prisoners_dilemma/strategies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import random


def tit_for_tat(game):
# Cooperate if opponent cooperated in the last round, otherwise defect
if not game.history:
return 'C'
return game.history[-1][1 - game.current_player]


def susp_tit_for_tat(game):
# Defect initially, then mimic opponent's previous move
if not game.history:
return 'D'
return game.history[-1][1 - game.current_player]


def always_cooperate(game):
# Always cooperate
return 'C'


def always_defect(game):
# Always defect
return 'D'


def random_strategy(game):
# Randomly choose between cooperation and defection
if random.random() >= 0.5:
return 'C'
return 'D'


def probable_coop(game):
# Cooperate with a given probability
prob = int(input("Enter the probability"))
if random.random() <= prob:
return 'C'
return 'D'


def grim(game):
# Cooperate until opponent defects, then always defect
if game.grim == 1:
return 'D'
game.grim = 1
return 'C'


def pavlov(game):
# Cooperate if last round had the same moves, otherwise defect
if game.history[-1][0] == game.history[-1][1]:
return 'C'
return 'D'


def n_pavlov(game):
# Adjust probability based on previous round's outcome
if game.history[-1] == ('D', 'D'):
game.pav_prob = max(0, game.pav_prob - 2/len(game.history))
if game.history[-1] == ('C', 'C'):
game.pav_prob = min(1, game.pav_prob + 1/len(game.history))
if game.history[-1][game.current_player] == 'D':
game.pav_prob = min(1, game.pav_prob + 2/len(game.history))
if game.history[-1][game.current_player] == 'C':
game.pav_prob = max(0, game.pav_prob - 1/len(game.history))


def reactive(game):
# Randomly cooperate with a reactive chance, otherwise defect
a = random.random()
if not game.history:
if a <= game.reactive:
return 'C'
return 'D'
if game.history[-1][1-game.current_player] == 'C':
if a <= game.reactive_c:
return 'C'
return 'D'
if a <= game.reactive_d:
return 'D'
return 'C'


def memory_one(game):
# Mimic opponent's last move with probabilities based on previous outcomes
a = random.random()
if not game.history:
return 'C'
if game.history[-1] == ('C', 'C'):
if a <= game.memory_one[0]:
return 'C'
return 'D'
if game.history[-1] == ('D', 'D'):
if a <= game.memory_one[1]:
return 'C'
return 'D'
if game.history[-1] == ('C', 'D'):
if a <= game.memory_one[2]:
return 'C'
return 'D'
if a <= game.memory_one[3]:
return 'C'
return 'D'
2 changes: 2 additions & 0 deletions build/lib/voting_systems/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .voting_models import Voting
from .voting_sim import simulate_voting
33 changes: 33 additions & 0 deletions build/lib/voting_systems/voting_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Voting:
def __init__(self, candidates):
self.candidates = candidates
self.votes = {candidate: 0 for candidate in candidates}

def plurality_voting(self, voter_preferences):
for preference in voter_preferences:
if preference in self.candidates:
self.votes[preference] += 1
break

def instant_runoff_voting(self, voter_preferences):
while True:
for preference in voter_preferences:
if preference in self.candidates:
self.votes[preference] += 1
break
lowest_candidate = min(self.votes, key=self.votes.get)
if self.votes[lowest_candidate] > sum(self.votes.values()) / 2:
break
del self.votes[lowest_candidate]

def approval_voting(self, voter_preferences):
for preference in voter_preferences:
for candidate in preference:
if candidate in self.candidates:
self.votes[candidate] += 1

def borda_count_voting(self, voter_preferences):
for i, preference in enumerate(voter_preferences):
for j, candidate in enumerate(preference):
if candidate in self.candidates:
self.votes[candidate] += len(preference) - j
8 changes: 8 additions & 0 deletions build/lib/voting_systems/voting_sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from voting_models import Voting

def simulate_voting(voting_system, num_voters):
for _ in range(num_voters):
voter_preferences = input("Enter voter preferences (comma-separated list): ").split(',')
voting_system(voter_preferences)
print("Voting Results:")
print(voting_system.votes)
Binary file added dist/stratepy-0.1-py3-none-any.whl
Binary file not shown.
Binary file added dist/stratepy-0.1.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
url='https://github.com/SidZRed/stratepy',
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: Apache License 2.0',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
)
11 changes: 11 additions & 0 deletions stratepy.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Metadata-Version: 2.1
Name: stratepy
Version: 0.1
Summary: A python package for testing and experimenting with strategies related to Game theory, voting systems and auction systems
Home-page: https://github.com/SidZRed/stratepy
Author: Siddharth Reddy
Author-email: [email protected]
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: Apache License 2.0
Classifier: Operating System :: OS Independent
License-File: LICENSE
16 changes: 16 additions & 0 deletions stratepy.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
LICENSE
README.md
setup.py
auction_systems/__init__.py
auction_systems/auction_models.py
auction_systems/auction_sim.py
prisoners_dilemma/__init__.py
prisoners_dilemma/game.py
prisoners_dilemma/strategies.py
stratepy.egg-info/PKG-INFO
stratepy.egg-info/SOURCES.txt
stratepy.egg-info/dependency_links.txt
stratepy.egg-info/top_level.txt
voting_systems/__init__.py
voting_systems/voting_models.py
voting_systems/voting_sim.py
1 change: 1 addition & 0 deletions stratepy.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions stratepy.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
auction_systems
prisoners_dilemma
voting_systems

0 comments on commit 9376bc1

Please sign in to comment.