Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1111111111111111 (Initial 16 species, choose 11 survivors)
- 0110110101101111 (11 species, choose 9 survivors)
- 0110110101100011 (9 species, choose 5 survivors)
- 0100100000100011 (End of simulation)
- import numpy as np
- def using_2D_matrix(nspp=1000, nscene=250):
- # define a matrix to hold the communities and
- # set the initial community
- m = np.zeros((nscene, nspp), dtype='bool_')
- m[0, ] = 1
- # loop over each extinction scene, looking up the indices
- # of live species and then selecting survivors
- for i in range(0, nscene - 1):
- candidates = np.where(m[i,])[0]
- n_surv = np.random.binomial(len(candidates), 0.99)
- surv = np.random.choice(candidates, size=n_surv, replace=False)
- m[i + 1, surv] = 1
- return m
- def using_dict_of_arrays(nspp=1000, nscene=250):
- # initialise a dictionary holding an array giving a
- # unique integer to each species
- m = {0: np.arange(nspp)}
- # loop over the scenes, selecting survivors
- for i in range(0, nscene - 1):
- n_surv = np.random.binomial(len(m[i]), 0.99)
- surv = np.random.choice(m[i], size=n_surv, replace=False)
- m[i + 1] = surv
- return m
- import timeit
- A = timeit.Timer(using_2D_matrix)
- A.timeit(100)
- # 1.6549
- B = timeit.Timer(using_dictionary_of_arrays)
- B.timeit(100)
- # 1.3580
- def using_bitarray(nspp=1000, nscene=250):
- # initialise the starting community
- m = {0: bitarray('1' * nspp)}
- for i in range(0, nscene):
- # pick how many die and which they are (fewer bits to swap)
- n_die = np.random.binomial(m[i].count(True), 0.01)
- unlucky = np.random.choice(m[i].search(bitarray('1')), size=n_die, replace=False)
- # clone the source community and kill some off
- m[i + 1] = bitarray(m[i])
- for s in unlucky:
- m[i + 1][s] = False
- return m
- C = timeit.Timer(using_bitarray)
- C.timeit(100)
- # 2.54035
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement