def PESIPS(ojf, lb, ub, ki, b, fname): sp.random.seed(int(os.urandom(4).encode('hex'), 16)) para = dict() para['kindex'] = ki[0] para['mprior'] = ki[1] para['sprior'] = ki[2] para['d'] = lb.size + 1 para['ninit'] = 10 para['volper'] = 1e-7 para['DH_SAMPLES'] = 12 para['DM_SAMPLES'] = 12 para['DM_SUPPORT'] = 1200 para['DM_SLICELCBPARA'] = 1. para['SUPPORT_MODE'] = [ ESutils.SUPPORT_LAPAPR ] #[ESutils.SUPPORT_SLICELCB,ESutils.SUPPORT_SLICEEI] para['sl'] = 0. para['su'] = 1. para['s'] = 0. para['sfn'] = None para['axis'] = 0 para['value'] = para['sl'] if os.path.exists(fname): print "starting from " + str(fname) OE = OPTutils.PESIPS(ojf, lb, ub, para, initstate=pickle.load(open(fname, 'rb'))) else: print "fresh start" OE = OPTutils.PESIPS(ojf, lb, ub, para) if sum(OE.C) >= b: print "no further steps needed" k = 0 while sum(OE.C[:k]) < b: k += 1 state = [ OE.X[:k, :], OE.Y[:k, :], OE.S[:k, :], OE.D[:k], OE.R[:k, :], OE.C[:k], OE.T[:k], OE.Tr[:k], OE.Ymin[:k], OE.Xmin[:k, :], OE.Yreg[:k, :], OE.Rreg[:k, :] ] else: pbar = tqdm(total=(b - sum(OE.C))) while sum(OE.C) < b: print "used {0} of {1} eval budget".format(sum(OE.C), b) pbar.update(OE.C[-1]) OE.step() state = [ OE.X, OE.Y, OE.S, OE.D, OE.R, OE.C, OE.T, OE.Tr, OE.Ymin, OE.Xmin, OE.Yreg, OE.Rreg ] pickle.dump(state, open(fname, 'wb')) return state
def PESVS(ojf, lb, ub, ki, s, b, cfn, lsl, lsu, fname): overridemaxsteps = 200 sp.random.seed(int(os.urandom(4).encode('hex'), 16)) para = dict() para['kindex'] = ki[0] para['mprior'] = ki[1] para['sprior'] = ki[2] para['s'] = s para['ninit'] = 10 para['volper'] = 1e-6 para['DH_SAMPLES'] = 12 para['DM_SAMPLES'] = 12 para['DM_SUPPORT'] = 1200 para['DM_SLICELCBPARA'] = 1. para['SUPPORT_MODE'] = [ESutils.SUPPORT_LAPAPR] para['cfn'] = cfn para['logsl'] = lsl para['logsu'] = lsu para['s'] = 10**lsu if os.path.exists(fname): print "starting from " + str(fname) OE = OPTutils.PESVS(ojf, lb, ub, para, initstate=pickle.load(open(fname, 'rb'))) else: print "fresh start" OE = OPTutils.PESVS(ojf, lb, ub, para) if sum(OE.C) >= b: print "no further steps needed" k = 0 while sum(OE.C[:k]) < b: k += 1 state = [ OE.X[:k, :], OE.Y[:k, :], OE.S[:k, :], OE.D[:k], OE.R[:k, :], OE.C[:k], OE.T[:k], OE.Tr[:k], OE.Ymin[:k], OE.Xmin[:k, :], OE.Yreg[:k, :], OE.Rreg[:k, :] ] else: pbar = tqdm(total=(b - sum(OE.C))) while sum(OE.C) < b and len(OE.C) < overridemaxsteps: print "used {0} of {1} eval budget".format(sum(OE.C), b) pbar.update(OE.C[-1]) OE.step() state = [ OE.X, OE.Y, OE.S, OE.D, OE.R, OE.C, OE.T, OE.Tr, OE.Ymin, OE.Xmin, OE.Yreg, OE.Rreg ] pickle.dump(state, open(fname, 'wb')) return state
def MLEFS(ojf, lb, ub, ki, s, b, fname): #use kernel ki and evaluate ojf with variance s at step for a budget b sp.random.seed(int(os.urandom(4).encode('hex'), 16)) d = lb.size volper = 1e-8 ninit = 10 para = [ki[0], ki[1], ki[2], volper, s, ninit] if os.path.exists(fname): print "starting from " + str(fname) OE = OPTutils.EIMLE(ojf, lb, ub, para, initstate=pickle.load(open(fname, 'rb'))) else: print "fresh start" OE = OPTutils.EIMLE(ojf, lb, ub, para) if sum(OE.C) >= b: print "no further steps needed" k = 0 while sum(OE.C[:k]) < b: k += 1 state = [ OE.X[:k, :], OE.Y[:k, :], OE.S[:k, :], OE.D[:k], OE.R[:k, :], OE.C[:k], OE.T[:k], OE.Tr[:k], OE.Ymin[:k], OE.Xmin[:k, :], OE.Yreg[:k, :], OE.Rreg[:k, :] ] else: while sum(OE.C) < b: print "used {0} of {1} eval budget".format(sum(OE.C), b) OE.step() state = [ OE.X, OE.Y, OE.S, OE.D, OE.R, OE.C, OE.T, OE.Tr, OE.Ymin, OE.Xmin, OE.Yreg, OE.Rreg ] print "saving as " + str(fname) pickle.dump(state, open(fname, 'wb')) return state
#!/usr/bin/env python2 #encoding: UTF-8 # To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. import scipy as sp from matplotlib import pyplot as plt n=12 def f(x): return 0.1*x**2-0.5*x+1.+0.1*(0.2*abs(x)+1)*sp.random.normal() r=[] y=[] for i in xrange(n): r.append(sorted(sp.random.uniform(0,10,40))) y.append(map(f,r[-1])) plt.plot(r[-1],y[-1],'c') import OPTutils X,Y,lb,ub = OPTutils.mergelines(r,y) plt.fill_between(X,lb,ub,facecolor='lightgreen') plt.plot(X,Y) plt.show()
import sys sys.path.append("./..") import OPTutils import scipy as sp from matplotlib import pyplot as plt import GPdc import ESutils from tqdm import tqdm, tqdm_gui runn = 60 d = 2 lb = sp.array([[-1.0] * d]) ub = sp.array([[1.0] * d]) [ojf, truexmin, trueymin] = OPTutils.gensquexpdraw(d, sp.array([-1.0] * d), sp.array([1.0] * d)) kindex = GPdc.SQUEXP mprior = sp.array([0.0] + [-1.0] * d) sprior = sp.array([1.0] * (d + 1)) maxf = 4000 volper = 1e-7 s = 1e-5 ninit = 10 # para = [kindex,hlb,hub,maxf,s,ninit] para = [kindex, mprior, sprior, volper, s, ninit]
import GPdc import OPTutils import search import os import pickle print 'start' print 'start' d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) pwr = 0.1 def cfn(s): ##print s #print 'cfn'+str(((1e-6)/s)**pwr) return ((1e-6)/s)**pwr ojf = OPTutils.genbranin(cfn=cfn) braninmin = 0.39788735772973816 kindex = GPdc.MAT52 prior = sp.array([0.]+[-1.]*d) sprior = sp.array([1.]*(d+1)) kernel = [kindex,prior,sprior] nreps = 4 bd = 15. slist = [1e3,1e-3] print 'start' f,a_ = plt.subplots(2) fnull,anull=plt.subplots(1) a = sp.hstack([anull,a_]) import os tmp=[] fc = ['lightblue','lightgreen']
import GPdc import OPTutils import search import os import pickle print 'start' print 'start' d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) pwr = 0.3 def cfn(s): #print s #print 'cfn'+str(((1e-6)/s)**pwr) return ((1e-6)/s)**pwr ojf = OPTutils.genbranin(cfn=cfn) braninmin = 0.39788735772973816 kindex = GPdc.MAT52 prior = sp.array([0.]+[-1.]*d) sprior = sp.array([1.]*(d+1)) kernel = [kindex,prior,sprior] nreps = 2 bd = 10 slist = [1e-4, 1e-5,1e-6,1e-7] plist = [0.1,0.2,0.3,0.4,0.5,0.6] print 'start' lines = [[[],[],[],[]] for s in slist] pline = [[],[],[],[]] for l,pwr in enumerate(plist):
# To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. import OPTutils import scipy as sp from matplotlib import pyplot as plt import GPdc import ESutils d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) [ojf,truexmin] = OPTutils.gensquexpdraw(d,sp.array([-1.]*d),sp.array([1.]*d),ignores=1e-3) O = OPTutils.opt(ojf,lb,ub) for i in xrange(20): O.step() para = dict() para['kindex'] = GPdc.SQUEXPCS para['mprior'] = sp.array([0.]+[-1.]*d+[-2.]) para['sprior'] = sp.array([1.]*(d+1)+[2.]) #para['s'] = 1e-6 para['ninit'] = 10 #para['maxf'] = 2500 para['volper'] = 1e-7 para['DH_SAMPLES'] = 8 para['DM_SAMPLES'] = 8 para['DM_SUPPORT'] = 400
import DIRECT seed=123 sp.random.seed(seed) d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) sl=0. su=1. sfn = lambda x:1e-8 fls = 1.0 sls = (su-sl)*fls dcc=1.0 cfn = lambda x: sp.exp(-dcc*x.flatten()[0]) [ojf,truexmin,ymin] = OPTutils.gensquexpIPdraw(d,lb,ub,sl,su,sfn,sls,cfn) #what are the best mins in planes away from true? def planemin(xp): def dirwrap(x,y): z,c = ojf(sp.hstack([xp,x]),-1,[sp.NaN],override=True) return (z,0) [xm,ym,ierror] = DIRECT.solve(dirwrap,lb,ub,user_data=[], algmethod=1, maxf=80000, logfilename='/dev/null') ye,c = ojf(sp.hstack([0.,xm]),-1,[sp.NaN],override=True) r=ye-ymin return [xm,ym,ye,r] plane00 = planemin(0.) plane025 = planemin(0.25) plane05 = planemin(0.5) plane001 = planemin(0.01)
import scipy as sp from matplotlib import pyplot as plt import GPdc import OPTutils import search import os import pickle d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) pwr = 0.2 cfn = lambda s:((1e-6)/s)**pwr ojf = OPTutils.gencamel(cfn=cfn) camelmin = -1.031628453489877 kindex = GPdc.MAT52 prior = sp.array([0.]+[-1.]*d) sprior = sp.array([1.]*(d+1)) kernel = [kindex,prior,sprior] nreps = 8 bd = 15 slist = [1e-4,1e-6,1e-8] f,a = plt.subplots(3) for s in slist: names = ["../cache/camelnoise/EIMLE_"+str(int(100*sp.log10(s)))+"_"+str(pwr)+"_"+str(i)+".p" for i in xrange(nreps)] results = search.multiMLEFS(ojf,lb,ub,kernel,s,bd,names) yr = [r[11].flatten() for r in results]
import scipy as sp from matplotlib import pyplot as plt import GPdc import OPTutils import search import os import pickle d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) pwr = 0.2 cfn = lambda s:((1e-6)/s)**pwr ojf = OPTutils.genbanana(cfn=cfn) kindex = GPdc.MAT52 prior = sp.array([0.]+[-1.]*d) sprior = sp.array([1.]*(d+1)) kernel = [kindex,prior,sprior] nreps = 5 bd = 15 slist = [1e-4,1e-6,1e-8] f,a = plt.subplots(2) for s in slist: names = ["../cache/rosennoise/EIMLE_"+str(int(100*sp.log10(s)))+"_"+str(pwr)+"_"+str(i)+".p" for i in xrange(nreps)] results = search.multiMLEFS(ojf,lb,ub,kernel,s,bd,names) yr = [r[11].flatten() for r in results] C = results[0][5]
lb = sp.array([[-1.] * d]) ub = sp.array([[1.] * d]) [ojf, truexmin] = [lambda x, s, d: [-1., -1.], None] para = dict() para['kindex'] = GPdc.SQUEXP para['mprior'] = sp.array([0.] + [-1.] * d) para['sprior'] = sp.array([1.] * (d + 1)) para['s'] = 1e-6 para['ninit'] = 10 #para['maxf'] = 2500 para['volper'] = 1e-6 para['DH_SAMPLES'] = 8 para['DM_SAMPLES'] = 8 para['DM_SUPPORT'] = 400 para['DM_SLICELCBPARA'] = 0.025 para['SUPPORT_MODE'] = ESutils.SUPPORT_SLICEPM OP = OPTutils.PESFS(ojf, lb, ub, para) import pickle [OP.X, OP.Y, OP.S, OP.D, OP.R, OP.C, OP.T, OP.Tr, OP.Ymin] = pickle.load(open('state.p', 'rb')) OP.pcs() #OP.step() plt.figure(1) #plt.plot(OP.R[-1,0],OP.R[-1,1],'r.') plt.plot(OP.X[:, 0], OP.X[:, 1], 'b.') plt.show()
# To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. import OPTutils import scipy as sp from matplotlib import pyplot as plt import GPdc import ESutils from tqdm import tqdm, tqdm_gui import copy runn = 10 d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) [ojf,truexmin,trueymin] = OPTutils.gensquexpdraw(d,sp.array([-1.]*d),sp.array([1.]*d),ignores=1e-9) print "truexmin "+ str(truexmin) f,a = plt.subplots(8) #ojf = OPTutils.banana #truexmin=OPTutils.bananamin #trueymin=0. O = OPTutils.opt(ojf,lb,ub) for i in tqdm(xrange(10)): O.step() initstate = copy.deepcopy([O.X,O.Y,O.S,O.D,O.R,O.C,O.T,O.Tr,O.Ymin,O.Xmin,O.Yreg,O.Rreg]) kindex = GPdc.MAT52 mprior = sp.array([0.]+[-1.]*d) sprior = sp.array([1.]*(d+1)) volper=1e-8
# To change this template file, choose Tools | Templates # and open the template in the editor. import sys sys.path.append("./..") import OPTutils import scipy as sp from matplotlib import pyplot as plt import GPdc import ESutils from tqdm import tqdm, tqdm_gui runn = 60 d = 2 lb = sp.array([[-1.] * d]) ub = sp.array([[1.] * d]) [ojf, truexmin, trueymin] = OPTutils.gensquexpdraw(d, sp.array([-1.] * d), sp.array([1.] * d)) kindex = GPdc.SQUEXP mprior = sp.array([0.] + [-1.] * d) sprior = sp.array([1.] * (d + 1)) maxf = 4000 volper = 1e-7 s = 1e-5 ninit = 10 #para = [kindex,hlb,hub,maxf,s,ninit] para = [kindex, mprior, sprior, volper, s, ninit] OE = OPTutils.EIMLE(ojf, lb, ub, para)
print 'start' print 'start' d = 2 lb = sp.array([[-1.] * d]) ub = sp.array([[1.] * d]) pwr = 0.3 def cfn(s): #print s #print 'cfn'+str(((1e-6)/s)**pwr) return ((1e-6) / s)**pwr ojf = OPTutils.genbranin(cfn=cfn) braninmin = 0.39788735772973816 kindex = GPdc.MAT52 prior = sp.array([0.] + [-1.] * d) sprior = sp.array([1.] * (d + 1)) kernel = [kindex, prior, sprior] nreps = 2 bd = 10 slist = [1e-4, 1e-5, 1e-6, 1e-7] plist = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] print 'start' lines = [[[], [], [], []] for s in slist] pline = [[], [], [], []] for l, pwr in enumerate(plist): f, a = plt.subplots(len(slist) + 1)
sprior = sp.array([1.]*(d+2)+[2.]) kernel = [kindex,prior,sprior] fnames = ["../cache/fith/PI{}.p".format(i) for i in xrange(5)] statesPI = search.multiPESIPS(ojfa,lb,ub,kernel,10,fnames) x=[] y=[] for stateEI in statesEI: a[2].plot(stateEI[11],'r') #a[3].plot([sum(stateEI[5][:i]) for i in xrange(len(stateEI[5]))],stateEI[11],'r') x.append([sum(stateEI[5][:i]) for i in xrange(len(stateEI[5]))]) y.append(stateEI[11].flatten()) print 'reccomended under EI: {} : {}'.format([10**i for i in stateEI[4][-1]],ojf(stateEI[4][-1],None,None)[0]) X_,Y_,lb_,ub_ = OPTutils.mergelines(x,y) a[3].fill_between(X_,lb_,ub_,facecolor='lightcoral',edgecolor='lightcoral',alpha=0.5) a[3].plot(X_,Y_,'r') x=[] y=[] for statePE in statesPE: a[2].plot(statePE[11],'b') #a[3].plot([sum(statePE[5][:i]) for i in xrange(len(statePE[5]))],statePE[11],'b') x.append([sum(statePE[5][:i]) for i in xrange(len(statePE[5]))]) y.append(statePE[11].flatten()) print 'reccomended under PE: {} : {}'.format([10**i for i in statePE[4][-1]],ojf(statePE[4][-1],None,None)[0]) X_,Y_,lb_,ub_ = OPTutils.mergelines(x,y) a[3].fill_between(X_,lb_,ub_,facecolor='lightblue',edgecolor='lightblue',alpha=0.5) a[3].plot(X_,Y_,'b')
#encoding: UTF-8 # To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. import scipy as sp from matplotlib import pyplot as plt n = 12 def f(x): return 0.1 * x**2 - 0.5 * x + 1. + 0.1 * (0.2 * abs(x) + 1) * sp.random.normal() r = [] y = [] for i in xrange(n): r.append(sorted(sp.random.uniform(0, 10, 40))) y.append(map(f, r[-1])) plt.plot(r[-1], y[-1], 'c') import OPTutils X, Y, lb, ub = OPTutils.mergelines(r, y) plt.fill_between(X, lb, ub, facecolor='lightgreen') plt.plot(X, Y) plt.show()
kernel = [kindex, prior, sprior] fnames = ["../cache/fith/PI{}.p".format(i) for i in xrange(5)] statesPI = search.multiPESIPS(ojfa, lb, ub, kernel, 10, fnames) x = [] y = [] for stateEI in statesEI: a[2].plot(stateEI[11], 'r') #a[3].plot([sum(stateEI[5][:i]) for i in xrange(len(stateEI[5]))],stateEI[11],'r') x.append([sum(stateEI[5][:i]) for i in xrange(len(stateEI[5]))]) y.append(stateEI[11].flatten()) print 'reccomended under EI: {} : {}'.format( [10**i for i in stateEI[4][-1]], ojf(stateEI[4][-1], None, None)[0]) X_, Y_, lb_, ub_ = OPTutils.mergelines(x, y) a[3].fill_between(X_, lb_, ub_, facecolor='lightcoral', edgecolor='lightcoral', alpha=0.5) a[3].plot(X_, Y_, 'r') x = [] y = [] for statePE in statesPE: a[2].plot(statePE[11], 'b') #a[3].plot([sum(statePE[5][:i]) for i in xrange(len(statePE[5]))],statePE[11],'b') x.append([sum(statePE[5][:i]) for i in xrange(len(statePE[5]))]) y.append(statePE[11].flatten())
print 'start' print 'start' d = 2 lb = sp.array([[-1.] * d]) ub = sp.array([[1.] * d]) pwr = 0.1 def cfn(s): ##print s #print 'cfn'+str(((1e-6)/s)**pwr) return ((1e-6) / s)**pwr ojf = OPTutils.genbranin(cfn=cfn) braninmin = 0.39788735772973816 kindex = GPdc.MAT52 prior = sp.array([0.] + [-1.] * d) sprior = sp.array([1.] * (d + 1)) kernel = [kindex, prior, sprior] nreps = 4 bd = 15. slist = [1e3, 1e-3] print 'start' f, a_ = plt.subplots(2) fnull, anull = plt.subplots(1) a = sp.hstack([anull, a_]) import os tmp = []
import scipy as sp from matplotlib import pyplot as plt import GPdc import OPTutils import search import os import pickle d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) pwr = 0.2 cfn = lambda s:((1e-6)/s)**pwr ojf = OPTutils.gencamel(cfn=cfn) camelmin = -1.031628453489877 kindex = GPdc.MAT52 prior = sp.array([0.]+[-1.]*d) sprior = sp.array([1.]*(d+1)) kernel = [kindex,prior,sprior] nreps = 12 bd = 100 s = 1e-6 f,a = plt.subplots(2) names = ["../cache/camelrecc/EIMLE_"+str(int(100*sp.log10(s)))+"_"+str(pwr)+"_"+str(i)+".p" for i in xrange(nreps)] results = search.multiMLEFS(ojf,lb,ub,kernel,s,bd,names) C = results[0][5]
sys.path.append("./..") import OPTutils import scipy as sp from matplotlib import pyplot as plt import GPdc import ESutils from tqdm import tqdm, tqdm_gui d=2 lb = sp.array([[-1.]*d]) ub = sp.array([[1.]*d]) sl=0. su=1. sfn = lambda x:1e-8 sls = (su-sl)*0.75 cfn = lambda x: sp.exp(-0.5*x.flatten()[0]) [ojf,truexmin,ymin] = OPTutils.gensquexpIPdraw(d,lb,ub,sl,su,sfn,sls,cfn) print truexmin print ojf(sp.array([sl,truexmin[0],truexmin[1]]),1e-8,[sp.NaN]) u = sp.empty(100) v = sp.empty(100) w = sp.empty(100) sup = sp.linspace(-1,1,100) spp = sp.linspace(sl,su,100) f,a = plt.subplots(3) for i in xrange(100): u[i] = ojf(sp.array([sl,sup[i],truexmin[1]]),1e-8,[sp.NaN])[0] v[i] = ojf(sp.array([sl,truexmin[0],sup[i]]),1e-8,[sp.NaN])[0] w[i] = ojf(sp.array([spp[i],truexmin[0],truexmin[1]]),1e-8,[sp.NaN])[0] a[0].plot(sup,u) a[1].plot(sup,v)