Exemplo n.º 1
0
    def _testKernelMaxEI(self):
        
        # test different methods of optimizing kernel
        S5 = Shekel5()
        
        hv = 0.1
        testkernels = [GaussianKernel_iso([hv]), 
                   GaussianKernel_ard([hv, hv, hv, hv]),
                   MaternKernel3([hv, 1.0])]
                   # MaternKernel5([hv, 1.0])]

        for kernel in testkernels:
            # print
            # print kernel.__class__
            
        
            # train GPs
            X = lhcSample(S5.bounds, 10, seed=0)
            Y = [S5.f(x) for x in X]
        
            GP = GaussianProcess(kernel, X, Y)
        
            eif = EI(GP)
            dopt, doptx = direct(eif.negf, S5.bounds, maxiter=10)
            copt, coptx = cdirect(eif.negf, S5.bounds, maxiter=10)
            mopt, moptx = maximizeEI(GP, S5.bounds, maxiter=10)
            # print dopt, doptx
            # print copt, coptx
            # print mopt, moptx
        
            self.failUnlessAlmostEqual(dopt, copt, 4)
            self.failUnlessAlmostEqual(-dopt, mopt, 4)
            self.failUnlessAlmostEqual(-copt, mopt, 4)
        
            self.failUnless(sum(abs(doptx-coptx)) < .01)
            self.failUnless(sum(abs(moptx-coptx)) < .01)
            self.failUnless(sum(abs(moptx-doptx)) < .01)
        
            # train GP w/prior
            pX = lhcSample(S5.bounds, 100, seed=101)
            pY = [S5.f(x) for x in pX]
            prior = RBFNMeanPrior()
            prior.train(pX, pY, bounds=S5.bounds, k=10, seed=102)
        
            GP = GaussianProcess(kernel, X, Y, prior=prior)        
        
            eif = EI(GP)
            pdopt, pdoptx = direct(eif.negf, S5.bounds, maxiter=10)
            pcopt, pcoptx = cdirect(eif.negf, S5.bounds, maxiter=10)
            pmopt, pmoptx = maximizeEI(GP, S5.bounds, maxiter=10)
        
            self.failIfAlmostEqual(pdopt, dopt, 3)
            self.failUnlessAlmostEqual(pdopt, pcopt, 4)
            self.failUnlessAlmostEqual(-pdopt, pmopt, 4)
            self.failUnlessAlmostEqual(-pcopt, pmopt, 4)
        
            self.failUnless(sum(abs(pdoptx-pcoptx)) < .01)
            self.failUnless(sum(abs(pmoptx-pcoptx)) < .01)
            self.failUnless(sum(abs(pmoptx-pdoptx)) < .01)
Exemplo n.º 2
0
    def testNoise(self):
        
        tf = Branin()
        
        X = lhcSample(tf.bounds, 10, seed=0)
        Y = [tf.f(x) for x in X]
        GP1 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=1e-4)
        self.failUnlessEqual(GP1.noise, 1e-4)

        eif1 = EI(GP1)
        dopt1, _ = direct(eif1.negf, tf.bounds, maxiter=10)
        copt1, _ = cdirect(eif1.negf, tf.bounds, maxiter=10)
        mopt1, _ = maximizeEI(GP1, tf.bounds, maxiter=10)

        self.failUnlessAlmostEqual(dopt1, copt1, 4)
        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)
        self.failUnlessAlmostEqual(-copt1, mopt1, 4)

        GP2 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=0.01)
        self.failUnlessEqual(GP2.noise, 0.01)
        
        eif2 = EI(GP2)
        dopt2, _ = direct(eif2.negf, tf.bounds, maxiter=10)
        copt2, _ = cdirect(eif2.negf, tf.bounds, maxiter=10)
        mopt2, _ = maximizeEI(GP2, tf.bounds, maxiter=10)
        self.failUnlessAlmostEqual(dopt2, copt2, 4)
        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)
        self.failUnlessAlmostEqual(-copt2, mopt2, 4)

        self.failIfAlmostEqual(dopt1, dopt2, 4)
        self.failIfAlmostEqual(copt1, copt2, 4)
        self.failIfAlmostEqual(mopt1, mopt2, 4)

        GP3 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=0.1)
        self.failUnlessEqual(GP3.noise, 0.1)
        eif3 = EI(GP3)
        dopt3, _ = direct(eif3.negf, tf.bounds, maxiter=10)
        copt3, _ = cdirect(eif3.negf, tf.bounds, maxiter=10)
        mopt3, _ = maximizeEI(GP3, tf.bounds, maxiter=10)
        self.failUnlessAlmostEqual(dopt3, copt3, 4)
        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)
        self.failUnlessAlmostEqual(-copt3, mopt3, 4)

        self.failIfAlmostEqual(dopt1, dopt3, 4)
        self.failIfAlmostEqual(copt1, copt3, 4)
        self.failIfAlmostEqual(mopt1, mopt3, 4)
        self.failIfAlmostEqual(dopt2, dopt3, 4)
        self.failIfAlmostEqual(copt2, copt3, 4)
        self.failIfAlmostEqual(mopt2, mopt3, 4)
