Created
February 12, 2021 18:35
-
-
Save eadanfahey/b79139054a0ad03be73445545074887c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Person: | |
def __init__(self, position, coins): | |
self.position = position | |
self.coins = coins | |
self.neighbors = [] | |
def make_bet(self): | |
if len(self.neighbors) == 0: | |
return | |
player = random.choice(self.neighbors) | |
stakes = min(self.coins, player.coins) // 10 | |
result = stakes if random.random() < 0.5 else -stakes | |
self.coins += result | |
player.coins -= result | |
def create_people_grid(num_people, init_coins, grid_size, search_radius): | |
""" | |
Initialize a grid containing a random scatter of agents. A person's | |
neighbors are those people within search_radius of the person's | |
position. | |
""" | |
# Randomly place agents on a 10 x 10 grid. A person's neighbors are all | |
# people within the distance `search_radius` from its position. | |
w, h = grid_size | |
people = [] | |
for i in range(num_people): | |
position = (random.randrange(0, w), random.randrange(0, h)) | |
coins = init_coins | |
people.append(Person(position, coins)) | |
# Set the neighbors | |
for person in people: | |
for neighbor in people: | |
if neighbor == person: | |
continue | |
x1, y1 = person.position | |
x2, y2 = neighbor.position | |
if (x2 - x1)**2 + (y2 - y1)**2 < search_radius**2: | |
person.neighbors.append(neighbor) | |
return people | |
def main(): | |
people = create_people_grid( | |
num_people=20, | |
init_coins=1000, | |
grid_size=(10, 10), | |
search_radius=5 | |
) | |
num_iterations = 1000 | |
for i in range(num_iterations): | |
for person in people: | |
person.make_bet() | |
print("maximum wealth:", max(p.coins for p in people)) | |
print("minimum wealth:", min(p.coins for p in people)) | |
print("average_wealth:", sum(p.coins for p in people) / len(people)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment