Exemple #1
0
def gpmaprecc(optstate, persist, **para):
    if para['onlyafter'] >= optstate.n or not optstate.n % para['everyn'] == 0:
        return argminrecc(optstate, persist, **para)
        #return [sp.NaN for i in para['lb']],{'didnotrun':True}
    logger.info('gpmap reccomender')
    d = len(para['lb'])
    lb = para['lb']
    ub = para['ub']
    maxf = para['maxf']
    x = sp.vstack(optstate.x)
    y = sp.vstack(optstate.y)
    s = sp.vstack([e['s'] + 10**optstate.condition for e in optstate.ev])
    dx = [e['d'] for e in optstate.ev]
    MAP = GPdc.searchMAPhyp(x, y, s, dx, para['mprior'], para['sprior'],
                            para['kindex'])
    logger.info('MAPHYP {}'.format(MAP))
    G = GPdc.GPcore(x, y, s, dx, GPdc.kernel(para['kindex'], d, MAP))

    def wrap(x):
        xq = copy.copy(x)
        xq.resize([1, d])
        a = G.infer_m_post(xq, [[sp.NaN]])
        return a[0, 0]

    xmin, ymin, ierror = gpbo.core.optutils.twopartopt(wrap, para['lb'],
                                                       para['ub'],
                                                       para['dpara'],
                                                       para['lpara'])
    return [i for i in xmin], persist, {'MAPHYP': MAP, 'ymin': ymin}
Exemple #2
0
def traincfn1dll(x, c):
    #cost modeled in a latent space c=exp(cl)
    n = x.size
    cl = sp.log(c)
    MAP = GPdc.searchMAPhyp(x, cl, sp.array([1e-3] * n), [[sp.NaN]] * n,
                            sp.array([1., 0., -1.]), sp.array([2., 2., 2.]),
                            GPdc.MAT52CS)
    print('MAPhyp in costfn {}'.format(MAP))
    g = GPdc.GPcore(x, cl, sp.array([1e-3] * n), [[sp.NaN]] * n,
                    GPdc.kernel(GPdc.MAT52CS, 1, MAP))

    if gpbo.core.debugoutput['cost1d']:
        print('plotting cost1d...')
        import time
        from matplotlib import pyplot as plt
        f, a = plt.subplots(2)
        low = min(0, min(x))
        high = max(1, max(x))
        xaxis = sp.linspace(low, high, 100)
        y, cy = g.infer_diag_post(xaxis, [[sp.NaN]] * 100)

        s = 2. * sp.sqrt(cy)
        u = sp.empty(100)
        l = sp.empty(100)
        for i in xrange(100):
            s = sp.sqrt(cy[0, i])
            u[i] = y[0, i] + 2. * s
            l[i] = y[0, i] - 2. * s
        a[0].plot(xaxis, y[0, :], 'b')
        a[0].fill_between(xaxis,
                          l,
                          u,
                          facecolor='lightblue',
                          edgecolor='lightblue',
                          alpha=0.5)
        for i in xrange(n):
            a[0].plot(x[i], cl[i], 'r.')
        a[0].set_ylabel('latent')

        a[1].plot(xaxis, sp.exp(y[0, :]), 'b')
        a[1].fill_between(xaxis,
                          sp.exp(l),
                          sp.exp(u),
                          facecolor='lightblue',
                          edgecolor='lightblue',
                          alpha=0.5)
        for i in xrange(n):
            a[1].plot(x[i], c[i], 'r.')
        a[1].set_ylabel('out')

        f.savefig(
            os.path.join(
                gpbo.core.debugoutput['path'],
                'cost1d' + time.strftime('%d_%m_%y_%H:%M:%S') + '.png'))
        f.clf()
        plt.close(f)
        del (f)
    return logcfnobj(g)