Exemplo n.º 3
0
    def testNoise(self):

        tf = Branin()

        X = lhcSample(tf.bounds, 10, seed=0)
        Y = [tf.f(x) for x in X]
        GP1 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=1e-4)
        self.failUnlessEqual(GP1.noise, 1e-4)

        eif1 = EI(GP1)
        dopt1, _ = direct(eif1.negf, tf.bounds, maxiter=10)
        copt1, _ = cdirect(eif1.negf, tf.bounds, maxiter=10)
        mopt1, _ = maximizeEI(GP1, tf.bounds, maxiter=10)

        self.failUnlessAlmostEqual(dopt1, copt1, 4)
        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)
        self.failUnlessAlmostEqual(-copt1, mopt1, 4)

        GP2 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=0.01)
        self.failUnlessEqual(GP2.noise, 0.01)

        eif2 = EI(GP2)
        dopt2, _ = direct(eif2.negf, tf.bounds, maxiter=10)
        copt2, _ = cdirect(eif2.negf, tf.bounds, maxiter=10)
        mopt2, _ = maximizeEI(GP2, tf.bounds, maxiter=10)
        self.failUnlessAlmostEqual(dopt2, copt2, 4)
        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)
        self.failUnlessAlmostEqual(-copt2, mopt2, 4)

        self.failIfAlmostEqual(dopt1, dopt2, 4)
        self.failIfAlmostEqual(copt1, copt2, 4)
        self.failIfAlmostEqual(mopt1, mopt2, 4)

        GP3 = GaussianProcess(MaternKernel3([1.0, 1.0]), X, Y, noise=0.1)
        self.failUnlessEqual(GP3.noise, 0.1)
        eif3 = EI(GP3)
        dopt3, _ = direct(eif3.negf, tf.bounds, maxiter=10)
        copt3, _ = cdirect(eif3.negf, tf.bounds, maxiter=10)
        mopt3, _ = maximizeEI(GP3, tf.bounds, maxiter=10)
        self.failUnlessAlmostEqual(dopt3, copt3, 4)
        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)
        self.failUnlessAlmostEqual(-copt3, mopt3, 4)

        self.failIfAlmostEqual(dopt1, dopt3, 4)
        self.failIfAlmostEqual(copt1, copt3, 4)
        self.failIfAlmostEqual(mopt1, mopt3, 4)
        self.failIfAlmostEqual(dopt2, dopt3, 4)
        self.failIfAlmostEqual(copt2, copt3, 4)
        self.failIfAlmostEqual(mopt2, mopt3, 4)
Exemplo n.º 4
0
    def testXi(self):
        
        S5 = Shekel5()
        
        GP1 = GaussianProcess(GaussianKernel_iso([.2]))
        # self.failUnlessEqual(GP1.xi, 0.0)
        X = lhcSample(S5.bounds, 10, seed=0)
        Y = [S5.f(x) for x in X]
        GP1.addData(X, Y)

        eif1 = EI(GP1, xi=0.0)
        dopt1, _ = direct(eif1.negf, S5.bounds, maxiter=10)
        copt1, _ = cdirect(eif1.negf, S5.bounds, maxiter=10)
        mopt1, _ = maximizeEI(GP1, S5.bounds, xi=0.0, maxiter=10)

        self.failUnlessAlmostEqual(dopt1, copt1, 4)
        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)
        self.failUnlessAlmostEqual(-copt1, mopt1, 4)

        GP2 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)
        eif2 = EI(GP2, xi=0.01)    
        self.failUnlessEqual(eif2.xi, 0.01)    
        dopt2, _ = direct(eif2.negf, S5.bounds, maxiter=10)
        copt2, _ = cdirect(eif2.negf, S5.bounds, maxiter=10)
        mopt2, _ = maximizeEI(GP2, S5.bounds, xi=0.01, maxiter=10)
        self.failUnlessAlmostEqual(dopt2, copt2, 4)
        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)
        self.failUnlessAlmostEqual(-copt2, mopt2, 4)

        self.failIfAlmostEqual(dopt1, dopt2, 4)
        self.failIfAlmostEqual(copt1, copt2, 4)
        self.failIfAlmostEqual(mopt1, mopt2, 4)

        GP3 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)
        eif3 = EI(GP3, xi=0.1)    
        dopt3, _ = direct(eif3.negf, S5.bounds, maxiter=10)
        copt3, _ = cdirect(eif3.negf, S5.bounds, maxiter=10)
        mopt3, _ = maximizeEI(GP3, S5.bounds, xi=0.1, maxiter=10)
        self.failUnlessAlmostEqual(dopt3, copt3, 4)
        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)
        self.failUnlessAlmostEqual(-copt3, mopt3, 4)

        self.failIfAlmostEqual(dopt1, dopt3, 4)
        self.failIfAlmostEqual(copt1, copt3, 4)
        self.failIfAlmostEqual(mopt1, mopt3, 4)
        self.failIfAlmostEqual(dopt2, dopt3, 4)
        self.failIfAlmostEqual(copt2, copt3, 4)
        self.failIfAlmostEqual(mopt2, mopt3, 4)
Exemplo n.º 5
0
    def testXi(self):

        S5 = Shekel5()

        GP1 = GaussianProcess(GaussianKernel_iso([.2]))
        # self.failUnlessEqual(GP1.xi, 0.0)
        X = lhcSample(S5.bounds, 10, seed=0)
        Y = [S5.f(x) for x in X]
        GP1.addData(X, Y)

        eif1 = EI(GP1, xi=0.0)
        dopt1, _ = direct(eif1.negf, S5.bounds, maxiter=10)
        copt1, _ = cdirect(eif1.negf, S5.bounds, maxiter=10)
        mopt1, _ = maximizeEI(GP1, S5.bounds, xi=0.0, maxiter=10)

        self.failUnlessAlmostEqual(dopt1, copt1, 4)
        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)
        self.failUnlessAlmostEqual(-copt1, mopt1, 4)

        GP2 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)
        eif2 = EI(GP2, xi=0.01)
        self.failUnlessEqual(eif2.xi, 0.01)
        dopt2, _ = direct(eif2.negf, S5.bounds, maxiter=10)
        copt2, _ = cdirect(eif2.negf, S5.bounds, maxiter=10)
        mopt2, _ = maximizeEI(GP2, S5.bounds, xi=0.01, maxiter=10)
        self.failUnlessAlmostEqual(dopt2, copt2, 4)
        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)
        self.failUnlessAlmostEqual(-copt2, mopt2, 4)

        self.failIfAlmostEqual(dopt1, dopt2, 4)
        self.failIfAlmostEqual(copt1, copt2, 4)
        self.failIfAlmostEqual(mopt1, mopt2, 4)

        GP3 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)
        eif3 = EI(GP3, xi=0.1)
        dopt3, _ = direct(eif3.negf, S5.bounds, maxiter=10)
        copt3, _ = cdirect(eif3.negf, S5.bounds, maxiter=10)
        mopt3, _ = maximizeEI(GP3, S5.bounds, xi=0.1, maxiter=10)
        self.failUnlessAlmostEqual(dopt3, copt3, 4)
        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)
        self.failUnlessAlmostEqual(-copt3, mopt3, 4)

        self.failIfAlmostEqual(dopt1, dopt3, 4)
        self.failIfAlmostEqual(copt1, copt3, 4)
        self.failIfAlmostEqual(mopt1, mopt3, 4)
        self.failIfAlmostEqual(dopt2, dopt3, 4)
        self.failIfAlmostEqual(copt2, copt3, 4)
        self.failIfAlmostEqual(mopt2, mopt3, 4)
