-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireworld.py
519 lines (443 loc) · 17.8 KB
/
wireworld.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
import json
import CA_generator
# defines the relative points that a cell considers its neighbours
relative_nbhd = ((-1,-1), (0,-1), (1,-1),
(-1, 0), (1, 0),
(-1, 1), (0, 1), (1, 1))
def ww_staterule(state, nbhd_state):
'''
The rules for the wireworld cellular automata.
Returns the next state for a given cell.
Args:
* state (int):
The state of the current cell.
* nbhd_state (dict)
A dictionary whose keys are the possible states and whose values are the number of adjacent
cells which have those states.
Returns:
int
'''
next_state = 0
if state == 1:
next_state = 2
if state == 2:
next_state = 3
if state == 3:
if nbhd_state[1] == 1 or nbhd_state[1] == 2:
next_state = 1
else:
next_state = 3
return next_state
def life_staterule(state, nbhd_state):
'''Rules for John Conway's game of life.'''
next_state = 0
if state == 0:
if nbhd_state[1] == 3:
next_state = 1
if state == 1:
if nbhd_state[1] == 2 or nbhd_state[1] == 3:
next_state = 1
return next_state
class CA:
'''Contains the information needed to define a cellular automata.'''
def __init__(self, rule=None, mode=None, states=None, getrandom=False, ruledict=None):
'''
Generate the information necessary to generate a cellular automata (CA).
Kwargs
* rule(function):
The function to be called when deciding the next state.
* mode(string):
Describes how to handle dead cells for performance.
* states(int or set):
Describes the possible states the CA can be in.
If states is a set, it is expected to be of the form set(range(n))
* getrandom(bool):
If true, generates a random CA.
* ruledict(dict):
May be passed in place of rule. A rule will be constructed from ruledict.
'''
if getrandom:
if states is None:
n = 3
elif type(states) is int:
n = states
else:
n = len(states)
# Initialise a
ca_rules = CA_generator.CA_rules(N_states=n)
self.rule = ca_rules.rules
self.mode = 'semistable'
self.states = set(range(n))
self.ruledict = ca_rules.CA_dict
elif ruledict is not None:
self.ruledict = ruledict
ca_rules = CA_generator.CA_rules(CA_dict=ruledict)
self.rule = ca_rules.rules
self.mode = mode
if type(states) is int:
self.states = set(range(states))
else:
self.states = states
else:
self.rule = rule
self.mode = mode
if type(states) is int:
self.states = set(range(states))
else:
self.states = states
self.ruledict = None
# initialise the relevant CA objects and store them in a dictionary for reference
ww_CA = CA(rule=ww_staterule, mode='stable', states=4)
life_CA = CA(rule=life_staterule, mode='semistable', states=2)
# ww_CA has two keys because I want to be able to pass it as arguments to both World and WireWorld while loading
CA_dict = {'wireworld': ww_CA,
'wireworld-slow': ww_CA,
'life': life_CA}
class World:
'''
An instance of a particular cellular automata or world.
'''
def __init__(self, size=(7, 7), content=None, CA=None, CA_type='wireworld'):
'''
Creates a particular cellular automata.
Default cellular automata is wireworld.
Args:
* size (tuple):
Sets the initial bounds of the world
Kwargs:
* content (dict):
Sets the initial state of the cells
* CA(CA object):
Contains the information about the cellular automata to be run.
* CA_type (string):
A label corresponding to the type of cellular automata to be run.
May be passed in place of CA.
'''
self.CA_type = CA_type
if CA is None:
self.CA = CA_dict[CA_type]
else:
self.CA = CA
self.size = size
if content is None:
self.grid = dict()
else:
# TODO run checks on content, perhaps reformat
self.grid = content
self.changeset = set(self.grid) # This set keeps track of which cells have changed after each update
self.copy_section = None # This keeps track of copied sections
def printself(self):
'''prints a representation of the current state in the console.'''
for y in range(self.size[1]):
rowlist = []
for x in range(self.size[0]):
try:
state = str(self.grid[(x, y)])
except:
state = '*'
rowlist.append(state)
print('-'.join(rowlist))
def editpoint(self, coord, value=None, cycle=True):
'''
Edit the state of a given cell.
Default behaviour is to decrement the state, cycling from 0 to the max state.
If a cell is empty, it is assumed to start at 0.
Args:
* coord (tuple):
2 dimensional coordinate describing the location of the cell.
Kwargs:
* value (int or None):
sets the new state of the cell. If None, state is decremented
'''
if value is None:
if cycle:
state = self.grid.setdefault(coord, 0)
state = (state-1) % (max(self.CA.states)+1)
else:
state = 0
else:
state = value
state = state % (max(self.CA.states)+1) # force the resulting state to be valid
if state == 0 and (self.CA.mode == 'stable' or self.CA.mode == 'semistable') and coord in self.grid:
self.grid.pop(coord)
else:
self.grid[coord] = state
self.changeset = set(coord)
def getneighbourcoords(self, coord):
'''Returns the coordinates neighbouring a given point.'''
return [(coord[0]+x, coord[1]+y) for x, y in relative_nbhd]
def getneighbours(self, coord):
'''
Returns the states of neighbouring cells.
Args:
* coord (tuple):
2 dimensional coordinate describing the location of the cell.
Returns:
dict
'''
state_dict = {state: 0 for state in self.CA.states}
for neighbour in self.getneighbourcoords(coord):
state = self.getcoordstate(neighbour)
state_dict[state] += 1
return state_dict
def pad(self):
'''Adds all cells adjacent to existing cells.'''
for coord in self.grid.copy():
for x, y in relative_nbhd:
neighbour = (coord[0] + x, coord[1] + y)
self.grid.setdefault(neighbour, 0)
def trim(self):
'''Removes all cells containing zeroes.'''
for coord, state in self.grid.copy().items():
if state == 0 or (self.CA_type == 'random' and (max(coord) > 120 or min(coord) < -120)):
del self.grid[coord]
def getcoordstate(self, coord):
'''
Returns the state at a coordinate, with empty coordinates defaulting to 0.
Returns:
int
'''
if coord in self.grid:
return self.grid[coord]
else:
return 0
def step(self):
'''Runs one step of the cellular automata.'''
new_states = dict()
if self.CA.mode == 'semistable':
self.pad()
self.changeset = set()
for coord, state in self.grid.items():
nbhd_state = self.getneighbours(coord)
new_state = self.CA.rule(state, nbhd_state)
new_states[coord] = new_state
if state != new_state:
self.changeset.add(coord)
self.grid = new_states
if self.CA.mode == 'stable' or self.CA.mode == 'semistable':
self.trim()
def getbounds(self):
'''Returns the bounds for the position of the active cells.'''
if not bool(self.grid):
return None
x_max = max(x for x, y in self.grid) # note: x,y is the coordinate, not the key value pair
x_min = min(x for x, y in self.grid)
y_max = max(y for x, y in self.grid)
y_min = min(y for x, y in self.grid)
return (x_min, x_max), (y_min, y_max)
def livecellcount(self):
'''Returns the number of live cells.'''
return len(self.grid)
def becomerandom(self, N=3):
'''Exchange current CA with a randomly generated one.'''
self.CA = CA(states=N, getrandom=True)
self.CA_type = 'random'
for coord, state in self.grid.items():
self.grid[coord] = state % N
def save_copy(self, first_coord, second_coord):
'''Save the data within the selected region'''
self.copy_section = CopySection(self, first_coord, second_coord)
def paste_copy(self, origin):
'''Paste the copied section from the selected point'''
if self.copy_section is not None:
top_left = (origin[0] + self.copy_section.offset[0], origin[1] + self.copy_section.offset[1])
for dy, row in enumerate(self.copy_section.state_array):
for dx, state in enumerate(row):
coord = (top_left[0] + dx, top_left[1] + dy)
self.editpoint(coord, value=state, cycle=False)
def erase_section(self, first_coord, second_coord):
'''Erase all points within the chosen section'''
top_left = (min(first_coord[0], second_coord[0]), min(first_coord[1], second_coord[1]))
bottom_right = (max(first_coord[0], second_coord[0]), max(first_coord[1], second_coord[1]))
for x in range(top_left[0], bottom_right[0] + 1):
for y in range(top_left[1], bottom_right[1] + 1):
coord = (x, y)
self.editpoint(coord, value=0)
def clear(self):
'''Removes all live cells.'''
self.grid = dict()
class CopySection:
'''Keeps track of copied sections'''
def __init__(self, world, first_coord, second_coord):
'''Create a copy of the world coordinates between the specified coordinates.'''
self.state_array = self.calculate_state_array(world, first_coord, second_coord)
self.offset = self.calculate_offset(first_coord, second_coord)
# self.max_states is not currently used, it was designed for the case where a section is pasted into a world
# which allows fewer states with a view that it would give an appropriate warning.
# This feature is not yet implemented.
self.max_states = self.calculate_max_states()
def calculate_state_array(self, world, first_coord, second_coord):
'''Returns an array containing all the states within the specified coordinates.'''
coord_array = self.calculate_coord_array(first_coord, second_coord)
state_array = []
for coord_row in coord_array:
state_row = []
for coord in coord_row:
if coord in world.grid:
state = world.grid[coord]
else:
state = None
state_row.append(state)
state_array.append(state_row)
return state_array
def calculate_coord_array(self, first_coord, second_coord):
'''Returns an array containing all the coordinates within the specified coordinates.'''
top_left = (min(first_coord[0], second_coord[0]), min(first_coord[1], second_coord[1]))
bottom_right = (max(first_coord[0], second_coord[0]), max(first_coord[1], second_coord[1]))
array = [[(x, y) for x in range(top_left[0], bottom_right[0]+1)] for y in range(top_left[1], bottom_right[1]+1)]
return array
def calculate_offset(self, first_coord, second_coord):
'''Returns the difference between the top left coord of the state_array and the first specified coord.'''
top_left = (min(first_coord[0], second_coord[0]), min(first_coord[1], second_coord[1]))
offset = (top_left[0] - first_coord[0], top_left[1] - first_coord[1])
return offset
def calculate_max_states(self):
'''
Keeps track of the maximum stored state.
Thsi may be useful if pasting to a different CA.
'''
def none_to_zero(row):
return [0 if x is None else x for x in row]
max_state = max([max(none_to_zero(row)) for row in self.state_array])
return max_state
class WireWorld(World):
'''
A version of the World class taylored to wireworld.
Performance is improved by only checking for updates near red (state 1) cells.
If there are many state (3) cells, these will not be checked while updating.
'''
def __init__(self, size=(7, 7), content=None):
super().__init__(size=size, content=content, CA_type='wireworld')
self.red_cells = self.get_state_cells(1)
self.blue_cells = self.get_state_cells(2)
def get_state_cells(self, target_state):
'''Returns a set of all cells in the target state.'''
state_cells = set()
for coord, state in self.grid.items():
if state == target_state:
state_cells.add(coord)
return state_cells
def step(self):
'''
Applies one iteration of the cellular automata rule
If the automata is wireworld, an improved algorithm is applied.
'''
if self.CA_type != 'wireworld':
super().step()
else:
self.changeset = set()
important_cells = set()
new_reds = set()
for coord in self.red_cells:
for neighbour in self.getneighbourcoords(coord):
if self.getcoordstate(neighbour) == 3:
important_cells.add(neighbour)
for coord in important_cells:
state = 3
nbhd_state = self.getneighbours(coord)
new_state = self.CA.rule(state, nbhd_state)
if new_state == 1:
new_reds.add(coord)
for coord in new_reds:
self.grid[coord] = 1
self.changeset.add(coord)
for coord in self.red_cells:
self.grid[coord] = 2
self.changeset.add(coord)
for coord in self.blue_cells:
self.grid[coord] = 3
self.changeset.add(coord)
self.blue_cells = self.red_cells
self.red_cells = new_reds
def editpoint(self, coord, value=None, cycle=True):
'''Edits a specified point while keeping track of red and blue cells.'''
start_value = self.getcoordstate(coord)
super().editpoint(coord, value=value, cycle=cycle)
next_value = self.getcoordstate(coord)
if start_value == 1:
self.red_cells.discard(coord)
elif start_value == 2:
self.blue_cells.discard(coord)
if next_value == 1:
self.red_cells.add(coord)
elif next_value == 2:
self.blue_cells.add(coord)
def clear(self):
'''Removes all live cells.'''
super().clear()
self.red_cells = set()
self.blue_cells = set()
#TODO add these as mothods for World
def load_world(infile):
'''Loads Json file into a World object.'''
if infile[-5:] != '.json':
raise Exception('File name must end in .json')
with open(infile) as json_file:
world_data = json.load(json_file)
CA_type = world_data['CA_type']
size = tuple(world_data['size'])
state = world_data['state']
if type(state) is dict:
state = {tuple(eval(k)): v for k, v in state.items()}
elif type(state) is list:
# TODO turn an array into a dict
pass
if 'ruledict' in world_data:
string_ruledict = world_data['ruledict']
ruledict = {eval(k): v for k, v in string_ruledict.items()}
n_states = world_data['Nstates']
mode = world_data['mode']
ca = CA(mode=mode, states=n_states, ruledict=ruledict)
world = World(size=size, content=state, CA=ca, CA_type=CA_type)
elif CA_type == 'wireworld':
world = WireWorld(size=size, content=state)
else:
world = World(size=size, content=state, CA_type=CA_type)
return world
def save_world(world, outfile, permission='x'):
'''Saves World object as Json file.'''
if outfile[-5:] != '.json':
raise Exception('File name must end in .json')
CA_type = world.CA_type
size = world.size
state = world.grid
state = {str(k): v for k, v in state.items()}
ruledict = world.CA.ruledict
world_data = {'CA_type': CA_type,
'size': size,
'state': state}
if ruledict is not None:
string_dict = {str(k): v for k, v in ruledict.items()}
world_data['ruledict'] = string_dict
world_data['Nstates'] = len(world.CA.states)
world_data['mode'] = world.CA.mode
with open(outfile, permission) as json_file:
json.dump(world_data, json_file)
def example_run():
# test_dict = {(0,1): 3, (1,0): 3, (1,2): 1, (2,1): 2}
#
# world = World(content=test_dict)
infile = 'example_01.json'
outfile = 'example_out_01.json'
world = load_world(infile)
world. printself()
print('---')
world.step()
world.printself()
print('---')
world.editpoint((1,3))
world.step()
world.printself()
save_world(world, outfile)
infile_2 = 'example_02.json'
world = load_world(infile_2)
print('---')
print('---')
world.printself()
for _ in range(4):
print('---')
world.step()
world.printself()
save_world(world, infile_2, permission='x')
if __name__ == "__main__":
example_run()