-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
87 lines (73 loc) · 2.89 KB
/
agent.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from collections import deque
import random
import numpy as np
#from tensorflow.python.keras.models import clone_model
import tensorflow as tf
#from main import reshape_state
import utils
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
class Agent:
def __init__(self, lr=0.001, gamma=.99, start=1, end=0.05, decay=.99, test_episodes=25, traget_accuracy=.85):
print('Create & initialize an agent ------------')
env = utils.createEnvironment()
self.set_environemnt(env)
self.set_hyperparameters(lr, gamma)
self.set_ep_greedy_parameters(start, end, decay)
self.set_evaluation_parameters(test_episodes, traget_accuracy)
def set_hyperparameters(self, lr=0.001, gamma=0.99):
self.learning_rate = lr
self.gamma = gamma
def set_evaluation_parameters(self, test_episodes, traget_accuracy):
self.test_episodes = test_episodes
self.traget_accuracy = traget_accuracy
def set_environemnt(self, env):
self.env = env
self.state_size = env.observation_space.n
self.action_size = env.action_space.n
def set_ep_greedy_parameters(self, start=1, end=.05, decay=.99):
self.start = start
self.end = end
self.decay = decay
def all_Qs(self, state):
pass
def act(self, state):
return np.argmax(self.all_Qs(state))
def get_epsilon(self, step=0):
return max(self.end, self.start * (self.decay**step))
def select_action(self, state, step):
epsilon = self.get_epsilon(step)
random_selection = (np.random.rand() <= epsilon)
return np.random.randint(0, self.action_size) if(random_selection) \
else self.act(state)
def evaluate(self):
total_reward = 0
for episode in range(self.test_episodes):
env = utils.random_env()
state = env.reset()
episode_reward = 0
step = 0
while True:
step = step + 1
action = self.act(state)
next_state, reward, done, _ = env.step(action)
episode_reward += reward
if(done or (next_state == state) or (step > self.state_size)):
total_reward = total_reward + reward
break
state = next_state
env.render()
self.print_policy(4)
accuracy = total_reward / self.test_episodes
print(accuracy)
return (accuracy)
def print_policy(self, ROW_SIZE=4):
display_actions = [' ⬅ ', ' ⬇ ', ' ➡ ', ' ⬆ ']
for i in range(self.state_size):
if 0 == (i % ROW_SIZE):
print()
print(display_actions[self.act(i)], end='')
print()
def log_reward_of_episode(self, episode, global_step, step, reward):
print(episode, end=': ')
utils.print_line(global_step, self.get_epsilon(episode), step, reward)