Exemplo n.º 6
0
    def testShekel(self):

        S = Shekel5(maximize=False)
        opt, optx = direct(S.f, S.bounds, maxiter=20, debug=False)
        self.assertAlmostEqual(opt, S.minimum, 3)
        for x, a in zip(optx, S.argmin):
            self.assertAlmostEqual(x, a, 3)
Exemplo n.º 7
0
    def testShekel(self):

        S = Shekel5(maximize=False)
        opt, optx = direct(S.f, S.bounds, maxiter=20, debug=False)
        self.assertAlmostEqual(opt, S.minimum, 3)
        for x, a in zip(optx, S.argmin):
            self.assertAlmostEqual(x, a, 3)
Exemplo n.º 8
0
    def testXi(self):

        S5 = Shekel5()

        GP1 = GaussianProcess(GaussianKernel_iso([.2]))
        # self.failUnlessEqual(GP1.xi, 0.0)
        X = lhcSample(S5.bounds, 10, seed=0)
        Y = [S5.f(x) for x in X]
        GP1.addData(X, Y)

        ucbf1 = UCB(GP1, len(S5.bounds), scale=0.5)
        dopt1, _ = direct(ucbf1.negf, S5.bounds, maxiter=10)
        copt1, _ = cdirect(ucbf1.negf, S5.bounds, maxiter=10)
        mopt1, _ = maximizeUCB(GP1, S5.bounds, scale=0.5, maxiter=10)

        self.failUnlessAlmostEqual(dopt1, copt1, 4)
        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)
        self.failUnlessAlmostEqual(-copt1, mopt1, 4)

        GP2 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)
        ucbf2 = UCB(GP2, len(S5.bounds), scale=0.01)
        dopt2, _ = direct(ucbf2.negf, S5.bounds, maxiter=10)
        copt2, _ = cdirect(ucbf2.negf, S5.bounds, maxiter=10)
        mopt2, _ = maximizeUCB(GP2, S5.bounds, scale=.01, maxiter=10)
        self.failUnlessAlmostEqual(dopt2, copt2, 4)
        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)
        self.failUnlessAlmostEqual(-copt2, mopt2, 4)

        self.failIfAlmostEqual(dopt1, dopt2, 4)
        self.failIfAlmostEqual(copt1, copt2, 4)
        self.failIfAlmostEqual(mopt1, mopt2, 4)

        GP3 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)
        ucbf3 = UCB(GP3, len(S5.bounds), scale=.9)
        dopt3, _ = direct(ucbf3.negf, S5.bounds, maxiter=10)
        copt3, _ = cdirect(ucbf3.negf, S5.bounds, maxiter=10)
        mopt3, _ = maximizeUCB(GP3, S5.bounds, scale=0.9, maxiter=10)
        self.failUnlessAlmostEqual(dopt3, copt3, 4)
        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)
        self.failUnlessAlmostEqual(-copt3, mopt3, 4)

        self.failIfAlmostEqual(dopt1, dopt3, 4)
        self.failIfAlmostEqual(copt1, copt3, 4)
        self.failIfAlmostEqual(mopt1, mopt3, 4)
        self.failIfAlmostEqual(dopt2, dopt3, 4)
        self.failIfAlmostEqual(copt2, copt3, 4)
        self.failIfAlmostEqual(mopt2, mopt3, 4)
