-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.py
60 lines (49 loc) · 1.91 KB
/
ops.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
import numpy as np
import tensorflow as tf
import gym
import cv2 as cv
from collections import namedtuple,deque
def preprocess(img):
grayImg = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
resized = cv.resize(grayImg,(84,110))
preProcessedImg = resized[:84,:84]
return preProcessedImg
def initExperienceReplay(env,initReplaySize,cell):
replayBuffer = deque()
state = env.reset()
state = preprocess(state)
state = np.stack([state]*4,axis=2)
print("Filling Experience memory of the agent")
for i in range(initReplaySize):
action = env.action_space.sample()
nextState, reward, isDone, _ = env.step(action)
nextState = preprocess(nextState)
nextState = np.append(state[:,:,1:],nextState[:,:,np.newaxis],axis=2)
replayBuffer.append(cell(state,reward,action,nextState,isDone))
if(isDone):
state = env.reset()
state = preprocess(state)
state = np.stack([state]*4,axis=2)
else:
state = nextState
env.close()
print("Filled memory of size {}".format(len(replayBuffer)))
return replayBuffer
def EGreedyPolicy(epsilon,QValues):
numActions = QValues.shape[1]
probs = np.ones(numActions, dtype=float) * epsilon / numActions
best_action = np.argmax(QValues)
probs[best_action] += (1.0 - epsilon)
#print(probs)
optimizedAction = np.random.choice(numActions,p=probs)
return optimizedAction
def copyParameters(sess,targetModel,QModel):
params1 = [var for var in tf.trainable_variables() if var.name.startswith(targetModel.scope)]
params1 = sorted(params1,key=lambda var: var.name)
params2 = [var for var in tf.trainable_variables() if var.name.startswith(QModel.scope)]
params2 = sorted(params2,key=lambda var: var.name)
copies = []
for p1,p2 in zip(params1,params2):
copy = p1.assign(p2)
copies.append(copy)
sess.run(copies)