Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DQNAgent:
- def __init__(self, state_size, action_size):
- self.state_size = state_size
- self.action_size = action_size
- self.memory = deque(maxlen=300)
- self.gamma = 0.95 # discount rate
- self.epsilon = 1.0 # exploration rate
- self.epsilon_min = 0.01
- self.epsilon_decay = 1e-3
- self.learning_rate = 0.001
- self.model = self._build_model()
- self.target_model = self._build_model()
- self.update_target_model()
- self.start_reduce_epsilon = 200
- def _huber_loss(self, y_true, y_pred, clip_delta=1.0):
- error = y_true - y_pred
- cond = K.abs(error) <= clip_delta
- squared_loss = 0.5 * K.square(error)
- quadratic_loss = 0.5 * K.square(clip_delta) + clip_delta * (K.abs(error) - clip_delta)
- return K.mean(tf.where(cond, squared_loss, quadratic_loss))
- def _build_model(self):
- # Neural Net for Deep-Q learning Model
- model = Sequential()
- model.add(Dense(24, input_dim=self.state_size, activation='relu'))
- model.add(Dense(24, activation='relu'))
- model.add(Dense(self.action_size, activation='linear'))
- model.compile(loss=self._huber_loss,
- optimizer=Adam(lr=self.learning_rate))
- return model
- def update_target_model(self):
- # copy weights from model to target_model
- self.target_model.set_weights(self.model.get_weights())
- def remember(self, state, action, reward, next_state, done):
- self.memory.append((state, action, reward, next_state, done))
- def act(self, state):
- if np.random.rand() <= self.epsilon:
- return random.randrange(self.action_size)
- act_values = self.model.predict(state)
- return np.argmax(act_values[0]) # returns action
- def replay(self, batch_size):
- minibatch = random.sample(self.memory, batch_size)
- for state, action, reward, next_state, done in minibatch:
- target = self.model.predict(state)
- if done:
- target[0][action] = reward
- else:
- t = self.target_model.predict(next_state)[0]
- target[0][action] = reward + self.gamma * np.amax(t)
- self.model.fit(state, target, epochs=1, verbose=0)
- def update_epsilon(self, total_step):
- if self.epsilon > self.epsilon_min and total_step > self.start_reduce_epsilon:
- self.epsilon -= self.epsilon_decay
- def load(self, name):
- self.model.load_weights(name)
- def save(self, name):
- self.model.save_weights(name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement