-
Notifications
You must be signed in to change notification settings - Fork 0
/
way_resolving.py
184 lines (149 loc) · 6.29 KB
/
way_resolving.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
import main, random
from nodes import Node
class new_way():
#creating a new way and copying data from the main module
def __init__(self, value, node):
data_csv = []
hash_row = {}
hash_column = {}
hash_matrix = {}
heur_row = {}
heur_column = {}
heur_matrix = {}
not_poss = []
for i in range(9):
arr_nodes = []
for j in range(9):
n = main.data_csv[i][j]
new_n = Node(n.data, n.index_row, n.index_matr, n.index_col, n.heuris)
if new_n.index_col == node.index_col and new_n.index_row == node.index_row: new_n.data = value
arr_nodes.append(new_n)
if new_n.data == 0: not_poss.append(new_n)
data_csv.append(arr_nodes)
matr_index = 0
for i in range(9):
row = main.hash_row[i]
arr_row = []
for j in range(9):
arr_row.append(row[j])
if ((i + 1) % 3 == 0) and ((j + 1) % 3 == 0):
get_nodeFromRange(matr_index, i , j, data_csv, hash_matrix, heur_matrix)
matr_index += 1
hash_row[i] = arr_row
for i in range(9):
col = main.hash_column[i]
arr_col = []
for j in range(9):
arr_col.append(col[j])
hash_column[i] = arr_col
for i in range(9):
heur_row[i] = main.heur_row[i]
for i in range(9):
heur_column[i] = main.heur_column[i]
for n in not_poss:
fill_poss_variants(n, hash_row, hash_column, hash_matrix)
change_dependents(node, value, hash_row, heur_row, hash_column, heur_column, hash_matrix, heur_matrix)
self.data_csv = data_csv
self.hash_row = hash_row
self.hash_column = hash_column
self.hash_matrix = hash_matrix
self.heur_row = heur_row
self.heur_column = heur_column
self.heur_matrix = heur_matrix
self.not_poss = not_poss
def find_resolving(self, reverse):
self.not_poss.sort(cmp=None, key=lambda Node: Node.heuris, reverse=True)
col = 0
for n in self.not_poss:
random.shuffle(n.pos_variants)
for x in n.pos_variants:
if is_possible(x, n, self.hash_row, self.hash_column, self.hash_matrix):
n.data = x
change_dependents(n, x, self.hash_row, self.heur_row, self.hash_column, self.heur_column, self.hash_matrix, self.heur_matrix)
col += 1
for n in self.not_poss:
if n.data == 0: return False
return True
#calculating heuristic value for each cell
def set_heuris(self):
for i in range(9):
for j in range(9):
n = self.data_csv[i][j]
if n.data ==0 :
fill_poss_variants(n, self.hash_row, self.hash_column, self.hash_matrix)
n.heuris = self.heur_row[n.index_row] + self.heur_column[n.index_col] + self.heur_matrix[n.index_matr]-len(n.pos_variants)
#creating matrices 3*3, creating hash table for matrices
#calculating heuristic value for matrices
def get_nodeFromRange(matr_index, i, j, source, ha_matrix, he_matrix):
arr = []
heuris_matr = 0
if i == 2 and j == 2: # first
i1 = 0
j1 = 0
elif i == 2 and j == 5:
i1 = 0
j1 = 3
elif i == 2 and j == 8:
i1 = 0
j1 = 6
elif i == 5 and j == 2: # second
i1 = 3
j1 = 0
elif i == 5 and j == 5:
i1 = 3
j1 = 3
elif i == 5 and j == 8:
i1 = 3
j1 = 6
elif i == 8 and j == 2: # third
i1 = 6
j1 = 0
elif i == 8 and j == 5:
i1 = 6
j1 = 3
elif i == 8 and j == 8:
i1 = 6
j1 = 6
for x in range(i1, (i + 1)):
for y in range(j1, (j + 1)):
n = source[x][y]
n.index_matr = matr_index
arr.append(n)
if not n.data == 0: heuris_matr += 1
ha_matrix[matr_index] = arr
he_matrix[matr_index] = heuris_matr
#filling possible variants for each cell
def fill_poss_variants(n, h_row, h_col, h_matr):
n.pos_variants = []
for x in range(1, 10):
poss = is_possible(x, n, h_row, h_col, h_matr)
if poss:
n.pos_variants.append(x)
#adding new value into depending (a row, a column, a matrix)
def change_dependents(node, x, ha_row, he_row, ha_col, he_col, ha_matr, he_matr):
ha_row[node.index_row][node.index_col] = x
he_row[node.index_row] = he_row[node.index_row] + 1
ha_col[node.index_col][node.index_row] = x
he_col[node.index_col] = he_col[node.index_col] + 1
matr_arr = ha_matr[node.index_matr]
result = [a for a in matr_arr if a.index_row == node.index_row and a.index_col == node.index_col]
result[0].data = x
he_matr[node.index_matr] = he_matr[node.index_matr] + 1
def del_x_from_pos_variants(x, n, arr):
arr_colleg = [a for a in arr if a.index_matr == n.index_matr or a.index_row == n.index_row or a.index_col == n.index_col]
for colleg in arr_colleg:
if colleg == n: continue
if x in colleg.pos_variants:
colleg.pos_variants.remove(x)
def check_obj_in_arr(i, arr):
result = [a for a in arr if a.data == i]
return False if len(result) == 0 else True
#checking, we can use this value for this cell
def is_possible(x, n, h_row, h_col, h_matr):
colArr = h_col[n.index_col]
rowArr = h_row[n.index_row]
matrArr = h_matr[n.index_matr]
if (x in colArr) or (x in rowArr) or check_obj_in_arr(x, matrArr):
return False
else:
return True