Exemplo n.º 1
0
def generatePlot(exp_paths):
    exp = ExperimentModel.load(exp_paths[0])
    results = loadResults(exp, 'step_return.csv')

    results_dict = splitOverParameter(results, 'tile-coder')

    # two new streams over results
    mine = results_dict['mine']
    tiles3 = results_dict['tiles3']

    # figure out what values of "tiles" we swept
    tiles = splitOverParameter(mine, 'tiles').keys()
    # figure out what values of "tilings" we swept
    tilings = splitOverParameter(mine, 'tilings').keys()

    f, axes = plt.subplots(len(tiles), len(tilings))

    # create an empty (2, tiles, tilings) shape array
    data = createEmptyList([2, len(tiles), len(tilings)])
    for tiles_idx, num_tiles in enumerate(tiles):
        for tilings_idx, num_tilings in enumerate(tilings):
            mine_result = whereParametersEqual(mine, { 'tiles': num_tiles, 'tilings': num_tilings })
            mine_result = getBest(mine_result, prefer='big')

            tiles3_result = whereParametersEqual(tiles3, { 'tiles': num_tiles, 'tilings': num_tilings })
            tiles3_result = getBest(tiles3_result, prefer='big')

            mine_data = [np.mean(curve) for curve in mine_result.load()]
            tiles3_data = [np.mean(curve) for curve in tiles3_result.load()]

            data[0][tiles_idx][tilings_idx] = mine_data
            data[1][tiles_idx][tilings_idx] = tiles3_data

    min_perf = np.min(data)
    max_perf = np.max(data)
    for rep, rep_name in enumerate(['Mine', 'Tiles3']):
        for tiles_idx, num_tiles in enumerate(tiles):
            for tilings_idx, num_tilings in enumerate(tilings):
                performance = data[rep][tiles_idx][tilings_idx]
                color = 'blue' if rep == 0 else 'red'

                kde = gaussian_kde(performance)
                lo = 0.95 * min_perf
                hi = 1.05 * max_perf
                dist_space = np.linspace(lo, hi, FIDELITY)
                dist = kde(dist_space)
                # dist = minMaxScale(kde(dist_space)) * DIST_HEIGHT
                axes[tiles_idx][tilings_idx].plot(dist_space, dist, label=rep_name, linewidth=2.0, color=color)
                axes[tiles_idx][tilings_idx].fill_between(dist_space, np.zeros(FIDELITY), dist, color=color, alpha=0.2)

                axes[tiles_idx][tilings_idx].set_xlim((0.95 * min_perf, 1.05 * max_perf))
                # axes[tiles_idx][tilings_idx].set_xlabel('Reward')

                title = f'({num_tiles} tiles, {num_tilings} tilings)'
                axes[tiles_idx][tilings_idx].set_title(title)

    plt.legend()

    return f, axes
def generatePlot(ax, exp_paths):
    exp = ExperimentModel.load(exp_paths[0])
    results = loadResults(exp, 'step_return.csv')

    results_dict = splitOverParameter(results, 'tile-coder')

    # two new streams over results
    mine = results_dict['mine']
    tiles3 = results_dict['tiles3']

    # figure out what values of "tiles" we swept
    tiles = splitOverParameter(mine, 'tiles').keys()
    # figure out what values of "tilings" we swept
    tilings = splitOverParameter(mine, 'tilings').keys()

    for num_tiles in tiles:
        for num_tilings in tilings:
            mine_result = whereParametersEqual(mine, {
                'tiles': num_tiles,
                'tilings': num_tilings
            })
            mine_result = getBest(mine_result, prefer='big')

            tiles3_result = whereParametersEqual(tiles3, {
                'tiles': num_tiles,
                'tilings': num_tilings
            })
            tiles3_result = getBest(tiles3_result, prefer='big')

            d_curves = []
            mine_curves = mine_result.load()
            tiles3_curves = tiles3_result.load()

            min_len = min(len(mine_curves), len(tiles3_curves))
            print(min_len)
            for i in range(min_len):
                d = mine_curves[i] - tiles3_curves[i]
                d_curves.append(d)

            mean = np.mean(d_curves, axis=0)
            stderr = np.std(d_curves, axis=0, ddof=1) / np.sqrt(len(d_curves))

            lineplot(ax,
                     mean,
                     stderr=stderr,
                     label=f'({num_tiles}, {num_tilings})')

    ax.set_xlim(0, 500)
    ax.axhline(0, color='black', linestyle='--', alpha=0.4)
    ax.set_ylabel(f'd = Mine - Tiles3 \n Bigger is better')
    plt.legend()
Exemplo n.º 3
0
def printStats(exp_paths, metric):
    print(f'-------------{metric}-------------')
    exp = ExperimentModel.load(exp_paths[0])
    results = loadResults(exp, f'{metric}.csv')

    results_dict = splitOverParameter(results, 'tile-coder')

    # two new streams over results
    mine = results_dict['mine']
    tiles3 = results_dict['tiles3']

    # figure out what values of "tiles" we swept
    tiles = splitOverParameter(mine, 'tiles').keys()
    # figure out what values of "tilings" we swept
    tilings = splitOverParameter(mine, 'tilings').keys()

    for num_tiles in tiles:
        for num_tilings in tilings:
            mine_results = list(
                whereParametersEqual(mine, {
                    'tiles': num_tiles,
                    'tilings': num_tilings
                }))
            tiles3_results = list(
                whereParametersEqual(tiles3, {
                    'tiles': num_tiles,
                    'tilings': num_tilings
                }))

            mine_means = []
            tiles3_means = []

            # loop over each value of alpha
            # this way we just get 3x as many samples of timing
            for i in range(len(mine_results)):
                mine_mean = mine_results[i].mean()[0]
                tiles3_mean = tiles3_results[i].mean()[0]

                mine_means.append(mine_mean)
                tiles3_means.append(tiles3_mean)

            mine_mean = np.mean(mine_means)
            tiles3_mean = np.mean(tiles3_means)

            # TODO: this is covering up a bug in results. Rerun results
            if metric == 'feature_utilization':
                mine_mean = mine_mean / (num_tilings * num_tiles**2)
                tiles3_mean = tiles3_mean / (num_tilings * num_tiles**2)

            print(
                f'({num_tiles}, {num_tilings}) -- {mine_mean}, {tiles3_mean}')
