-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathagent_to_train.py
More file actions
63 lines (51 loc) · 1.58 KB
/
agent_to_train.py
File metadata and controls
63 lines (51 loc) · 1.58 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Module that contains the Agent, an extencion of Trainer class.
This classes can be used to train a model.
It contains the following classes:
Agent
"""
# Local import
from .agent_to_play import AgentPlay
from .model import Model
# General imports
from random import randint, random
from copy import deepcopy as copy
__version__ = '0.5'
__author__ = 'Daniel Alcocer (daniel.alcocer@est.fib.upc.edu)'
"""
Extended class from Trainer that use RL.
"""
class AgentTrain(AgentPlay):
def __init__(self, role, pokemon, model,
epsilon_min = 0.01, epsilon_decay = 0.995):
self.epsilon = 1.0 # exploration rate
self.epsilon_min = epsilon_min
self.epsilon_decay = epsilon_decay
AgentPlay.__init__(self, role, pokemon, model)
#self.replay() #to quick debug
def set_state(self, state):
AgentPlay.set_state(self, state)
self.last_state = copy(state)
def choice_action(self):
if random() <= self.epsilon:
self._idmove = randint(0, 3)
self._target = randint(0, 1)
else:
AgentPlay.choice_action(self)
def recive_results(self, attacks, done):
state = self.last_state
next_state = self.actual_state
self.model.remember(state, self._idmove, self._target, \
self.role, attacks, next_state, done)
self.last_state = copy(self.actual_state)
# train the agent with the experience of the episode and restart the agent
def replay(self, pokemon):
self.model.train()
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
#reset trainer
self._pk=pokemon
def save_model(self):
self.model.save()