Skip to content

Instantly share code, notes, and snippets.

Created May 7, 2023 08:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# Hell is Game Theory Folk Theorems
import random
import math
def minimize(prev_list, prev_temp):
return 30
def maximize(prev_list, prev_temp):
return 100
def equilibrium(prev_list, prev_temp):
if min(prev_list) < 99 and prev_temp != 100: return 100
return 99
def randomize(prev_list, prev_temp):
return random.choice(prev_list)
def average(prev_list, prev_temp):
return int(prev_temp)
STRATS = (minimize, maximize, equilibrium, equilibrium, randomize, average)
class Agent:
def __init__(self):
self.strategy = random.choice(STRATS)
self.total_reward = 0
self.buffer = {v: 0 for v in STRATS}
def execute(self, prev_list, prev_temp):
return self.strategy(prev_list, prev_temp)
def reward(self, v):
self.total_reward += v
self.buffer[self.strategy] += v / 1000
if random.random() < 1/8:
strats = list(self.buffer.keys())
weights = [math.exp(rew) for rew in self.buffer.values()]
self.strategy = random.choices(strats, weights=weights)[0]
def __str__(self):
return f'[Agent | strategy:{self.strategy.__name__}\t| total: {self.total_reward:.2f}]'
def str(self, v):
return f'[Agent | strategy:{self.strategy.__name__}\t| total: {self.total_reward:.2f} | prev: {v}]'
class Field:
def __init__(self, N=10):
self.agents = [Agent() for _ in range(N)]
self.prev = [30 for _ in range(N)] = 30
def simulate(self):
actions = [agent.execute(self.prev, for agent in self.agents]
self.prev = actions[:] = sum(self.prev) / len(self.prev)
for agent in self.agents: agent.reward(50 -
# print(self)
def __str__(self):
return f'\nPrev temp: {}\n' + '\n'.join(' ' + s.str(n) for s,n in zip(self.agents, self.prev))
f = Field()
for i in range(1000):
for _ in range(100): f.simulate()
print(f'\nStep {i+1}00')
import traceback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment