-
Notifications
You must be signed in to change notification settings - Fork 0
/
Greedy-misplaced.py
64 lines (53 loc) · 1.55 KB
/
Greedy-misplaced.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
from Board import *
from NodeMis import *
import heapq
import time
#algoritmo geral misturado com best-first.search
def best_first_search(start,func):
visto=set()
h=[]
if (not start.possible()):
print("Impossible To Solve! ")
return False
no=NodeMis(start)
heapq.heappush(h,no)
visto.add(no_tuplo(no))
while(len(h)>0):
prinode=heapq.heappop(h) #este algortimo faz pop sempre do primeiro elemento
if prinode.NodeSolution():
x = len(visto)
print("Memory:", x)
y = len(prinode.path())-1
print("Path:", y)
return prinode.path()#a fazer
child_list=prinode.expandNode()
h=func(h,child_list,visto)
print("Solution Not Found")
return False
#funçao de inserçao g
def insert_g(heap,child_list,visto):#gulosa
for i in child_list:
if no_tuplo(i) not in visto:
heapq.heappush(heap,i)
visto.add(no_tuplo(i))
return heap
#greedy algorithm
def greedy_search(start):
return best_first_search(start,insert_g)
#estando a usar nos com heuristica misplaced:
def Greedymisplaced():
print("Greedy Search - Heuristic Misplaced")
start=[int(n) for n in input().split()]
goal=[int(n) for n in input().split()]
mstart=list_to_matrix(start)
jogo=Board(mstart,goal)
inicio=time.time()
result=greedy_search(jogo)
fim=time.time()
total=fim-inicio
print("Execution Time:", total)
if result!=False:
for i in result:
print(i.brd)
return
Greedymisplaced()