-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.py
121 lines (96 loc) · 4.48 KB
/
car.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
import random
import road
import unittest
class Car:
lookaheadDistance = 5
def __init__(self, pos, speedFactor, carinfront, theRoad):
self.pos = pos
self.speed = 0.0
self.speedFactor = speedFactor
self.carinfront = carinfront
self.theRoad = theRoad
self.lastSpeed = -1.0
self.speedChangeFactor = 0.9
def getDesiredSpeed(self):
return float(self.speedFactor * self.getSpeedLimit())
def getSpeedLimit(self):
for i in range(1, self.lookaheadDistance):
if (self.theRoad.hasSpeedLimitChangeAt(self.pos + i)):
if (self.theRoad.getSpeedLimit(self.pos + i)<self.theRoad.getSpeedLimit(self.pos)):
return (self.theRoad.getSpeedLimit(self.pos + i) + self.theRoad.getSpeedLimit(self.pos))/2
else:
return self.theRoad.getSpeedLimit(self.pos)
return self.theRoad.getSpeedLimit(self.pos)
def __calculateSpeed(self):
self.speed = self.getDesiredSpeed()
carinfrontpos = self._getCarInFrontPosition()
if carinfrontpos - self.pos <= self.lookaheadDistance:
if self.speed > self.carinfront.speed:
self.speed = self.speed*self.speedChangeFactor
elif self.speed < self.carinfront.speed:
self.speed = self.speed/self.speedChangeFactor
self.lastSpeed = self.speed
def _getCarInFrontPosition(self):
return self.carinfront.pos
def move(self):
self.__calculateSpeed()
self.pos = self.pos + int(self.speed)
self.__avoidCrashing()
def __avoidCrashing(self):
if self.pos >= self._getCarInFrontPosition():
self.pos = self._getCarInFrontPosition() - 1
def getPosition(self):
return self.pos % 80
if __name__ == "__main__":
class Test(unittest.TestCase):
def testReduceSpeedWhenCarInFrontHasZeroSpeed(self):
speedlimit = 10
thisCarPos = 1
otherCarPos = 10
car = Car(thisCarPos,1.0,self._getDummyCar(otherCarPos),self.__getDummyRoad(speedlimit))
self.assertEquals(0, car.speed)
car.move()
self.assertEquals(speedlimit*car.speedFactor*car.speedChangeFactor, car.speed)
def testReduceSpeedWhenCarInFrontMovesSlowerInsideLookahead(self):
speedlimit = 10
myRoad = road.Road([(0,lambda: speedlimit)])
dummyCar = self._getDummyCar(1000)
firstCar = Car(5,0.8,dummyCar,myRoad)
car = Car(1,1.0,firstCar,myRoad)
firstCar.move()
car.move()
self.assertEquals(speedlimit*car.speedChangeFactor,car.speed)
def testDriveDesiredSpeedWhenCarInFrontIsOutsideLookahead(self):
speedlimit = 10
car = Car(1,1.2,self._getDummyCar(1000),road.Road([(0,lambda: speedlimit)]))
car.move()
self.assertEquals(car.getDesiredSpeed(),car.speed)
self.assertEquals(12,car.speed)
self.assertEquals(13,car.pos)
def testCalculatesCorrectSpeedLimit(self):
initLimit = 3
firstLimit = 4
secondLimit = 6
thirdLimit = 5
car = Car(1, 1.0, self._getDummyCar(1000), road.Road([
(0, lambda: initLimit),
(3, lambda: firstLimit),
(5, lambda: secondLimit),
(7, lambda: thirdLimit)]))
car.lookaheadDistance = 7
self.assertEquals(initLimit, car.getSpeedLimit())
car.pos = 3
self.assertEquals(firstLimit, car.getSpeedLimit())
car.pos = 4
self.assertEquals(firstLimit, car.getSpeedLimit())
car.pos = 5
self.assertEquals((thirdLimit+secondLimit)/2, car.getSpeedLimit())
car.pos = 7
self.assertEquals(thirdLimit, car.getSpeedLimit())
car.pos = 80
self.assertEquals(thirdLimit, car.getSpeedLimit())
def _getDummyCar(self,position):
return Car(position,1.0,None,None)
def __getDummyRoad(self, speedLimit):
return road.Road([(0,lambda: speedLimit)])
unittest.main()