Skip to content

Commit

Permalink
✨ Adicionando nova heuristica
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-RCastro committed Jun 9, 2021
1 parent d8e9d7a commit bb6c3d2
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions agentes/problemas/eightpuzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_posicao_vazia(tabuleiro):

@staticmethod
def funcao_avaliacao(tabuleiro, custo_acumulado):
return ProblemaEightPuzzle.funcao_heuristica(tabuleiro) + custo_acumulado
return ProblemaEightPuzzle.funcao_heuristica2(tabuleiro) + custo_acumulado

@staticmethod
def funcao_heuristica(tabuleiro):
Expand All @@ -109,7 +109,22 @@ def funcao_heuristica(tabuleiro):
if tabuleiro[i] != solucao[i]:
incorretos += 1
return incorretos


@staticmethod
def funcao_heuristica2(tabuleiro):
tamanho_tabuleiro = len(tabuleiro)
resultado, contador = 0, 1
for i in range(0, tamanho_tabuleiro):
for j in range(0, tamanho_tabuleiro):
index = tabuleiro[i][j] - 1
if index == -1:
distance = (2 - i) + (2 - j)
else:
distance = abs(i - (index / tamanho_tabuleiro)) + abs(j - (index % tamanho_tabuleiro))
resultado += distance
contador += 1
return resultado

@staticmethod
def get_solucao():
return [1,2,3,4,5,6,7,8,0]
Expand Down

0 comments on commit bb6c3d2

Please sign in to comment.