-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.py
130 lines (105 loc) · 4.01 KB
/
matrix.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
import numpy as np
from random import randint, choice
class Matrix:
def __init__(self, dim = 50, p_one = 0.35, p_two = 0.35, threshold = 0.6):
self.dim = dim
self.entries = dim * dim
self.n_one = int(self.entries * p_one)
self.n_two = int(self.entries * p_two)
self.n_empy = self.entries - (self.n_one + self.n_two)
self.threshold = threshold
self.matrix = np.zeros(shape=(dim, dim))
self.populate()
def populate(self):
i = 0
while i < self.n_one:
x = randint(0, self.dim - 1)
y = randint(0, self.dim - 1)
if self.matrix[x][y] == 0:
self.matrix[x][y] = 1
i += 1
i = 0
count_two = 0
while i < self.n_two:
x = randint(0, self.dim - 1)
y = randint(0, self.dim - 1)
if self.matrix[x][y] == 0:
self.matrix[x][y] = 2
i += 1
def assert_unsatisfied(self):
unsatisfied = []
for x in range(self.dim):
for y in range(self.dim):
if self.matrix[x][y] != 0:
if self.check_position(x, y):
unsatisfied.append((x, y))
#print("Unsastisfied: ", unsatisfied)
return unsatisfied
def check_position(self, x, y):
neighborhood = self.get_neighborhood(x,y)
position = (x, y)
return self.check_neighborhood(neighborhood, position)
def get_neighborhood(self, x, y):
neighbors = list()
to_remove = list()
neighbors.append((x, y - 1))
neighbors.append((x, y + 1))
neighbors.append((x - 1, y))
neighbors.append((x + 1, y))
neighbors.append((x - 1, y - 1))
neighbors.append((x - 1, y + 1))
neighbors.append((x + 1, y - 1))
neighbors.append((x + 1, y + 1))
for t in neighbors:
if t[0] < 0 or t[0] > self.dim-1:
to_remove.append(t)
elif t[1] < 0 or t[1] > self.dim-1:
to_remove.append(t)
for r in to_remove:
neighbors.remove(r)
#return self.check_neighborhood(neighbors, position)
return neighbors
# number of different races not being used
def check_neighborhood(self, neighborhood, pos) -> bool:
ratio = self.get_position_ratio(neighborhood, pos)
return ratio < self.threshold
def get_position_ratio(self, neighborhood, pos):
my_race = self.matrix[pos[0]][pos[1]]
# print("NEIGHBORHOOD: ", neighborhood)
same_race = 0
num_neighbors = 0
for neighbor in neighborhood:
if self.matrix[neighbor[0]][neighbor[1]] == 0:
continue
elif self.matrix[neighbor[0]][neighbor[1]] == my_race:
same_race += 1
num_neighbors += 1
try:
ratio = same_race / num_neighbors
except ZeroDivisionError:
ratio = 1
return ratio
def move_unsatisfied(self, unsat):
empty_positions = self.empty_positions()
for u in unsat:
random_empty = choice(empty_positions)
self.matrix[random_empty[0]][random_empty[1]] = self.matrix[u[0]][u[1]]
self.matrix[u[0]][u[1]] = 0
empty_positions.remove(random_empty)
empty_positions.append((u[0], u[1]))
def empty_positions(self) -> list:
empty = list()
for i in range(self.dim):
for j in range(self.dim):
if self.matrix[i][j] == 0:
empty.append((i, j))
return empty
def calculate_segregation(self):
similarity = []
for x in range(self.dim):
for y in range(self.dim):
if self.matrix[x][y] != 0:
neighborhood = self.get_neighborhood(x ,y)
position = (x, y)
similarity.append(self.get_position_ratio(neighborhood, position))
return sum(similarity)/len(similarity) if len(similarity) != 0 else 0