예제 #1
0
def genbiasedmat52ojf(d, lb, ub, xls, sls):
    #s normalised to 0 exact, 1
    from ESutils import gen_dataset
    nt = 30
    [X, Y, S, D] = gen_dataset(nt, d + 1, lb + [0], ub + [1], GPdc.MAT52,
                               sp.array([1.5] + [xls] * d + [sls]))

    G = GPdc.GPcore(
        X, Y, S, D,
        GPdc.kernel(GPdc.MAT52, d + 1, sp.array([1.5] + [0.30] * d + [sls])))

    def ojf(x, **ev):
        #print "\nojfinput: {} : {}".format(x,ev)
        dx = ev['d']
        s = ev['s']
        if ev['s'] > 0:
            noise = sp.random.normal(scale=sp.sqrt(ev['s']))
        else:
            noise = 0
        #print 'noise in ojf {}'.format(noise)
        xa = ev['xa']
        x = sp.array(x)
        xfull = sp.hstack([x, xa])
        return G.infer_m(xfull, [dx])[0, 0] + noise, 1., dict()

    def dirwrap(x, y):
        z = G.infer_m(sp.hstack(sp.array(x) + [0.]), [[sp.NaN]])[0, 0]
        return (z, 0)

    [xmin, ymin, ierror] = DIRECT.solve(dirwrap,
                                        lb,
                                        ub,
                                        user_data=[],
                                        algmethod=1,
                                        maxf=40000,
                                        logfilename='/dev/null')

    #print [xmin, ymin]
    def spowrap(x):
        z = G.infer_m(sp.hstack(sp.array(x) + [0.]), [[sp.NaN]])[0, 0]
        return z

    y = spm(spowrap,
            xmin,
            method='l-bfgs-b',
            bounds=[(-1, 1)] * d,
            options={'ftol': 1e-15})
    xmin = y.x
    ymin = spowrap(y.x)
    #print [xmin,ymin]

    if sp.isnan(ymin):
        logger.warning('generator got nan optimizing objective. retrying...')
        return genbiasedmat52ojf(d, lb, ub, xls, sls)
    logger.info(
        'generated function xmin {} ymin {} globopt:{} locopt:{}'.format(
            xmin, ymin, ierror, y.status))
    return ojf, xmin, ymin
예제 #2
0
def gendecayingpositiveojf(d, lb, ub, sim):

    # s normalised to 0 exact, 1
    from ESutils import gen_dataset
    nt = 20
    cl = 2.

    [X, Y, S, D] = gen_dataset(nt, d, lb, ub, GPdc.MAT52,
                               sp.array([1] + [0.30] * d))
    G0 = GPdc.GPcore(X, Y, S, D,
                     GPdc.kernel(GPdc.MAT52, d, sp.array([1] + [0.30] * d)))

    [X, Y, S, D] = gen_dataset(nt, d, lb, ub, GPdc.MAT52,
                               sp.array([1] + [0.30] * d))
    G1 = GPdc.GPcore(X, Y, S, D,
                     GPdc.kernel(GPdc.MAT52, d, sp.array([1] + [0.30] * d)))

    [X, Y, S, D] = gen_dataset(nt, d, lb, ub, GPdc.MAT52,
                               sp.array([1] + [0.30] * d))
    G2 = GPdc.GPcore(X, Y, S, D,
                     GPdc.kernel(GPdc.MAT52, d, sp.array([1] + [0.30] * d)))

    def p0(x):
        v = G0.infer_m(x, [[sp.NaN]])[0, 0]
        y = v + 1 if v > 0 else sp.exp(v)
        return y

    def p1(x):
        v = G1.infer_m(x, [[sp.NaN]])[0, 0]
        y = v + 1 if v > 0 else sp.exp(v)
        return y

    def p2(x):
        v = G2.infer_m(x, [[sp.NaN]])[0, 0]
        y = v + 1 if v > 0 else sp.exp(v)
        return y

    def ojf(x, **ev):
        #print "ex: {} {}".format(x,ev['xa'])
        # print "\nojfinput: {} : {}".format(x,ev)
        dx = ev['d']
        s = ev['s']
        if ev['s'] > 0:
            noise = sp.random.normal(scale=sp.sqrt(ev['s']))
        else:
            noise = 0
        # print 'noise in ojf {}'.format(noise)
        xa = ev['xa']
        x = sp.array(x)

        y0 = p0(x)
        y1 = sim * p1(x) + y0
        l = p2(x)

        A = (y1 - y0) / (sp.exp(l) - 1)
        B = y0 - A
        y = A * sp.exp(l * xa) + B

        c = sp.exp(-cl * xa)
        return y + noise, c, dict()

    def dirwrap(x, y):
        z = ojf(x, **{'d': [sp.NaN], 's': 0, 'xa': 0})[0]
        return (z, 0)

    [xmin, ymin, ierror] = DIRECT.solve(dirwrap,
                                        lb,
                                        ub,
                                        user_data=[],
                                        algmethod=1,
                                        maxf=20000,
                                        logfilename='/dev/null')

    # print [xmin, ymin]
    def spowrap(x):
        z = ojf(x, **{'d': [sp.NaN], 's': 0, 'xa': 0})[0]
        return z

    y = spm(spowrap, xmin, method='nelder-mead', options={'fatol': 1e-12})
    xmin = y.x
    ymin = spowrap(y.x)
    # print [xmin,ymin]
    logger.info(
        'generated function xmin {} ymin {} yminisnan{} globopt:{} locopt:{}'.
        format(xmin, ymin, sp.isnan(ymin), ierror, y.status))
    if sp.isnan(ymin):
        logger.warning('generator got nan optimizing objective. retrying...')
        return gendecayingpositiveojf(d, lb, ub)
    return ojf, xmin, ymin
