Skip to content

Commit 675796c

Browse files
authored
Funtions Implementation
BFS DFS A* Greedy
1 parent e4a07c2 commit 675796c

1 file changed

Lines changed: 199 additions & 0 deletions

File tree

Functions.py

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#! /usr/bin/env python3
2+
3+
from collections import deque
4+
5+
6+
class MinHeap:
7+
def __init__(self, goalstate, compare):
8+
self.data = [None]
9+
self.size = 0
10+
self.comparator = compare
11+
self.goalstate = goalstate
12+
13+
def __len__(self):
14+
return self.size
15+
16+
def __contains__(self, item):
17+
return item in self.data
18+
19+
def __str__(self):
20+
return str(self.data)
21+
22+
def _compare(self, x, y):
23+
x = self.comparator(self.data[x], self.goalstate)
24+
y = self.comparator(self.data[y], self.goalstate)
25+
26+
if x < y:
27+
return True
28+
else:
29+
return False
30+
31+
def getpos(self, x):
32+
for i in range(self.size+1):
33+
if x == self.data[i]:
34+
return i
35+
return None
36+
37+
def _upHeap(self, i):
38+
while i > 1 and self._compare(i, int(i/2)):
39+
self._swap(i, int(i/2))
40+
i = int(i/2)
41+
42+
def _downHeap(self, i):
43+
size = self.size
44+
while 2*i <= size:
45+
j = 2*i
46+
if j < size and self._compare(j+1, j):
47+
j += 1
48+
if self._compare(i, j):
49+
break
50+
self._swap(i, j)
51+
i = j
52+
53+
def _swap(self, i, j):
54+
t = self.data[i]
55+
self.data[i] = self.data[j]
56+
self.data[j] = t
57+
58+
def push(self, x):
59+
self.size += 1
60+
self.data.append(x)
61+
self._upHeap(self.size)
62+
63+
def pop(self):
64+
if self.size < 1:
65+
return None
66+
t = self.data[1]
67+
self.data[1] = self.data[self.size]
68+
self.data[self.size] = t
69+
self.size -= 1
70+
self._downHeap(1)
71+
self.data.pop()
72+
return t
73+
74+
def peek(self):
75+
if self.size < 1:
76+
return None
77+
return self.data[1]
78+
79+
80+
# comparadores
81+
def hamming(inicialState, goalstate):
82+
inicial = inicialState.estado
83+
goal = goalstate.estado
84+
depth = inicialState.profundidade
85+
sum = 0
86+
for x, y in zip(goal, inicial):
87+
if x != y and x != '0':
88+
sum += 1
89+
return sum + depth
90+
91+
92+
def manhattan(inicialState, goalstate):
93+
inicial = inicialState.estado
94+
goal = goalstate.estado
95+
depth = inicialState.profundidade
96+
sum = 0
97+
for i in range(16):
98+
if goal[i] == '0':
99+
continue
100+
x1, y1 = (int(i / 4), i % 4)
101+
for j in range(16):
102+
if goal[i] == inicial[j]:
103+
x2, y2 = (int(j / 4), j % 4)
104+
sum += abs(x1 - x2) + abs(y1 - y2)
105+
break
106+
return sum + depth
107+
108+
109+
# Algoritmos de Pesquisa
110+
111+
# Breadth-first search
112+
def bfs(inicialState, goalstate):
113+
total_nos = 1
114+
fronteira = deque()
115+
fronteira.append(inicialState)
116+
117+
while len(fronteira) > 0:
118+
state = fronteira.popleft()
119+
120+
if goalstate == state:
121+
return state.backtrack, total_nos
122+
for filho in state.moves():
123+
total_nos += 1
124+
fronteira.append(filho)
125+
return False, total_nos
126+
127+
# Depth-first search
128+
def dfs(inicialState, goalstate, depth):
129+
total_nos = 1
130+
fronteira = list()
131+
visitados = set()
132+
fronteira.append(inicialState)
133+
134+
while len(fronteira) > 0:
135+
state = fronteira.pop()
136+
visitados.add(state)
137+
138+
if state == goalstate:
139+
return state.backtrack, total_nos
140+
141+
for filho in state.moves():
142+
total_nos += 1
143+
if filho.profundidade <= depth:
144+
if filho not in visitados or filho not in fronteira:
145+
fronteira.append(filho)
146+
return False, total_nos
147+
148+
# Iteractive Depth-first search
149+
def idfs(inicialState, goalstate):
150+
total_nos = 1
151+
i = 1
152+
moves, nodes = dfs(inicialState, goalstate, i)
153+
total_nos += nodes
154+
while not moves:
155+
i += 1
156+
moves, nodes = dfs(inicialState, goalstate, i)
157+
total_nos += nodes
158+
return moves, total_nos
159+
160+
# Greedy
161+
def guloso(inicialState, goalstate, comparador):
162+
total_nos = 1
163+
state = inicialState
164+
165+
while state != goalstate:
166+
filhos = state.moves()
167+
state = filhos.pop()
168+
for x in filhos:
169+
total_nos += 1
170+
if comparador(x, goalstate) < comparador(state, goalstate):
171+
state = x
172+
173+
return state.backtrack, total_nos
174+
175+
# A*
176+
def astar(inicialState, goalstate, comparador):
177+
total_nos = 1
178+
fronteira = MinHeap(goalstate, comparador)
179+
fronteira.push(inicialState)
180+
visitados = set()
181+
182+
while len(fronteira) > 0:
183+
state = fronteira.pop()
184+
visitados.add(state)
185+
186+
if goalstate == state:
187+
return state.backtrack, total_nos
188+
189+
for filho in state.moves():
190+
total_nos += 1
191+
if filho not in fronteira and filho not in visitados:
192+
fronteira.push(filho)
193+
elif filho in fronteira:
194+
i = fronteira.getpos(filho)
195+
if fronteira.data[i].profundidade > filho.profundidade:
196+
fronteira.data[i] = filho
197+
fronteira._upHeap(i)
198+
199+
return False, total_nos

0 commit comments

Comments
 (0)