-
Notifications
You must be signed in to change notification settings - Fork 1
/
dot.py
126 lines (96 loc) · 3.11 KB
/
dot.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
import numpy as np
from brain import *
from graphics import *
class Dot:
def __init__(self, width, height, framewidth, x = None, y = None, velX = None, velY = None, brain = None, steps = 700):
self.x = x
self.y = y
self.prevX = x
self.prevY = y
self.vel = np.array([0,0])
if(x == None):
self.x = width / 2 + framewidth
self.prevX = width / 2 + framewidth
if(y == None):
self.y = height - 20
self.prevY = height - 20
if(not velX == None and not velY == None):
self.vel = np.array([velX, velY])
self.brain = Brain(brain=brain, size=steps)
self.alive = True
self.winner = False
self.fitness = 0
self.steps = steps
def getPoint(self):
return Point(self.x, self.y)
def move(self, index, maxSpeed):
self.vel[0] = self.vel[0] + self.brain.getDirection(index)[0]
self.vel[1] = self.vel[1] + self.brain.getDirection(index)[1]
if(self.vel[0] > maxSpeed):
self.vel[0] = maxSpeed
if(self.vel[0] < -maxSpeed):
self.vel[0] = -maxSpeed
if(self.vel[1] > maxSpeed):
self.vel[1] = maxSpeed
if(self.vel[1] < -maxSpeed):
self.vel[1] = -maxSpeed
self.prevX = self.x
self.prevY = self.y
self.x = self.x + self.vel[0]
self.y = self.y + self.vel[1]
def kill(self, steps):
self.alive = False
def isAlive(self):
return self.alive
def makeWinner(self, steps):
self.winner = True
self.steps = steps
def isWinner(self):
return self.winner
def getDistance(self, x, y):
return np.sqrt((self.x - x)**2 + (self.y - y)**2)
def calculateFitness(self, x, y):
distance = self.getDistance(x, y)
if(self.isAlive()):
if(self.isWinner()):
self.fitness = 10000 - 10 * self.steps - distance
else:
self.fitness = - 10 * self.steps - distance
else:
self.fitness = -10000 - 10 * self.steps - distance
def getX(self):
return self.x
def getY(self):
return self.y
def getPrevX(self):
return self.prevX
def getPrevY(self):
return self.prevY
def getVel(self):
return self.vel[0], self.vel[1]
def getFitness(self):
return self.fitness
def getSteps(self):
return self.steps
def getBrain(self):
return self.brain
def getWinner(self):
return self.winner
def setBrain(self, brain):
self.brain = brain
def setPosition(self, x, y):
self.x = x
self.y = y
def setToPrevPosition(self):
self.x = self.prevX
self.y = self.prevY
def reset(self, steps, width, height, framewidth):
self.x = width / 2 + framewidth
self.y = height - 20
self.prevX = width / 2 + framewidth
self.prevY = height - 20
self.vel = np.array([0,0])
self.alive = True
self.winner = False
self.steps = steps
self.fitness = 0