示例#1
0
文件: _3ec_maps.py 项目: annikc/MEMRL
def plot_one_prefpol(ec_maps, map_index, save=False):
    fig, axes = plt.subplots(1, 4, figsize=(10, 3), sharex='col')
    cmap = plt.cm.Spectral_r
    cNorm = colors.Normalize(vmin=0, vmax=1)
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cmap)
    chance_threshold = 0.2

    if map_index == 3:
        rwd_colrow = (16, 9)
    else:
        rwd_colrow = (14, 14)

    for ind, ax in enumerate(axes.flatten()):
        sizes = list(ec_maps[list(ec_maps.keys())[map_index]].keys())
        print(sizes)
        env_ec_maps = ec_maps[list(ec_maps.keys())[map_index]]
        cache_size = list(env_ec_maps.keys())[ind]

        policy_array = env_ec_maps[cache_size]
        ax.pcolor(grids[map_index], vmin=0, vmax=1, cmap='bone')
        ax.add_patch(plt.Rectangle(rwd_colrow, 1, 1, edgecolor='w',
                                   fill=False))
        for i in range(20):
            for j in range(20):
                policy = tuple(policy_array[i, j])
                dx, dy = 0.0, 0.0
                for pind, k in enumerate(policy):
                    action = pind
                    prob = k
                    if prob < 0.01:
                        pass
                    else:
                        dx1, dy1, head_w, head_l = make_arrows(action, prob)
                        dx += dx1 * prob
                        dy += dy1 * prob
                if dx == 0.0 and dy == 0.0:
                    pass
                else:
                    colorVal1 = scalarMap.to_rgba(entropy(policy))
                    ax.arrow(j + 0.5,
                             i + 0.5,
                             dx,
                             dy,
                             head_width=0.3,
                             head_length=0.2,
                             color=colorVal1)

        ax.set_aspect('equal')
        ax.set_title(f'{cache_size}')
        ax.invert_yaxis()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    plt.tight_layout()
    if save:
        save_format = 'svg'
        plt.savefig(f'../figures/CH1/mem_map_{map_index}.{save_format}',
                    format=save_format)

    plt.show()
示例#2
0
文件: _3ec_maps.py 项目: annikc/MEMRL
def plot_all_maxpol(save=False):
    fig, axs = plt.subplots(4, 5, sharex='col')
    cmap = plt.cm.Spectral_r
    cNorm = colors.Normalize(vmin=0, vmax=1)
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cmap)
    chance_threshold = 0.2

    for ind, name in enumerate(ec_maps.keys()):
        if name[-2:] == '51':
            rwd_colrow = (16, 9)
        else:
            rwd_colrow = (14, 14)
        rect = plt.Rectangle(rwd_colrow, 1, 1, color='g', alpha=0.3)
        axs[ind, 0].pcolor(grids[ind],
                           cmap='bone_r',
                           edgecolors='k',
                           linewidths=0.1)
        axs[ind, 0].axis(xmin=0, xmax=20, ymin=0, ymax=20)
        axs[ind, 0].set_aspect('equal')
        axs[ind, 0].add_patch(rect)
        axs[ind, 0].get_xaxis().set_visible(False)
        axs[ind, 0].get_yaxis().set_visible(False)
        axs[ind, 0].invert_yaxis()

        for jnd, cache_size in enumerate(list(ec_maps[name].keys())):
            policy_array = ec_maps[name][cache_size]
            axs[ind, jnd + 1].pcolor(grids[ind], vmin=0, vmax=1, cmap='bone')
            axs[ind, jnd + 1].add_patch(
                plt.Rectangle(rwd_colrow, 1, 1, edgecolor='w', fill=False))
            for i in range(20):
                for j in range(20):
                    action = np.argmax(tuple(policy_array[i][j]))
                    prob = max(policy_array[i][j])

                    dx1, dy1, head_w, head_l = make_arrows(action, prob)
                    if prob > chance_threshold:
                        if (dx1, dy1) == (0, 0):
                            pass
                        else:
                            colorVal1 = scalarMap.to_rgba(prob)
                            axs[ind, jnd + 1].arrow(j + 0.5,
                                                    i + 0.5,
                                                    dx1,
                                                    dy1,
                                                    head_width=0.3,
                                                    head_length=0.2,
                                                    color=colorVal1)
                    else:
                        pass
            axs[ind, jnd + 1].set_aspect('equal')
            axs[ind, jnd + 1].invert_yaxis()
            axs[ind, jnd + 1].get_xaxis().set_visible(False)
            axs[ind, jnd + 1].get_yaxis().set_visible(False)
    if save:
        plt.savefig('../figures/CH1/ec_throttled_maps.svg', format='svg')

    plt.show()
示例#3
0
文件: _3ec_maps.py 项目: annikc/MEMRL
def plt_maxpol(env_num, env_grid, maps_dict, plot_name, save=False):
    '''
    :param env_name: int specifying environment type
    :param maps_dict: dictionary containing policy maps
    :param save: whether to save generated plot
    :return: nothing
    '''

    fig, axes = plt.subplots(1,
                             len(list(maps_dict.keys())),
                             figsize=(2 * len(list(maps_dict.keys())) + 2, 3))
    cmap = plt.cm.Spectral_r
    cNorm = colors.Normalize(vmin=0, vmax=1)
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cmap)
    chance_threshold = 0.2

    if env_num == 5:
        rwd_colrow = (16, 9)
    else:
        rwd_colrow = (14, 14)

    for ind, ax in enumerate(axes.flatten()):
        # lay down base map
        ax.pcolor(env_grid, vmin=0, vmax=1, cmap='bone')
        ax.add_patch(plt.Rectangle(rwd_colrow, 1, 1, edgecolor='w',
                                   fill=False))

        #index corresponds to one of the elements of the maps dict
        cache_size = list(maps_dict.keys())[ind]
        policy_array = maps_dict[cache_size]
        rows, columns = env_grid.shape
        for i in range(rows):
            for j in range(columns):
                action = np.argmax(tuple(policy_array[i][j]))
                prob = max(policy_array[i][j])

                dx1, dy1, head_w, head_l = make_arrows(action, prob)
                if prob > chance_threshold:
                    if (dx1, dy1) == (0, 0):
                        pass
                    else:
                        colorVal1 = scalarMap.to_rgba(prob)
                        ax.arrow(j + 0.5,
                                 i + 0.5,
                                 dx1,
                                 dy1,
                                 head_width=0.3,
                                 head_length=0.2,
                                 color=colorVal1)
                else:
                    pass

        ax.set_aspect('equal')
        ax.set_title(f'{cache_size}')
        ax.invert_yaxis()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    plt.suptitle(f'{plot_name}')
    plt.tight_layout()
    if save:
        save_format = 'svg'
        plt.savefig(f'../figures/CH2/mem_map_{env_num}.{save_format}',
                    format=save_format)