コード例 #1
0
ファイル: test_funcs.py プロジェクト: rongrong1314/gpsearch-1
    def __init__(self,
                 function,
                 domain,
                 ymin,
                 xmin,
                 args={},
                 kwargs={},
                 noise_var=0.0,
                 rescale_X=False):
        self.ymin = ymin
        self.bounds = domain
        self.function = function

        if rescale_X:
            self.xmin = self.rescale_X(xmin)
            domain = [[0, 1]] * len(domain)
            self.my_map = BlackBox(self.function_sc,
                                   args=args,
                                   kwargs=kwargs,
                                   noise_var=noise_var)
        else:
            self.xmin = xmin
            self.my_map = BlackBox(self.function,
                                   args=args,
                                   kwargs=kwargs,
                                   noise_var=noise_var)

        self.inputs = UniformInputs(domain)
コード例 #2
0
def run(casename):

    fh = Dataset("../data/" + casename + ".nc", mode="r")
    lons = fh.variables["lon"][:]
    lats = fh.variables["lat"][:]
    depths = fh.variables["elevation"][:]

    # Rescale data and flip depths to have anomalies as minima

    lons = MinMaxScaler().fit_transform(lons.reshape(-1, 1))
    lats = MinMaxScaler().fit_transform(lats.reshape(-1, 1))
    tmp = StandardScaler().fit_transform(depths.reshape(-1, 1))
    depths = -tmp.reshape(depths.shape)
    if casename in ["cuba", "challenger", "izu", "izu2", "izu3"]:
        depths = -depths

    domain = [[0, 1], [0, 1]]
    inputs = UniformInputs(domain)
    inputs = GaussianInputs(domain, [0.5, 0.5], [0.01, 0.01])
    noise_var = 0.0

    interpolant = interpolate.interp2d(lons, lats, depths, kind="cubic")
    my_map = BlackBox(map_def, args=(interpolant, ), noise_var=noise_var)
    idx_max = np.unravel_index(depths.argmin(), depths.shape)
    true_xmin = [lons[idx_max[1]], lats[idx_max[0]]]
    true_ymin = np.min(depths)

    record_time = np.linspace(0, 15, 226)
    n_trials = 50
    n_jobs = 40

    pts = inputs.draw_samples(n_samples=int(1e5), sample_method="uni")
    yy = my_map.evaluate(pts, parallel=True, include_noise=False)
    y_list = [yy] * len(record_time)
    pt_list = [custom_KDE(yy, weights=inputs.pdf(pts))] * len(record_time)
    true_ymin_list = [true_ymin] * len(record_time)
    true_xmin_list = [true_xmin] * len(record_time)

    metric = [(rmse, dict(pts=pts, y_list=y_list, t_list=record_time)),
              (mll, dict(pts=pts, y_list=y_list, t_list=record_time)),
              (log_pdf, dict(pts=pts, pt_list=pt_list, t_list=record_time)),
              (distmin_model,
               dict(true_xmin_list=true_xmin_list, t_list=record_time)),
              (regret_tmap,
               dict(true_ymin_list=true_ymin_list,
                    tmap=my_map,
                    t_list=record_time))]

    X_pose = (0, 0, np.pi / 4)
    planner = PathPlanner(inputs.domain, look_ahead=0.2, turning_radius=0.02)

    acq_list = ["US_IW", "US_LW", "IVR_IW", "IVR_LW"]

    for acq in acq_list:
        print("Benchmarking " + acq)
        b = BenchmarkerPath(my_map, acq, planner, X_pose, record_time, inputs,
                            metric)
        result = b.run_benchmark(n_trials,
                                 n_jobs=n_jobs,
                                 filename=casename + "_" + acq)