Exemple #3
0
def gpmap2upperrecc(optstate, persist, **para):
    if para['onlyafter'] >= optstate.n:
        print('{} <= {} : switch to argmin'.format(optstate.n,
                                                   para['onlyafter']))
        return argminrecc(optstate, persist, **para)
        #return [sp.NaN for i in para['lb']],{'didnotrun':True}
    logger.info('gpmapucb2 reccomender')
    d = len(para['lb'])
    x = sp.vstack(optstate.x)
    y = sp.vstack(optstate.y)
    s = sp.vstack([e['s'] + 10**optstate.condition for e in optstate.ev])
    dx = [e['d'] for e in optstate.ev]
    MAP = GPdc.searchMAPhyp(x, y, s, dx, para['mprior'], para['sprior'],
                            para['kindex'])
    logger.info('MAPHYP {}'.format(MAP))
    G = GPdc.GPcore(x, y, s, dx, GPdc.kernel(para['kindex'], d, MAP))
    count = 0

    def wrap(xq):
        xq.resize([1, d])
        a, v = G.infer_diag_post(xq, [[sp.NaN]])
        return a[0, 0] + 2. * sp.sqrt(v[0, 0])

    #print('nevals={}\n\n'.format(count))
    #[xmin,ymin,ierror] = direct(directwrap,para['lb'],para['ub'],user_data=[], algmethod=1, maxf=para['maxf'], logfilename='/dev/null')
    xmin, ymin, ierror = gpbo.core.optutils.twopartopt(wrap, para['lb'],
                                                       para['ub'],
                                                       para['dpara'],
                                                       para['lpara'])
    logger.info('DIRECT found post. min {} at {} {}'.format(
        ymin, xmin, ierror))
    a, v = G.infer_diag_post(sp.array([[xmin]]), [[sp.NaN]])
    print('mean {} std{} '.format(a[0, 0], sp.sqrt(v[0, 0])))

    x, p, di = argminrecc(optstate, persist, **para)
    a, v = G.infer_diag_post(sp.array([[x]]), [[sp.NaN]])
    print('at argmin mean {} std {} wasactually {}'.format(
        a[0, 0], sp.sqrt(v[0, 0]), di['yinc']))
    import sys
    sys.stdout.flush()
    return [i for i in xmin], persist, {'MAPHYP': MAP, 'ymin': ymin}
Exemple #4
0
def gpmapasrecc(optstate, persist, **para):
    if para['onlyafter'] >= optstate.n or not optstate.n % para['everyn'] == 0:
        return argminrecc(optstate, persist, **para)
        #return [sp.NaN for i in para['lb']],{'didnotrun':True}
    logger.info('gpmapas reccomender')
    d = len(para['lb'])

    x = sp.hstack(
        [sp.vstack(optstate.x),
         sp.vstack([e['xa'] for e in optstate.ev])])

    y = sp.vstack(optstate.y)
    s = sp.vstack([e['s'] + 10**optstate.condition for e in optstate.ev])
    dx = [e['d'] for e in optstate.ev]
    MAP = GPdc.searchMAPhyp(x, y, s, dx, para['mprior'], para['sprior'],
                            para['kindex'])
    logger.info('MAPHYP {}'.format(MAP))
    G = GPdc.GPcore(x, y, s, dx, GPdc.kernel(para['kindex'], d + 1, MAP))

    def wrap(xq):
        xq.resize([1, d])
        xe = sp.hstack([xq, sp.array([[0.]])])
        a = G.infer_m_post(xe, [[sp.NaN]])
        return a[0, 0]

    #[xmin,ymin,ierror] = direct(directwrap,para['lb'],para['ub'],user_data=[], algmethod=1, maxf=para['maxf'], logfilename='/dev/null')
    xmin, ymin, ierror = gpbo.core.optutils.twopartopt(wrap, para['lb'],
                                                       para['ub'],
                                                       para['dpara'],
                                                       para['lpara'])
    logger.info('reccsearchresult: {}'.format([xmin, ymin, ierror]))

    from gpbo.core import debugoutput
    if debugoutput['datavis']:
        A2 = MAP[0]
        l = MAP[3]
        from matplotlib import pyplot as plt
        fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(10, 30))

        n = 200
        x_ = sp.linspace(-1, 1, n)
        y_ = sp.linspace(-1, 1, n)
        z_ = sp.empty([n, n])
        s_ = sp.empty([n, n])
        for i in xrange(n):
            for j in xrange(n):
                m_, v_ = G.infer_diag_post(sp.array([y_[j], x_[i], 0.]),
                                           [[sp.NaN]])
                z_[i, j] = m_[0, 0]
                s_[i, j] = sp.sqrt(v_[0, 0])
        CS = ax[1].contour(x_, y_, z_, 20)
        ax[1].clabel(CS, inline=1, fontsize=10)
        CS = ax[2].contour(x_, y_, s_, 20)
        ax[2].clabel(CS, inline=1, fontsize=10)
        for i in xrange(x.shape[0]):
            ax[0].plot(x[i, 0], x[i, 1], 'b.')
            circle = plt.Circle([x[i, 0], x[i, 1]],
                                radius=0.5 * x[i, 2] * l,
                                edgecolor="none",
                                color='lightblue',
                                alpha=0.8 - 0.6 * x[i, 2])
            ax[0].add_patch(circle)

        ax[0].axis([-1., 1., -1., 1.])
        fig.savefig(
            os.path.join(
                os.path.expanduser('~'), 'Dropbox/debugoutput',
                'datavis' + time.strftime('%d_%m_%y_%H:%M:%S') + '.png'))
        fig.clf()
        plt.close(fig)
        del (fig)
    return [i for i in xmin], persist, {'MAPHYP': MAP, 'ymin': ymin}
