-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacons.py
344 lines (289 loc) · 9.16 KB
/
acons.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# https://www.alphagrader.com/courses/6/assignments/11
import re
import time
import copy
from queue import PriorityQueue
def input_to_stacks(input_string):
stacks = []
for stack_string in input_string.split(';'):
stacks.append([ box for box in re.sub(r'[ ()]','',stack_string).split(',') if box != ''])
for i,stack in enumerate(stacks):
if len(stack) > 0:
if stack[0] == 'X':
stacks[i] = 'X'
return stacks
# takes list of lists and returns a tuple of tuples
def stack_to_state(stacks):
state = ()
for stack in stacks:
column = ()
for box in stack:
column += (box, )
state += (column, )
return state
# takes tuple of tuples and returns a list of lists
def state_to_stack(state):
stacks = []
for i,column in enumerate(state):
stacks.append([])
for j in column:
stacks[i].append(j)
return stacks
def move_box(current,neu_position,stacks):
neu_stacks = stacks[:]
cost = int(0.5 + abs(current - neu_position) + 0.5)
box = neu_stacks[current].pop()
neu_stacks[neu_position].append(box)
return neu_stacks, cost
def test_goal(stacks,goal_stacks):
valid = True
for i,stack in enumerate(goal_stacks):
if stack == 'X':
pass
else:
for j,box in enumerate(stack):
try:
if stacks[i][j] != box:
valid = False
break
except:
valid = False
return valid
def test_in_list(stack,stacks):
for x in stacks:
if test_goal(stack,x):
return True
return False
def check_better_cost(stacks, cost, queue):
valid = False
not_in_list = True
for state in queue:
if test_goal(stacks,state[0]):
not_in_list = False
if cost < state[1]:
valid = True
if not_in_list:
return True
else:
return valid
def track_moves(state,moves):
current = moves[state]
search_string = str(current[2])
while True:
if current[0] not in moves.keys():
break
current = moves[current[0]]
if current[0] in moves.keys():
search_string = str(current[2]) + '; ' + search_string
return search_string
def dfs(max_height,stacks,goal_stacks):
initial_state = stack_to_state(stacks)
moves = {initial_state: [(),0,(0,0), False]}
stack = [initial_state]
while True:
if len(stack) == 0:
return False
current_state = stack.pop()
moves[current_state][3] = True
for i,box in enumerate(current_state):
for j in range(len(current_state)):
if i != j and len(current_state[j]) < max_height and len(current_state[i]) > 0:
neu_stacks, cost = move_box(i,j,state_to_stack(current_state))
#if not visitado
neu_state = stack_to_state(neu_stacks)
if neu_state not in moves.keys():
moves[neu_state] = [current_state,cost+moves[current_state][1],(i,j), False]
current_cost = cost+moves[current_state][1]
if test_goal(neu_stacks, goal_stacks):
print('Nodes: '+ str(len(moves.keys())))
return current_cost, track_moves(neu_state,moves)
stack.append(neu_state)
def bfs(max_height,stacks,goal_stacks):
initial_state = stack_to_state(stacks)
moves = {initial_state: [(),0,(0,0), False]}
queue = [initial_state]
while True:
if len(queue) == 0:
return False
current_state = queue.pop()
moves[current_state][3] = True
for i,box in enumerate(current_state):
for j in range(len(current_state)):
if i != j and len(current_state[j]) < max_height and len(current_state[i]) > 0:
neu_stacks, cost = move_box(i,j,state_to_stack(current_state))
#if not visitado
neu_state = stack_to_state(neu_stacks)
if neu_state not in moves.keys():
moves[neu_state] = [current_state,cost+moves[current_state][1],(i,j), False]
current_cost = cost+moves[current_state][1]
if test_goal(neu_stacks, goal_stacks):
print('Nodes: '+ str(len(moves.keys())))
return current_cost, track_moves(neu_state,moves)
queue.insert(0,neu_state)
def uniform_cost(max_height,stacks,goal_stacks):
initial_state = stack_to_state(stacks)
moves = {initial_state: [(),0,(0,0), False]}
queue = PriorityQueue()
queue.put((0, initial_state))
while True:
if queue.empty():
return False
current_state = queue.get()[1]
moves[current_state][3] = True
for i,box in enumerate(current_state):
for j in range(len(current_state)):
if i != j and len(current_state[j]) < max_height and len(current_state[i]) > 0:
neu_stacks, cost = move_box(i,j,state_to_stack(current_state))
#if not visitado
neu_state = stack_to_state(neu_stacks)
if neu_state not in moves.keys():
moves[neu_state] = [current_state,cost+moves[current_state][1],(i,j), False]
current_cost = cost+moves[current_state][1]
if test_goal(neu_stacks, goal_stacks):
print('Nodes: '+ str(len(moves.keys())))
return current_cost, track_moves(neu_state,moves)
queue.put((cost+moves[current_state][1],neu_state))
def cons_heuristic_cost(current_stacks,goal_stacks):
total = 0
for i,stack in enumerate(goal_stacks):
if stack == 'X':
pass
else:
for j,box in enumerate(stack):
try:
if stacks[i][j] != box:
total += 1
except:
total += 1
return total
def incons_heuristic_cost(current_stacks,goal_stacks):
# calcular manhattan?
# Se esta calculando a partir de cuantos stacks se tiene que mover cada caja
total = 0
for i,stack in enumerate(goal_stacks):
if stack == 'X':
pass
else:
for j,box in enumerate(stack):
for k, current_stack in enumerate(current_stacks):
if box in current_stack:
total += abs(k-i)
return total
def a_star_cons(max_height,stacks,goal_stacks):
initial_state = stack_to_state(stacks)
moves = {initial_state: [(),0,(0,0), False]}
queue = PriorityQueue()
queue.put((0, initial_state))
while True:
if queue.empty():
return False
current_state = queue.get()[1]
moves[current_state][3] = True
for i,box in enumerate(current_state):
for j in range(len(current_state)):
if i != j and len(current_state[j]) < max_height and len(current_state[i]) > 0:
neu_stacks, cost = move_box(i,j,state_to_stack(current_state))
#if not visitado
neu_state = stack_to_state(neu_stacks)
if neu_state not in moves.keys():
moves[neu_state] = [current_state,cost+moves[current_state][1],(i,j), False]
current_cost = cost+moves[current_state][1]
if test_goal(neu_stacks, goal_stacks):
# print('Nodes: '+ str(len(moves.keys())))
return current_cost, track_moves(neu_state,moves)
priority_cost = current_cost + cons_heuristic_cost(neu_stacks,goal_stacks)
queue.put((priority_cost,neu_state))
def a_star_incons(max_height,stacks,goal_stacks):
initial_state = stack_to_state(stacks)
moves = {initial_state: [(),0,(0,0), False]}
queue = PriorityQueue()
queue.put((0, initial_state))
while True:
if queue.empty():
return False
current_state = queue.get()[1]
moves[current_state][3] = True
for i,box in enumerate(current_state):
for j in range(len(current_state)):
if i != j and len(current_state[j]) < max_height and len(current_state[i]) > 0:
neu_stacks, cost = move_box(i,j,state_to_stack(current_state))
#if not visitado
neu_state = stack_to_state(neu_stacks)
if neu_state not in moves.keys():
moves[neu_state] = [current_state,cost+moves[current_state][1],(i,j), False]
current_cost = cost+moves[current_state][1]
if test_goal(neu_stacks, goal_stacks):
print('Nodes: '+ str(len(moves.keys())))
return current_cost, track_moves(neu_state,moves)
priority_cost = current_cost + incons_heuristic_cost(neu_stacks,goal_stacks)
queue.put((priority_cost,neu_state))
import fileinput
def main():
lines = []
for line in fileinput.input():
lines.append(line)
lines = list(map(lambda x: x.strip(), lines))
# print(lines)
max_height = int(lines[0])
stacks = input_to_stacks(lines[1])
stacks_original = list(stacks)
goal_stacks = input_to_stacks(lines[2])
answer = a_star_cons(max_height,stacks,goal_stacks)
if answer:
cost, moves = answer
print(int(cost))
print(moves, end='')
else:
print('No solution found',end='')
# def main():
# lines = []
# for line in fileinput.input():
# lines.append(line)
# max_height = int(input())
# stacks = input_to_stacks(input())
# stacks_original = list(stacks)
# goal_stacks = input_to_stacks(input())
# answer = a_star_cons(max_height,stacks,goal_stacks)
# if answer:
# cost, moves = answer
# print(int(cost))
# print(moves, end='')
# else:
# print('No solution found',end='')
# # print(stacks)
if __name__ == '__main__':
main()
# print("\nDFS")
# answer = dfs(max_height,stacks,goal_stacks)
# if answer:
# cost, moves = answer
# print(int(cost))
# print(moves, end='')
# else:
# print('No solution found',end='')
# print("\nBFS")
# answer = bfs(max_height,stacks,goal_stacks)
# if answer:
# cost, moves = answer
# print(int(cost))
# print(moves, end='')
# else:
# print('No solution found',end='')
# print("\nA*: Uniform Cost")
# answer = uniform_cost(max_height,stacks,goal_stacks)
# if answer:
# cost, moves = answer
# print(int(cost))
# print(moves, end='')
# else:
# print('No solution found',end='')
# print("\nA*: Cons")
# print("\nA*: Incons")
# answer = a_star_incons(max_height,stacks,goal_stacks)
# if answer:
# cost, moves = answer
# print(int(cost))
# print(moves, end='')
# else:
# print('No solution found',end='')
# print(goal_stacks)