예제 #3
0
def genmat52ojf(d, lb, ub, A=1., ls=0.3, fixs=-1, ki=GPdc.MAT52):
    from ESutils import gen_dataset
    if isinstance(ls, float):
        ls = [ls] * d
    #nt=sp.maximum(250,sp.minimum(int(d*100./sp.product(ls)),3000))
    nt = sp.maximum(150, sp.minimum(int(d * 20. / sp.product(ls)), 2500))
    #print(nt)
    #nt=500

    s = 1e-9

    for s in sp.logspace(-9, 0, 20):
        try:
            [X, Y, S, D] = gen_dataset(nt,
                                       d,
                                       lb,
                                       ub,
                                       ki,
                                       sp.array([A] + ls),
                                       s=1e-9)
            break
        except:
            raise

# from matplotlib import pyplot as plt
# plt.figure()
# plt.plot(sp.array(X),sp.array(Y),'b.')
# plt.show(block=True)
    print('training GP')
    G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(ki, d, sp.array([A] + ls)))

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

    dpara = {
        'user_data': [],
        'algmethod': 1,
        'maxf': 40000,
        'logfilename': '/dev/null'
    }
    lpara = {'ftol': 1e-20, 'maxfun': 1200}
    xmin, ymin, ierror = gpbo.core.optutils.twopartopt(wrap, lb, ub, dpara,
                                                       lpara)

    print('init {} {}'.format(xmin, ymin))
    for i in xrange(250):
        p = sp.random.normal(size=d) * 1e-2
        res = spm(wrap,
                  xmin + p,
                  method='L-BFGS-B',
                  bounds=tuple([(lb[j], ub[j]) for j in xrange(d)]),
                  options={'gtol': 1e-30})
        # print(res)
        #print(xmin,res.x,wrap(xmin),wrap(res.x)<wrap(xmin))
        if wrap(res.x) < wrap(xmin):
            xmin = res.x
            #    ymin = ymin+res.fun
            print('change: {} {}'.format(xmin, wrap(res.x) - ymin))
    ymin = wrap(xmin)

    def ojf(x, **ev):
        dx = ev['d']
        s = ev['s']
        if fixs < 0:
            if ev['s'] > 0 and not 'cheattrue' in list(ev.keys()):
                noise = sp.random.normal(scale=sp.sqrt(ev['s']))
            else:
                noise = 0
        else:
            if not 'cheattrue' in list(ev.keys()):
                noise = sp.random.normal(scale=sp.sqrt(fixs))
            else:
                noise = 0.
        y = wrap(x) + noise  #G.infer_m(sp.array(x),[dx])[0,0]+noise
        if not 'silent' in list(ev.keys()):
            print('ojf at {} {} returned {} noise {}'.format([i for i in x],
                                                             ev, y, noise))
        return y - ymin, 1., dict()

    logger.info(
        'generated function xmin {} ymin {}(shifted to 0.) opt:{}'.format(
            xmin, ymin, ierror))
    return ojf, xmin, 0.