-
Notifications
You must be signed in to change notification settings - Fork 0
/
brute_force.py
57 lines (46 loc) · 1.87 KB
/
brute_force.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
# from main import Solver
import numpy as np
class BruteForceSolver:
def __init__(self, solver):
idx = 0
mapping = dict()
inv_mapping = dict()
# first run, get indices right
for i, j in solver.frontiers:
for di, dj in [(0, 1), (0, -1), (1, 1), (1, -1),
(-1, 1), (-1, -1), (-1, 0), (1, 0)]:
cur_i = i + di
cur_j = j + dj
if solver._checkUnopened(cur_i, cur_j):
if (cur_i, cur_j) not in mapping:
mapping[(cur_i, cur_j)] = idx
inv_mapping[idx] = (cur_i, cur_j)
idx += 1
coeffs = []
b = []
tot_unknown = len(mapping)
# second run, get coeffs right
for i, j in solver.frontiers:
new = np.zeros(tot_unknown, dtype=np.int32)
mines = 0
for di, dj in [(0, 1), (0, -1), (1, 1), (1, -1),
(-1, 1), (-1, -1), (-1, 0), (1, 0)]:
cur_i = i + di
cur_j = j + dj
if solver._checkUnopened(cur_i, cur_j):
new[mapping[(cur_i, cur_j)]] = 1
if solver._checkMine(cur_i, cur_j):
mines += 1
if np.any(new):
coeffs.append(new)
b.append(solver.state[i, j] - mines)
coeffs = np.array(coeffs)
b = np.array(b)
sol = np.linalg.pinv(coeffs) @ b
self.open_lst = []
self.mine_lst = []
for i, x in enumerate(sol):
if x < 0.: # say, likely to an opening
self.open_lst.append(inv_mapping[i])
elif x > .7: # say, likely to be a mine
self.mine_lst.append(inv_mapping[i])