-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch18-gameengine.py
68 lines (45 loc) · 1.61 KB
/
ch18-gameengine.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
import math
import random
import feedback as fb
class GameEngine( fb.Component ):
def __init__( self ):
self.n = 0 # Number of game objects
self.t = 0 # Steps since last change
self.resolutions = [ 100, 200, 400, 800, 1600 ] # memory per game obj
def work( self, u ):
self.t += 1
if self.t > random.expovariate( 0.1 ): # 1 chg every 10 steps on avg
self.t = 0
self.n += random.choice( [-1,1] )
self.n = max( 1, min( self.n, 50 ) ) # 1 <= n <= 50
crr = self.resolutions[u] # current resolution
return crr*self.n # current memory consumption
def monitoring( self ):
return "%d" % (self.n,)
class DeadzoneController( fb.Component ):
def __init__( self, deadzone ):
self.deadzone = deadzone
def work( self, u ):
if abs( u ) < self.deadzone:
return 0
if u < 0: return -1
else: return 1
class ConstrainingIntegrator( fb.Component ):
def __init__( self ):
self.state = 0
def work( self, u ):
self.state += u
self.state = max(0, min( self.state, 4 ) ) # Constrain to 0..4
return self.state
class Logarithm( fb.Component ):
def work( self, u ):
if u <= 0: return 0
return math.log(u)
if __name__ == '__main__':
fb.DT = 1
def setpoint(t):
return 3.5*math.log( 10.0 )
c = DeadzoneController( 0.5*math.log(8.0) )
p = GameEngine()
fb.closed_loop( setpoint, c, p,actuator=ConstrainingIntegrator(),
returnfilter=Logarithm() )