-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzle.py
More file actions
130 lines (114 loc) · 4.25 KB
/
Puzzle.py
File metadata and controls
130 lines (114 loc) · 4.25 KB
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
#! /usr/bin/env python3
import sys
import argparse
from Class import Tabuleiro
from Functions import bfs, dfs, idfs, astar, guloso, hamming, manhattan
def main():
# command line
parser = argparse.ArgumentParser(description='This is a 15 puzzle solver.')
parser.add_argument('--dfs', type=int,
help='Run Depth-first search (provide a positive integer, max depth to search)')
parser.add_argument('--bfs', action='store_true',
help='Run Breadth-first search')
parser.add_argument('--idfs', action='store_true',
help='Run Iteractive Depth-first search')
parser.add_argument('--astar', '--a', type=int, choices=[1, 2],
help='Run A* search (1 -hamming; 2 -manhattan)')
parser.add_argument('--greedy', '--gulosa', type=int, choices=[1, 2],
help='Run Greedy search (1 -hamming; 2 -manhattan)')
parser.add_argument('--input', '-i', help='Specify an input file for tests')
args = parser.parse_args()
# Ler tabuleiros
if args.input is None:
# terminal
inicialState = input("Starting Board:\n").split()
goalState = input("Goal Board:\n").split()
else:
# file
try:
numbers = []
with open(args.input, "r") as f:
lines = f.readlines()
for line in lines:
numbers = numbers + line.split()
inicialState = numbers[:16]
numbers = numbers[16:]
goalState = numbers[:16]
except FileNotFoundError:
sys.stderr.write("Invalid file path")
sys.exit(1)
# iniciar tabuleiros
inicialState = Tabuleiro(inicialState)
goalState = Tabuleiro(goalState)
print(inicialState)
print(goalState)
if inicialState == goalState:
print("Both are the same. Already solved!")
sys.exit(0)
if inicialState.solvabilidade() ^ goalState.solvabilidade():
print('Invalid boards.')
sys.exit(1)
# testing args / actual solver
if args.astar is None and not args.bfs and args.dfs is None and not args.idfs and args.greedy is None:
sys.stderr.write("Provide a valid input.")
sys.stderr.write("Choose at least one algorithm to solve.")
sys.exit(1)
if inicialState == goalState:
print("Both are the same. Already solved!")
sys.exit(0)
if args.dfs is not None:
if args.dfs < 0:
parser.print_help(sys.stderr)
sys.exit(1)
print("Depth-first search:")
moves, nodes= dfs(inicialState, goalState, args.dfs)
print(nodes, "nodes used.")
if moves:
print("Path to goal:")
print(" -> ".join(moves))
else:
print("No solution found.")
if args.bfs:
print("Breadth-first search:")
moves, nodes = bfs(inicialState, goalState)
print(nodes, "nodes used.")
if moves:
print("Path to goal:")
print(" -> ".join(moves))
else:
print("No solution found.")
if args.idfs:
print("Iteractive Depth-first search:")
moves, nodes = idfs(inicialState, goalState)
print(nodes, "nodes used.")
if moves:
print("Path to goal:")
print(" -> ".join(moves))
else:
print("No solution found.")
if args.astar:
print("A* search:")
comp = manhattan
if args.astar == 1:
comp = hamming
moves, nodes = astar(inicialState, goalState, comp)
print(nodes, "nodes used.")
if moves:
print("Path to goal:")
print(" -> ".join(moves))
else:
print("No solution found.")
if args.greedy:
print("Greedy search:")
comp = manhattan
if args.astar == 1:
comp = hamming
moves, nodes = guloso(inicialState, goalState, comp)
print(nodes, "nodes used.")
if moves:
print("Path to goal:")
print(" -> ".join(moves))
else:
print("No solution found.")
if __name__ == '__main__':
main()