forked from AurelienNioche/KalterKriegDerKulturen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
489 lines (357 loc) · 16.8 KB
/
main.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import matplotlib
matplotlib.use('TkAgg')
from pylab import plt, np
from tqdm import tqdm
from itertools import product
def set_seed(seed):
"""Initialize the seed of pseudo-random generator."""
if seed is None:
import time
seed = int((time.time()*10**6) % 4294967295)
try:
np.random.seed(seed)
print("Seed used for pseudo-random values generator:", seed, "\n")
except:
print("!!! WARNING !!!: Seed was not set correctly.")
return seed
class Convictions(np.ndarray):
'''
Convictions inherits from numpy array.
What it does in more is to modify the culture when it is modified itself.
'''
def __new__(cls, array_like, culture):
self = np.array(array_like).view(cls)
return self
def __init__(self, array_like, culture):
self.parent = culture
def __setitem__(self, instance, value):
super().__setitem__(instance, value)
self.parent[:] = self[:] > 0
class Culture(np.ndarray):
'''
Culture inherits from numpy array.
What it does in more is to have a 'Convictions' object as attribute
'''
def __new__(cls, convictions):
self = np.array(np.zeros(len(convictions), dtype=int)).view(cls)
return self
def __init__(self, convictions):
self.convictions = Convictions(convictions, self)
self[:] = self.convictions[:] > 0
def contains_belief(self, belief):
# Imagine that a belief is something like np.array([np.nan, 0, 1]) for '#01'
return self[np.where(belief == 0)] == 0 and self[np.where(belief == 1)] == 1
def get_most_robust_convictions(self, n=1):
# np.argsort() returns the indices that would sort the array
# [::-1] reverse the sorting from increasing to decreasing order
# [:n] selects the n first elements
unique_convictions_values = np.unique(self.convictions)
if len(unique_convictions_values) == len(self.convictions):
return np.argsort(np.absolute(self.convictions))[::-1][:n]
else:
# Class in decreasing order the unique different values of convictions
sorted_unique_values = sorted(unique_convictions_values, reverse=True)
to_return = np.zeros(len(self.convictions), dtype=int)
i = 0
for value in sorted_unique_values:
list_of_idx = np.where(self.convictions == value)[0]
to_return[i: i + len(list_of_idx)] = np.random.permutation(list_of_idx)
i += len(list_of_idx)
return np.asarray(to_return)
# temp_order = np.argsort(np.absolute(self.convictions))[::-1][:n]
class Agent(object):
""" An agent is defined by its culture/convictions, suggestibility and proselytism.
suggestibility and proselytism are supposedly 'innate' attributes,
while culture and convictions are subject to changes while being influenced by other agents.
convictions and culture are interdependent, culture is the 'binarized' (set {0, 1}) version
of convictions vector (range [-1,1]).
suggestibility: real in range [0, 1]
proselytism: integer in range [0, culture size]
convictions: vector of reals in range [-1, 1]
culture: vector of boolean values
"""
def __init__(self, suggestibility, proselytism, convictions):
# --- Parameters --- #
self.suggestibility = suggestibility
self.proselytism = proselytism
# ----------------- #
self.culture = Culture(convictions=convictions)
def try_to_convince(self):
arguments_idx = self.culture.get_most_robust_convictions(n=self.proselytism)
arguments_strength = self.culture.convictions[arguments_idx]
return arguments_idx, arguments_strength
def apply_linear_ind_influence(self, arguments_idx, arguments_strength, verbose=False):
""" Apply linear and independent influence formula.
Inputs:
- arguments_idx: array of indices of convictions
- arguments_strength: array of same size as arguments_idx with the correspding strength of arguments of the attacker
"""
if verbose:
print("---apply_linear_ind_influence()")
print("self.culture.convictions[arguments_idx]")
print(self.culture.convictions[arguments_idx])
print("self.suggestibility")
print(self.suggestibility)
print("arguments_strength")
print(arguments_strength)
self.culture.convictions[arguments_idx] += self.suggestibility * arguments_strength
def apply_threshold_influence(self):
# Apply threshold in order to not exceed the limits [-1,1]
self.culture.convictions[self.culture.convictions<-1] = -1
self.culture.convictions[self.culture.convictions>1] = 1
# TODO: the following line with "np.clip()" does not work. It produces a strange error at a latter point in the program:
# AttributeError: 'Convictions' object has no attribute 'parent'
# self.culture.convictions = np.clip(self.culture.convictions, -1, 1)
def get_influenced(self, arguments_idx, arguments_strength):
""" Modify the own convictions of an agent based on the 'attacker's argument
strength and the agent's own suggestibility.
The formula is linear and is independent on the self convictions.
Inputs:
- arguments_idx: array of indices of convictions
- arguments_strength: array of same size as arguments_idx with the correspding strength of arguments of the attacker
Ideas for further dev:
suggestibility could be 2-fold depending if the attacker has arguments that goes in the same 'direction'."""
# Apply influence formula
self.apply_linear_ind_influence(arguments_idx, arguments_strength)
# Apply threshold
self.apply_threshold_influence()
def get_influenced_nonlinear(self, arguments_idx, arguments_strength):
""" Modify the own convictions of an agent based on the 'attacker's argument
strength and the agent's own suggestibility.
The formula is non-linear and is dependent on the self convictions.
Now the influence is considered to be non-linear :
- if self convictions are between -0.5 and +0.5,
then the formula is the same than the "linear" version (i.e. get_influenced()).
- if self convictions are below are above this range,
if the attackers arguments are or same sign, the formulu
then the self suggestibility gets weaker the more the convictions gets to the extremes (-1 or +1).
suggestibility could be 2-fold depending if the attacker has arguments that goes in the same 'direction'."""
# Apply influence formula
for (i,s) in zip(arguments_idx, arguments_strength):
if self.culture.convictions[i] >= -0.1 or self.culture.convictions[i] <= 0.1:
self.apply_linear_ind_influence(np.asarray([i]), np.asarray([s]))
else:
# If the self convitions are in the opposite direction than the attacker's
if self.culture.convictions * s < 0:
# The influence is decreased the more the self convictions are closed to the extremes {-1 ; 1}
self.culture.convictions[i] += 2 * (1 - abs(self.culture.convictions[i])) \
* self.suggestibility * s
# For abs(self.culture.convictions[i]) equal to 0.5 this is identical
# to the classical formula because the new factor equals 1: 2 * (1 - 0.5) = 1
elif self.culture.convictions * s > 0:
# The influence is increased the more the self convictions are closed to the extremes {-1 ; 1}
if 1 - abs(self.culture.convictions[i]) < 10**-3:
self.culture.convictions[i] += 10**3 * self.suggestibility * s
else:
self.culture.convictions[i] += 1 / (2 * (1 - abs(self.culture.convictions[i]))) \
* self.suggestibility * s
else:
# Nothing happens in case it is equal to 0, because the product would be 0 anyway.
pass
# Apply threshold
self.apply_threshold_influence()
class Environment(object):
def __init__(self, n_agent, t_max, culture_length, influence_type=None):
# --- Parameters --- #
self.t_max = t_max
self.culture_length = culture_length
self.n_agent = n_agent
self.influence_type = influence_type
if self.influence_type is None:
self.influence_type = 'linear'
# ----------------- #
self.agents = []
# Generation of random agents
self.create_agents()
def _assert_agent(self, agent):
# sanity checks before influence round
assert np.all(agent.culture.convictions >= -1)
assert np.all(agent.culture.convictions <= 1)
assert agent.suggestibility >= 0
assert agent.suggestibility <= 1
assert agent.proselytism >= 0
assert agent.proselytism <= self.culture_length
# TODO: add check if proselytism is an integer
def get_matrix_of_agents_culture(self):
return np.array([a.culture for a in self.agents])
def get_matrix_of_agents_convictions(self):
return np.array([a.culture.convictions for a in self.agents])
def create_agents(self):
# intialize list of agents
self.agents = []
# create random agents
for i in range(self.n_agent):
a = Agent(
proselytism=np.random.randint(self.culture_length),
suggestibility=np.random.random(),
convictions=np.random.random(self.culture_length) * 2 - 1
)
self.agents.append(a)
def create_orthogonal_agents(self):
""" Create agents that have different cultures.
Orthogonal means that one the culture space any agent should differ at least by one bit. """
pass
def run(self):
for t in tqdm(range(self.t_max)):
self.one_step()
def one_step(self, verbose=False):
# Take a random order among the indexes of the agents.
random_order = np.random.permutation(self.n_agent)
for i in random_order:
# Each agent is "initiator' during one period.
initiator = self.agents[i]
# A 'responder' is randomly selected.
responder = self.agents[np.random.choice(np.delete(np.arange(self.n_agent), i))]
if verbose:
print("Agent initiator: "+str(i))
print("Agent influenced: "+str(self.agents.index(responder)))
# Sanity checks/asserts
self._assert_agent(initiator)
self._assert_agent(responder)
arg_idx, arg_strength = initiator.try_to_convince()
if self.influence_type == 'linear':
responder.get_influenced(arguments_idx=arg_idx, arguments_strength=arg_strength)
elif self.influence_type == 'nonlinear':
responder.get_influenced_nonlinear(arguments_idx=arg_idx, arguments_strength=arg_strength)
else:
str_err = "self.influence_type not set to a correct value: " + str(self.influence_type)
raise Exception(str_err)
def mult_cul_all_agents(self, factor):
""" Multiply the culture of all agents by a given factor."""
for c in [a.culture.convictions for a in self.agents]:
c *= factor
def make_agent_dictator(self, agent_indices):
""" Modifiy all agents with the given indices to make them dictators.
An agent become a dictator by getting a suggestibility of 0
and a proselytism at the maximal value (i.e. the length of the culture)."""
for i in agent_indices:
# A dictator cannot change his believes/opinions
self.agents[i].suggestibility = 0.
# A dictator influences from all his culture
self.agents[i].proselytism = self.culture_length
def plot(self):
self.plot_culture()
self.plot_convictions()
def plot_culture(self):
# plt.figure()
plt.matshow(self.get_matrix_of_agents_culture())
plt.colorbar()
plt.clim(0,1) # Sets the min/max limits of colorbar
plt.title("Culture")
def plot_convictions(self):
# plt.figure()
plt.matshow(self.get_matrix_of_agents_convictions())
plt.colorbar()
plt.clim(-1,1) # Sets the min/max limits of colorbar
plt.title("Convictions")
def print_agents_culture(self):
print("Cultures of agents:")
print([a.culture for a in self.agents])
print("Convictions of agents:")
print([a.culture.convictions for a in self.agents])
class Experiment(object):
def __init__(self, seed=None):
self.n_agent_list = [100]
self.t_max_list = [10]
self.cul_len_list = [4] # culture_length
self.seed = set_seed(seed)
def run(self):
print("Experiment: Running.")
print()
for culture_length, t_max, n_agent \
in zip(self.cul_len_list, self.t_max_list, self.n_agent_list):
env = Environment(culture_length=culture_length, t_max=t_max, n_agent=n_agent)
env.run()
def run_evo_plot(self):
""" Awesome! All agents become chips!"""
t_max = 100
culture_length = 5
env = Environment(culture_length=culture_length, t_max=t_max, n_agent=1000, influence_type='linear')
all_possible_cultures = list(product([False,True],repeat=culture_length))
all_time_cul = np.zeros((t_max, len(all_possible_cultures)))
#TODO: try to understand why
#all_time_cul = np.zeros((t_max, len(all_possible_cultures)))
# do not work
print(len(all_possible_cultures))
print(2**culture_length)
# env.plot()
for t in tqdm(range(t_max)):
env.one_step()
for agent in env.agents:
all_time_cul[t, all_possible_cultures.index(tuple(agent.culture))] += 1
plt.figure()
plt.plot(all_time_cul)
# env.plot()
def run_dictatorship(self):
""" Why dictator is good with few agents and 'stupid alone' when agents are numerous?
--> How many agents are necessary for the dictator to be isolated?"""
# set_seed(1)
env = Environment(culture_length=30, t_max=500, n_agent=100, influence_type='linear')
print("---init")
env.print_agents_culture()
# print("---lowered")
# env.mult_cul_all_agents(factor=0.1)
# env.print_agents_culture()
env.plot()
print("---make dictators")
env.make_agent_dictator([0])
env.print_agents_culture()
print("---run")
env.run()
print("---runned")
env.print_agents_culture()
print(env.get_matrix_of_agents_convictions())
env.plot()
def save(self):
pass
def plot(self, results):
pass
# ---------------- TEST FUNCTIONS ---------------- #
def test_culture():
print("CULTURE DEMO")
k = 5
culture = Culture(convictions=np.random.random(k) * 2 - 1)
print("First culture", culture)
print("First convictions", culture.convictions)
culture.convictions[:] = 0.3, - 0.4, 0.6, 0.8, -0.5
print("New convictions", culture.convictions)
print("New culture", culture)
print("3 most robust convictions", culture.get_most_robust_convictions(n=3))
def test_influence():
print("INFLUENCE DEMO")
a = Agent(suggestibility=1, proselytism=3, convictions=[0.5, 0.3, 0.4, 0.2])
print("Old culture of agent", a.culture)
print("Old convictions of agent", a.culture.convictions)
a.get_influenced(arguments_idx=[1, 3], arguments_strength=[-0.4, -0.4])
print("New convictions of agent", a.culture.convictions)
print("New culture of agent", a.culture)
def test_conviction():
print("CONVICTION DEMO")
a = Agent(suggestibility=1, proselytism=3, convictions=[0.5, 0.3, -0.4, 0.2])
print("Culture of agent", a.culture)
print("Convictions of agent", a.culture.convictions)
best_convictions, values = a.try_to_convince()
print("Best 3 convictions are:", best_convictions)
print("Values for these convictions are:", values)
# ---------------- MAIN ---------------- #
def main():
test_culture()
print()
print("*" * 10)
print()
test_influence()
print()
print("*" * 10)
print()
test_conviction()
print()
print("*" * 10)
print()
exp = Experiment(seed=None)
exp.run()
if __name__ == "__main__":
# main()
exp = Experiment()
#exp.run_dictatorship()
exp.run_evo_plot()
plt.show()