Exemplo n.º 9
0
    def testXi(self):
        
        S5 = Shekel5()
        
        GP1 = GaussianProcess(GaussianKernel_iso([.2]))
        # self.failUnlessEqual(GP1.xi, 0.0)
        X = lhcSample(S5.bounds, 10, seed=0)
        Y = [S5.f(x) for x in X]
        GP1.addData(X, Y)

        ucbf1 = UCB(GP1, len(S5.bounds), scale=0.5)
        dopt1, _ = direct(ucbf1.negf, S5.bounds, maxiter=10)
        copt1, _ = cdirect(ucbf1.negf, S5.bounds, maxiter=10)
        mopt1, _ = maximizeUCB(GP1, S5.bounds, scale=0.5, maxiter=10)

        self.failUnlessAlmostEqual(dopt1, copt1, 4)
        self.failUnlessAlmostEqual(-dopt1, mopt1, 4)
        self.failUnlessAlmostEqual(-copt1, mopt1, 4)

        GP2 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)
        ucbf2 = UCB(GP2, len(S5.bounds), scale=0.01)    
        dopt2, _ = direct(ucbf2.negf, S5.bounds, maxiter=10)
        copt2, _ = cdirect(ucbf2.negf, S5.bounds, maxiter=10)
        mopt2, _ = maximizeUCB(GP2, S5.bounds, scale=.01, maxiter=10)
        self.failUnlessAlmostEqual(dopt2, copt2, 4)
        self.failUnlessAlmostEqual(-dopt2, mopt2, 4)
        self.failUnlessAlmostEqual(-copt2, mopt2, 4)

        self.failIfAlmostEqual(dopt1, dopt2, 4)
        self.failIfAlmostEqual(copt1, copt2, 4)
        self.failIfAlmostEqual(mopt1, mopt2, 4)

        GP3 = GaussianProcess(GaussianKernel_iso([.3]), X, Y)
        ucbf3 = UCB(GP3, len(S5.bounds), scale=.9)    
        dopt3, _ = direct(ucbf3.negf, S5.bounds, maxiter=10)
        copt3, _ = cdirect(ucbf3.negf, S5.bounds, maxiter=10)
        mopt3, _ = maximizeUCB(GP3, S5.bounds, scale=0.9, maxiter=10)
        self.failUnlessAlmostEqual(dopt3, copt3, 4)
        self.failUnlessAlmostEqual(-dopt3, mopt3, 4)
        self.failUnlessAlmostEqual(-copt3, mopt3, 4)

        self.failIfAlmostEqual(dopt1, dopt3, 4)
        self.failIfAlmostEqual(copt1, copt3, 4)
        self.failIfAlmostEqual(mopt1, mopt3, 4)
        self.failIfAlmostEqual(dopt2, dopt3, 4)
        self.failIfAlmostEqual(copt2, copt3, 4)
        self.failIfAlmostEqual(mopt2, mopt3, 4)
Exemplo n.º 10
0
    def testArgs(self):

        # test passing args into DIRECT
        def foo(x, a1, a2):
            return -sum(sin(array(x) * a1) + array(x) * a2)

        bounds = [[0., 5.]] * 3
        args1 = [3.0, 0.0]
        opt, optx = direct(foo, bounds, args=args1, maxiter=20)
        # print 'args = %s, opt=%s, optx=%s' % (args1, opt, optx)
        self.assertAlmostEqual(opt, -3.0, 1)
        self.assertAlmostEqual(optx[0], 2.6234, 1)

        args2 = [-2.0, 2.0]
        opt, optx = direct(foo, bounds, args=args2, maxiter=10)
        # print 'args = %s, opt=%s, optx=%s' % (args2, opt, optx)
        # self.failUnlessAlmostEqual(opt, -31.0540, 4)
        self.assert_(abs(optx[0] - 5) < .5)
Exemplo n.º 11
0
    def testArgs(self):
        
        # test passing args into DIRECT
        def foo(x, a1, a2):
            return -sum(sin(array(x)*a1)+array(x)*a2)
        
        bounds = [[0., 5.]] * 3
        args1 = [3.0, 0.0]
        opt, optx = direct(foo, bounds, args=args1, maxiter=20)        
        # print 'args = %s, opt=%s, optx=%s' % (args1, opt, optx)
        self.assertAlmostEqual(opt, -3.0, 1)
        self.assertAlmostEqual(optx[0], 2.6234, 1)

        args2 = [-2.0, 2.0]
        opt, optx = direct(foo, bounds, args=args2, maxiter=10)
        # print 'args = %s, opt=%s, optx=%s' % (args2, opt, optx)
        # self.failUnlessAlmostEqual(opt, -31.0540, 4)
        self.assert_(abs(optx[0]-5) < .5)
Exemplo n.º 12
0
def maximizeEI(model,
               bounds,
               useCDIRECT=True,
               xi=0.01,
               maxiter=50,
               maxtime=30,
               maxsample=10000,
               **kwargs):
    """
    Try to maximize EI in the most efficient way possible.  The methods
    will be attempted in this order:
    
        1.  If there is a C implementation of the EI objective for the
            kernel, it will be used (currently only works for Gaussian
            kernel with ARD).
            
        2.  If the C implementation of DIRECT is available, it will be used.
        
        3.  Otherwise, the Python implementation of DIRECT will be used.
    """
    if not useCDIRECT:
        print('using DIRECT')
        ei = EI(model, xi, **kwargs)
        opt, optx = direct(ei.negf,
                           bounds,
                           maxiter=maxiter,
                           maxtime=maxtime,
                           maxsample=maxsample,
                           **kwargs)
        return -opt, optx

    if isinstance(model, GaussianProcess):
        return cdirectGP(model,
                         bounds,
                         maxiter,
                         maxtime,
                         maxsample,
                         acqfunc='ei',
                         xi=xi,
                         **kwargs)

    elif isinstance(model, RandomForest):
        return cdirectRF(model,
                         bounds,
                         maxiter,
                         maxtime,
                         maxsample,
                         acqfunc='ei',
                         xi=xi,
                         **kwargs)
Exemplo n.º 13
0
def maximizeMean(model, bounds, maxiter=50, maxtime=30, maxsample=10000):
    """
    Maximize the GP-mean using DIRECT
    Python DIRECT is used.
    """
    print "Using Python DIRECT."

    def gp_mean_obj(x, gp=model):
        mu, sig2 = gp.posterior(x)
        return -mu

    opt, optx = direct(
        gp_mean_obj, bounds, args=None, debug=False, maxiter=maxiter, maxtime=maxtime, maxsample=maxsample
    )
    return optx, opt
Exemplo n.º 14
0
def maximizePI(model, bounds, xi=0.01, maxiter=50, maxtime=30, maxsample=10000, useCDIRECT=True, **kwargs):
    """
    Maximize the probability of improvement, as described in [Lizotte 2008].
    """
    if not useCDIRECT:
        print "using DIRECT"
        pi = PI(model, xi, **kwargs)
        opt, optx = direct(pi.negf, bounds, maxiter=maxiter, maxtime=maxtime, maxsample=maxsample, **kwargs)
        return -opt, optx

    if isinstance(model, GaussianProcess):
        return cdirectGP(model, bounds, maxiter, maxtime, maxsample, acqfunc="pi", xi=xi, **kwargs)

    elif isinstance(model, RandomForest):
        return cdirectRF(model, bounds, maxiter, maxtime, maxsample, acqfunc="pi", xi=xi, **kwargs)

    else:
        raise ValueError
Exemplo n.º 15
0
    def testMaxEIPrior(self):

        # make sure that the prior works with the different methods of EI
        # maximization
        
        S5 = Shekel5()
        pX = lhcSample(S5.bounds, 100, seed=511)
        pY = [S5.f(x) for x in pX]
        prior = RBFNMeanPrior()
        prior.train(pX, pY, bounds=S5.bounds, k=10, seed=504)
        
        hv = .1
        hyper = [hv, hv, hv, hv]
        kernel = GaussianKernel_ard(hyper)
        X = lhcSample(S5.bounds, 10, seed=512)
        Y = [S5.f(x) for x in X]
        GP = GaussianProcess(kernel, X, Y, prior=prior)
        
        # validation
        valX = list(x.copy() for x in X)
        valY = copy(Y)

        eif = EI(GP)
        dopt, _ = direct(eif.negf, S5.bounds, maxiter=20)
        copt, _ = cdirect(eif.negf, S5.bounds, maxiter=20)
        mopt, _ = maximizeEI(GP, S5.bounds, maxiter=20)

        self.failUnlessAlmostEqual(dopt, copt, 2)
        self.failUnlessAlmostEqual(-dopt, mopt, 2)
        
        for i in xrange(len(GP.X)):
            self.failUnless(all(valX[i]==GP.X[i]))
            self.failUnless(valY[i]==GP.Y[i])
        
        GP.prior.mu(GP.X[0])
        self.failUnless(all(valX[0]==GP.X[0]))
        # print GP.X
        
        for i in xrange(len(GP.X)):
            self.failUnless(all(valX[i]==GP.X[i]))
            self.failUnless(valY[i]==GP.Y[i])
        
        GP.prior.mu(GP.X[0])
        self.failUnless(all(valX[0]==GP.X[0]))
Exemplo n.º 16
0
def maximizePI(model,
               bounds,
               xi=0.01,
               maxiter=50,
               maxtime=30,
               maxsample=10000,
               useCDIRECT=True,
               **kwargs):
    """
    Maximize the probability of improvement, as described in [Lizotte 2008].
    """
    if not useCDIRECT:
        print('using DIRECT')
        pi = PI(model, xi, **kwargs)
        opt, optx = direct(pi.negf,
                           bounds,
                           maxiter=maxiter,
                           maxtime=maxtime,
                           maxsample=maxsample,
                           **kwargs)
        return -opt, optx

    if isinstance(model, GaussianProcess):
        return cdirectGP(model,
                         bounds,
                         maxiter,
                         maxtime,
                         maxsample,
                         acqfunc='pi',
                         xi=xi,
                         **kwargs)

    elif isinstance(model, RandomForest):
        return cdirectRF(model,
                         bounds,
                         maxiter,
                         maxtime,
                         maxsample,
                         acqfunc='pi',
                         xi=xi,
                         **kwargs)

    else:
        raise ValueError
Exemplo n.º 17
0
def maximizeUCB(model, bounds, delta=0.1, scale=0.2, useCDIRECT=True, maxiter=50, maxtime=30, maxsample=10000, **kwargs):
    """
    Maximize the upper confidence bound (UCB), as described in 
    [Srinivas 2009a].
    """
    if not useCDIRECT:
        print 'using DIRECT'
        ucb = UCB(model, len(bounds), delta=delta, scale=scale, **kwargs)
        opt, optx = direct(ucb.negf, bounds, **kwargs)
        return -opt, optx
        
    if isinstance(model, GaussianProcess):
        return cdirectGP(model, bounds, maxiter, maxtime, maxsample, acqfunc='ucb', delta=delta, scale=scale, **kwargs)

    elif isinstance(model, RandomForest):
        return cdirectRF(model, bounds, maxiter, maxtime, maxsample, acqfunc='ucb', delta=delta, scale=scale, **kwargs)

    else:
        raise ValueError
Exemplo n.º 18
0
def maximizeUCB(model,
                bounds,
                delta=0.1,
                scale=0.2,
                useCDIRECT=True,
                maxiter=50,
                maxtime=30,
                maxsample=10000,
                **kwargs):
    """
    Maximize the upper confidence bound (UCB), as described in 
    [Srinivas 2009a].
    """
    if not useCDIRECT:
        print('using DIRECT')
        ucb = UCB(model, len(bounds), delta=delta, scale=scale, **kwargs)
        opt, optx = direct(ucb.negf, bounds, **kwargs)
        return -opt, optx

    if isinstance(model, GaussianProcess):
        return cdirectGP(model,
                         bounds,
                         maxiter,
                         maxtime,
                         maxsample,
                         acqfunc='ucb',
                         delta=delta,
                         scale=scale,
                         **kwargs)

    elif isinstance(model, RandomForest):
        return cdirectRF(model,
                         bounds,
                         maxiter,
                         maxtime,
                         maxsample,
                         acqfunc='ucb',
                         delta=delta,
                         scale=scale,
                         **kwargs)

    else:
        raise ValueError
Exemplo n.º 19
0
    def test1DcUCB(self):

        f = lambda x: float(sin(x * 5.))
        X = lhcSample([[0., 1.]], 5, seed=22)
        Y = [f(x) for x in X]

        kernel = GaussianKernel_ard(array([1.0]))
        GP = GaussianProcess(kernel)
        GP.addData(X, Y)

        # should use optimizeGP.cpp
        ucbf = UCB(GP, 1)
        dopt, doptx = direct(ucbf.negf, [[0., 1.]], maxiter=10)
        copt, coptx = cdirect(ucbf.negf, [[0., 1.]], maxiter=10)
        mopt, moptx = maximizeUCB(GP, [[0., 1.]], maxiter=10)

        self.failUnlessAlmostEqual(dopt, copt, 4)
        self.failUnlessAlmostEqual(-dopt, mopt, 4)
        self.failUnlessAlmostEqual(-copt, mopt, 4)

        self.failUnless(sum(abs(doptx - coptx)) < .01)
        self.failUnless(sum(abs(moptx - coptx)) < .01)
        self.failUnless(sum(abs(moptx - doptx)) < .01)
Exemplo n.º 20
0
    def test1DcUCB(self):
        
        f = lambda x: float(sin(x*5.))
        X = lhcSample([[0., 1.]], 5, seed=22)
        Y = [f(x) for x in X]

        kernel = GaussianKernel_ard(array([1.0]))
        GP = GaussianProcess(kernel)
        GP.addData(X, Y)
        
        # should use optimizeGP.cpp
        ucbf = UCB(GP, 1)
        dopt, doptx = direct(ucbf.negf, [[0., 1.]], maxiter=10)
        copt, coptx = cdirect(ucbf.negf, [[0., 1.]], maxiter=10)
        mopt, moptx = maximizeUCB(GP, [[0., 1.]], maxiter=10)
        
        self.failUnlessAlmostEqual(dopt, copt, 4)
        self.failUnlessAlmostEqual(-dopt, mopt, 4)
        self.failUnlessAlmostEqual(-copt, mopt, 4)
    
        self.failUnless(sum(abs(doptx-coptx)) < .01)
        self.failUnless(sum(abs(moptx-coptx)) < .01)
        self.failUnless(sum(abs(moptx-doptx)) < .01)
Exemplo n.º 21
0
def maximizeEI(model, bounds, useCDIRECT=True, xi=0.01, maxiter=50, maxtime=30, maxsample=10000, **kwargs):
    """
    Try to maximize EI in the most efficient way possible.  The methods
    will be attempted in this order:
    
        1.  If there is a C implementation of the EI objective for the
            kernel, it will be used (currently only works for Gaussian
            kernel with ARD).
            
        2.  If the C implementation of DIRECT is available, it will be used.
        
        3.  Otherwise, the Python implementation of DIRECT will be used.
    """
    if not useCDIRECT:
        print "using DIRECT"
        ei = EI(model, xi, **kwargs)
        opt, optx = direct(ei.negf, bounds, maxiter=maxiter, maxtime=maxtime, maxsample=maxsample, **kwargs)
        return -opt, optx

    if isinstance(model, GaussianProcess):
        return cdirectGP(model, bounds, maxiter, maxtime, maxsample, acqfunc="ei", xi=xi, **kwargs)

    elif isinstance(model, RandomForest):
        return cdirectRF(model, bounds, maxiter, maxtime, maxsample, acqfunc="ei", xi=xi, **kwargs)
