forked from bhattacharyya/reach_circle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreach_circle_explore_exploit.py
More file actions
160 lines (133 loc) · 4.23 KB
/
Copy pathreach_circle_explore_exploit.py
File metadata and controls
160 lines (133 loc) · 4.23 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
from tkinter import *
from tkinter.ttk import *
import random
import time
import numpy as np
# Set global values
alpha = 0.9
gamma = 1.0
freq = 10 # Set the time gap of canvas update
initial_pos_x1 = 50
initial_pos_y1 = 80
mov_list = [-20,-10,10,20]
fout = open("logfile.txt","w")
#Set up Q table
state_space = {} # Set of all possible states
temp_list = []
for n in range(0,500,10):
for m in range(0,360,10):
temp_list.append((n,m))
for k in range(1800):
state_space[k] = temp_list[k]
action_space = {} # Set of all possible actions
temp_list = []
for n in mov_list:
for m in mov_list:
temp_list.append((m,n))
for k in range(16):
action_space[k] = temp_list[k]
q_table = np.zeros([len(state_space),len(action_space)])
#q_table = np.loadtxt("qtable.csv",delimiter=',')
#Set up Environment and Simulation
class GFG:
def __init__(self, master = None):
# Set up local variables
self.steps = 0
self.checkpoint = 0
self.pos_x1 = initial_pos_x1
self.pos_y1 = initial_pos_y1
self.x1 = 10
self.y1 = 10
self.game = 0
self.reward = 0
self.reset = 0
# canvas object to create shape
self.canvas = Canvas(master,width=500, height=360, bg="black")
# creating circle
self.circle = self.canvas.create_oval( 42, 72, 60, 90, fill = "green")
self.circle4 = self.canvas.create_oval( 230, 160, 270, 200, outline = "yellow")
self.canvas.pack()
# Set purely exploration mode
self.epsilon = 1.0
# Switch between explore and exploit
self.exploit_flag = 0
self.movement()
def movement(self): # Movement of Green Ball
if self.reset == 1:
new_x1 = (initial_pos_x1 - self.pos_x1)
new_y1 = (initial_pos_y1 - self.pos_y1)
self.canvas.move(self.circle,new_x1,new_y1)
self.pos_x1 += new_x1
self.pos_y1 += new_y1
self.reset = 0
time.sleep(1)
action = 0
self.reward = 0
state = list(state_space.keys())[list(state_space.values()).index((self.pos_x1, self.pos_y1))]
self.steps += 1
if self.game % 50 == 0 and self.exploit_flag == 1:
remainder = self.game / 50
self.epsilon = 1 - 0.2*(remainder-1) # Slowly exlore less and exploit more
# If max steps is reached
if self.steps % 200 == 0:
self.checkpoint = self.steps
self.game += 1
print("Game : "+str(self.game) + " lost")
fout.write("game "+str(self.game) + " lost\n")
fout.flush()
new_x1 = (initial_pos_x1 - self.pos_x1)
new_y1 = (initial_pos_y1 - self.pos_y1)
self.canvas.move(self.circle, new_x1, new_y1)
self.pos_x1 += new_x1
self.pos_y1 += new_y1
if self.game < 300: # Purely explore till these many games
self.x1 = random.choice(mov_list)
self.y1 = random.choice(mov_list)
action = list(action_space.keys())[list(action_space.values()).index((self.x1, self.y1))]
else:
# Start progressively exploiting more
self.exploit_flag = 1
if random.random() < self.epsilon:
self.x1 = random.choice(mov_list)
self.y1 = random.choice(mov_list)
action = list(action_space.keys())[list(action_space.values()).index((self.x1, self.y1))]
else:
action = np.argmax(q_table[state])
self.x1, self.y1 = action_space[action]
# Bounce back from boundaries
if self.pos_x1 > 450:
self.x1 = -10
if self.pos_x1 < 25:
self.x1 = 10
if self.pos_y1 > 300:
self.y1 = -10
if self.pos_y1 < 25:
self.y1 = 10
# Update position
self.canvas.move(self.circle, self.x1, self.y1)
self.pos_x1 += self.x1
self.pos_y1 += self.y1
# If target obtained
if self.pos_x1 == 250 and self.pos_y1 == 180:
self.reward = 100
self.game += 1
print("Game : "+str(self.game) + " won")
fout.write("game "+str(self.game) + " won\n")
fout.flush()
self.steps = self.checkpoint # Reset steps to start of the run for 200
self.reset = 1
# Update the Q table
old_q_value = q_table[state, action]
next_state = list(state_space.keys())[list(state_space.values()).index((self.pos_x1, self.pos_y1))]
next_max = np.max(q_table[next_state])
q_target = self.reward + gamma * next_max
q_delta = q_target - old_q_value
q_table[state, action] = old_q_value + alpha * q_delta
self.canvas.after(freq, self.movement)
if __name__ == "__main__":
# object of class Tk, resposible for creating
# a tkinter toplevel window
master = Tk()
gfg = GFG(master)
mainloop()