-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulation.py
97 lines (79 loc) · 4.4 KB
/
Simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy as np
class Agent():
'''represents each agent in the simulation'''
def __init__(self,initial_belief = 1, old_belief_coefficient = 1, new_signal_coefficient = 1, public_belief_coefficient = 1, friends_signal_coefficient = 1,*args, **kwargs):
'''initial belief is the agent's prior on state of the world being 1
coefficients are coefficients in agents linear update '''
self.belief = initial_belief
self.old_belief_coefficient = old_belief_coefficient
self.new_signal_coefficient = new_signal_coefficient
self.public_belief_coefficient = public_belief_coefficient
self.friends_signal_coefficient = friends_signal_coefficient
self.private_coefficient_sum = self.old_belief_coefficient+self.new_signal_coefficient+self.public_belief_coefficient
self.friend_coefficient_sum = self.old_belief_coefficient+self.friends_signal_coefficient+self.public_belief_coefficient
self.neighbours = None
def set_neighbours(self, neighbours):
'''sets neighbours, otherwise neighbours are None'''
self.neighbours = neighbours
def update_belief_with_private(self, new_signal, public_signal):
'''Updates the belief based on new private signal'''
self.belief = (self.old_belief_coefficient*self.belief+
self.new_signal_coefficient*new_signal+
self.public_belief_coefficient*public_signal) / self.private_coefficient_sum
def update_belief_from_friend(self, new_signal, public_signal):
''' Updates belief based on a friend's map'''
self.belief = (self.old_belief_coefficient*self.belief+
self.friends_signal_coefficient*new_signal+
self.public_belief_coefficient*public_signal) / self.private_coefficient_sum
def get_mapped_belief(self):
'''returns map of the belief'''
if (self.belief > 0.5):
return 1
return 0
def recieve_and_act(self, private_signal, public_signal, transparency):
''' recieves signals, updates himself and stochastically
based on transparency updates neighbours with map of his signal
returns its signal to others
'''
self.private_signal = private_signal
self.update_belief_with_private(private_signal, public_signal)
signal = self.get_mapped_belief()
if( np.random.uniform(size = 1) < transparency):
for agent in self.neighbours:
agent.update_belief_from_friend(signal ,public_signal)
return signal
class Simulation():
def __init__(self, *args, **kwargs):
pass
def initialize(self, number_of_agents, transparency = 0.5, initial_belief = 1, initial_public_signal = 1, old_belief_coefficient = 1, new_signal_coefficient = 1, public_signal_coefficient = 1, p = 0.5):
'''Creates and initializes agents
p is parameter of the bernouli
'''
self.number_of_agents = number_of_agents
self.agents = [Agent(initial_belief ,old_belief_coefficient ,new_signal_coefficient,public_signal_coefficient)
for i in range(number_of_agents)]
self.public_signal = initial_public_signal
self.p = p
self.transparency = transparency
for agent in self.agents:
neighbours = self.agents.copy()
neighbours.remove(agent)
agent.set_neighbours(neighbours)
def simulate(self, number_of_experiments):
'''Runs experiment for the number_of_experiments iterations
at each step in every iteration each agent recieves a private signal and updates his/her belief
based on transparency updates neighbours with map of his signal
public signal updates at the end of each cycle with map of agents\' signals '''
for i in range(number_of_experiments):
sum = 0
for agent in self.agents:
private_signal = self.create_signal()
sum += agent.recieve_and_act(private_signal, self.public_signal, self.transparency)
self.public_signal = (self.public_signal*i + sum/self.number_of_agents) / (i+1)
print(self.public_signal)
def create_signal(self):
''' create a signal based on informativeness '''
return np.random.binomial(1, self.p, size=1)[0]
sim = Simulation()
sim.initialize(100,transparency=1, p = 0.2)
sim.simulate(1000)