Exemplo n.º 22
0
def cdirectGP(model,
              bounds,
              maxiter,
              maxtime,
              maxsample,
              acqfunc=None,
              xi=-1,
              beta=-1,
              scale=-1,
              delta=-1,
              **kwargs):
    try:
        if acqfunc == 'ei':
            acquisition = 0
            parm = xi
        elif acqfunc == 'pi':
            acquisition = 1
            parm = xi
        elif acqfunc == 'ucb':
            acquisition = 2
            t = len(model.Y) + 1
            NA = len(bounds)
            parm = sqrt(scale * 2.0 * log(t**(NA / 2 + 2) * pi**2 /
                                          (3.0 * delta)))
        else:
            raise NotImplementedError('unknown acquisition function %s' %
                                      acqfunc)

        if isinstance(model.kernel, GaussianKernel_ard):
            kerneltype = 0
        elif isinstance(model.kernel, GaussianKernel_iso):
            kerneltype = 1
        elif isinstance(model.kernel, MaternKernel3):
            kerneltype = 2
        elif isinstance(model.kernel, MaternKernel5):
            print('Matern 5')
            kerneltype = 3
        else:
            raise NotImplementedError('kernel not implemented in C++: %s' %
                                      model.kernel.__class__)

        lpath = ctypes.util.find_library('ego')
        while lpath is None:
            for lp in [
                    'cpp/libs/libego.dylib',
                    '/Users/eric/Dropbox/EGOcode/ego/libs/libego.so'
            ]:
                if os.path.exists(lp):
                    lpath = lp
        if lpath is None:
            print(
                '\n[python] could not find ego library!  Did you forget to export DYLD_LIBRARY_PATH?'
            )
        lib = cdll[lpath]
        lib.acqmaxGP.restype = POINTER(c_double)
        lib.acqmaxGP.argtypes = [
            c_int,
            POINTER(c_double),
            POINTER(c_double),
            POINTER(c_double),
            POINTER(c_double),
            POINTER(c_double), c_int, c_int, c_int,
            POINTER(c_double), c_int,
            POINTER(c_double),
            POINTER(c_double), c_double,
            POINTER(c_double),
            POINTER(c_double), c_double, c_double, c_int, c_int, c_int
        ]
        NX = len(model.Y)
        NA = len(bounds)
        npbases = 0 if model.prior is None else len(model.prior.means)
        pbtheta = 0 if model.prior is None else model.prior.theta
        if model.prior is None:
            c_pmeans = array([0], dtype=c_double)
            c_pbeta = array([0], dtype=c_double)
            c_pblowerb = array([0], dtype=c_double)
            c_pbwidth = array([0], dtype=c_double)
        else:
            c_pmeans = array(array(model.prior.means).reshape(-1),
                             dtype=c_double)
            c_pbeta = array(model.prior.beta, dtype=c_double)
            c_pblowerb = array(model.prior.lowerb, dtype=c_double)
            c_pbwidth = array(model.prior.width, dtype=c_double)

        c_lower = array([b[0] for b in bounds], dtype=c_double)
        c_upper = array([b[1] for b in bounds], dtype=c_double)
        c_hyper = array(model.kernel.hyperparams, dtype=c_double)

        # TODO: use cholesky on the C++ side
        if isinstance(model, PrefGaussianProcess) and model.C is not None:
            c_invR = array(linalg.inv(model.R +
                                      linalg.inv(model.C)).reshape(-1),
                           dtype=c_double)
        else:
            c_invR = array(linalg.inv(model.R).reshape(-1), dtype=c_double)

        c_X = array(array(model.X).reshape(-1), dtype=c_double)
        c_Y = array(model.Y, dtype=c_double)

        # print c_int(NA)
        # print c_lower.ctypes.data_as(POINTER(c_double))
        # print c_upper.ctypes.data_as(POINTER(c_double))
        # print c_invR.ctypes.data_as(POINTER(c_double))
        # print c_X.ctypes.data_as(POINTER(c_double))
        # print c_Y.ctypes.data_as(POINTER(c_double))
        # print c_int(NX)
        # print c_int(acqfunc)
        # print c_int(kerneltype)
        # print c_hyper.ctypes.data_as(POINTER(c_double))
        # print c_int(npbases)
        # print c_pmeans.ctypes.data_as(POINTER(c_double))
        # print c_pbeta.ctypes.data_as(POINTER(c_double))
        # print c_double(pbtheta)
        # print c_pblowerb.ctypes.data_as(POINTER(c_double))
        # print c_pbwidth.ctypes.data_as(POINTER(c_double))
        # print c_double(xi)
        # print c_double(model.noise)
        # print c_int(maxiter)
        # print c_int(maxtime)
        # print c_int(maxsample)

        # print '[python] calling C++ %s (%d) with X.shape = %s' % (acqfunc, acquisition, model.X.shape)
        result = lib.acqmaxGP(c_int(NA),
                              c_lower.ctypes.data_as(POINTER(c_double)),
                              c_upper.ctypes.data_as(POINTER(c_double)),
                              c_invR.ctypes.data_as(POINTER(c_double)),
                              c_X.ctypes.data_as(POINTER(c_double)),
                              c_Y.ctypes.data_as(POINTER(c_double)), c_int(NX),
                              c_int(acquisition), c_int(kerneltype),
                              c_hyper.ctypes.data_as(POINTER(c_double)),
                              c_int(npbases),
                              c_pmeans.ctypes.data_as(POINTER(c_double)),
                              c_pbeta.ctypes.data_as(POINTER(c_double)),
                              c_double(pbtheta),
                              c_pblowerb.ctypes.data_as(POINTER(c_double)),
                              c_pbwidth.ctypes.data_as(POINTER(c_double)),
                              c_double(parm), c_double(model.noise),
                              c_int(maxiter), c_int(maxtime), c_int(maxsample))
        # print '[python] result =', result.__class__
        # print '[python] result =', result[0]

        opt = -result[0]
        optx = array([x for x in result[1:NA + 1]])

        # free the pointer
        libc = CDLL(ctypes.util.find_library('libc'))
        libc.free.argtypes = [c_void_p]
        libc.free.restype = None
        libc.free(result)

    except:
        try:
            print(
                '[python] C++ MaxEI implementation unavailable, attempting C++ DIRECT on Python objective function.'
            )
            opt, optx = cdirect(ei.negf,
                                bounds,
                                maxiter=maxiter,
                                maxtime=maxtime,
                                maxsample=maxsample,
                                **kwargs)
        except:
            # couldn't access cDIRECT, use Python DIRECT
            print('[python] C++ DIRECT unavailable, attempting Python DIRECT')
            opt, optx = direct(ei.negf,
                               bounds,
                               maxiter=maxiter,
                               maxtime=maxtime,
                               maxsample=maxsample,
                               **kwargs)
        opt = -opt

    if False:
        # do a few random searches to see if we can get a better result.
        # mostly necessary for 1D or 2D optimizations which terminate too
        # soon.
        ei = EI(model)
        for s in lhcSample(bounds, 500):
            if -ei.negf(s) > opt:
                opt = -ei.negf(s)
                optx = s
    return opt, optx
