-
Notifications
You must be signed in to change notification settings - Fork 1
/
RubixcubeSolutionIDFS.py
160 lines (129 loc) · 5.1 KB
/
RubixcubeSolutionIDFS.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
from ScrambleRubixcube import xInitial, make_move
import numpy as np
from datetime import datetime
import time
class State:
cube = None
cost = 0
parent = None
move = None
# checks if goal reached. if reached writes goal state in output.txt
def goal_reached(cube):
for ref in [0, 3, 6, 9, 12, 15]:
first = cube[ref, 0]
for i in range(3):
for j in range(3):
if first != cube[ref + i, j]:
return False
# goal reached
file = open('output.txt', 'w')
file.write(" " + str(cube[0, 0:3]) + '\n')
file.write(" " + str(cube[1, 0:3]) + '\n')
file.write(" " + str(cube[2, 0:3]) + '\n')
file.write(str(cube[3, 0:3]) + ' ' + str(cube[6, 0:3]) + ' ' + str(cube[9, 0:3]) + ' ' + str(cube[12, 0:3]) + '\n')
file.write(str(cube[4, 0:3]) + ' ' + str(cube[7, 0:3]) + ' ' + str(cube[10, 0:3]) + ' ' + str(cube[13, 0:3]) + '\n')
file.write(str(cube[5, 0:3]) + ' ' + str(cube[8, 0:3]) + ' ' + str(cube[11, 0:3]) + ' ' + str(cube[14, 0:3]) + '\n')
file.write(" " + str(cube[15, 0:3]) + '\n')
file.write(" " + str(cube[16, 0:3]) + '\n')
file.write(" " + str(cube[17, 0:3]) + '\n')
return True
# checks if child ascendant of parent
def contains1(child, parent):
curr = parent.parent
while curr is not None:
if np.array_equal(curr.cube, child): return True
curr = curr.parent
return False
# checks if frontier contains child
def contains2(child, frontier):
for curr in frontier:
if np.array_equal(curr.cube, child): return True
return False
def idfs(start):
cost_limit = 1
nodes = 0
frontier = list()
branching_factors = list()
while True:
frontier.append(start)
while len(frontier) != 0:
curr = frontier.pop()
if goal_reached(curr.cube):
print('Goal Height:', curr.cost)
print('Branching Factor:', sum(branching_factors)/len(branching_factors))
# while curr is not None:
# if curr.move is not None:
# print(curr.move)
# curr = curr.parent
print("Nodes Generated:", nodes)
return
if curr.cost + 1 <= cost_limit:
child_cost = curr.cost + 1
b = 0
for i in range(12):
nodes = nodes + 1
new = State()
new.cube = np.array(curr.cube)
new.cost = child_cost
new.parent = curr
new.move = make_move(new.cube, i + 1, 0)
# if curr.parent is not None and np.array_equal(curr.parent.cube, new.cube):
if curr.parent is not None and (contains1(new.cube, curr) or contains2(new.cube, frontier)):
continue
frontier.append(new)
b = b + 1
branching_factors.append(b)
branching_factors.clear()
cost_limit = cost_limit + 1
def manhattan_distance(cube, i, z, corner):
x = i / 3
y = i % 3
center = None
for c in [1, 4, 7, 10, 13, 16]:
if cube[i, z] == cube[c, 1]:
center = c
break
if corner:
d1 = abs((center - 1) / 3 - x) + abs((center - 1) % 3 - y) + abs(z - 0)
d2 = abs((center - 1) / 3 - x) + abs((center - 1) % 3 - y) + abs(z - 2)
d3 = abs((center + 1) / 3 - x) + abs((center + 1) % 3 - y) + abs(z - 0)
d4 = abs((center + 1) / 3 - x) + abs((center + 1) % 3 - y) + abs(z - 2)
return min(d1, d2, d3, d4)
else:
d1 = abs((center - 1) / 3 - x) + abs((center - 1) % 3 - y) + abs(z - 1)
d2 = abs(center / 3 - x) + abs(center % 3 - y) + abs(z - 0)
d3 = abs(center / 3 - x) + abs(center % 3 - y) + abs(z - 2)
d4 = abs((center + 1) / 3 - x) + abs((center + 1) % 3 - y) + abs(z - 1)
return min(d1, d2, d3, d4)
def corner_edge_sum_max(cube):
corners = 0
edges = 0
for i in range(18):
if i % 3 == 0 or i % 3 == 2:
corners = corners + manhattan_distance(cube, i, 0, True) + manhattan_distance(cube, i, 2, True)
edges = edges + manhattan_distance(cube, i, 1, False)
else:
edges = edges + manhattan_distance(cube, i, 0, False) + manhattan_distance(cube, i, 2, False)
return max(corners / 4, edges / 4)
##########################################
curr = State()
curr.cube = np.array(xInitial)
handle = open('input.txt')
indexes = [0, 1, 2, 3, 6, 9, 12, 4, 7, 10, 13, 5, 8, 11, 14, 15, 16, 17]
index = 0
for line in handle:
line = line.replace(' ', '')
for row in line.split('['):
if len(row) != 0:
i = indexes[index]
curr.cube[i, 0] = row[1]
curr.cube[i, 1] = row[4]
curr.cube[i, 2] = row[7]
index = index + 1
time.ctime()
fmt = '%H:%M:%S'
start = time.strftime(fmt)
idfs(curr)
time.ctime()
end = time.strftime(fmt)
print("Time taken(sec):", datetime.strptime(end, fmt) - datetime.strptime(start, fmt))