Exemplo n.º 4
0
def getSensitivityData(results, param, reducer='best', bestBy='auc'):
    if reducer == 'best':
        split = splitOverParameter(results, param)
        bestStream = {}
        for k in split:
            bestStream[k] = getBest(split[k], prefer='big')

    elif reducer == 'slice':
        l, r = tee(results)
        best = getBest(l, prefer='big')

        bestStream = sliceOverParameter(r, best, param)

    else:
        raise NotImplementedError()

    x = sorted(list(bestStream))
    best = bestStream

    metric = getCurveReducer(bestBy)

    y = np.array([metric(best[k].mean()) for k in x])
    e = np.array([metric(best[k].stderr()) for k in x])

    e[np.isnan(y)] = 0.000001
    y[np.isnan(y)] = 100000

    return x, y, e
Exemplo n.º 5
0
def generatePlot(exp_paths):
    exp = ExperimentModel.load(exp_paths[0])
    results = loadResults(exp, 'step_return.csv')

    results_dict = splitOverParameter(results, 'tile-coder')

    # two new streams over results
    mine = results_dict['mine']
    tiles3 = results_dict['tiles3']

    # figure out what values of "tiles" we swept
    tiles = splitOverParameter(mine, 'tiles').keys()
    # figure out what values of "tilings" we swept
    tilings = splitOverParameter(mine, 'tilings').keys()

    f, axes = plt.subplots(len(tiles), len(tilings))

    for tiles_idx, num_tiles in enumerate(tiles):
        for tilings_idx, num_tilings in enumerate(tilings):
            mine_result = whereParametersEqual(mine, {
                'tiles': num_tiles,
                'tilings': num_tilings
            })
            mine_result = getBest(mine_result, prefer='big')

            tiles3_result = whereParametersEqual(tiles3, {
                'tiles': num_tiles,
                'tilings': num_tilings
            })
            tiles3_result = getBest(tiles3_result, prefer='big')

            plotBest(mine_result,
                     axes[tiles_idx][tilings_idx],
                     color='blue',
                     label='Mine')
            plotBest(tiles3_result,
                     axes[tiles_idx][tilings_idx],
                     color='red',
                     label='Tiles3')

            axes[tiles_idx][tilings_idx].set_title(
                f'Tiles: {num_tiles} Tilings: {num_tilings}')

    axes[0][0].legend()

    return f, axes
Exemplo n.º 6
0
    def test_splitOverParameter(self):
        results = (Result('fake/path', exp, i) for i in listIndices(exp))

        split_results = splitOverParameter(results, 'alpha')
        self.assertEqual(list(split_results), [1.0, 0.5, 0.25])  # check keys
        self.assertEqual(len(split_results[1.0]), 8)

        for key in split_results:
            sub_results = split_results[key]
            for res in sub_results:
                self.assertEqual(res.params['alpha'], key)

        results = (Result('fake/path', exp, i) for i in listIndices(exp))

        split_results = splitOverParameter(results, 'model.name')
        self.assertEqual(list(split_results), ['PR', 'ESARSA'])  # check keys
        self.assertEqual(len(split_results['PR']), 12)
def getSensitivityData(results,
                       param,
                       reducer='best',
                       overStream=None,
                       bestBy='end'):
    useOtherStream = overStream is not None
    overStream = overStream if useOtherStream else results

    if reducer == 'best':
        split = splitOverParameter(overStream, param)
        bestStream = {}
        for k in split:
            bestStream[k] = getBest(split[k])

    elif reducer == 'slice':
        l, r = tee(overStream)
        best = getBest(l)

        bestStream = sliceOverParameter(r, best, param)

    else:
        raise NotImplementedError()

    x = sorted(list(bestStream))
    if useOtherStream:
        best = {}
        teed = tee(results, len(x))
        for i, k in enumerate(x):
            best[k] = find(teed[i], bestStream[k])

    else:
        best = bestStream

    if bestBy == 'end':
        metric = lambda m: np.mean(m[-int(m.shape[0] * .1):])
    elif bestBy == 'auc':
        metric = np.mean

    y = []
    e = []
    for k in x:
        mean, stderr, runs = best[k].load()
        y.append(metric(mean))
        e.append(metric(stderr))

    y = np.array(y)
    e = np.array(e)
    e[np.isnan(y)] = 0.000001
    y[np.isnan(y)] = 100000

    return x, y, e
Exemplo n.º 8
0
def generatePlot(ax, exp_paths, bounds):
    for exp_path in exp_paths:
        exp = ExperimentModel.load(exp_path)

        results = loadResults(exp, 'return_summary.npy')
        results = whereParameterEquals(results, 'epsilon', 0.05)

        param_values = splitOverParameter(results, PARAM)

        for key, param_results in param_values.items():
            best = getBest(param_results)
            print('best parameters:', exp_path, f'{PARAM}={key}')
            print(best.params)

            alg = exp.agent

            b = plotBest(best, ax, label=alg + f' {PARAM}={key}', dashed=False)
            bounds.append(b)