Esempio n. 1
0
def load_sard_sampler_lib():
    import ASAW_config
    
    global libsardsampler
    if libsardsampler == None:
        #libsardsampler = c.cdll.LoadLibrary("/home/zwang/project/AdaptSAW/python/libsardsampler_ctypes.so")
        libsardsampler = c.cdll.LoadLibrary(ASAW_config.get_lib_dir() + "libsardsampler_ctypes.so")
Esempio n. 2
0
def load_im_sampler_lib():
    global libimsampler
    if libimsampler == None:
	import ASAW_config
        libimsampler = \
	    c.cdll.LoadLibrary(ASAW_config.get_lib_dir() + "libimsampler_ctypes.so")
Esempio n. 3
0
	test = isg.ISG3D_UAI(9, 9, 9)
    elif test_name == 'rbm':
	print 'Running RBM_UAI for {0} Iterations.'.format(num_iter)
	test = isg.RBM_UAI()
    elif test_name == 'ferrof':
	print 'Running Ferro2D_Frustrated for {0} Iterations.'.format(num_iter)
	test = isg.Ferro2D_Frustrated(60, 60)
    elif test_name == 'chi':
	print "Running Chimera."
	test = isg.test = isg.Chimera(4,4,4)
    
    temp = Ising_Gibbs(test)
    
    import time
    
    time_1 = time.time()
    temp.gibbs_random(num_iter)
    time_2 = time.time()
    print "Sampling lasted: ", time_2 - time_1
    
    print 'Saving results in ' + './Regular_Gibbs_' + test_name + '_' + str(num_iter) + '.npy'
    
    cur_res_num = ASAW_config.get_and_inc_cur_res_num('path_config.cfg')
    f = open('Regular_Gibbs_' + test_name + '_' + str(num_iter) + '.npy', 'w')
    save(f, temp.energies[1:])
    f.close()
    
    temp.compute_acf(max_lag = min(num_iter-1, 500), subsample = max(num_iter/100000, 1))
    
    temp.plots()
Esempio n. 4
0
        num_iter = int(sys.argv[1])
    elif len(sys.argv) > 1:
        num_iter = int(sys.argv[1])

    if batch:
        rbm = isg.RBM_UAI_BLOCK()
        v = zeros(784)

        total_energy = []

        for i in xrange(5):
            v[0:428] = ones(428)
            bbs = Block_RBM_Sampler(rbm, v, None)
            total_energy.append(bbs.iterate(num_iter))

        cur_res_num = ASAW_config.get_and_inc_cur_res_num("path_config.cfg")
        bbs.acf_plot_multi_energies(total_energy, res_num=cur_res_num)
        f = open("Block_RBM_" + str(cur_res_num) + ".npy", "w")
        save(f, total_energy)

        f.close()
    else:
        rbm = isg.RBM_UAI_BLOCK()
        v = zeros(784)
        total_energy = []

        v[0:428] = ones(428)
        bbs = Block_RBM_Sampler(rbm, v, None)
        import time

        time_1 = time.time()
Esempio n. 5
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
Esempio n. 6
0
import ConfigParser
import ASAW_config

config = ConfigParser.RawConfigParser()

config.add_section('Section1')
config.set('Section1', 'lib_dir', ASAW_config.get_cur_path()+'python/')

with open('path_config.cfg', 'wb') as configfile:
    config.write(configfile)