Exemple #5
0
def traincfnfull(x, c):
    #cost modeled in a latent space c=exp(cl)
    n, d = x.shape
    c_ = sp.log(c)
    off = sp.mean(c_)
    cl = c_ - off
    MAP = GPdc.searchMAPhyp(x, cl, sp.array([1e-6] * n), [[sp.NaN]] * n,
                            sp.array([1.] + [-0.] * d),
                            sp.array([2.] * (d + 1)), GPdc.MAT52)
    print('MAPhyp in costfn {}'.format(MAP))
    g = GPdc.GPcore(x, cl, sp.array([1e-3] * n), [[sp.NaN]] * n,
                    GPdc.kernel(GPdc.MAT52, 1, MAP))
    if gpbo.core.debugoutput['cost1d']:
        print('plotting cost...')
        import time
        from matplotlib import pyplot as plt
        f, a = plt.subplots(d, 2)

        n = 60
        x = sp.linspace(-1, 1, n)
        xa = sp.linspace(0, 1, n)
        z = sp.empty([n, n])
        vz = sp.empty([n, n])
        for D in xrange(d - 1):
            for i in xrange(n):
                for j in xrange(n):
                    q = sp.zeros([1, d])
                    q[0, 0] = xa[j]
                    q[0, D + 1] = x[i]
                    m, v = g.infer_diag(q, [[sp.NaN]])
                    z[i, j] = m[0, 0]
                    vz[i, j] = v[0, 0]
            try:
                CS = a[D, 0].contour(xa, x, z, 30)
                a[D, 0].clabel(CS, inline=1, fontsize=8)
            except ValueError:
                pass
            try:
                CS = a[D, 1].contour(xa, x, vz, 30)
                a[D, 1].clabel(CS, inline=1, fontsize=8)
            except ValueError:
                pass

        for i in xrange(n):
            for j in xrange(n):
                q = sp.zeros([1, d])
                q[0, 0] = 0.
                q[0, 1] = x[j]
                q[0, 2] = x[i]
                m, v = g.infer_diag(q, [[sp.NaN]])
                z[i, j] = m[0, 0]
                vz[i, j] = v[0, 0]
        try:
            CS = a[d - 1, 0].contour(x, x, z, 30)
            a[d - 1, 0].clabel(CS, inline=1, fontsize=8)
        except ValueError:
            pass
        try:
            CS = a[d - 1, 1].contour(x, x, vz, 30)
            a[d - 1, 1].clabel(CS, inline=1, fontsize=8)
        except ValueError:
            pass

        f.savefig(
            os.path.join(
                debugoutput['path'],
                'cost1d' + time.strftime('%d_%m_%y_%H:%M:%S') + '.png'))
        f.clf()
        plt.close(f)
        del (f)
    return logcfnobjfull(g, offset=off)