def train(self, num_episodes=1000, mean_stopping=True):
    for episode in range(num_episodes):
        state = self.env.reset()
        reward_for_episode = 0
        max_num_steps = 1000
        for step in range(max_num_steps):
            action = self.get_action(state)
            if isinstance(action, tf.Tensor):
                action = action.numpy()
            (next_state, reward, done, info) = self.env.step(int(action))
            self.buffer.append(Experience(state, action, reward, next_state, done))
            reward_for_episode += reward
            state = next_state
            self.update_weights()
            if (((step + 1) % self.target_update_interval) == 0):
                self._update_target(self.model_target.variables, self.model.variables)
            if done:
                break
        self.rewards_list.append(reward_for_episode)
        if (self.epsilon > self.epsilon_min):
            self.epsilon *= self.epsilon_decay
        last_rewards_mean = np.mean(self.rewards_list[(- 100):])
        if ((last_rewards_mean > 250) and mean_stopping):
            print('DQN Training Complete...')
            break
        print('[{:0>3}] Reward: {: >8.3f} | Avg Reward: {: >8.3f} | e: {:.3f} | Episode Length: {:}'.format(episode, reward_for_episode, last_rewards_mean, self.epsilon, step))
        if self.log_wandb:
            wandb.log({'Episode': episode, 'Reward': reward_for_episode, 'Avg-Reward-100e': last_rewards_mean, 'Epsilon': self.epsilon, 'Episode Length': step})
def train(self, num_episodes=1000, mean_stopping=True):
    for episode in range(num_episodes):
        state = self.env.reset()
        reward_for_episode = 0
        max_num_steps = 1000
        for step in range(max_num_steps):
            action = self.get_action(state)
            if isinstance(action, tf.Tensor):
                action = action.numpy()
            (next_state, reward, done, info) = self.env.step(int(action))
            self.buffer.append(Experience(state, action, reward, next_state, done))
            reward_for_episode += reward
            state = next_state
            self.update_weights()
            if (((step + 1) % self.target_update_interval) == 0):
                self._update_target(self.model_target.variables, self.model.variables)
            if done:
                break
        self.rewards_list.append(reward_for_episode)
        if (self.epsilon > self.epsilon_min):
            self.epsilon *= self.epsilon_decay
        last_rewards_mean = np.mean(self.rewards_list[(- 100):])
        if ((last_rewards_mean > 250) and mean_stopping):
            print('DQN Training Complete...')
            break
        print('[{:0>3}] Reward: {: >8.3f} | Avg Reward: {: >8.3f} | e: {:.3f} | Episode Length: {:}'.format(episode, reward_for_episode, last_rewards_mean, self.epsilon, step))
        if self.log_wandb:
            wandb.log({'Episode': episode, 'Reward': reward_for_episode, 'Avg-Reward-100e': last_rewards_mean, 'Epsilon': self.epsilon, 'Episode Length': step})