Exemplo n.º 23
0
def cdirectGP(model, bounds, maxiter, maxtime, maxsample, acqfunc=None, xi=-1, beta=-1, scale=-1, delta=-1, **kwargs):
    try:
        if acqfunc == "ei":
            acquisition = 0
            parm = xi
        elif acqfunc == "pi":
            acquisition = 1
            parm = xi
        elif acqfunc == "ucb":
            acquisition = 2
            t = len(model.Y) + 1
            NA = len(bounds)
            parm = sqrt(scale * 2.0 * log(t ** (NA / 2 + 2) * pi ** 2 / (3.0 * delta)))
        else:
            raise NotImplementedError("unknown acquisition function %s" % acqfunc)

        if isinstance(model.kernel, GaussianKernel_ard):
            kerneltype = 0
        elif isinstance(model.kernel, GaussianKernel_iso):
            kerneltype = 1
        elif isinstance(model.kernel, MaternKernel3):
            kerneltype = 2
        elif isinstance(model.kernel, MaternKernel5):
            print "Matern 5"
            kerneltype = 3
        else:
            raise NotImplementedError("kernel not implemented in C++: %s" % model.kernel.__class__)

        lpath = ctypes.util.find_library("ego")
        if lpath is None:
            import ASAW_config

            lpath = ASAW_config.get_ego_lib_dir() + "libego.so"

            # print '\n[python] could not find ego library!  Did you forget to export DYLD_LIBRARY_PATH?'
        lib = cdll[lpath]
        lib.acqmaxGP.restype = POINTER(c_double)
        lib.acqmaxGP.argtypes = [
            c_int,
            POINTER(c_double),
            POINTER(c_double),
            POINTER(c_double),
            POINTER(c_double),
            POINTER(c_double),
            c_int,
            c_int,
            c_int,
            POINTER(c_double),
            c_int,
            POINTER(c_double),
            POINTER(c_double),
            c_double,
            POINTER(c_double),
            POINTER(c_double),
            c_double,
            c_double,
            c_int,
            c_int,
            c_int,
        ]
        NX = len(model.Y)
        NA = len(bounds)
        npbases = 0 if model.prior is None else len(model.prior.means)
        pbtheta = 0 if model.prior is None else model.prior.theta
        if model.prior is None:
            c_pmeans = array([0], dtype=c_double)
            c_pbeta = array([0], dtype=c_double)
            c_pblowerb = array([0], dtype=c_double)
            c_pbwidth = array([0], dtype=c_double)
        else:
            c_pmeans = array(array(model.prior.means).reshape(-1), dtype=c_double)
            c_pbeta = array(model.prior.beta, dtype=c_double)
            c_pblowerb = array(model.prior.lowerb, dtype=c_double)
            c_pbwidth = array(model.prior.width, dtype=c_double)

        c_lower = array([b[0] for b in bounds], dtype=c_double)
        c_upper = array([b[1] for b in bounds], dtype=c_double)
        c_hyper = array(model.kernel.hyperparams, dtype=c_double)

        # TODO: use cholesky on the C++ side
        if isinstance(model, PrefGaussianProcess) and model.C is not None:
            c_invR = array(linalg.inv(model.R + linalg.inv(model.C)).reshape(-1), dtype=c_double)
        else:
            c_invR = array(linalg.inv(model.R).reshape(-1), dtype=c_double)

        c_X = array(array(model.X).reshape(-1), dtype=c_double)
        c_Y = array(model.Y, dtype=c_double)

        # print c_int(NA)
        # print c_lower.ctypes.data_as(POINTER(c_double))
        # print c_upper.ctypes.data_as(POINTER(c_double))
        # print c_invR.ctypes.data_as(POINTER(c_double))
        # print c_X.ctypes.data_as(POINTER(c_double))
        # print c_Y.ctypes.data_as(POINTER(c_double))
        # print c_int(NX)
        # print c_int(acqfunc)
        # print c_int(kerneltype)
        # print c_hyper.ctypes.data_as(POINTER(c_double))
        # print c_int(npbases)
        # print c_pmeans.ctypes.data_as(POINTER(c_double))
        # print c_pbeta.ctypes.data_as(POINTER(c_double))
        # print c_double(pbtheta)
        # print c_pblowerb.ctypes.data_as(POINTER(c_double))
        # print c_pbwidth.ctypes.data_as(POINTER(c_double))
        # print c_double(xi)
        # print c_double(model.noise)
        # print c_int(maxiter)
        # print c_int(maxtime)
        # print c_int(maxsample)

        # print '[python] calling C++ %s (%d) with X.shape = %s' % (acqfunc, acquisition, model.X.shape)
        result = lib.acqmaxGP(
            c_int(NA),
            c_lower.ctypes.data_as(POINTER(c_double)),
            c_upper.ctypes.data_as(POINTER(c_double)),
            c_invR.ctypes.data_as(POINTER(c_double)),
            c_X.ctypes.data_as(POINTER(c_double)),
            c_Y.ctypes.data_as(POINTER(c_double)),
            c_int(NX),
            c_int(acquisition),
            c_int(kerneltype),
            c_hyper.ctypes.data_as(POINTER(c_double)),
            c_int(npbases),
            c_pmeans.ctypes.data_as(POINTER(c_double)),
            c_pbeta.ctypes.data_as(POINTER(c_double)),
            c_double(pbtheta),
            c_pblowerb.ctypes.data_as(POINTER(c_double)),
            c_pbwidth.ctypes.data_as(POINTER(c_double)),
            c_double(parm),
            c_double(model.noise),
            c_int(maxiter),
            c_int(maxtime),
            c_int(maxsample),
        )
        # print '[python] result =', result.__class__
        # print '[python] result =', result[0]

        opt = -result[0]
        optx = array([x for x in result[1 : NA + 1]])

        # free the pointer
        libc = CDLL(ctypes.util.find_library("libc"))
        libc.free.argtypes = [c_void_p]
        libc.free.restype = None
        libc.free(result)

    except:

        try:
            print "[python] C++ MaxEI implementation unavailable, attempting C++ DIRECT on Python objective function."
            opt, optx = cdirect(ei.negf, bounds, maxiter=maxiter, maxtime=maxtime, maxsample=maxsample, **kwargs)
        except:
            # couldn't access cDIRECT, use Python DIRECT
            print "[python] C++ DIRECT unavailable, attempting Python DIRECT"
            opt, optx = direct(ei.negf, bounds, maxiter=maxiter, maxtime=maxtime, maxsample=maxsample, **kwargs)
        opt = -opt

    if False:
        # do a few random searches to see if we can get a better result.
        # mostly necessary for 1D or 2D optimizations which terminate too
        # soon.
        ei = EI(model)
        for s in lhcSample(bounds, 500):
            if -ei.negf(s) > opt:
                opt = -ei.negf(s)
                optx = s
    return opt, optx