-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstub.py
61 lines (40 loc) · 1.47 KB
/
stub.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
import numpy.random as npr
import sys
from SwingyMonkey import SwingyMonkey
class Learner:
def __init__(self):
self.last_state = None
self.last_action = None
self.last_reward = None
def reset(self):
self.last_state = None
self.last_action = None
self.last_reward = None
def action_callback(self, state):
'''Implement this function to learn things and take actions.
Return 0 if you don't want to jump and 1 if you do.'''
# You might do some learning here based on the current state and the last state.
# You'll need to take an action, too, and return it.
# Return 0 to swing and 1 to jump.
new_action = npr.rand() < 0.1
new_state = state
self.last_action = new_action
self.last_state = new_state
return self.last_action
def reward_callback(self, reward):
'''This gets called so you can see what reward you get.'''
self.last_reward = reward
iters = 100
learner = Learner()
for ii in range(iters):
# Make a new monkey object.
swing = SwingyMonkey(sound=False, # Don't play sounds.
text="Epoch %d" % (ii), # Display the epoch on screen.
tick_length=1, # Make game ticks super fast.
action_callback=learner.action_callback,
reward_callback=learner.reward_callback)
# Loop until you hit something.
while swing.game_loop():
pass
# Reset the state of the learner.
learner.reset()