-
Notifications
You must be signed in to change notification settings - Fork 100
/
test.py
74 lines (59 loc) · 2.16 KB
/
test.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
import random
import numpy as np
from environment import Env
import tensorflow as tf
from tensorflow.keras.layers import Dense
# 딥살사 인공신경망
class DeepSARSA(tf.keras.Model):
def __init__(self, action_size):
super(DeepSARSA, self).__init__()
self.fc1 = Dense(30, activation='relu')
self.fc2 = Dense(30, activation='relu')
self.fc_out = Dense(action_size)
def call(self, x):
x = self.fc1(x)
x = self.fc2(x)
q = self.fc_out(x)
return q
# 그리드월드 예제에서의 딥살사 에이전트
class DeepSARSAgent:
def __init__(self, state_size, action_size):
# 상태의 크기와 행동의 크기 정의
self.state_size = state_size
self.action_size = action_size
self.epsilon = 0.01
self.model = DeepSARSA(self.action_size)
self.model.load_weights('save_model/trained/model')
# 입실론 탐욕 정책으로 행동 선택
def get_action(self, state):
if np.random.rand() <= self.epsilon:
return random.randrange(self.action_size)
else:
q_values = self.model(state)
return np.argmax(q_values[0])
if __name__ == "__main__":
# 환경과 에이전트 생성
env = Env(render_speed=0.05)
state_size = 15
action_space = [0, 1, 2, 3, 4]
action_size = len(action_space)
agent = DeepSARSAgent(state_size, action_size)
scores, episodes = [], []
EPISODES = 10
for e in range(EPISODES):
score = 0
done = False
# env 초기화
state = env.reset()
state = np.reshape(state, [1, state_size])
while not done:
# 현재 상태에 대한 행동 선택
action = agent.get_action(state)
# 선택한 행동으로 환경에서 한 타임스텝 진행 후 샘플 수집
next_state, reward, done = env.step(action)
next_state = np.reshape(next_state, [1, state_size])
state = next_state
score += reward
if done:
# 에피소드마다 학습 결과 출력
print("episode: {:3d} | score: {:3d}".format(e, score))