<jupyter_start><jupyter_text>Win/Lose in corners<jupyter_code>wins = []
loses = []

for episode in episodes:
    if episode.reward[-1] == -1:
        loses.append(episode.agent_id)
    elif episode.reward[-1] == 1:
        wins.append(episode.agent_id)
    else:
        print("FUCK")

plt.hist(loses)
plt.hist(wins)
plt.show()
np.bincount(loses)<jupyter_output><empty_output><jupyter_text>Episode length<jupyter_code>episode_length = []
for episode in episodes:
    episode_length.append(episode.get_num_steps())
    
plt.figure(figsize=(17,4))
plt.bar(range(0, len(episode_length)), episode_length)
plt.plot([0, len(episode_length)], [np.mean(episode_length), np.mean(episode_length)], 'r')
plt.legend(['mean', 'episode length'])
plt.show()
np.min(episode_length), np.max(episode_length), np.sum(np.array(episode_length) < 30)<jupyter_output><empty_output><jupyter_text>Rewards<jupyter_code>nr_wins = [0] * 4
nr_loose = [0] * 4
nr_tie = [0] * 4
average_score = [[], [], [], []]

for episode in episodes:
    if episode.reward[-1] == 1:
        nr_wins[episode.agent_id] += 1
    else:
        nr_loose[episode.agent_id] += 1
    average_score[episode.agent_id].append(episode.reward[-1])
average_score[0] = np.mean(average_score[0])
average_score[1] = np.mean(average_score[1])
average_score[2] = np.mean(average_score[2])
average_score[3] = np.mean(average_score[3])

plt.figure(figsize=(17,4))
plt.subplot(141)
plt.bar(range(0,4), nr_wins)
plt.title('nr_wins')
plt.subplot(142)
plt.bar(range(0,4), nr_loose)
plt.title('nr_loose')
plt.subplot(143)
plt.bar(range(0,4), average_score)
plt.title('average_score')
plt.show()<jupyter_output><empty_output><jupyter_text>Consequtive actions<jupyter_code>actions_length = []
for episode in episodes:
    current_action = episode.actions[0]
    action_length = 0
    for action in episode.actions:
        if current_action != action:
            actions_length.append(action_length)            
            current_action = action
            action_length = 0
        action_length += 1
np.mean(actions_length), np.median(actions_length), np.max(actions_length)
plt.figure(figsize=(17,10))
plt.plot(actions_length)
plt.plot([np.mean(actions_length)] * len(actions_length))
plt.title('action length')
plt.show()<jupyter_output><empty_output><jupyter_text>Actions frequency<jupyter_code>actions_count = np.zeros(6)
for episode in episodes:    
    for action in episode.actions:
        actions_count[action] += 1
        
plt.figure(figsize=(17,10))
plt.bar(range(0,6), actions_count)
plt.title('actions count')
plt.show()<jupyter_output><empty_output><jupyter_text>Some data preprocessing Remove episodes with too small length. Maybe it is suicide.<jupyter_code>episodes = [episode for episode in episodes if episode.get_num_steps() > 35 or episode.reward[-1] == 1]
len(episodes)<jupyter_output><empty_output><jupyter_text>Let's remove consequtive actions. I will use threshold 12.<jupyter_code>threshold = 12

for episode in tqdm(episodes):
    # New observations
    obs = []
    act = []
    rew = []
    
    # Initial values
    cur_action = episode.actions[0]
    seq_length = 0
    for ind in range(len(episode.actions)):
        # Current action
        action = episode.actions[ind]
        
        # If an action the same
        if action == cur_action:
            seq_length += 1
        else:
            seq_length = 0
            cur_action = action
        # If a sequence is less than threashold
        if seq_length <= threshold:
            obs.append(episode.observations[ind])
            act.append(episode.actions[ind])
            rew.append(episode.reward[ind])
    
    # Save preprocessed observations
    episode.actions      = act
    episode.observations = obs
    episode.reward       = rew<jupyter_output><empty_output><jupyter_text>Let's rerun plots.  Save stuff<jupyter_code>observations = []
actions = []
rewards = []
for episode in episodes:
    observations.extend(episode.observations)
    actions.extend(episode.actions)
    rewards.extend(episode.reward)

observations = np.array(observations)
actions = np.array(actions)
rewards = np.array(rewards)
np.save(train_data_obs, observations)
np.save(train_data_labels, actions)
np.save(train_data_reward, rewards)
observations = np.load(train_data_obs)
actions      = np.load(train_data_labels)
rewards      = np.load(train_data_reward)
observations.shape, actions.shape, rewards.shape<jupyter_output><empty_output><jupyter_text>Featurize<jupyter_code>def featurize(obs):
    shape = (BOARD_SIZE, BOARD_SIZE, 1)

    def get_matrix(board, key):
        res = board[key]
        return res.reshape(shape).astype(np.float32)

    def get_map(board, item):
        map = np.zeros(shape)
        map[board == item] = 1
        return map

    board = get_matrix(obs, 'board')

    path_map       = get_map(board, 0)          # Empty space
    rigid_map      = get_map(board, 1)          # Rigid = 1
    wood_map       = get_map(board, 2)          # Wood = 2
    bomb_map       = get_map(board, 3)          # Bomb = 3
    flames_map     = get_map(board, 4)          # Flames = 4
    fog_map        = get_map(board, 5)          # TODO: not used for first two stages Fog = 5
    extra_bomb_map = get_map(board, 6)          # ExtraBomb = 6
    incr_range_map = get_map(board, 7)          # IncrRange = 7
    kick_map       = get_map(board, 8)          # Kick = 8
    skull_map      = get_map(board, 9)          # Skull = 9

    position = obs["position"]
    my_position = np.zeros(shape)
    my_position[position[0], position[1], 0] = 1

    team_mates = get_map(board, obs["teammate"].value) # TODO during documentation it should be an array

    enemies = np.zeros(shape)
    for enemy in obs["enemies"]:
        enemies[board == enemy.value] = 1

    bomb_blast_strength = get_matrix(obs, 'bomb_blast_strength')
    bomb_life           = get_matrix(obs, 'bomb_life')

    ammo           = np.full((BOARD_SIZE, BOARD_SIZE, 1), obs["ammo"])
    blast_strength = np.full((BOARD_SIZE, BOARD_SIZE, 1), obs["blast_strength"])
    can_kick       = np.full((BOARD_SIZE, BOARD_SIZE, 1), int(obs["can_kick"]))
    
    obs = np.concatenate([my_position, enemies, team_mates, path_map, rigid_map, 
                          wood_map, bomb_map, flames_map, fog_map, extra_bomb_map,
                          incr_range_map, kick_map, skull_map, bomb_blast_strength,
                          bomb_life, ammo, blast_strength, can_kick], axis=2)
    return obs.astype(np.int16)
observations_map = []
for obs in tqdm(observations):
    observations_map.append(featurize(obs))
np.save(train_data_obs_map, observations_map)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12419379031048447, 'fraction_numerical': 0.016499175041247937, 'mean_word_length': 3.442371752165223, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '499651', 'license': 'Apache-2.0', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2316, 'n_words': 428, 'path': 'rl_agent/generate_data.ipynb', 'repo_name': 'AntonPotapchuk/playground', 'size': '6667'}