-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeMis.py
74 lines (60 loc) · 1.72 KB
/
NodeMis.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
from Board import *
class NodeMis:
def __init__(self,brd,parent=None):
self.brd = brd
self.parent = parent
self.children = []
self.depth=0
self.heur=self.misplaced()
if parent:
self.depth = parent.depth + 1
def __eq__(self,other):
if self.heur==other.heur:
return True
return False
def __lt__(self,other):
if self.heur<other.heur:
return True
return False
def get_brd(self):
return self.brd
def get_parent(self):
return self.parent
def path(self):
path = []
node = self
while node is not None:
path.append(node)
node = node.get_parent()
return list(reversed(path))
def NodeSolution(self):
if not self.brd.isSolution():
return False
return True
def expandNode(self):
#left
o=self.brd.expand("left")
if o!=False:
l=NodeMis(o,parent=self)#no
self.children.append(l)
#rigth
e=self.brd.expand("right")
if e!=False:
r=NodeMis(e,parent=self)#no
self.children.append(r)
#up
n=self.brd.expand("up")
if n!=False:
u=NodeMis(n,parent=self)#No
self.children.append(u)
#down
s=self.brd.expand("down")
if s!=False:
d=NodeMis(s,parent=self)#no
self.children.append(d)
return self.children
#heuristicas
def misplaced(self):
return self.brd.out_of_place()
def manhattan(self):
return self.brd.manhattan_distance()