-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCA_FINAL.py
More file actions
406 lines (357 loc) · 11.5 KB
/
CA_FINAL.py
File metadata and controls
406 lines (357 loc) · 11.5 KB
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
import numpy as np
import random as rd
import seaborn as sns
import matplotlib.pyplot as plt
import pickle as pc
import copy as cp
import matplotlib.cm as cm
class CA:
"""
NOTES:
According to Topa the complexity of the river system is measured by the number of
channels and bifurcations
”Anastomosing river” term refers to river system that possess extremely complex
network of forking and joining channels
The new channels usually merge with the others, creating a complex network composed
of splitting and merging water channels and small lakes
"""
def __init__(
self, size, slope, mu, gamma, rho, time_limit, rand_lower=0.9999, rand_upper=1.00001,
branch_tresh=0.1, init_water=1, delta_water=0.0008, viz=False
):
self.size = size
self.slope = slope
self.time_limit = time_limit
self.branch_tresh = branch_tresh
self.init_water_level = init_water
# starting point in the middle of the grid
self.starting_column = int(self.size / 2)
self.delta_w = delta_water
self.terrain = np.zeros((size, size))
self.peat_bog = np.zeros((size, size))
self.nutrients = np.zeros((size, size))
self.cur_river_nr = 1
self.rivers = {}
self.path = np.zeros((size, size))
self.mu = mu # viscosity
self.gamma = gamma # gradient of nutrients concentration
self.rho = rho # proportionality coefficient
self.rand_lower = rand_lower
self.rand_upper = rand_upper
self.river_coors = set()
self.split_dict = {}
self.segment_dict = {}
self.segment_grid = self.path.copy()
self.viz=viz
if self.viz:
self.path_list = []
def moore_neighborhood(self, grid, i, j):
"""[summary]
Arguments:
grid {[type]} -- [description]
i {[type]} -- [description]
j {[type]} -- [description]
Returns:
[type] -- [description]
"""
if i == 0 and j == 0:
neighborhood = [
grid[i + 1, j + 1],
grid[i, j + 1],
grid[i + 1, j],
]
locations = [
[i + 1, j + 1],
[i, j + 1],
[i + 1, j]
]
elif i == 0 and j == (self.size - 1):
neighborhood = [
grid[i, j - 1],
grid[i + 1, j - 1],
grid[i + 1, j],
]
locations = [
[i, j - 1],
[i + 1, j - 1],
[i + 1, j],
]
elif i == 0 and 0 < j < (self.size - 1):
neighborhood = [
grid[i, j - 1],
grid[i, j + 1],
grid[i + 1, j - 1],
grid[i + 1, j],
grid[i + 1, j + 1],
]
locations = [
[i, j - 1],
[i, j + 1],
[i + 1, j - 1],
[i + 1, j],
[i + 1, j + 1],
]
elif i == (self.size - 1) and j == 0:
neighborhood = [
grid[i, j + 1],
grid[i - 1, j + 1],
grid[i - 1, j],
]
locations = [
[i, j + 1],
[i - 1, j + 1],
[i - 1, j],
]
elif i == (self.size - 1) and j == (self.size - 1):
neighborhood = [
grid[i - 1, j - 1],
grid[i, j - 1],
grid[i - 1, j],
]
locations = [
[i - 1, j - 1],
[i, j - 1],
[i - 1, j],
]
elif i == (self.size - 1) and 0 < j < (self.size - 1):
neighborhood = [
grid[i, j - 1],
grid[i, j + 1],
grid[i - 1, j - 1],
grid[i - 1, j],
grid[i - 1, j + 1],
]
locations = [
[i, j - 1],
[i, j + 1],
[i - 1, j - 1],
[i - 1, j],
[i - 1, j + 1],
]
elif 0 < i < (self.size - 1) and j == 0:
neighborhood = [
grid[i - 1, j],
grid[i - 1, j + 1],
grid[i, j + 1],
grid[i + 1, j],
grid[i + 1, j + 1],
]
locations = [
[i - 1, j],
[i - 1, j + 1],
[i, j + 1],
[i + 1, j],
[i + 1, j + 1],
]
elif 0 < i < (self.size - 1) and j == (self.size - 1):
neighborhood = [
grid[i - 1, j],
grid[i - 1, j - 1],
grid[i, j - 1],
grid[i + 1, j],
grid[i + 1, j - 1],
]
locations = [
[i - 1, j],
[i - 1, j - 1],
[i, j - 1],
[i + 1, j],
[i + 1, j - 1],
]
else:
neighborhood = [
grid[i - 1, j - 1],
grid[i - 1, j],
grid[i - 1, j + 1],
grid[i, j - 1],
grid[i, j + 1],
grid[i + 1, j - 1],
grid[i + 1, j],
grid[i + 1, j + 1],
]
locations = [
[i - 1, j - 1],
[i - 1, j],
[i - 1, j + 1],
[i, j - 1],
[i, j + 1],
[i + 1, j - 1],
[i + 1, j],
[i + 1, j + 1],
]
return neighborhood, locations
def initialize_terrain(self):
terrain = np.ones((self.size, self.size))
for i in range(self.size - 1):
terrain[i + 1] = terrain[i] * (1 - self.slope)
for i in range(self.size):
for j in range(self.size):
neighbors = self.moore_neighborhood(terrain, i, j)[0]
if rd.random() < 0.01:
perturb = rd.uniform(0.999, 1.0001)
else:
perturb = rd.uniform(self.rand_lower, self.rand_upper)
terrain[i, j] = np.mean(neighbors) * perturb
# create hill top
hill_coords = [
(0, int(self.size / 2)),
# (5, 12),
# (7, 40),
# (1, 60),
# (1, 135),
# (5, 150),
# (5, 170),
# (7, 185),
# (1, 195),
# (28, 150),
# (33, 115),
# (20, 70),
# (45, 80),
# (60, 110),
]
for hill_coord in hill_coords:
# terrain[hill_coord] = terrain[hill_coord] * 1.04 # rd.uniform(1.01, 1.04)
terrain[hill_coord] = terrain[hill_coord]*1
for _ in range(2):
for i in range(self.size):
for j in range(self.size):
neighborhood, locations = self.moore_neighborhood(terrain, i, j)
for n, neighbor in enumerate(neighborhood):
location = (locations[n][0], locations[n][1])
if ((terrain[i, j] - neighbor) / neighbor) > 0.01:
terrain[location] = terrain[i, j] * rd.uniform(0.995, 0.999)
for i in range(self.size - 1, 0, -1):
for j in range(self.size - 1, 0, -1):
neighborhood, locations = self.moore_neighborhood(terrain, i, j)
for n, neighbor in enumerate(neighborhood):
location = (locations[n][0], locations[n][1])
if ((terrain[i, j] - neighbor) / neighbor) > 0.01:
terrain[location] = terrain[i, j] * rd.uniform(0.995, 0.999)
self.terrain = terrain
return self.terrain
def get_location_of_lowest_neighbor(self, grid, i, j, temp_ends):
neighborhood = self.moore_neighborhood(grid, i, j)
neighborhood0, neighborhood1 = [], []
for i, val in enumerate(neighborhood[1]):
if tuple(val) not in self.river_coors:
neighborhood0.append(neighborhood[0][i])
neighborhood1.append(val)
try:
value, location = (list(t) for t in zip(*sorted(zip(neighborhood0, neighborhood1))))
except ValueError:
value, location = [], []
return value, location
def get_path(self, prev_val, coor_list, value_list):
for i, coor in enumerate(coor_list):
tup = tuple(coor)
if tup not in self.river_coors:
self.river_coors.add(tup)
else:
pass
self.path[tup] = self.path[tup] + float(prev_val[i])*(1-self.delta_w)
return self.path
def create_path_from_start(self):
self.path = self.get_path([self.init_water_level/(1-self.delta_w)],[(0, self.starting_column)], [self.init_water_level])
self.river_coors.add((0, self.starting_column))
self.cur_ends = {}
self.cur_ends[(0, self.starting_column)] = self.cur_river_nr
self.segment_dict = {self.cur_river_nr:[(0, self.starting_column)]}
self.segment_grid[(0, self.starting_column)] = self.cur_river_nr
self.cur_river_nr += 1
for x in range(1, self.time_limit):
temp_ends = {}
for item, val in self.cur_ends.items():
if self.path[item] > self.branch_tresh:
old_value = self.terrain[item]
sort_values, sort_location = self.get_location_of_lowest_neighbor(self.terrain, item[0], item[1], temp_ends)
if not sort_values:
continue
next_cell, next_value = [tuple(sort_location[0])], [sort_values[0]]
next_water = [self.path[item]]
if old_value < sort_values[0] and len(sort_location) > 1:
next_cell.append(tuple(sort_location[1]))
next_value.append(sort_values[1])
next_water = self.new_water_ratio(item, tuple(sort_location[0]), tuple(sort_location[1]))
temp_ends[next_cell[0]] = self.cur_river_nr
self.segment_grid[next_cell[0]] = self.cur_river_nr
self.segment_dict[self.cur_river_nr] = [next_cell[0]]
self.cur_river_nr += 1
temp_ends[next_cell[1]] = self.cur_river_nr
self.segment_grid[next_cell[1]] = self.cur_river_nr
self.segment_dict[self.cur_river_nr] = [next_cell[1]]
self.cur_river_nr += 1
self.split_dict[self.segment_grid[item]] = (self.segment_grid[next_cell[1]], self.segment_grid[next_cell[0]])
else:
temp_ends[(next_cell[0])] = self.segment_grid[item]
self.segment_grid[(next_cell[0])] = self.segment_grid[item]
self.segment_dict[self.segment_grid[item]].append(next_cell[0])
self.path = self.get_path(next_water, next_cell, next_value)
if self.viz:
self.path_list.append(self.path.copy())
self.cur_ends = temp_ends.copy()
if not self.cur_ends:
if self.viz:
return self.path_list
return self.path, self.segment_grid, self.split_dict, self.segment_dict
if self.viz:
return self.path_list
return self.path, self.segment_grid, self.split_dict, self.segment_dict
def new_water_ratio(self, old, coor_split1, coor_split2):
new_l = self.terrain[coor_split1] - self.terrain[old]
new_r = self.terrain[coor_split2] - self.terrain[old]
l = new_l/(new_l + new_r) * self.path[old]
r = new_r/(new_l + new_r) * self.path[old]
return [l, r]
if __name__ == "__main__":
for i in range(1, 51):
size = 50
print(i)
# slopes = [0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001]
# waters = [0.0001, 0.0005, 0.0010, 0.0015]
slopes = [0.0001]
waters = [0.0008]
for slope in slopes:
for water in waters:
ca = CA(size=size, slope=slope, mu=0.0004, gamma=0.0002, rho=0.02, time_limit=size, delta_water=water)
terrain = ca.initialize_terrain()
path, segments, split_dict, segment_dict = ca.create_path_from_start()
# np.savetxt(f'tests/test_final.csv', path, delimiter=',')
fig, axes = plt.subplots(1, 2,figsize=(15,5))
sns.heatmap(terrain[:, 0:size-1], cmap="Greens", ax=axes[0])
sns.heatmap(path, cmap="Blues", ax=axes[1])
axes[1].set_title("Path of river")
plt.savefig(f'plots/FINAL_50_{i}.png', dpi=300)
# with open(f'pickles/splits_slope_{slope}_water_{water}_version_{i}.p', 'wb') as handle:
# pc.dump(split_dict, handle, protocol=pc.HIGHEST_PROTOCOL)
# with open(f'pickles/segments_slope_{slope}_water_{water}_version_{i}.p', 'wb') as handle:
# pc.dump(segment_dict, handle, protocol=pc.HIGHEST_PROTOCOL)
# np.savetxt(f'tests/path_matrix_with_slope_{slope}_water_{water}_version_{i}.csv', path, delimiter=',')
# for i in range(1, 2):
# size = 200
# # slopes = [0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001]
# slopes = [0.0005]
# for slope in slopes:
# ca = CA(size=size, slope=slope, mu=0.0004, gamma=0.0002, rho=0.02, time_limit=size)
# terrain = ca.initialize_terrain()
# path, segments,_,_ = ca.create_path_from_start()
# # plt.figure(figsize=(15, 5))
# # plt.subplot2grid((1, 2), (0, 0))
# # sns.heatmap(terrain[:, 0:199], cmap="Greens")
# # plt.title(f"Terrain with a slope of {slope*100} %")
# # my_cmap = cp.copy(cm.get_cmap('Blues'))
# # my_cmap.set_under(alpha=0.001)
# # # plt.subplot2grid((1, 2), (0, 1))
# # sns.heatmap(path, cmap=my_cmap)
# # plt.title("River")
# # # plt.savefig(f'plots/river_{i}.png', dpi=300)
# # plt.show()
# # Generate some data...
# # gray_data = np.arange(10000).reshape(100, 100)
# # masked_data = np.random.random((100,100))
# masked_data = np.ma.masked_where(path < 0.01, path)
# print(np.min(path), np.max(path))
# # Overlay the two images
# fig, ax = plt.subplots()
# ax.imshow(terrain[:, 0:199], cmap='Greens')
# ax.imshow(masked_data, cmap='Blues',vmin=-1,vmax=1, interpolation='none')
# plt.show()