Skip to content

Commit 6146fb6

Browse files
author
Anmol Jagetia
committedJun 28, 2017
Adds simple reinforcement learning model to repo
1 parent 72e516b commit 6146fb6

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
 

‎simple_rl.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import gym
2+
import universe
3+
import random
4+
5+
6+
#reinforcement learning step
7+
def determine_turn(turn, observation_n, j, total_sum, prev_total_sum, reward_n):
8+
#for every 15 iterations, sum the total observations, and take the average
9+
#if lower than 0, change the direction
10+
#if we go 15+ iterations and get a reward each step, we're doing something right
11+
#thats when we turn
12+
if(j >= 15):
13+
if(total_sum/ j ) == 0:
14+
turn = True
15+
else:
16+
turn = False
17+
18+
#reset vars
19+
total_sum = 0
20+
j = 0
21+
prev_total_sum = total_sum
22+
total_sum = 0
23+
24+
else:
25+
turn = False
26+
if(observation_n != None):
27+
#increment counter and reward sum
28+
j+=1
29+
total_sum += reward_n
30+
return(turn, j, total_sum, prev_total_sum)
31+
32+
33+
34+
35+
def main():
36+
37+
#init environment
38+
env = gym.make('flashgames.DuskDrive-v0')
39+
observation_n = env.reset()
40+
41+
#init variables
42+
# num of game iterations
43+
n = 0
44+
j = 0
45+
#sum of observations
46+
total_sum = 0
47+
prev_total_sum = 0
48+
turn = False
49+
50+
#define our turns or keyboard actions
51+
left = [('KeyEvent', 'ArrowUp', True) ,('KeyEvent', 'ArrowLeft', True), ('KeyEvent', 'ArrowRight', False)]
52+
right = [('KeyEvent', 'ArrowUp', True) ,('KeyEvent', 'ArrowLeft', False), ('KeyEvent', 'ArrowRight', True)]
53+
Forward = [('KeyEvent', 'ArrowUp', True) ,('KeyEvent', 'ArrowLeft', False), ('KeyEvent', 'ArrowRight', False)]
54+
55+
56+
#main logic
57+
while True:
58+
#increment a counter for number of iterations
59+
n+=1
60+
61+
#if at least one iteration is made, check if turn is needed
62+
if(n > 1):
63+
#if at least one iteration, check if a turn
64+
if(observation_n[0] != None):
65+
#store the reward in the previous score
66+
prev_score = reward_n[0]
67+
68+
#should we turn?
69+
if(turn):
70+
#pick a random event
71+
#where to turn?
72+
event = random.choice([left,right])
73+
#perform an action
74+
action_n = [event for ob in observation_n]
75+
#set turn to false
76+
turn = False
77+
78+
elif(~turn):
79+
#if no turn is needed, go straight
80+
action_n = [Forward for ob in observation_n]
81+
82+
83+
#if there is an obseravtion, game has started, check if turn needed
84+
if(observation_n[0] != None):
85+
turn, j, total_sum, prev_total_sum = determine_turn(turn, observation_n[0], j, total_sum, prev_total_sum, reward_n[0])
86+
87+
#save new variables for each iteration
88+
observation_n, reward_n, done_n, info = env.step(action_n)
89+
90+
env.render()
91+
92+
if __name__ == '__main__':
93+
main()

0 commit comments

Comments
 (0)