This repository was archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnt.py
More file actions
86 lines (67 loc) · 2.16 KB
/
Ant.py
File metadata and controls
86 lines (67 loc) · 2.16 KB
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
class Ant:
path = []
colony = 0
def __init__(self, tourSize):
self.path = []
self.colony = 0
self.isReverted = False
self.oldPath = []
def setColony(self, colony):
# Here am I?
self.colony = colony
def move(self, toCity):
if len(self.path) == 0:
self.path.append((self.colony, toCity))
return
(origin, destiny) = self.path[len(self.path)-1]
self.path.append((destiny, toCity))
def visited(self, city):
for (origin, destiny) in self.path:
if destiny == city:
return True
return False
def lastVisited(self):
if len(self.path) == 0:
return self.colony
(origin, destiny) = self.path[len(self.path)-1]
return destiny
def lastStay(self):
if len(self.path) == 0:
return self.colony
(origin, destiny) = self.path[len(self.path)-1]
return origin
def removePath(self, fromCity, toCity):
remain = []
for (origin, destiny) in self.path:
remain.append((origin, destiny))
print("remain", remain)
if origin == fromCity:
if toCity == toCity:
remain = []
if destiny == fromCity:
if origin == toCity:
remain = []
self.path = remain
def trailLength(self, graph):
length = 0
for (origin, destiny) in self.path:
length += graph[origin][destiny]
return length
def clear(self, colony=0):
self.path = []
self.colony = colony
self.isReverted = False
self.oldPath = []
isReverted = False
oldPath = []
def reverse(self):
if len(self.path) == 0:
return
if self.isReverted:
(origin, destiny) = self.oldPath.pop()
self.move(origin)
return
self.oldPath = self.path
(origin, destiny) = self.oldPath.pop()
self.path = [(destiny, origin)]
self.isReverted = True