This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathANT.py
executable file
·77 lines (66 loc) · 2.24 KB
/
ANT.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
__author__ = 'derketzer'
import GENOME
def sign(x):
return 1 if(x >= 0) else -1
def absolute_value(x):
return x if(x >= 0) else -x
class ANT:
def __init__(self, size, g = None):
if g is None:
self.genome = GENOME.GENOME(size)
else:
self.genome = g
self.prior = -1
self.applesWasEat = 0
self.moveToEatAllApples = 0
self.livingTime = 1
def changePrior(self, p):
self.prior = p
def returnPriority(self):
return self.prior
def returnAppleNumber(self):
return self.applesWasEat
def makeMoves(self, maxStep, mapSize, appleNumber, mapSource):
torMap = [[mapSource[i][j] for j in range(0, len(mapSource[i]))] for i in range(0, len(mapSource))]
self.applesWasEat = 0
self.moveToEatAllApples = 0
self.livingTime += 1
x = 0
y = 0
rotation = 0
st = self.genome.getStartState()
moves = 0
apples = appleNumber
while not apples == 0 and moves < 1.5 * maxStep:
nextX = x
nextY = y
if absolute_value(rotation) == 1:
nextY += sign(rotation)
else:
nextX += sign(rotation)
if nextX >= mapSize or nextX < 0:
nextX -= mapSize * sign(nextX)
if nextY >= mapSize or nextY < 0:
nextY -= mapSize * sign(nextY)
action = st.getAction(torMap[nextY][nextX])
outState = st.getOutState(torMap[nextY][nextX])
st = self.genome.getState(outState)
if action == 0:
x = nextX
y = nextY
if torMap[y][x] == 1:
apples -= 1
torMap[y][x] = 0
if moves < maxStep:
self.applesWasEat += 1
elif action == 2:
rotation -= 1
if rotation < -2:
rotation = 1
elif action == 1:
rotation += 1
if rotation > 1:
rotation = -2
moves += 1
self.moveToEatAllApples = moves
self.prior = self.applesWasEat + (maxStep - self.moveToEatAllApples) / (float(maxStep))