-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.py
152 lines (122 loc) · 3.65 KB
/
Vertex.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
from Edge import Edge
class Vertex():
def __init__(self, name, adjecencyList = []):
self.name = name
self.adjecent = adjecencyList
self.distance = None
self.previous = None
self.known = False
self.indegree = 0
#
# Adding A-Star properties ...
#
self.g = 0
self.h = 0
self.f = 0
#
# And a function returning x,y reference based on name of vertex being xnym where n and m are numbers
#
def __iter__(self):
return self
def __lt__(self, other):
if other:
if isinstance(other, Vertex):
return self.f < other.f
else:
return False
def __le__(self, other):
if other:
if isinstance(other, Vertex):
return self.f <= other.f
else:
return False
def __gt__(self, other):
if other:
if isinstance(other, Vertex):
return self.f > other.f
else:
return False
def __ge__(self, other):
if other:
if isinstance(other, Vertex):
return self.f >= other.f
else:
return False
def position(self):
# remnove "x" from the string
s = self.name.split('x')
pos = s[1].split('y')
return int(pos[0]), int(pos[1])
@property
def f(self):
return self.__f
@f.setter
def f(self, value):
self.__f = value
@property
def g(self):
return self.__g
@g.setter
def g(self, value):
self.__g = value
@property
def h(self):
return self.__h
@h.setter
def h(self, value):
self.__h = value
@property
def name(self):
return self.__name
@name.setter
def name(self, name):
self.__name = name
@property
def adjecent(self):
return self.__adjecencylist
@adjecent.setter
def adjecent(self, adjecencylist):
self.__adjecencylist = adjecencylist
@property
def distance(self):
return self.__distance
@distance.setter
def distance(self, distance):
self.__distance = distance
@property
def previous(self):
return self.__previous
@previous.setter
def previous(self, previous_node):
self.__previous = previous_node
@property
def known(self):
return self.__known
@known.setter
def known(self, known):
self.__known = known
@property
def indegree(self):
return self.__indegree
@indegree.setter
def indegree(self, indegree):
self.__indegree = indegree
def add_adjecent_edge(self,edge = None):
if edge:
if isinstance(edge, Edge):
if self.adjecent == []:
self.adjecent = [edge]
else:
self.adjecent.append(edge)
else:
raise TypeError("Attempt to add something in " + \
"adjecencylist which is not an edge")
def __str__(self):
retval = str(self.name) + ': ['
for e in self.adjecent:
retval += e.vertex.name + '(' + str(e.weight) + ') '
retval += '](known = ' + str(self.known) + ')(distance = ' + str(self.distance) + ')'
if self.distance:
retval += '(previous=\'' + str(self.previous.name) + '\')'
retval += '(indegree=\'' + str(self.indegree) + '\')'
return retval