コード例 #3
0
def main():

    ndim = 2
    np.random.seed(3)

    tf = 25
    nsteps = 1000
    u_init = [0, 0]
    noise = Noise([0, tf])
    oscil = Oscillator(noise, tf, nsteps, u_init)
    my_map = BlackBox(map_def, args=(oscil, ))

    n_init = 4
    n_iter = 80

    mean, cov = np.zeros(ndim), np.ones(ndim)
    domain = [[-6, 6]] * ndim
    inputs = GaussianInputs(domain, mean, cov)
    X = inputs.draw_samples(n_init, "lhs")
    Y = my_map.evaluate(X)

    o = OptimalDesign(X,
                      Y,
                      my_map,
                      inputs,
                      fix_noise=True,
                      noise_var=0.0,
                      normalize_Y=True)
    m_list = o.optimize(n_iter,
                        acquisition="US",
                        num_restarts=10,
                        parallel_restarts=True)

    # Compute true pdf
    filename = "map_samples{:d}D.txt".format(ndim)
    try:
        smpl = np.genfromtxt(filename)
        pts = smpl[:, 0:-1]
        yy = smpl[:, -1]
    except:
        pts = inputs.draw_samples(n_samples=100, sample_method="grd")
        yy = my_map.evaluate(pts, parallel=True, include_noise=False)
        np.savetxt(filename, np.column_stack((pts, yy)))
    pdf = custom_KDE(yy, weights=inputs.pdf(pts))

    for ii in np.arange(0, n_iter + 1, 10):
        pb, pp, pm = model_pdf(m_list[ii], inputs, pts=pts)
        plot_pdf(pdf,
                 pb, [pm, pp],
                 filename="pdfs%.4d.pdf" % (ii),
                 xticks=[-3, 0, 3],
                 yticks=[-8, -3, 2])
        plot_smp(m_list[ii],
                 inputs,
                 n_init,
                 filename="smps%.4d.pdf" % (ii),
                 xticks=[-6, 0, 6],
                 yticks=[-5, 0, 5],
                 cmapticks=[-2, -1, 0, 1, 2])
コード例 #4
0
def test_blackbox_shape_2D():
    n_init = 10
    domain = [[-1., 2.], [-1., 2.]]
    inputs = UniformInputs(domain)
    X = inputs.draw_samples(n_init, "lhs")
    Ys = BlackBox(map_def2D).evaluate(X)
    Yp = BlackBox(map_def2D).evaluate(X, parallel=True)
    np.testing.assert_allclose(Ys, Yp)
コード例 #5
0
def main():

    ndim = 2
    tf = 25
    nsteps = 1000
    u_init = [0, 0]
    noise = Noise([0, tf])
    oscil = Oscillator(noise, tf, nsteps, u_init)

    mean, cov = np.zeros(ndim), np.ones(ndim)
    domain = [[-6, 6]] * ndim
    inputs = GaussianInputs(domain, mean, cov)

    prefix = "oscill_"
    noise_var = 1e-3
    my_map = BlackBox(map_def, args=(oscil, ), noise_var=noise_var)

    n_init = 3
    n_iter = 80
    n_trials = 100
    n_jobs = 30

    # Compute true pdf
    filename = "map_samples{:d}D.txt".format(ndim)
    try:
        smpl = np.genfromtxt(filename)
        pts = smpl[:, 0:-1]
        yy = smpl[:, -1]
    except:
        pts = inputs.draw_samples(n_samples=100, sample_method="grd")
        yy = my_map.evaluate(pts, parallel=True, include_noise=False)
        np.savetxt(filename, np.column_stack((pts, yy)))
    weights = inputs.pdf(pts)
    pt = custom_KDE(yy, weights=weights)

    metric = [("log_pdf", dict(pt=pt, pts=pts))]
    acq_list = ["US", "US_LW", "IVR_IW", "IVR_LW"]

    for acq in acq_list:
        print("Benchmarking " + acq)
        b = Benchmarker(my_map, acq, n_init, n_iter, inputs, metric)
        result = b.run_benchmark(n_trials, n_jobs, filename=prefix + acq)

    b = BenchmarkerLHS(my_map, n_init, n_iter, inputs, metric)
    result = b.run_benchmark(n_trials, n_jobs, filename=prefix + "LHS")
コード例 #6
0
def main():

    ndim = 3
    np.random.seed(2)

    tf = 4000
    dt = 0.01
    nsteps = int(tf/dt)
    u_init = [0, 0.01, 0.01]
    pre = PRE(tf, nsteps, u_init)
    u, t = pre.solve()
    plot_observable(t, u[:,2], ylabel="z",
                    xticks=[0,2000,4000], yticks=[-0.5,0,0.5,1])
    plot_trajectory(t, u)

    # Do PCA
    iostep = 100
    tsnap = t[::iostep]
    usnap = u[::iostep]
    lam, psi, usnap_mean = comp_pca(usnap, ndim)
    print("PCA Eigenvalues:", lam)

    n_init = 3
    n_iter = 50

    mean = np.zeros(ndim)
    cov = np.diag(lam)
    a_bnds = 4.0
    domain = [ [-a, a] for a in a_bnds*np.sqrt(np.diag(cov)) ]
    inputs = GaussianInputs(domain, mean, cov)
    my_map = BlackBox(map_def, args=(psi,usnap_mean))

    X = inputs.draw_samples(n_init, "lhs")
    Y = my_map.evaluate(X)

    o = OptimalDesign(X, Y, my_map, inputs,
                      fix_noise=True,
                      noise_var=0.0,
                      normalize_Y=True)

    m_list = o.optimize(n_iter,
                        acquisition="EI",
                        num_restarts=10,
                        parallel_restarts=True)
