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}
def gphinrecc(optstate, persist, **para): #print( [para['onlyafter'],optstate.n]) if para['onlyafter'] >= optstate.n or not optstate.n % para['everyn'] == 0: #return [sp.NaN for i in para['lb']],{'didnotrun':True} return argminrecc(optstate, persist, **para) logger.info('gphin 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] # print optstate.aux G = GPdc.GPcore(x, y, s, dx, [ GPdc.kernel(optstate.aux['kindex'], d, h) for h in optstate.aux['HYPdraws'] ]) lb = para['lb'] ub = para['ub'] 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, lb, ub, para['dpara'], para['lpara']) return [i for i in xmin], persist, {'ymin': ymin}
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
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)
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}
def gphinasargminrecc(optstate, persist, **para): if para['onlyafter'] >= optstate.n or not optstate.n % para['everyn'] == 0: # return [sp.NaN for i in para['lb']],{'didnotrun':True} return argminrecc(optstate, persist, **para) logger.info('gpmapas reccomender') d = len(para['lb']) x = sp.hstack( [sp.vstack([e['xa'] for e in optstate.ev]), 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] G = GPdc.GPcore(x, y, s, dx, [ GPdc.kernel(optstate.aux['kindex'], d + 1, h) for h in optstate.aux['HYPdraws'] ]) def wrap(xq): xq.resize([1, d]) xe = sp.hstack([sp.array([[0.]]), xq]) # print xe a = G.infer_m_post(xe, [[sp.NaN]]) return a[0, 0] best = sp.Inf incumbent = None for i in xrange(len(optstate.x)): thisone = wrap(sp.array(optstate.x[i])) if thisone < best: best = thisone incumbent = optstate.x[i] logger.info('reccsearchresult: x {} pred.y {}'.format(incumbent, best)) return [i for i in incumbent], persist, {'ymin': best}
def traincfn1d(x, c): n = x.size g = GPdc.GPcore(x, c, sp.ones([n, 1]) * 1e-1, [[sp.NaN]] * n, GPdc.kernel(GPdc.MAT52, 1, [1., 0.2])) if gpbo.core.debugoutput['cost1d']: print('plotting cost1d...') import time from matplotlib import pyplot as plt f, a = plt.subplots(1) 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) a.plot(xaxis, y[0, :], 'b') 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.fill_between(xaxis, l, u, facecolor='lightblue', edgecolor='lightblue', alpha=0.5) for i in xrange(n): a.plot(x[i], c[i], 'r.') f.savefig( os.path.join( gpbo.core.debugoutput['path'], 'cost1d' + time.strftime('%d_%m_%y_%H:%M:%S') + '.png')) del (f) return cfnobj(g)
def gpfixrecc(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] G = GPdc.GPcore(x, y, s, dx, GPdc.kernel(para['kindex'], d, para['hyper'])) def wrap(x, null): xq = copy.copy(x) xq.resize([1, d]) a = G.infer_m_post(xq, [[sp.NaN]]) return a[0, 0], 0 xmin, ymin, ierror = gpbo.core.optutils.silentdirect( wrap, para['lb'], para['ub'], **para['dpara']) return [i for i in xmin], persist, {'ymin': ymin}
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
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.
def gphinasrecc(optstate, persist, **para): if para['onlyafter'] >= optstate.n or not optstate.n % para['everyn'] == 0: #return [sp.NaN for i in para['lb']],{'didnotrun':True} return argminrecc(optstate, persist, **para) logger.info('gpmapas reccomender') d = len(para['lb']) x = sp.hstack( [sp.vstack([e['xa'] for e in optstate.ev]), 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] G = GPdc.GPcore(x, y, s, dx, [ GPdc.kernel(optstate.aux['kindex'], d + 1, h) for h in optstate.aux['HYPdraws'] ]) # def directwrap(xq,y): # xq.resize([1,d]) # xe = sp.hstack([sp.array([[0.]]),xq]) # #print xe # a = G.infer_m_post(xe,[[sp.NaN]]) # return (a[0,0],0) # [xmin,ymin,ierror] = direct(directwrap,para['lb'],para['ub'],user_data=[], algmethod=1, maxf=para['maxf'], logfilename='/dev/null') def wrap(x): xq = copy.copy(x) xq.resize([1, d]) xe = sp.hstack([sp.array([[0.]]), xq]) a = G.infer_m_post(xe, [[sp.NaN]]) return a[0, 0] 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']: if not os.path.exists(debugoutput['path']): os.mkdir(debugoutput['path']) l = sp.mean([h[3] for h in optstate.aux['HYPdraws']]) 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([0., y_[j], x_[i]]), [[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] - 1): ax[0].plot(x[i, 1], x[i, 2], 'b.') circle = plt.Circle([x[i, 1], x[i, 2]], radius=0.5 * x[i, 0] * l, edgecolor="none", color='lightblue', alpha=0.8 - 0.6 * x[i, 2]) ax[0].add_patch(circle) ax[0].plot(x[i + 1, 1], x[i + 1, 2], 'r.') circle = plt.Circle([x[i + 1, 1], x[i + 1, 2]], radius=0.5 * x[i, 0] * 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.]) ax[1].plot(xmin[0], xmin[1], 'ro') fig.savefig( os.path.join( debugoutput['path'], 'datavis' + time.strftime('%d_%m_%y_%H:%M:%S') + '.png')) fig.clf() plt.close(fig) del (fig) return [i for i in xmin], persist, {'ymin': ymin}
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)