-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_dynamics_solution2and3.py
4809 lines (4151 loc) · 219 KB
/
load_dynamics_solution2and3.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import pickle
import numpy as np
import random
import scipy.integrate as spi
from scipy.sparse import *
import itertools
import networkx as nx
import matplotlib.pyplot as plt
import torch
from torch_scatter import scatter_sum, scatter_mean
from moviepy.editor import VideoClip
from moviepy.video.io.bindings import mplfig_to_npimage
#########===================================================
##
## build network topology
##
#########===================================================
from plots import plot_functions
def grid_8_neighbor_graph(N):
"""
Build discrete grid graph, each node has 8 neighbors
:param n: sqrt of the number of nodes
:return: A, the adjacency matrix
"""
N = int(N)
n = int(N ** 2)
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
dy = [-1, -1, -1, 0, 0, 1, 1, 1]
A = torch.zeros(n, n)
for x in range(N):
for y in range(N):
index = x * N + y
for i in range(len(dx)):
newx = x + dx[i]
newy = y + dy[i]
if N > newx >= 0 and N > newy >= 0:
index2 = newx * N + newy
A[index, index2] = 1
return A.float()
def build_topology(N, topo_type, seed, **params):
"""
:param N: #nodes
:param topo_type: the type of topology
:param seed: random seed
:param params:
:return: G
"""
print("building network topology [%s] ..." % topo_type)
if topo_type == 'grid':
nn = int(np.ceil(np.sqrt(N))) # grid-layout pixels :20
A = grid_8_neighbor_graph(nn)
G = nx.from_numpy_array(A.numpy())
elif topo_type == 'random':
if 'p' in params:
p = params['p']
print("setting p to %s ..." % p)
else:
print("setting default values [0.1] to p ...")
p = 0.1
G = nx.erdos_renyi_graph(N, p, seed=seed)
elif topo_type == 'power_law':
if 'm' in params:
m = params['m']
print("setting m to %s ..." % m)
else:
print("setting default values [5] to m ...")
m = 5
if N <= m:
N = N + m
G = nx.barabasi_albert_graph(N, m, seed=seed)
elif topo_type == 'small_world':
if 'k' in params:
k = params['k']
print("setting k to %s ..." % k)
else:
print("setting default values [5] to k ...")
k = 5
if 'p' in params:
p = params['p']
print("setting p to %s ..." % p)
else:
print("setting default values [0.5] to p ...")
p = 0.5
G = nx.newman_watts_strogatz_graph(N, k, p, seed=seed)
elif topo_type == 'community':
n1 = int(N / 3)
n2 = int(N / 3)
n3 = int(N / 4)
n4 = N - n1 - n2 - n3
if 'p_in' in params:
p_in = params['p_in']
print("setting p_in to %s ..." % p_in)
else:
print("setting default values [0.25] to p_in ...")
p_in = 0.25
if 'p_out' in params:
p_out = params['p_out']
print("setting p_out to %s ..." % p_out)
else:
print("setting default values [0.01] to p_out ...")
p_out = 0.01
G = nx.random_partition_graph([n1, n2, n3, n4], p_in, p_out, seed=seed)
elif topo_type == 'full_connected':
G = nx.complete_graph(N)
G.add_edges_from([(i, i) for i in range(N)]) # add self_loop
elif topo_type == 'directed_full_connected':
G = nx.complete_graph(N, nx.DiGraph())
# G.add_edges_from([(i, i) for i in range(N)]) # add self_loop
else:
print("ERROR topo_type [%s]" % topo_type)
exit(1)
return G
#########===================================================
##
## network dynamics
##
#########===================================================
def heat_diffusion_dynamics(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
"""
dx_i(t)/dt = -k_{i}\sum_{j=1}^{n}A_{i,j}(x_i-x_j)
governed by Newton’s law of cooling [1], which states
that the rate of heat change of node i is proportional to
the difference of the temperature between node i and its
neighbors with heat capacity matrix A.
[1] A v Luikov. 2012. Analytical heat diffusion theory. Elsevier.
input:
X: [N, d]
sparse_A: [row, col], which means row -> col
params['K'] : int
return X: [steps, N, d]
"""
N, x_dim = X.shape
if 'K' in params:
print("setting K to %s ..." % params['K'])
K = np.ones(N) * params['K']
else:
print("setting default values [0.1] to K ...")
K = np.ones(N) * 1.
row, col = sparse_A
# print(len(row),len(col),X.shape)
def diff_heat(X, t):
# dx_i/dt = k_{i,j} \sum_{j=1}^{n} A_{i,j}(x_j-x_i)
X_j = X.reshape(-1, x_dim)[row]
X_i = X.reshape(-1, x_dim)[col]
# we do not know the scatter_sum in numpy package, so we use scatter_sum in torch instead.
dX = K.reshape(-1, 1) * scatter_sum(torch.from_numpy(
X_j - X_i
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
# dX = K.reshape(-1, 1) * (A @ X.reshape(-1, x_dim) - np.sum(A, axis=1, keepdims=True) * X.reshape(-1, x_dim))
return dX.reshape(-1)
t_range = np.arange(t_start, t_end + t_inc, t_inc)
New_X = spi.odeint(diff_heat, X.reshape(-1), t_range)
if diff_flag:
return New_X.reshape(len(t_range), N, x_dim), \
np.gradient(New_X, axis=0, edge_order=1).reshape(len(t_range), N, x_dim), \
t_range.reshape(-1, 1)
else:
return New_X.reshape(len(t_range), N, x_dim), t_range.reshape(-1, 1)
def mutualistic_interaction_dynamics(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
"""
dx_i(t)/dt = b_i+x_i(1 - x_i/k_i)(x_i/c_i - 1)+\sum_{j=1}^{n}A_{i,j}(x_i*x_j)/(d_i + e_i*x_i + h_j*x_j)
The mutualistic differential equation systems [1] capture the abundance xifi(t) of species i,
consisting of incoming migration term bi, logistic growth with population capacity ki [2] and
Allee effect [3] with cold-start threshold ci , and mutualistic interaction term with interaction network A.
[1] Jianxi Gao, Baruch Barzel, and Albert-László Barabási. 2016. Universal resilience patterns in complex networks.
Nature 530, 7590 (2016), 307.
[2] Chengxi Zang, Peng Cui, Christos Faloutsos, and Wenwu Zhu. 2018. On Power Law Growth of Social Networks.
IEEE Transactions on Knowledge and Data Engineering 30, 9 (2018), 1727–1740
[3] Warder Clyde Allee, Orlando Park, Alfred Edwards Emerson, Thomas Park, Karl Patterson Schmidt, et al. 1949.
Principles of animal ecology. Technical Report. Saunders Company Philadelphia, Pennsylvania, USA.
input:
X: [N, d]
sparse_A: [row, col], which means row -> col
return X: [steps, N, d]
"""
N, x_dim = X.shape
if 'b' in params:
print("setting b to %s ..." % params['b'])
b = np.ones(N) * params['b']
else:
default_val = 1.
print("setting default values [%s] to b ..." % default_val)
b = np.ones(N) * default_val
if 'c' in params:
print("setting c to %s ..." % params['c'])
c = np.ones(N) * params['c']
else:
default_val = 1.
print("setting default values [%s] to c ..." % default_val)
c = np.ones(N) * default_val
if 'd' in params:
print("setting d to %s ..." % params['d'])
d = np.ones(N) * params['d']
else:
default_val = 5.
print("setting default values [%s] to d ..." % default_val)
d = np.ones(N) * default_val
if 'e' in params:
print("setting e to %s ..." % params['e'])
e = np.ones(N) * params['e']
else:
default_val = 0.9
print("setting default values [%s] to e ..." % default_val)
e = np.ones(N) * default_val
if 'h' in params:
print("setting h to %s ..." % params['h'])
h = np.ones(N) * params['h']
else:
default_val = 0.1
print("setting default values [%s] to h ..." % default_val)
h = np.ones(N) * default_val
if 'k' in params:
print("setting k to %s ..." % params['k'])
k = np.ones(N) * params['k']
else:
default_val = 5.
print("setting default values [%s] to k ..." % default_val)
k = np.ones(N) * default_val
row, col = sparse_A
def diff_mutual(X, t):
# dx_i(t)/dt = b_i+x_i(1 - x_i/k_i)(x_i/c_i - 1)+\sum_{j=1}^{n}A_{i,j}(x_i*x_j)/(d_i + e_i*x_i + h_j*x_j)
X_j = X.reshape(-1, x_dim)[row]
X_i = X.reshape(-1, x_dim)[col]
# we do not know the scatter_sum in numpy package, so we use scatter_sum in torch instead.
dX = b.reshape(-1, 1) + \
X.reshape(-1, x_dim) * (1. - X.reshape(-1, x_dim) / k.reshape(-1, 1)) * (
X.reshape(-1, x_dim) / c.reshape(-1, 1) - 1) + \
scatter_sum(torch.from_numpy(
(X_i * X_j) / (d.reshape(-1, 1)[col] + e.reshape(-1, 1)[col] * X_i + h.reshape(-1, 1)[row] * X_j)
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
return dX.reshape(-1)
t_range = np.arange(t_start, t_end + t_inc, t_inc)
New_X = spi.odeint(diff_mutual, X.reshape(-1), t_range)
if diff_flag:
return New_X.reshape(len(t_range), N, x_dim), \
np.gradient(New_X, axis=0, edge_order=1).reshape(len(t_range), N, x_dim), \
t_range.reshape(-1, 1)
else:
return New_X.reshape(len(t_range), N, x_dim), t_range.reshape(-1, 1)
def gene_regulatory_dynamics(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
"""
The gene regulatory dynamics governed by Michaelis-Menten equation
dx_i(t)/dt = -b_i*x_i^f + \sum_{j=1}^{n}A_{i,j}*x_j^h/(x_j^h + 1)
where the first term models degradation when f = 1 or dimerization when f = 2, and the second term
captures genetic activation tuned by the Hill coefficient h [1, 2].
[1] Uri Alon. 2006. An introduction to systems biology: design principles of biological circuits.
Chapman and Hall/CRC.
[2] Jianxi Gao, Baruch Barzel, and Albert-László Barabási. 2016. Universal resilience patterns in
complex networks. Nature 530, 7590 (2016), 307.
input:
X: [N, d]
sparse_A: [row, col], which means row -> col
return X: [steps, N, d]
"""
N, x_dim = X.shape
if 'b' in params:
print("setting b to %s ..." % params['b'])
b = np.ones(N) * params['b']
else:
default_val = 2. # [0.5,2.]
print("setting default values [%s] to b ..." % default_val)
b = np.ones(N) * default_val
if 'f' in params:
print("setting f to %s ..." % params['f'])
f = params['f']
else:
default_val = 1.
print("setting default values [%s] to f ..." % default_val)
f = default_val
if 'h' in params:
print("setting h to %s ..." % params['h'])
h = params['h']
else:
default_val = 2.
print("setting default values [%s] to h ..." % default_val)
h = default_val
row, col = sparse_A
def diff_gene(X, t):
# dx_i(t)/dt = -b_i*x_i^f + \sum_{j=1}^{n}A_{i,j}*x_j^h/(x_j^h + 1)
X_j = X.reshape(-1, x_dim)[row]
# we do not know the scatter_sum in numpy package, so we use scatter_sum in torch instead.
dX = -b.reshape(-1, 1) * (X.reshape(-1, x_dim) ** f) + \
scatter_sum(torch.from_numpy(
(X_j ** h) / (X_j ** h + 1)
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
return dX.reshape(-1)
t_range = np.arange(t_start, t_end + t_inc, t_inc)
New_X = spi.odeint(diff_gene, X.reshape(-1), t_range)
if diff_flag:
return New_X.reshape(len(t_range), N, x_dim), \
np.gradient(New_X, axis=0, edge_order=1).reshape(len(t_range), N, x_dim), \
t_range.reshape(-1, 1)
else:
return New_X.reshape(len(t_range), N, x_dim), t_range.reshape(-1, 1)
def combination_dynamics(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
N, x_dim = X.shape
row, col = sparse_A
# a dynamics
if 'a_K' in params:
print("setting a_K to %s ..." % params['a_K'])
a_K = np.ones(N) * params['a_K']
else:
print("setting default values [0.1] to a_K ...")
a_K = np.ones(N) * 1.
# b dynamics
if 'b_b' in params:
print("setting b_b to %s ..." % params['b_b'])
b_b = np.ones(N) * params['b_b']
else:
default_val = 1.
print("setting default values [%s] to b_b ..." % default_val)
b_b = np.ones(N) * default_val
if 'b_c' in params:
print("setting b_c to %s ..." % params['b_c'])
b_c = np.ones(N) * params['b_c']
else:
default_val = 1.
print("setting default values [%s] to b_c ..." % default_val)
b_c = np.ones(N) * default_val
if 'b_d' in params:
print("setting b_d to %s ..." % params['b_d'])
b_d = np.ones(N) * params['b_d']
else:
default_val = 5.
print("setting default values [%s] to b_d ..." % default_val)
b_d = np.ones(N) * default_val
if 'b_e' in params:
print("setting b_e to %s ..." % params['b_e'])
b_e = np.ones(N) * params['b_e']
else:
default_val = 0.9
print("setting default values [%s] to b_e ..." % default_val)
b_e = np.ones(N) * default_val
if 'b_h' in params:
print("setting b_h to %s ..." % params['b_h'])
b_h = np.ones(N) * params['b_h']
else:
default_val = 0.1
print("setting default values [%s] to b_h ..." % default_val)
b_h = np.ones(N) * default_val
if 'b_k' in params:
print("setting b_k to %s ..." % params['b_k'])
b_k = np.ones(N) * params['b_k']
else:
default_val = 5.
print("setting default values [%s] to b_k ..." % default_val)
b_k = np.ones(N) * default_val
# c dynamics
if 'c_b' in params:
print("setting c_b to %s ..." % params['c_b'])
c_b = np.ones(N) * params['c_b']
else:
default_val = 2. # [0.5,2.]
print("setting default values [%s] to c_b ..." % default_val)
c_b = np.ones(N) * default_val
if 'c_f' in params:
print("setting c_f to %s ..." % params['c_f'])
c_f = params['c_f']
else:
default_val = 1.
print("setting default values [%s] to c_f ..." % default_val)
c_f = default_val
if 'c_h' in params:
print("setting c_h to %s ..." % params['c_h'])
c_h = params['c_h']
else:
default_val = 2.
print("setting default values [%s] to c_h ..." % default_val)
c_h = default_val
def diff_combination(X, t):
# dx_i/dt = k_{i,j} \sum_{j=1}^{n} A_{i,j}(x_j-x_i)
X_j = X.reshape(-1, x_dim)[row]
X_i = X.reshape(-1, x_dim)[col]
# we do not know the scatter_sum in numpy package, so we use scatter_sum in torch instead.
# a dyanmics
dX_a = a_K.reshape(-1, 1) * scatter_sum(torch.from_numpy(
X_j - X_i
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
# b dynamics
dX_b = b_b.reshape(-1, 1) + \
X.reshape(-1, x_dim) * (1. - X.reshape(-1, x_dim) / b_k.reshape(-1, 1)) * (
X.reshape(-1, x_dim) / b_c.reshape(-1, 1) - 1) + \
scatter_sum(torch.from_numpy(
(X_i * X_j) / (
b_d.reshape(-1, 1)[col] + b_e.reshape(-1, 1)[col] * X_i + b_h.reshape(-1, 1)[row] * X_j)
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
# c dynamics
dX_c = -c_b.reshape(-1, 1) * (X.reshape(-1, x_dim) ** c_f) + \
scatter_sum(torch.from_numpy(
(X_j ** c_h) / (X_j ** c_h + 1)
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
dX = dX_a + dX_b + dX_c
return dX.reshape(-1)
t_range = np.arange(t_start, t_end + t_inc, t_inc)
New_X = spi.odeint(diff_combination, X.reshape(-1), t_range)
if diff_flag:
return New_X.reshape(len(t_range), N, x_dim), \
np.gradient(New_X, axis=0, edge_order=1).reshape(len(t_range), N, x_dim), \
t_range.reshape(-1, 1)
else:
return New_X.reshape(len(t_range), N, x_dim), t_range.reshape(-1, 1)
def combination_dynamics_vary_coeff(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
N, x_dim = X.shape
row, col = sparse_A
# coeff
if 'lam_1' in params:
print("setting lam_1 to %s ..." % params['lam_1'])
lam_1 = params['lam_1']
else:
print("setting default values [1/3] to lam_1 ...")
lam_1 = 1. / 3.
if 'lam_2' in params:
print("setting lam_2 to %s ..." % params['lam_2'])
lam_2 = params['lam_2']
else:
print("setting default values [1/3] to lam_2 ...")
lam_2 = 1. / 3.
if 'lam_3' in params:
print("setting lam_3 to %s ..." % params['lam_3'])
lam_3 = params['lam_3']
else:
print("setting default values [1/3] to lam_3 ...")
lam_3 = 1. / 3.
# a dynamics
if 'a_K' in params:
print("setting a_K to %s ..." % params['a_K'])
a_K = np.ones(N) * params['a_K']
else:
print("setting default values [0.1] to a_K ...")
a_K = np.ones(N) * 1.
# b dynamics
if 'b_b' in params:
print("setting b_b to %s ..." % params['b_b'])
b_b = np.ones(N) * params['b_b']
else:
default_val = 1.
print("setting default values [%s] to b_b ..." % default_val)
b_b = np.ones(N) * default_val
if 'b_c' in params:
print("setting b_c to %s ..." % params['b_c'])
b_c = np.ones(N) * params['b_c']
else:
default_val = 1.
print("setting default values [%s] to b_c ..." % default_val)
b_c = np.ones(N) * default_val
if 'b_d' in params:
print("setting b_d to %s ..." % params['b_d'])
b_d = np.ones(N) * params['b_d']
else:
default_val = 5.
print("setting default values [%s] to b_d ..." % default_val)
b_d = np.ones(N) * default_val
if 'b_e' in params:
print("setting b_e to %s ..." % params['b_e'])
b_e = np.ones(N) * params['b_e']
else:
default_val = 0.9
print("setting default values [%s] to b_e ..." % default_val)
b_e = np.ones(N) * default_val
if 'b_h' in params:
print("setting b_h to %s ..." % params['b_h'])
b_h = np.ones(N) * params['b_h']
else:
default_val = 0.1
print("setting default values [%s] to b_h ..." % default_val)
b_h = np.ones(N) * default_val
if 'b_k' in params:
print("setting b_k to %s ..." % params['b_k'])
b_k = np.ones(N) * params['b_k']
else:
default_val = 5.
print("setting default values [%s] to b_k ..." % default_val)
b_k = np.ones(N) * default_val
# c dynamics
if 'c_b' in params:
print("setting c_b to %s ..." % params['c_b'])
c_b = np.ones(N) * params['c_b']
else:
default_val = 2. # [0.5,2.]
print("setting default values [%s] to c_b ..." % default_val)
c_b = np.ones(N) * default_val
if 'c_f' in params:
print("setting c_f to %s ..." % params['c_f'])
c_f = params['c_f']
else:
default_val = 1.
print("setting default values [%s] to c_f ..." % default_val)
c_f = default_val
if 'c_h' in params:
print("setting c_h to %s ..." % params['c_h'])
c_h = params['c_h']
else:
default_val = 2.
print("setting default values [%s] to c_h ..." % default_val)
c_h = default_val
def diff_combination(X, t):
# dx_i/dt = k_{i,j} \sum_{j=1}^{n} A_{i,j}(x_j-x_i)
X_j = X.reshape(-1, x_dim)[row]
X_i = X.reshape(-1, x_dim)[col]
# we do not know the scatter_sum in numpy package, so we use scatter_sum in torch instead.
# a dyanmics
dX_a = a_K.reshape(-1, 1) * scatter_sum(torch.from_numpy(
X_j - X_i
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
# b dynamics
dX_b = b_b.reshape(-1, 1) + \
X.reshape(-1, x_dim) * (1. - X.reshape(-1, x_dim) / b_k.reshape(-1, 1)) * (
X.reshape(-1, x_dim) / b_c.reshape(-1, 1) - 1) + \
scatter_sum(torch.from_numpy(
(X_i * X_j) / (
b_d.reshape(-1, 1)[col] + b_e.reshape(-1, 1)[col] * X_i + b_h.reshape(-1, 1)[row] * X_j)
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
# c dynamics
dX_c = -c_b.reshape(-1, 1) * (X.reshape(-1, x_dim) ** c_f) + \
scatter_sum(torch.from_numpy(
(X_j ** c_h) / (X_j ** c_h + 1)
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
dX = lam_1 * dX_a + lam_2 * dX_b + lam_3 * dX_c
return dX.reshape(-1)
t_range = np.arange(t_start, t_end + t_inc, t_inc)
New_X = spi.odeint(diff_combination, X.reshape(-1), t_range)
if diff_flag:
return New_X.reshape(len(t_range), N, x_dim), \
np.gradient(New_X, axis=0, edge_order=1).reshape(len(t_range), N, x_dim), \
t_range.reshape(-1, 1)
else:
return New_X.reshape(len(t_range), N, x_dim), t_range.reshape(-1, 1)
def vary_dynamics_with_vary_type_and_coeff(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
N, x_dim = X.shape
row, col = sparse_A
# choose_list
if 'choose_1' in params:
print("setting choose_1 to %s ..." % params['choose_1'])
choose_1 = params['choose_1']
else:
print("setting default values [0] to choose_1 ...")
choose_1 = 0
if 'choose_2' in params:
print("setting choose_2 to %s ..." % params['choose_2'])
choose_2 = params['choose_2']
else:
print("setting default values [0] to choose_2 ...")
choose_2 = 0
# for first half
# a dynamics
if 'a_K_1' in params:
print("setting a_K_1 to %s ..." % params['a_K_1'])
a_K_1 = np.ones(N) * params['a_K_1']
else:
print("setting default values [0.1] to a_K_1 ...")
a_K_1 = np.ones(N) * 1.
# b dynamics
if 'b_b_1' in params:
print("setting b_b_1 to %s ..." % params['b_b_1'])
b_b_1 = np.ones(N) * params['b_b_1']
else:
default_val = 1.
print("setting default values [%s] to b_b_1 ..." % default_val)
b_b_1 = np.ones(N) * default_val
if 'b_c_1' in params:
print("setting b_c_1 to %s ..." % params['b_c_1'])
b_c_1 = np.ones(N) * params['b_c_1']
else:
default_val = 1.
print("setting default values [%s] to b_c_1 ..." % default_val)
b_c_1 = np.ones(N) * default_val
if 'b_d_1' in params:
print("setting b_d_1 to %s ..." % params['b_d_1'])
b_d_1 = np.ones(N) * params['b_d_1']
else:
default_val = 5.
print("setting default values [%s] to b_d_1 ..." % default_val)
b_d_1 = np.ones(N) * default_val
if 'b_e_1' in params:
print("setting b_e_1 to %s ..." % params['b_e_1'])
b_e_1 = np.ones(N) * params['b_e_1']
else:
default_val = 0.9
print("setting default values [%s] to b_e_1 ..." % default_val)
b_e_1 = np.ones(N) * default_val
if 'b_h_1' in params:
print("setting b_h_1 to %s ..." % params['b_h_1'])
b_h_1 = np.ones(N) * params['b_h_1']
else:
default_val = 0.1
print("setting default values [%s] to b_h_1 ..." % default_val)
b_h_1 = np.ones(N) * default_val
if 'b_k_1' in params:
print("setting b_k to %s ..." % params['b_k_1'])
b_k_1 = np.ones(N) * params['b_k_1']
else:
default_val = 5.
print("setting default values [%s] to b_k_1 ..." % default_val)
b_k_1 = np.ones(N) * default_val
# c dynamics
if 'c_b_1' in params:
print("setting c_b to %s ..." % params['c_b_1'])
c_b_1 = np.ones(N) * params['c_b_1']
else:
default_val = 2. # [0.5,2.]
print("setting default values [%s] to c_b_1 ..." % default_val)
c_b_1 = np.ones(N) * default_val
if 'c_f_1' in params:
print("setting c_f_1 to %s ..." % params['c_f_1'])
c_f_1 = params['c_f_1']
else:
default_val = 1.
print("setting default values [%s] to c_f_1 ..." % default_val)
c_f_1 = default_val
if 'c_h_1' in params:
print("setting c_h to %s ..." % params['c_h_1'])
c_h_1 = params['c_h_1']
else:
default_val = 2.
print("setting default values [%s] to c_h_1 ..." % default_val)
c_h_1 = default_val
# for second half
# a dynamics
if 'a_K_2' in params:
print("setting a_K_2 to %s ..." % params['a_K_2'])
a_K_2 = np.ones(N) * params['a_K_2']
else:
print("setting default values [0.1] to a_K_2 ...")
a_K_2 = np.ones(N) * 1.
# b dynamics
if 'b_b_2' in params:
print("setting b_b_2 to %s ..." % params['b_b_2'])
b_b_2 = np.ones(N) * params['b_b_2']
else:
default_val = 1.
print("setting default values [%s] to b_b_2 ..." % default_val)
b_b_2 = np.ones(N) * default_val
if 'b_c_2' in params:
print("setting b_c_2 to %s ..." % params['b_c_2'])
b_c_2 = np.ones(N) * params['b_c_2']
else:
default_val = 1.
print("setting default values [%s] to b_c_2 ..." % default_val)
b_c_2 = np.ones(N) * default_val
if 'b_d_2' in params:
print("setting b_d_2 to %s ..." % params['b_d_2'])
b_d_2 = np.ones(N) * params['b_d_2']
else:
default_val = 5.
print("setting default values [%s] to b_d_2 ..." % default_val)
b_d_2 = np.ones(N) * default_val
if 'b_e_2' in params:
print("setting b_e_2 to %s ..." % params['b_e_2'])
b_e_2 = np.ones(N) * params['b_e_2']
else:
default_val = 0.9
print("setting default values [%s] to b_e_2 ..." % default_val)
b_e_2 = np.ones(N) * default_val
if 'b_h_2' in params:
print("setting b_h_2 to %s ..." % params['b_h_2'])
b_h_2 = np.ones(N) * params['b_h_2']
else:
default_val = 0.1
print("setting default values [%s] to b_h_2 ..." % default_val)
b_h_2 = np.ones(N) * default_val
if 'b_k_2' in params:
print("setting b_k_2 to %s ..." % params['b_k_2'])
b_k_2 = np.ones(N) * params['b_k_2']
else:
default_val = 5.
print("setting default values [%s] to b_k_2 ..." % default_val)
b_k_2 = np.ones(N) * default_val
# c dynamics
if 'c_b_2' in params:
print("setting c_b_2 to %s ..." % params['c_b_2'])
c_b_2 = np.ones(N) * params['c_b_2']
else:
default_val = 2. # [0.5,2.]
print("setting default values [%s] to c_b_2 ..." % default_val)
c_b_2 = np.ones(N) * default_val
if 'c_f_2' in params:
print("setting c_f_2 to %s ..." % params['c_f_2'])
c_f_2 = params['c_f_2']
else:
default_val = 1.
print("setting default values [%s] to c_f_2 ..." % default_val)
c_f_2 = default_val
if 'c_h_2' in params:
print("setting c_h_2 to %s ..." % params['c_h_2'])
c_h_2 = params['c_h_2']
else:
default_val = 2.
print("setting default values [%s] to c_h_2 ..." % default_val)
c_h_2 = default_val
def diff_combination(X, t):
# dx_i/dt = k_{i,j} \sum_{j=1}^{n} A_{i,j}(x_j-x_i)
X_j = X.reshape(-1, x_dim)[row]
X_i = X.reshape(-1, x_dim)[col]
if t < 0.5:
choose_dynamics = choose_1
a_K = a_K_1
b_b = b_b_1
b_c = b_c_1
b_d = b_d_1
b_e = b_e_1
b_h = b_h_1
b_k = b_k_1
c_b = c_b_1
c_f = c_f_1
c_h = c_h_1
else:
choose_dynamics = choose_2
a_K = a_K_2
b_b = b_b_2
b_c = b_c_2
b_d = b_d_2
b_e = b_e_2
b_h = b_h_2
b_k = b_k_2
c_b = c_b_2
c_f = c_f_2
c_h = c_h_2
if choose_dynamics == 0:
# we do not know the scatter_sum in numpy package, so we use scatter_sum in torch instead.
# a dyanmics
dX = a_K.reshape(-1, 1) * scatter_sum(torch.from_numpy(
X_j - X_i
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
elif choose_dynamics == 1:
# b dynamics
dX = b_b.reshape(-1, 1) + \
X.reshape(-1, x_dim) * (1. - X.reshape(-1, x_dim) / b_k.reshape(-1, 1)) * (
X.reshape(-1, x_dim) / b_c.reshape(-1, 1) - 1) + \
scatter_sum(torch.from_numpy(
(X_i * X_j) / (b_d.reshape(-1, 1)[col] + b_e.reshape(-1, 1)[col] * X_i + b_h.reshape(-1, 1)[
row] * X_j)
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
elif choose_dynamics == 2:
# c dynamics
dX = -c_b.reshape(-1, 1) * (X.reshape(-1, x_dim) ** c_f) + \
scatter_sum(torch.from_numpy(
(X_j ** c_h) / (X_j ** c_h + 1)
), torch.from_numpy(col).long(), dim=0, dim_size=X.shape[0]).numpy()
else:
print('Wrong choose_dynamics [%s]' % choose_dynamics)
dX = dX
return dX.reshape(-1)
t_range = np.arange(t_start, t_end + t_inc, t_inc)
New_X = spi.odeint(diff_combination, X.reshape(-1), t_range)
if diff_flag:
return New_X.reshape(len(t_range), N, x_dim), \
np.gradient(New_X, axis=0, edge_order=1).reshape(len(t_range), N, x_dim), \
t_range.reshape(-1, 1)
else:
return New_X.reshape(len(t_range), N, x_dim), t_range.reshape(-1, 1)
def opinion_dynamics(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
N, x_dim = X.shape
if 'a' in params:
print('setting a to %s ...' % params['a'])
a = params['a']
else:
default_val = 30. # [30, 60]
print('setting default values [%s] to a ...' % default_val)
a = default_val
if 'b' in params:
print('setting b to %s ...' % params['b'])
b = params['b']
else:
default_val = 3. # [3, 6]
print('setting default values [%s] to b ...' % default_val)
b = default_val
if 'c' in params:
print('setting c to %s ...' % params['c'])
c = params['c']
else:
default_val = 0.7 # [0.1, 0.9]
print('setting default values [%s] to c ...' % default_val)
c = default_val
row, col = sparse_A # sparse_A in opinion_dynamics should be a fully-connected graph.
def diff_opinion(X, t):
# dx_i/dt = \frac{1}{n} \sum_{j=1}^{n} \phi(||x_j-x_i||)(x_j-x_i)
# \phi(r) := 1 0 <= r < 1/sqrt(2);
# 0.1 1/sqrt(2) <= r < 1;
# 0 1 <= r.
X_j = X.reshape(-1, x_dim)[row]
X_i = X.reshape(-1, x_dim)[col]
def phi(r):
new_r = np.zeros_like(r)
new_r[(r >= 0) & (r < c)] = a
new_r[(r >= c) & (r < 1)] = b
new_r[r >= 1] = 0.
return new_r
dX = phi(np.linalg.norm(x=X_j - X_i, ord=2, axis=1, keepdims=True)) * (X_j - X_i)
dX = (1 / N) * np.sum(dX.reshape(N, N, x_dim), axis=0)
return dX.reshape(-1)
t_range = np.arange(t_start, t_end + t_inc, t_inc)
New_X = spi.odeint(diff_opinion, X.reshape(-1), t_range)
if diff_flag:
return New_X.reshape(len(t_range), N, x_dim), \
np.gradient(New_X, axis=0, edge_order=1).reshape(len(t_range), N, x_dim), \
t_range.reshape(-1, 1)
else:
return New_X.reshape(len(t_range), N, x_dim), t_range.reshape(-1, 1)
def opinion_dynamics_Baumann2021(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
N, x_dim = X.shape
if 'a' in params:
print('setting a to %s ...' % params['a'])
a = params['a']
else:
default_val = 0.5 # [0.01, 0.5]
print('setting default values [%s] to a ...' % default_val)
a = default_val
if 'k' in params:
print('setting k to %s ...' % params['k'])
k = params['k']
else:
default_val = 1. # [0.5, 1.5]
print('setting default values [%s] to k ...' % default_val)
k = default_val
if 'c' in params:
print('setting c to %s ...' % params['c'])
c = params['c']
else:
default_val = 1. # [1., 3.]
print('setting default values [%s] to c ...' % default_val)
c = default_val
row, col = sparse_A # sparse_A in opinion_dynamics should be a fully-connected graph.
def diff_opinion(X, t):
# dx_i/dt = -x_i \sum_{j=1}^{n} A_i_j*tanh(a*x_j)
X_j = X.reshape(-1, x_dim)[row]
# X_i = X.reshape(-1, x_dim)[col]
# we do not know the scatter_sum in numpy package, so we use scatter_sum in torch instead.
dX = -c * X.reshape(-1, x_dim) + k * scatter_sum(torch.from_numpy(np.tanh(a * X_j)),
torch.from_numpy(col).long(), dim=0,
dim_size=X.shape[0]).numpy()
return dX.reshape(-1)
t_range = np.arange(t_start, t_end + t_inc, t_inc)
New_X = spi.odeint(diff_opinion, X.reshape(-1), t_range)
if diff_flag:
return New_X.reshape(len(t_range), N, x_dim), \
np.gradient(New_X, axis=0, edge_order=1).reshape(len(t_range), N, x_dim), \
t_range.reshape(-1, 1)
else:
return New_X.reshape(len(t_range), N, x_dim), t_range.reshape(-1, 1)
def opinion_dynamics_Baumann2021_2topic(X, sparse_A,
t_start=0, t_end=100, t_inc=1, diff_flag=False, **params):
N, x_dim = X.shape
if 'a1' in params:
print('setting a1 to %s ...' % params['a1'])
a1 = params['a1']
else:
default_val = 0.05 # [0.05, 3.]
print('setting default values [%s] to a1 ...' % default_val)
a1 = default_val
if 'a2' in params:
print('setting a2 to %s ...' % params['a2'])
a2 = params['a2']
else:
default_val = 3. # [0.05, 3.]
print('setting default values [%s] to a2 ...' % default_val)
a2 = default_val
if 'k' in params:
print('setting k to %s ...' % params['k'])
k = params['k']
else:
default_val = 3 # [1, 3]
print('setting default values [%s] to k ...' % default_val)
k = default_val
if 'c' in params:
print('setting c to %s ...' % params['c'])
c = params['c']
else:
default_val = 1. # [1., 3.]
print('setting default values [%s] to c ...' % default_val)
c = default_val
if 'd' in params:
print('setting d to %s ...' % params['d'])
d = params['d']
else:
# default_val = 3 * np.pi / 4. # [0, pi/2]
default_val = np.pi / 2. # [0, pi/2]
print('setting default values [%s] to d ...' % default_val)
d = default_val
row, col = sparse_A # sparse_A in opinion_dynamics should be a fully-connected graph.
def diff_opinion(X, t):
# dx_i/dt = -x_i \sum_{j=1}^{n} A_i_j*tanh(a*x_j)
X_j = X.reshape(-1, x_dim)[row]
# X_i = X.reshape(-1, x_dim)[col]
# we do not know the scatter_sum in numpy package, so we use scatter_sum in torch instead.
dX_col1 = -c * X.reshape(-1, x_dim)[:, 0] + k * scatter_sum(torch.from_numpy(