コード例 #7
0
ファイル: run_benchmark.py プロジェクト: ablancha/gpsearch
def main():

    ndim = 8 
    noise_var = 1e-2 
    my_map = BlackBox(map_def, noise_var=noise_var)
    inputs = CustomInputs([ [0,1] ] * ndim)
    
    np.random.seed(3)

    filename = "map_samples{:d}D.txt".format(ndim)
    try:
        smpl = np.genfromtxt(filename)
        pts = smpl[:,0:-1]
        yy = smpl[:,-1]
    except:
        pts = inputs.draw_samples(n_samples=int(1e6), sample_method="uni")
        yy = my_map.evaluate(pts, parallel=True, include_noise=False)
        np.savetxt(filename, np.column_stack((pts,yy)))
    weights = inputs.pdf(pts)
    pt = custom_KDE(yy, weights=weights)

    prefix = "gmm2_noise2"

    n_init = ndim + 1
    n_iter = 160
    n_trials = 100
    n_jobs = 15

    metric = [ ("log_pdf", dict(pt=pt, pts=pts)) ]
    acq_list = ["US", "US_LW", 
                "IVR", "IVR_IW", "IVR_LW"]

    for acq in acq_list:
        print("Benchmarking " + acq)
        b = Benchmarker(my_map, acq, n_init, n_iter, inputs, metric)
        result = b.run_benchmark(n_trials, n_jobs, filename=prefix+acq)

    b = BenchmarkerLHS(my_map, n_init, n_iter, inputs, metric)
    result = b.run_benchmark(n_trials, n_jobs, filename=prefix+"LHS")
コード例 #8
0
def main():

    # Generate data for PCA
    tf = 2000
    dt = 0.01
    nsteps = int(tf / dt)
    u_init = [0, 0.01, 0.01]
    pre = PRE(tf, nsteps, u_init)
    u, t = pre.solve()

    # Do PCA
    ndim = 2
    iostep = 100
    tsnap = t[::iostep]
    usnap = u[::iostep]
    lam, psi, usnap_mean = comp_pca(usnap, ndim)
    print("PCA Eigenvalues:", lam)

    # Set up black box
    mean = np.zeros(ndim)
    cov = np.diag(lam)
    a_bnds = 4.0
    domain = [[-a, a] for a in a_bnds * np.sqrt(np.diag(cov))]
    inputs = GaussianInputs(domain, mean, cov)

    noise_var = 1e-3
    my_map = BlackBox(map_def, args=(psi, usnap_mean), noise_var=noise_var)

    # Do Benchmark
    prefix = "prec2d_"

    n_init = 3
    n_iter = 100
    n_trials = 100
    n_jobs = 40

    metric = [("regret_tmap", dict(tmap=my_map, true_ymin=0.0)),
              ("regret_obs", dict(true_ymin=0.0))]

    acq_list = [
        "EI", "PI", "LCB", "LCB_LW", "IVR", "IVR_BO", "IVR_LW", "IVR_LWBO"
    ]

    for acq in acq_list:
        print("Benchmarking " + acq)
        b = Benchmarker(my_map, acq, n_init, n_iter, inputs, metric)
        result = b.run_benchmark(n_trials, n_jobs, filename=prefix + acq)
コード例 #9
0
def main():

    np.random.seed(2)
    n_init = 10

    domain = [[-1., 2.]]
    inputs = UniformInputs(domain)
    X = inputs.draw_samples(n_init, "lhs")
    Ys = BlackBox(map_def1D).evaluate(X)
    Yp = BlackBox(map_def1D).evaluate(X, parallel=True)
    OptimalDesign(X, Ys, BlackBox(map_def1D), inputs)
    OptimalDesign(X, Yp, BlackBox(map_def1D), inputs)
    print(X, Ys, Yp)

    domain = [[-1., 2.], [-1., 2.]]
    inputs = UniformInputs(domain)
    X = inputs.draw_samples(n_init, "lhs")
    Ys = BlackBox(map_def2D).evaluate(X)
    Yp = BlackBox(map_def2D).evaluate(X, parallel=True)
    OptimalDesign(X, Ys, BlackBox(map_def2D), inputs)
    OptimalDesign(X, Yp, BlackBox(map_def2D), inputs)
    print(X, Ys, Yp)
コード例 #10
0
ファイル: run_benchmark.py プロジェクト: ablancha/gpsearch
def main():

    ndim = 10

    x2, y2 = 1, 0.3
    x = np.linspace(0, x2, ndim + 2)
    x_pts = x[1:-1]

    xc, yc = cycloid(x2, y2, N=200)
    tc = travel_time(xc, yc)  # Best ever
    ff = interpolate.interp1d(xc, yc)

    noise_var = 1e-2
    my_map = BlackBox(map_def, args=(x, y2, tc), noise_var=noise_var)
    domain = [[0.05, 2 * y2] for n in np.arange(0, ndim)]
    inputs = UniformInputs(domain)

    prefix = "tclog_noise2_"

    n_init = 10
    n_iter = 300
    n_trials = 40
    n_jobs = 40

    true_ymin = 0.0
    metric = [("regret_tmap", dict(true_ymin=true_ymin, tmap=my_map)),
              ("regret_obs", dict(true_ymin=true_ymin))]

    acq_list = ["EI", "PI", "LCB", "LCB_LW", "LCB_LWraw", "IVR_BO", "IVR_LWBO"]

    for acq in acq_list:
        print("Benchmarking " + acq)
        b = Benchmarker(my_map, acq, n_init, n_iter, inputs, metric=metric)
        result = b.run_benchmark(n_trials,
                                 n_jobs=n_jobs,
                                 filename=prefix + acq)
コード例 #11
0
def main():

    np.random.seed(2)
    casename = "izu3"

    fh = Dataset("../benchmarks/bathymetry/data/" + casename + ".nc", mode="r")
    lons = fh.variables["lon"][:]
    lats = fh.variables["lat"][:]
    depths = fh.variables["elevation"][:]

# Rescale data and flip depths to have anomalies as minima

    lons = MinMaxScaler().fit_transform(lons.reshape(-1, 1))
    lats = MinMaxScaler().fit_transform(lats.reshape(-1, 1))
    tmp = StandardScaler().fit_transform(depths.reshape(-1, 1))
    depths = -tmp.reshape(depths.shape)
    if casename in ["cuba", "challenger", "izu", "izu2"]: depths = -depths

    domain = [ [0, 1], [0, 1] ]
   #inputs = UniformInputs(domain)
    inputs = GaussianInputs(domain, [0.5,0.5], [0.01,0.01])
    noise_var = 0.0

    interpolant = interpolate.interp2d(lons, lats, depths, kind="cubic")
    my_map = BlackBox(map_def, args=(interpolant,), noise_var=noise_var)

# Set up path-planner

    record_time = np.linspace(0,2,31)
   #record_time = np.linspace(0,4,61)
   #record_time = np.linspace(0,10,151)
    my_map.kwargs["time"] = record_time[0]
    X_pose = (0,0,np.pi/4); X = X_pose[0:2]
    Y_pose = my_map.evaluate(X); print(Y_pose); 

    p = PathPlanner(inputs.domain, 
                    look_ahead=0.2, 
                    turning_radius=0.02)

# Run mission

  # o = OptimalPathStatic(X_pose, Y_pose, my_map, inputs)
    o = OptimalPath(X_pose, Y_pose, my_map, inputs)#, static=True)
    m_list, p_list, s_list = o.optimize(record_time,
                                        path_planner=p, 
                                        acquisition="US")

# Plot stuff

    ngrid = 50
    pts = inputs.draw_samples(n_samples=ngrid, sample_method="grd")
    ndim = pts.shape[-1]
    grd = pts.reshape( (ngrid,)*ndim + (ndim,) ).T
    X, Y = grd[0], grd[1]
    yy = my_map.evaluate(pts, include_noise=False)
    ZZt = yy.reshape( (ngrid,)*ndim ).T 

    r_list = [ np.array(p.make_itinerary(path,1000)[0]) for path in p_list ]
    for ii in range(0, len(m_list)):
        model = m_list[ii]
        stamp = model.X[-1,-1]*np.ones(len(pts))
        yy = model.predict( np.hstack((pts, stamp[:,None])) )[0]
        ZZ = yy.reshape( (ngrid,)*ndim ).T

        fig = plt.figure(figsize=(10,4))
        plt.subplot(121)
        plt.contourf(X, Y, ZZ);
        for rr in r_list[0:ii+1]:
            plt.plot(rr[:,0], rr[:,1], 'r-')
        plt.plot(model.X[:,0],model.X[:,1],'ro')
        plt.xlim(0,1); plt.ylim(0,1)
        plt.axis('equal');

        plt.subplot(122)
        plt.contourf(X, Y, ZZt);
        plt.xlim(0,1); plt.ylim(0,1)
        plt.axis('equal');
        plt.show();