-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·262 lines (210 loc) · 8.03 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
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
import ia
from random import *
import random
import time
comparacoes = 0
def cria_tabuleiro(items):
"""
Funcao que cria um tabuleiro novo para ser resolvido pelos algoritmos inteligentes
@paaram items: itens que vao compor o tabuleiro. O maximo de itens e 9 para
construir uma matriz 3x3
"""
tabuleiro = {}
# posicionando as pecas
tabuleiro["pecas"] = [[items[i * 3 + j] for j in range(3)] for i in range(3)]
for i in range(3):
for j in range(3):
# encontrando a peca "branca" para movimentar
if tabuleiro["pecas"][i][j] == 0:
tabuleiro["posicao"] = i, j
break
return tabuleiro
# operacoes definidas em cima do tabuleiro. Todos devidamente testados
def move_esquerda(tabuleiro):
"""
Move a peca branca para esquerda.
@param tabuleiro: tabuleiro onde a peca sera movida
@return uma copia resultante do trabalho de movimentar a peca ou None caso continue
o mesmo.
"""
i, j = tabuleiro["posicao"] # obtendo a linha e a coluna do tabuleiro atual
if j == 0: return None # caso nao de pra realizar nenhum movimento a partir daqui
novo = {}
novo["pecas"] = [[elemento for elemento in linha] for linha in tabuleiro["pecas"]]
novo["pecas"][i][j], novo["pecas"][i][j - 1] = novo["pecas"][i][j - 1], novo["pecas"][i][j]
novo["posicao"] = i, j - 1
return novo
def move_direita(tabuleiro):
"""
Move a peca branca para direita.
@param tabuleiro: tabuleiro onde a peca sera movida
@return uma copia resultante do trabalho de movimentar a peca ou None caso continue
o mesmo.
"""
"""
Move a peca branca para cima.
@param tabuleiro: tabuleiro onde a peca sera movida
@return uma copia resultante do trabalho de movimentar a peca ou None caso continue
o mesmo.
"""
i, j = tabuleiro["posicao"] # obtendo a linha e a coluna do tabuleiro atual
if j == 2: return None # caso nao de pra realizar nenhum movimento a partir daqui
novo = {}
novo["pecas"] = [[elemento for elemento in linha] for linha in tabuleiro["pecas"]]
novo["pecas"][i][j], novo["pecas"][i][j + 1] = novo["pecas"][i][j + 1], novo["pecas"][i][j]
novo["posicao"] = i, j + 1
return novo
def move_cima(tabuleiro):
"""
Move a peca branca para cima.
@param tabuleiro: tabuleiro onde a peca sera movida
@return uma copia resultante do trabalho de movimentar a peca ou None caso continue
o mesmo.
"""
i, j = tabuleiro["posicao"] # obtendo a linha e a coluna do tabuleiro atual
if i == 0: return None # caso nao de pra realizar nenhum movimento a partir daqui
novo = {}
novo["pecas"] = [[elemento for elemento in linha] for linha in tabuleiro["pecas"]]
novo["pecas"][i][j], novo["pecas"][i - 1][j] = novo["pecas"][i - 1][j], novo["pecas"][i][j]
novo["posicao"] = i - 1, j
return novo
def move_baixo(tabuleiro):
"""
Move a peca branca para baixo.
@param tabuleiro: tabuleiro onde a peca sera movida
@return uma copia resultante do trabalho de movimentar a peca ou None caso continue
o mesmo.
"""
i, j = tabuleiro["posicao"] # obtendo a linha e a coluna do tabuleiro autal
if i == 2: return None
novo = {}
novo["pecas"] = [[elemento for elemento in linha] for linha in tabuleiro["pecas"]]
novo["pecas"][i][j], novo["pecas"][i + 1][j] = novo["pecas"][i + 1][j], novo["pecas"][i][j]
novo["posicao"] = i + 1, j
return novo
def funcao_custo(tabuleiro):
"""
Retorna o custo do arranjo atual do tabuleiro ate a meta
@param tabuleiro: tabuleiro em seu arranjo atual de pecas
@return: a quantidade de pecas fora do lugar
"""
return 1
def teste_meta(tabuleiro):
"""
Funcao que verifica se o arranjo atual do tabuleiro esta de acordo com a meta
@param tabuleiro: estado atual do tabuleiro
@return true se o tabuleiro e a meta, false caso contrario
"""
return tabuleiro["pecas"] == [[1, 2, 3], [4, 5, 6], [7, 8, 0]]
def enfileira_fifo(lista_1, lista_2):
return lista_2 + lista_1
def enfileira_lifo(lista_1,lista_2):
return lista_1 + lista_2
def embaralha(tabuleiro,operadores):
random.seed()
n = randint(1000,10000)
l = [0,1,2,3]
for i in range(n):
random.shuffle(l)
for j in l:
if operadores[j](tabuleiro) != None:
tabuleiro = operadores[j](tabuleiro)
#print tabuleiro["pecas"]
tabuleiro = operadores[randint(0,3)](tabuleiro)
return tabuleiro
def heuristica_desordenado(tabuleiro):
meta = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]
contador = 0
for i in range(3):
for j in range(3):
if meta[i][j] != tabuleiro["pecas"][i][j]:
contador = contador + 1
return contador
def heuristica_manhattan(tabuleiro):
meta = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]
soma = 0
#print tabuleiro["pecas"]
for i in range(3):
for j in range(3):
for k in range(3):
for l in range(3):
if meta[i][j] == tabuleiro["pecas"][k][l]:
soma = soma + abs(int(i - k)) + abs(int(j - l))
#print soma
return soma
def main():
items = [4,1,3,2,6,8,7,5,0] # items do tabuleiro
#random.shuffle(items) # embaralhando os items do tabuleiro
teste1 = cria_tabuleiro(items) # criando o tabuleiro
operadores = [move_baixo, move_cima, move_direita, move_esquerda] # lista de operadores do problema
# instanciando o problema
#teste = embaralha(t,operadores)
#teste1 = [[1,2,3],[4,0,6],[7,5,8]]
#Problemas:
"""Sem heuristica:"""
#largura
problema = ia.Problema(teste1, operadores, teste_meta, funcao_custo)
#profundiade
problema1 = ia.Problema(teste1, operadores, teste_meta, funcao_custo)
#gulosa
problema2 = ia.Problema(teste1, operadores, teste_meta, heuristica_desordenado)
#A* desordenado
problema3 = ia.Problema(teste1, operadores, teste_meta, heuristica_desordenado)
#A* Manhattan
problema4 = ia.Problema(teste1, operadores, teste_meta, heuristica_manhattan)
ini = time.time()
resultado = ia.busca(problema, enfileira_fifo)
fim = time.time()
ini1 = time.time()
resultado1 = ia.busca(problema1, enfileira_lifo)
fim1 = time.time()
ini2 = time.time()
resultado2 = ia.buscagulosa(problema2, enfileira_lifo)
fim2 = time.time()
ini3 = time.time()
resultado3 = ia.buscaaestrela(problema3, enfileira_lifo)
fim3 = time.time()
ini4 = time.time()
resultado4 = ia.buscaaestrela(problema4, enfileira_lifo)
fim4 = time.time()
print "Busca em Largura"
print "Estado Inicial:",teste1["pecas"]
print "Saida Busca em Largura: ", resultado
print "Tempo: ", fim - ini
print "Numero de comparacoes: ", problema.comparacoes
print "-------------------------------------------------"
print "Busca em Profundiade"
print "Estado Inicial:",teste1["pecas"]
print "Saida Busca em Largura: ", resultado1
print "Tempo: ", fim1 - ini1
print "Numero de comparacoes: ", problema1.comparacoes
print "-------------------------------------------------"
print "Busca em Gulosa"
print "Estado Inicial:",teste1["pecas"]
print "Saida Busca em Largura: ", resultado2
print "Tempo: ", fim2 - ini2
print "Numero de comparacoes: ", problema2.comparacoes
print "-------------------------------------------------"
print "Busca A* numero de pessas fora do lugar"
print "Estado Inicial:",teste1["pecas"]
print "Saida Busca em Largura: ", resultado3
print "Tempo: ", fim3 - ini3
print "Numero de comparacoes: ", problema3.comparacoes
print "-------------------------------------------------"
print "Busca A* Distancia de Manhattan"
print "Estado Inicial:",teste1["pecas"]
print "Saida Busca em Largura: ", resultado4
print "Tempo: ", fim4 - ini4
print "Numero de comparacoes: ", problema4.comparacoes
print "-------------------------------------------------"
"""Com heuristica:"""
""" Numero de pecas fora de posicao """
"""problema = ia.Problema(teste, operadores, teste_meta, heuristica_desordenado)"""
""" Distancia de Manhattan """
"""problema = ia.Problema(teste, operadores, teste_meta, heuristica_manhattan)"""
"""Buscas:"""
"""print "Saida Busca em Profundidade:", ia.busca(problema, enfileira_lifo)"""
"""print "Saida Busca Gulosa:", ia.buscagulosa(problema, enfileira_lifo)"""
"""print "Saida Busca A*:", ia.buscaaestrela(problema, enfileira_lifo)"""
return 0
print "O programa executou com saida %d" % (main())