forked from hoskinsegan/replicating-propagandist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJulia code
More file actions
184 lines (160 loc) · 4.52 KB
/
Julia code
File metadata and controls
184 lines (160 loc) · 4.52 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
addprocs(3-length(procs()))
@everywhere using Distributions
@everywhere type Player
belief
result
friends
goodfriends
#n
end
@everywhere type Game
binom_n
epsilon
players
observers
# converged
# round_converged
end
@everywhere Game() = Game(0,0,[],[])
@everywhere function Update(game,player)
for i in player.goodfriends
top = (1-player.belief)*((.5-game.epsilon)/(.5+game.epsilon))^(2*game.players[i].result-game.binom_n)
player.belief = 1/(1+top/player.belief)
end
end
@everywhere function Round(game,propagandist)
for x in game.players
y = []
for z in x.friends
if game.players[z].belief > .5
y = vcat(y,z)
end
end
x.goodfriends = y
if x.belief > .5
x.result = rand(Binomial(game.binom_n,.5+game.epsilon))
end
end
for x in game.observers
y = []
for z in x.friends
if game.players[z].belief > .5
y = vcat(y,z)
end
end
if propagandist
for z = 1:length(game.players)
if game.players[z].result < game.binom_n/2
y = vcat(y,z)
end
end
end
x.goodfriends = y
end
for x in game.players
Update(game,x)
end
for x in game.observers
Update(game,x)
end
end
@everywhere function InitializeGame(game,network,popSize,binom_n,epsilon)
game.binom_n = binom_n
game.epsilon = epsilon
game.players = Array{Player}(popSize)
game.observers = Array{Player}(popSize)
# game.converged = 0
# game.round_converged = 0
if network == "cycle"
game.players[1] = Player(rand(),0,[1,2,popSize],[])
game.players[popSize] = Player(rand(),0,[popSize-1,popSize, 1],[])
for i = 2:(popSize-1)
game.players[i] = Player(rand(),0,[i-1,i, i+1],[])
end
y = Array(Int64,0)
for i = 1:popSize
y = vcat(y,i)
game.observers[i] = Player(rand()/2,0,y,[])
end
elseif network == "complete"
x = []
for j = 1:popSize
x = vcat(x,j)
end
for i = 1:popSize
game.players[i] = Player(rand(),0,x,[])
end
y = Array(Int64,0)
for i = 1:popSize
y = vcat(y,i)
game.observers[i] = Player(rand()/2,0,y,[])
end
else
error("no such network type")
end
end
@everywhere function reInitializeGame(game)
# game.converged = 0
# game.round_converged = 0
for x in game.players
x.belief = rand()
end
for x in game.observers
x.belief = rand()/2
end
end
@everywhere function DoIt(popSize, networkType, epsilon, binom_n, runs,propagandist)
#numberConverged = 0
#sumRoundConverged = 0
Zollman = Game()
InitializeGame(Zollman,networkType,popSize,binom_n,epsilon)
results=Array(Int64,(0,popSize+1))
for x = 1:runs
roundCounter = 0
convergenceCheck = 0
while convergenceCheck == 0
y=[]
for x in Zollman.players
y=vcat(y, x.belief)
end
if minimum(y) >= .99
convergenceCheck = 1
#sumRoundConverged = sumRoundConverged + roundCounter
summary = Array(Float64,(1,0))
summary = hcat(summary,roundCounter)
for x in Zollman.observers
summary = hcat(summary,x.belief)
end
results = vcat(results,summary)
#numberConverged = numberConverged + 1
elseif maximum(y) <= .5
convergenceCheck = 1
end
Round(Zollman,propagandist)
roundCounter += 1
end
reInitializeGame(Zollman)
end
mean(results,1)
#println("Portion Converged: ", numberConverged/runs)
#println("Average Round Converged: ", sumRoundConverged/numberConverged)
end
for popSize in [30]
#results = Array(Any,(0,popSize+6))
for binom_n in [10]
for epsilon in [.05]
for prop in [false]
proc2 = @spawn DoIt(popSize,"complete",epsilon,binom_n,500,prop)
proc1 = @spawn DoIt(popSize,"complete",epsilon,binom_n,500,prop)
#println(hcat([popSize "complete" epsilon binom_n prop], mean([fetch(proc1),fetch(proc2)])))
results = vcat(results,DoIt(popSize,"cycle",epsilon,binom_n,1000,prop))
end
end
end
writecsv("Cailin extra data no prop popSize$popSize.csv", results)
end
#writecsv("Results NoProp Size$popSize Binom$binom_n Eps$epsilon Complete.txt", DoIt(6,"complete",epsilon,binom_n,10,false))
#writecsv("Results NoProp Size$popSize Binom$binom_n Eps$epsilon Cycle.txt", DoIt(6,"cycle",epsilon,binom_n,10,false))
#writecsv("Results Prop Size$popSize Binom$binom_n Eps$epsilon Complete.txt", DoIt(6,"complete",epsilon,binom_n,10,true))
#writecsv("Results Prop Size$popSize Binom$binom_n Eps$epsilon Cycle.txt", DoIt(6,"cycle",epsilon,binom_n,10,true))
#writecsv("results.csv",DoIt)