-
Notifications
You must be signed in to change notification settings - Fork 2
/
Fin4 Simulation-2.py
567 lines (388 loc) · 12.7 KB
/
Fin4 Simulation-2.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().run_line_magic('matplotlib', 'inline')
# In[2]:
import random
import pandas as pd
import matplotlib.pylab as pl
# In[3]:
import cadCAD
from cadCAD import engine
from cadCAD.configuration import Configuration
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
# In[4]:
simulation_parameters = {
'T': range(20),
'N': 1,
'M': {}
}
# # Test of Basic Design
# In[5]:
# general constants
TIMESTEP = 'timestep' # access to current timestep with s[TIMESTEP]
# In[6]:
def get_update_name(variable_name):
return "add_" + variable_name
# In[7]:
USERS = "users"
PATS = "pats"
OPATS = "opats"
CLAIMS = "claims"
ADD_USERS = get_update_name(USERS)
ADD_PATS = get_update_name(PATS)
ADD_OPATS = get_update_name(OPATS)
ADD_CLAIMS = get_update_name(CLAIMS)
WALLET_GAINS = "wallet_gains"
# In[8]:
initial_conditions = {
USERS: {0:{}}, # users are numerical ids and a dictionary of their current wallet
PATS: [0], # pats are currently simply numerical ids
}
# ## update functions
# In[9]:
# create single user with empty wallet
def create_users(params, step, sL, s):
next_user_id = len(s[USERS])
x = {ADD_USERS:[
(next_user_id, {}),
#{random.choice(s[PATS]):random.randint(1,10)}
]}
return x
# In[10]:
def create_pat(params, step, sL, s):
next_pat_id = len(s[PATS])
x = {ADD_PATS:[next_pat_id,]}
return x
# In[11]:
def user_actions(params, step, sL, s):
gain = {}
pats = s[PATS]
for user in s[USERS]:
for pat in random.sample(pats, random.randint(0, len(pats))):
gain[(user, pat)] = 1
return {WALLET_GAINS: gain}
# ## update variables
# In[12]:
def add_users(params, step, sL, s, _input):
x = s[USERS].copy()
for new_user_id, wallet in _input[ADD_USERS]:
x[new_user_id] = wallet
return (USERS, x)
# In[13]:
def update_user_wallets(params, step, sL, s, _input):
x = s[USERS].copy()
changes = _input[WALLET_GAINS]
for (user, pat), gain in changes.items():
x[user][pat] = x[user].get(pat, 0) + gain
return (USERS, x)
# In[14]:
def add_pat(params, step, sL, s, _input):
x = s[PATS].copy()
x.extend(_input[ADD_PATS])
return (PATS, x)
# ## test of basic functions
# In[15]:
partial_state_update_blocks = [
{
'policies': {
'create_users': create_users,
},
'variables': {
USERS: add_users,
}
},
{
'policies': {
'user_actions': user_actions,
},
'variables': {
USERS: update_user_wallets,
}
},
{
'policies': {
'create_pat': create_pat,
},
'variables': {
PATS: add_pat,
}
},
]
config = Configuration(initial_state=initial_conditions, #dict containing variable names and initial values
partial_state_update_blocks=partial_state_update_blocks, #dict containing state update functions
sim_config=simulation_parameters #dict containing simulation parameters
)
exec_mode = ExecutionMode()
exec_context = ExecutionContext(exec_mode.single_proc)
executor = Executor(exec_context, [config]) # Pass the configuration object inside an array
raw_result, tensor = executor.execute()
df = pd.DataFrame(raw_result)
print(df)
# # Initial design V0
# In[34]:
initial_conditions = {
USERS: {0:{}}, # users are numerical ids and a dictionary of their current wallet
PATS: [0], # pats are currently simply numerical ids
CLAIMS: {},
}
# In[41]:
def users_join(params, step, sL, s):
"""Each step one user joins the system"""
next_user_id = len(s[USERS])
x = {ADD_USERS:[
(next_user_id, {}),
]}
return x
def claim_pat(params, step, sL, s):
"""Each step each user creates a random amount of claims for a random selection of PATs"""
maximum_number_of_actions = 10
new_claims = {}
pats = s[PATS]
for user in s[USERS]:
for pat in random.sample(pats, random.randint(0, min(len(pats), maximum_number_of_actions))):
new_claims[(user, pat)] = 1
return ({ADD_CLAIMS: new_claims})
def prove_claim(params, step, sL, s):
"""Each steps all claims are accepted with a certain probability"""
acceptance_probability = 1
gains = {}
for (user, pat), gain in s[CLAIMS].items():
if random.random() < acceptance_probability:
gains[(user, pat)] = gain
return {WALLET_GAINS:gains}
def create_new_pats(params, step, sL, s):
"""Each step one new PAT is added to the system"""
next_pat_id = len(s[PATS])
x = {ADD_PATS:[next_pat_id,]}
return x
# In[69]:
def update_users(params, step, sL, s, _input):
x = s[USERS]
# add new users
for new_user_id, wallet in _input[ADD_USERS]:
x[new_user_id] = wallet
# update wallet according to proven claims
changes = _input[WALLET_GAINS]
for (user, pat), gain in changes.items():
x[user][pat] = x[user].get(pat, 0) + gain
return (USERS, x)
def update_pats(params, step, sL, s, _input):
x = s[PATS]
x.extend(_input[ADD_PATS])
return (PATS, x)
def update_claims(params, step, sL, s, _input):
return (CLAIMS, _input[ADD_CLAIMS])
# In[70]:
# Initial Design V0
partial_state_update_blocks = [
{
'policies': {
'users_join': users_join,
'claim_pats': claim_pat,
'prove_claim': prove_claim,
'create_new_pats': create_new_pats
},
'variables': {
USERS: update_users,
PATS: update_pats,
CLAIMS: update_claims,
}
},
]
# In[92]:
simulation_parameters = {
'T': range(100),
'N': 1,
'M': {}
}
# In[93]:
config = Configuration(initial_state=initial_conditions, #dict containing variable names and initial values
partial_state_update_blocks=partial_state_update_blocks, #dict containing state update functions
sim_config=simulation_parameters #dict containing simulation parameters
)
exec_mode = ExecutionMode()
exec_context = ExecutionContext(exec_mode.single_proc)
executor = Executor(exec_context, [config]) # Pass the configuration object inside an array
raw_result, tensor = executor.execute()
df = pd.DataFrame(raw_result)
#print(df)
print(raw_result[-1])
# # Analysis
# - Too many different Pats with too little token/activity per PAT
# - Multiple of same kind
# In[97]:
def calculate_total_tokens_per_pat(state):
distribution = {pat:0 for pat in state[PATS]}
for user_wallet in state[USERS].values():
for pat, number_of_tokens in user_wallet.items():
distribution[pat] += number_of_tokens
return distribution
print(calculate_total_tokens_per_pat(raw_result[-1]))
pl.hist(calculate_total_tokens_per_pat(raw_result[-1]).values(), bins=300)
pl.show()
pl.scatter(calculate_total_tokens_per_pat(raw_result[-1]).keys(),
calculate_total_tokens_per_pat(raw_result[-1]).values())
# In[95]:
def calculate_active_user_per_pat(state):
distribution = {pat:0 for pat in state[PATS]}
for user_wallet in state[USERS].values():
for pat, number_of_tokens in user_wallet.items():
distribution[pat] += 1
return distribution
print(calculate_active_user_per_pat(raw_result[-1]))
pl.hist(calculate_active_user_per_pat(raw_result[-1]).values(), bins=300)
pl.show()
pl.scatter(calculate_active_user_per_pat(raw_result[-1]).keys(),
calculate_active_user_per_pat(raw_result[-1]).values())
# In[96]:
def calculate_distributions_of_number_of_tokens(state):
distribution = []
for user_wallet in state[USERS].values():
distribution.append(len(user_wallet))
return distribution
print(calculate_distributions_of_number_of_tokens(raw_result[-1]))
pl.hist(calculate_distributions_of_number_of_tokens(raw_result[-1]), bins=300)
pl.show()
# # New Design (V1)
# - Req: Concentration/Focus/Emphasis on good PATs
# - Proposal: List of accepted/good/official PATs (using TCR -> new Token GOV)
# - Initial Approach, 1 GOV per User (democracy, one vote for each user)
# In[106]:
initial_conditions = {
USERS: {0:{}}, # users are numerical ids and a dictionary of their current wallet
PATS: [0], # pats are currently simply numerical ids
OPATS: [],
CLAIMS: {},
}
# In[162]:
def claim_pat_with_opats(params, step, sL, s):
"""Each step each user creates a random amount of claims for a random selection of PATs"""
maximum_number_of_actions = 10
probability_on_opat_actions = .9
new_claims = {}
pats = s[PATS]
opats = s[OPATS]
for user in s[USERS]:
number_of_actions = random.randint(0, min(len(pats), maximum_number_of_actions))
opat_actions = min(len(opats), int(number_of_actions*probability_on_opat_actions))
usual_actions = number_of_actions - opat_actions
for pat in random.sample(opats, opat_actions):
new_claims[(user, pat)] = 1
for pat in random.sample(pats, random.randint(0, usual_actions)):
new_claims[(user, pat)] = 1
return ({ADD_CLAIMS: new_claims})
def propose_opats(params, step, sL, s):
if 5 < s[TIMESTEP] < 20:
proposal_probability = .5
else:
proposal_probability = .01
pats = s[PATS]
opats = s[OPATS]
new_opats = []
if random.random() < proposal_probability:
next_opat = random.choice(pats)
i = 0
while next_opat in opats:
i += 1
if i >= 10:
break
next_opat = random.choice(pats)
else:
new_opats.append(next_opat)
return {ADD_OPATS:new_opats}
# In[163]:
def update_opats(params, step, sL, s, _input):
x = s[OPATS]
x.extend(_input[ADD_OPATS])
return (OPATS, x)
# In[164]:
# Design V1 with
partial_state_update_blocks = [
{
'policies': {
'users_join': users_join,
'claim_pats': claim_pat_with_opats,
'prove_claim': prove_claim,
'create_new_pats': create_new_pats,
'propose_opats': propose_opats,
},
'variables': {
USERS: update_users,
PATS: update_pats,
CLAIMS: update_claims,
OPATS: update_opats,
}
},
]
# In[165]:
simulation_parameters = {
'T': range(100),
'N': 1,
'M': {}
}
# In[166]:
config = Configuration(initial_state=initial_conditions, #dict containing variable names and initial values
partial_state_update_blocks=partial_state_update_blocks, #dict containing state update functions
sim_config=simulation_parameters #dict containing simulation parameters
)
exec_mode = ExecutionMode()
exec_context = ExecutionContext(exec_mode.single_proc)
executor = Executor(exec_context, [config]) # Pass the configuration object inside an array
raw_result, tensor = executor.execute()
df = pd.DataFrame(raw_result)
#print(df)
print(raw_result[-1])
# # Analysis
# In[167]:
print(calculate_total_tokens_per_pat(raw_result[-1]))
pl.hist(calculate_total_tokens_per_pat(raw_result[-1]).values(), bins=300)
pl.show()
pl.scatter(calculate_total_tokens_per_pat(raw_result[-1]).keys(),
calculate_total_tokens_per_pat(raw_result[-1]).values())
# In[168]:
pl.hist(calculate_active_user_per_pat(raw_result[-1]).values(), bins=300)
pl.show()
pl.scatter(calculate_active_user_per_pat(raw_result[-1]).keys(),
calculate_active_user_per_pat(raw_result[-1]).values())
# In[169]:
print(calculate_distributions_of_number_of_tokens(raw_result[-1]))
pl.hist(calculate_distributions_of_number_of_tokens(raw_result[-1]), bins=300)
pl.show()
# # Analysis
# - It could work??
# - Vulnerability: Sybil attack
# - #Req: Introduction of REP as "Proof-of-work" for user actions
# In[ ]:
# new policy functions with REP
# In[ ]:
# Design V2 with
partial_state_update_blocks = [
{
'policies': {
'users_join': users_join,
'claim_pats': claim_pat_with_opats,
'prove_claim': prove_claim,
'create_new_pats': create_new_pats
},
'variables': {
USERS: update_users,
PATS: update_pats,
CLAIMS: update_claims,
}
},
{
'policies': {
'propose_opats': propose_opats,
'challenge_opats': challenge_opats,
'vote_on_opats': vote_on_opats,
},
'variables': {
'proposals': update_proposals,
'challenges': update_challenges,
OPATS: update_opats,
'failed_proposals': update_failed_proposals,
'failed_challenges': update_failed_challenges,
}
},
]