-
Notifications
You must be signed in to change notification settings - Fork 8
/
grid.py
66 lines (53 loc) · 1.96 KB
/
grid.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
import numpy as np
from cell import Cell
from pattern import Pattern
class Grid:
"""
Grid is made of Cells
"""
def __init__(self, size, num_pattern):
self.size = size
self.grid = np.empty(self.size, dtype=object)
for position in np.ndindex(self.size):
self.grid[position] = Cell(num_pattern, position, self)
# self.grid = np.array([[Cell(num_pattern, (x, y), self) for x in range(self.size)] for y in range(self.size)])
# self.grid = np.array([Cell(num_pattern, (x,), self) for x in range(self.size)])
def find_lowest_entropy(self):
min_entropy = 999999
lowest_entropy_cells = []
for cell in self.grid.flat:
if cell.is_stable():
continue
entropy = cell.entropy()
if entropy == min_entropy:
lowest_entropy_cells.append(cell)
elif entropy < min_entropy:
min_entropy = entropy
lowest_entropy_cells = [cell]
if len(lowest_entropy_cells) == 0:
return None
cell = lowest_entropy_cells[np.random.randint(len(lowest_entropy_cells))]
return cell
def get_cell(self, index):
"""
Returns the cell contained in the grid at the provided index
:param index: (...z, y, x)
:return: cell
"""
return self.grid[index]
def get_image(self):
"""
Returns the grid converted from index to back to color
:return:
"""
image = np.vectorize(lambda c: c.get_value())(self.grid)
image = Pattern.index_to_img(image)
return image
def check_contradiction(self):
for cell in self.grid.flat:
if len(cell.allowed_patterns) == 0:
return True
return False
def print_allowed_pattern_count(self):
grid_allowed_patterns = np.vectorize(lambda c: len(c.allowed_patterns))(self.grid)
print(grid_allowed_patterns)