5. 강화학습 공부 📖
•좋은 리소스가 많이 있음
•김성훈 교수님 YouTube 강의
•Sutton 교수님 책 + David Silver 강의
•이웅원님 책 https://www.gitbook.com/book/dnddnjs/rl/details
•각종 블로그 + GitHub내 프로젝트들
66. def _step(self, action):
# 주어진 동작을 위치로 마크를 배치
loc = action
reward = NO_REWARD
self.board[loc] = tocode(self.mark)
# 승/패가 결정되면 마크에 맞는 리워드를 반환
status = check_game_status(self.board)
if status >= 0:
self.done = True
if status in [1, 2]:
reward = O_REWARD if self.mark == 'O' else X_REWARD
# 다음 플레이어로 교체
self.mark = next_mark(self.mark)
# 새 상태, 리워드, 에피소드 종료 여부 반환
return self._get_obs(), reward, self.done, None
68. def _render(self, mode='human', close=False):
# human 모드에서만 보드를 표시
if mode == 'human':
self._show_board()
def _show_board(self):
# 보드 그리기
for j in range(0, 9, 3):
# 마크 코드를 문자로 0 -> ‘ ‘, 1 -> ‘O’, 2 -> ’X’
print(‘|’.join([tomark(self.board[i])
for i in range(j, j+3)]))
if j < 6:
printprint('-----')
78. class TDAgent(object):
# 상태에 대해 e-greedy정책으로 동작결정
def egreedy_policy(self, state, ava_actions):
...
# 임의 동작
def random_action(self, ava_actions):
...
# 상태에 대한 탐욕적 동작
def greedy_action(self, state, ava_actions):
...
# 상태 가치 업데이트
def backup(self, state, nstate, reward):
...
80. def egreedy_policy(self, state, ava_actions):
e = random.random()
# 랜덤 값이 epsilon 보다 작으면
if e < self.epsilon:
# 임의의 동작 선택
action = self.random_action(ava_actions)
else:
# 아니면 가치 기준으로 동작 선택
action = self.greedy_action(state, ava_actions)
return action
82. def greedy_action(self, state, ava_actions):
ava_values = []
# 모든 가능한 동작에 대해
for action in ava_actions:
# 동작 후 상태의 가치를 구함
nstate = self.after_action_state(state, action)
nval = self.ask_value(nstate)
ava_values.append(nval)
c # 그중 ‘O’는 최대 가치, ‘X’는 최소 가치의 동작을 선택
val_arr = np.array(ava_values)
if self.mark == 'O':
midx = np.argwhere(val_arr == np.max(val_arr))
else:
midx = np.argwhere(val_arr == np.min(val_arr))
aidx = np.random.choice(midx.ravel())
action = ava_actions[aidx]
return action