-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path1_landscape_creation.py
304 lines (265 loc) · 11.8 KB
/
1_landscape_creation.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
# -*- coding: utf-8 -*-
'''
Created on Wed Jun 13 15:42:13 2018
Updated on Tue May 21 08:49:00 2019
@author: Maciej Workiewicz
The code has been tested on Python 2.7 and 3.6 and higher
'''
print('''
----------------------------------------------------
Running Module 1: NK landscape creation and analysis
----------------------------------------------------
''')
# COMMENTS
# =============================================================================
# This code generates NK landscapes for a specific interaction matrix (IM) and
# number of interactions between the decision variables (K). It has been created
# for NK landscapes with N=6, but it can be adapted to for other values of N.
# You can choose the type of an interaction matrix by setting variable
# "which_imatrix" to:
# 1 - for a random interaction matrix (IM)
# 2 - for a modular (block-diagonal) IM
# 3 - for a nearly modular IM
# 4 - for a diagonal IM
# 5 - highly influential IM (Baumann & Siggelkow 2013)
# 6 - highly dependent IM (Baumann & Siggelkow 2013)
# 7 - Local IM (Rivkin and Siggelkow, 2007)
#
# For the random IM the user can also set K from 0 to N-1 to tune the number of
# interactions.
# =============================================================================
# *** IMPORTED PACKAGES ***
import numpy as np
import itertools
import os # new
from time import time
import matplotlib.pyplot as plt
import random
start = time() # starts the clock used to measure the execution speed
# *** MODEL INPUTS ****************************************************
# NK landscape parameters -----------------------------------------
N = 6 # number of detailed decisions per lower level landscape |
i = 1000 # we will generate 1000 NK landscapes to begin with |
# -----------------------------------------------------------------
# You can change the following variables:
which_imatrix = 1 # defines the type of an interaction matrix
# choose 1 for random, 2 for modular, 3 for nearly modular,
# 4 for diagonal, 5 for highly influential, and
# 6 for highly dependent, 7 local (see below)
K = 5 # only has an effect when you choose the random interaction matrix (1)
# set to 2 for other interaction matrices
# *** GENERATING INTERACTION MATRICES ***************************************
def imatrix_rand():
'''
This function takes the number of N elements and K interdependencies
and creates a random interaction matrix.
'''
Int_matrix_rand = np.zeros((N, N))
for aa1 in np.arange(N):
Indexes_1 = list(range(N))
Indexes_1.remove(aa1) # remove self
np.random.shuffle(Indexes_1)
Indexes_1.append(aa1)
Chosen_ones = Indexes_1[-(K+1):] # this takes the last K+1 indexes
for aa2 in Chosen_ones:
Int_matrix_rand[aa1, aa2] = 1 # we turn on the interactions with K other variables
return(Int_matrix_rand)
#==============================================================================
# Below are the other three types of interaction matrices.
# You can edit those if you want to check other petterns of interactions.
#==============================================================================
if which_imatrix == 2: # MODULAR
K = 2 # set to the average value
Int_matrix = \
np.array([
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1]
])
elif which_imatrix == 3: # NEARLY MODULAR
K = 2 # set to the average value
Int_matrix = \
np.array([
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1]
])
elif which_imatrix == 4: # DIAGONAL
K = 2 # set to average value and updated code below to poke three random holes
Int_matrix4 = \
np.array([
[1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1]
])
elif which_imatrix == 5: # HIGHLY INFLUENTIAL Baumann & Siggelkow 2013
K = 2 # set to the average value
Int_matrix = \
np.array([
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 1, 0, 1, 0, 0],
[1, 1, 0, 0, 1, 0],
[1, 1, 0, 0, 0, 1]
])
elif which_imatrix == 6: # HIGHLY DEPENDENT Baumann & Siggelkow 2013
K = 2 # set to the average value
Int_matrix = \
np.array([
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]
])
elif which_imatrix == 7: # LOCAL Rivkin and Siggelkow, 2007
K = 2 # set to the average value
Int_matrix = \
np.array([
[1, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1],
[1, 0, 0, 0, 1, 1]
])
# *** NK GENERATING FUNCTIONS ***********************************************
def calc_fit(NK_land_, inter_m, Current_position, Power_key_):
'''
Takes the landscape and a given combination and returns a vector of fitness
values for the vector of the N decision variables.
'''
Fit_vector = np.zeros(N)
for ad1 in np.arange(N):
Fit_vector[ad1] = NK_land_[np.sum(Current_position * inter_m[ad1]
* Power_key_), ad1]
return(Fit_vector)
def comb_and_values(NK_land_, Power_key_, inter_m):
'''
Calculates values for all combinations on the landscape. The resulting
array contains:
- the first columns indexed from 0 to N-1 are for each of the combinations
- columns indexed from N to 2*N-1 are for the fit value (vector) of those combinations
- the column indexed 2N is for the total fit (average of the entire vector)
- column indexed 2N+1 is a dummy, with 1 indicating a local peak
- the last column is a dummy, with 1 indicating the global peak
'''
Comb_and_value = np.zeros((2**N, N*2+3)) # to capture the results
c1 = 0 # starting counter for location
for c2 in itertools.product(range(2), repeat=N):
# this takes time so be carefull with landscapes of bigger size
Combination1 = np.array(c2) # taking each combination
fit_1 = calc_fit(NK_land_, inter_m, Combination1, Power_key_)
Comb_and_value[c1, :N] = Combination1 # combination and values
Comb_and_value[c1, N:2*N] = fit_1
Comb_and_value[c1, 2*N] = np.mean(fit_1)
c1 = c1 + 1
for c3 in np.arange(2**N): # now let's see if it is a local peak
loc_p = 1 # first, assume it is
for c4 in np.arange(N): # check the local neighbourhood
new_comb = Comb_and_value[c3, :N].copy().astype(int)
new_comb[c4] = abs(new_comb[c4] - 1)
if ((Comb_and_value[c3, 2*N] <
Comb_and_value[np.sum(new_comb*Power_key_), 2*N])):
loc_p = 0 # if smaller than the neighbour, then it is not peak
Comb_and_value[c3, 2*N+1] = loc_p
max_ind = np.argmax(Comb_and_value[:, 2*N])
Comb_and_value[max_ind, 2*N+2] = 1
return(Comb_and_value)
# *** GENERATING THE NK LANDSCAPES ******************************************
Power_key = np.power(2, np.arange(N - 1, -1, -1)) # used to find addresses on the landscape
Landscape_data = np.zeros((i, 2**N, N*2+3)) # we prepare an array to receive the data
for i_1 in np.arange(i):
'''
Now we create the landscapes
'''
if which_imatrix==1:
Int_matrix = imatrix_rand().astype(int)
elif which_imatrix==4: # diagonal
'''
The code below serves to poke three holes in the diagonal IM so that
K=2. It is a little bit cumbersome but does the job :-)
Note that it only works with N=6
'''
Int_matrix = Int_matrix4.copy()
id_change = random.sample(range(15), 3)
for index in id_change:
if index == 0:
Int_matrix[1,0] = 0
elif index == 1:
Int_matrix[2,0] = 0
elif index == 2:
Int_matrix[2,1] = 0
elif index == 3:
Int_matrix[3,0] = 0
elif index == 4:
Int_matrix[3,1] = 0
elif index == 5:
Int_matrix[3,2] = 0
elif index == 6:
Int_matrix[4,0] = 0
elif index == 7:
Int_matrix[4,1] = 0
elif index == 8:
Int_matrix[4,2] = 0
elif index == 9:
Int_matrix[4,3] = 0
elif index == 10:
Int_matrix[5,0] = 0
elif index == 11:
Int_matrix[5,1] = 0
elif index == 12:
Int_matrix[5,2] = 0
elif index == 13:
Int_matrix[5,3] = 0
elif index == 14:
Int_matrix[5,4] = 0
NK_land = np.random.rand(2**N, N) # this is a table of random U(0,1) numbers
# Now it is time to survey the topography of our NK landscape
Landscape_data[i_1] = comb_and_values(NK_land, Power_key, Int_matrix)
# *** CALCULATING SUMMARY STATISTICS ****************************************
number_of_peaks = np.zeros(i)
max_values = np.zeros(i)
min_values = np.zeros(i)
for i_2 in np.arange(i):
number_of_peaks[i_2] = np.sum(Landscape_data[i_2, :, 2*N+1])
max_values[i_2] = np.max(Landscape_data[i_2, :, 2*N])
min_values[i_2] = np.min(Landscape_data[i_2, :, 2*N])
# Let's print some summary statistics of our sample of NK landscapes
print('Summary statistics for IMatrix: ' + str(which_imatrix) + ' K=' + str(K))
print('average number of peaks: ' + str(np.mean(number_of_peaks)))
print('maximum number of peaks: ' + str(np.max(number_of_peaks)))
print('minimum number of peaks: ' + str(np.min(number_of_peaks)))
print('average maximum value: ' + str(np.mean(max_values)))
print('average minimum value: ' + str(np.mean(min_values)))
# plot histogram of the number of local peaks in our sample
plt.figure(1, facecolor='white', figsize=(8, 6), dpi=150) # for screens with
# higher resolution change dpi to 150 or 200. For normal use 75.
plt.hist(number_of_peaks, bins=20, range=(1, 20), color='dodgerblue', edgecolor='black') # adjust if necessary
plt.title('Distribution of the number of peaks', size=12)
plt.xlabel('number of peaks', size=10)
plt.ylabel('frequency', size=10)
# *** SAVING THE LANDSCAPES AS A BINARY FILE FOR FUTURE RETRIEVAL ************
#==============================================================================
# If you are saving files on a Mac, change the double back-slash \\ into a
# single slash /
#==============================================================================
file_name = os.path.expanduser("~") # we will save it in your home folder
if not os.path.exists(file_name + '\\NK_workshop\\'):
os.makedirs(file_name + '\\NK_workshop\\')
np.save(file_name + '\\NK_workshop\\NK_land_type_' + str(which_imatrix) +
'_K_' + str(K) + '_i_' + str(i) + '.npy', Landscape_data)
elapsed_time = time() - start
print('time: ' + str("%.2f" % elapsed_time) + ' sec')
# END OF LINE