Created
May 29, 2020 13:40
-
-
Save axsk/8a007c383851dbbbe909042897c8e5ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
t_adopt = .5 | |
p_adopt = .9 | |
p_expl = .1 | |
def timestep(state, influence): | |
influenced = (np.abs(np.dot(influence,state)) > t_adopt) | |
r = np.random.rand(np.size(state)) | |
# vectorized: 415us | |
#state = logic(state, influenced, r) | |
# loop: 737us | |
#for i in range(state.size): | |
# state[i] = logic1(state[i], influenced[i], r[i]) | |
#map: 489 us | |
state = list(map(logic1, state, influenced, r)) | |
#numba 180 us | |
#state = logicnumba(state, influenced, r) | |
return state | |
def init(n): | |
s = np.random.rand(n) > .5 | |
I = np.random.rand(n,n) | |
return s, I | |
def run(n = 1000, iter=1000): | |
s, I = init(n) | |
for i in range(iter): | |
s = timestep(s,I) | |
return s | |
import numba | |
@numba.vectorize | |
def logic(state, influenced, rand): | |
if influenced: | |
if p_adopt > rand: | |
state = not(state) | |
else: | |
if p_expl > rand: | |
state = not(state) | |
return state | |
def logic1(state, influenced, rand): | |
if influenced: | |
if p_adopt > rand: | |
state = not(state) | |
else: | |
if p_expl > rand: | |
state = not(state) | |
return state | |
logic = np.vectorize(logic1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment