-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (88 loc) · 3.25 KB
/
main.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
# File: main.py
# Authors: Alejandro Cirugeda & Juancarlos Quintana
# Description:
# This is the main function of the problem. It will execute 3 different puzzle
# inceasing the difficulty and the computantional difficuty.
from Node import Node
from Game import *
import time
# We create 3 different games with his own solutions
# Game 1: 5 blank spaces
# Game 2: 13 blank spaces
# Game 3: 17 blank spacesa
game1 = [[1, 0,-1, 1,-1, 0],
[ 1, 1, 0, 0, 1, 0],
[ 0, 0, 1, 1, 0, 1],
[ 0, 1, 0, 1,-1, 1],
[ 1,-1, 1, 0, 1, 0],
[ 0, 1, 0, 0,-1, 1]]
solution1 =[[ 1, 0, 1, 1, 0, 0],
[ 1, 1, 0, 0, 1, 0],
[ 0, 0, 1, 1, 0, 1],
[ 0, 1, 0, 1, 0, 1],
[ 1, 0, 1, 0, 1, 0],
[ 0, 1, 0, 0, 1, 1]]
game2 = [[0,-1, 1,-1, 0, 1],
[ 1,-1, -1, 0, 1, 0],
[-1, 1, 1,-1, 1, 0],
[-1,-1, 0, 1,-1, 1],
[ 0,-1, 1,-1, 0, 1],
[ 1,-1, 0, 1,-1, 0]]
solution2 = [[0, 0, 1, 1, 0, 1],
[ 1, 1, 0, 0, 1, 0],
[ 0, 1, 1, 0, 1, 0],
[ 1, 0, 0, 1, 0, 1],
[ 0, 1, 1, 0, 0, 1],
[ 1, 0, 0, 1, 1, 0]]
game3 = [[0,-1, 1, 0,-1, 0],
[ 0, 1, 1, 0,-1, 1],
[ 1, 0,-1, 1,-1,-1],
[ 0,-1, 0,-1,-1,-1],
[ 1,-1, 1,-1, 0, 1],
[-1,-1,-1,-1,-1, 0]]
solution3 = [[0, 1, 1, 0, 1, 0],
[ 0, 1, 1, 0, 0, 1],
[ 1, 0, 0, 1, 1, 0],
[ 0, 1, 0, 1, 1, 0],
[ 1, 0, 1, 0, 0, 1],
[ 1, 0, 0, 1, 0, 0]]
def main():
#create the game and add the diferent puzzles
game = Game()
game.add_game(game1)
game.add_game(game2)
game.add_game(game3)
game.add_solution(solution1)
game.add_solution(solution2)
game.add_solution(solution3)
for i in range(0,3):
if i == 0:
initial_root = Node(game1)
solution = solution1
print("\n ---- First puzzle: ----")
elif i == 1:
initial_root = Node(game2)
solution = solution2
print("\n ---- Second puzzle: ----")
else:
initial_root = Node(game3)
solution = solution3
print("\n ---- Third puzzle: ----")
initial_root.print()
print("\nWe create the graph with all posible solution, this may take some time")
initial_root.create_node_child_rec()
print("Now we look for the solution with Breadth-First")
start_time = time.time()
bfs_visited_nodes = breadth_first_search(initial_root, solution)
end_time = time.time()
print(" Solution found with BdF visiting " + str(bfs_visited_nodes) + " Nodes")
print(" Execution time of BdF algorithm: {} ms \n" .format( round(((end_time - start_time)*1000),3)))
print("Now we will use the informed search with A*:")
start_time = time.time()
a_visited_nodes = A_search(initial_root, solution)
end_time = time.time()
print(" Solution found with A* visiting " + str(a_visited_nodes) + " Nodes")
print(" Execution time of A* algorithm: {} ms\n" .format( round((end_time - start_time)*1000, 3)))
return
if __name__ == '__main__':
main()