Пример #1
0
Optimal X:  [ 3.47754433  0.06314631  0.41764262 -1.96400342 -1.58139261  4.92449457
 -4.71530316 -2.10776775  1.49432157  3.43835685  2.89568113  0.30602795
  4.18443959  6.11357951 -3.97028203]'''
'''Optimal Y:  [-0.40181033]
Optimal X:  [ 4.41825932  0.54252292 -1.37572338 -1.78126762 -2.77853702  1.85727287
 -0.90816901 -0.52089679 -1.14786593  4.64987452 -0.81675974  7.5770911
  6.50357604  2.99004245 -3.51097471]'''
'''[-3.03131596  0.04519549  0.05640378  2.83386375 -2.04397627  5.82513742
  2.94663176 -3.23828459  4.31864194 -3.06726588  3.35944805  1.04605141
  4.87498366 -0.61144085  0.9063866 ]'''

inputDimension = 15
# optimalX = x0 = np.zeros((1, inputDimension))
rbfn = RBFN.RBFN(1, 5, 3, runtime)  # indim, numCenters, outdim, time
rbfn.setHyperParams(optimalX.reshape(5, 3))
timeInterval = safeopt.linearly_spaced_combinations([(0, runtime)],
                                                    runtime * 10)
priorities = np.zeros((3, 0))
for time in timeInterval:
    priorities = np.column_stack((priorities, rbfn.calOutput(time).T))

print(priorities)
plt.figure()
plt.plot(timeInterval, priorities[0], color='r')
plt.plot(timeInterval, priorities[1], color='g')
plt.plot(timeInterval, priorities[2], color='b')
plt.ioff()

displayResult = evaluationInter.evaluationInter(runtime=runtime)
displayResult.updateHyperParams(optimalX)
print(displayResult.evaluation())
Пример #2
0
def test_safeopt_fun():
    mpl.rcParams['figure.figsize'] = (20.0, 10.0)
    mpl.rcParams['font.size'] = 20
    mpl.rcParams['lines.markersize'] = 20

    # Measurement noise
    noise_var = 0.05**2
    noise_var2 = 1e-5
    noise_var3 = 1e-3

    # Bounds on the inputs variable
    bounds = [(-10., 10.)]

    # Define Kernel
    kernel = GPy.kern.RBF(input_dim=len(bounds),
                          variance=2.,
                          lengthscale=1.0,
                          ARD=True)
    kernel2 = kernel.copy()
    kernel3 = kernel.copy()

    # set of parameters
    parameter_set = safeopt.linearly_spaced_combinations(bounds, 1000)

    # Initial safe point
    x0 = np.zeros((1, len(bounds)))

    # Generate function with safe initial point at x=0
    def sample_safe_fun():
        fun = safeopt.sample_gp_function(kernel, bounds, noise_var, 100)
        while True:
            fun2 = safeopt.sample_gp_function(kernel2, bounds, noise_var2, 100)
            if fun2(0, noise=False) > 1:
                break
        while True:
            fun3 = safeopt.sample_gp_function(kernel3, bounds, noise_var3, 100)
            if fun3(0, noise=False) > 1:
                break

        def combined_fun(x, noise=True):
            return np.hstack([fun(x, noise), fun2(x, noise), fun3(x, noise)])

        return combined_fun

    # Define the objective function
    fun = sample_safe_fun()

    # The statistical model of our objective function and safety constraint
    y0 = fun(x0)
    gp = GPy.models.GPRegression(x0,
                                 y0[:, 0, None],
                                 kernel,
                                 noise_var=noise_var)
    gp2 = GPy.models.GPRegression(x0,
                                  y0[:, 1, None],
                                  kernel2,
                                  noise_var=noise_var2)
    gp3 = GPy.models.GPRegression(x0,
                                  y0[:, 2, None],
                                  kernel2,
                                  noise_var=noise_var2)

    # The optimization routine
    # opt = safeopt.SafeOptSwarm([gp, gp2], [-np.inf, 0.], bounds=bounds, threshold=0.2)
    opt = safeopt.SafeOpt([gp, gp2, gp3],
                          parameter_set, [-np.inf, 0., 0.],
                          lipschitz=0.1,
                          threshold=1)

    def plot():
        # Plot the GP
        opt.plot(100)
        # Plot the true function
        y = fun(parameter_set, noise=False)
        for manager, true_y in zip(
                mpl._pylab_helpers.Gcf.get_all_fig_managers(), y.T):
            figure = manager.canvas.figure
            figure.gca().plot(parameter_set, true_y, color='C2', alpha=0.3)

    while opt.t < 25:
        # Obtain next query point
        x_next = opt.optimize()
        # Get a measurement from the real system
        y_meas = fun(x_next)
        # Add this to the GP model
        opt.add_new_data_point(x_next, y_meas)
        if (y_meas[0][1] < 0) or (y_meas[0][2] < 0):
            print('error')
            return 1
            break

    return 0
Пример #3
0
import GPy
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

import safeopt
noise_var = 0.05**2
bounds = [(-10, 10)]
parameter_set = safeopt.linearly_spaced_combinations(bounds, 1000)
kernel = GPy.kern.RBF(input_dim=len(bounds),
                      variance=2,
                      lengthscale=1.0,
                      ARD=True)


def sample_safe_fun():
    while True:
        fun = safeopt.sample_gp_function(kernel, bounds, noise_var, 100)
        if fun(0, noise=False) > 0.5:
            break
    return fun


x0 = np.zeros((1, len(bounds)))
fun = sample_safe_fun()

gp = GPy.models.GPRegression(x0, fun(x0), kernel, noise_var=noise_var)
opt = safeopt.SafeOptSwarm(gp, 0., bounds=bounds, threshold=0.2)


def plot_gp():
Пример #4
0
import GPy
import numpy as np
import pandas as pd


noise_var = 0.05 ** 2

# Set fixed Gaussian measurement noise
likelihood = GPy.likelihoods.gaussian.Gaussian(variance=noise_var)
likelihood.constrain_fixed(warning=False)

# Bounds on the inputs variable
bounds = [(-5.0, 5.0), (-5.0, 5.0)]

# set of parameters
parameter_set = safeopt.linearly_spaced_combinations(bounds, 1000)

# Define Kernel
kernel = GPy.kern.RBF(input_dim=len(bounds), variance=2.0, lengthscale=1.0, ARD=True)

# Initial safe point
x0 = np.zeros((1, len(bounds)))

# Generate function with safe initial point at x=0
def sample_safe_fun():
    while True:
        fun = safeopt.sample_gp_function(kernel, bounds, noise_var, 10)
        if fun([0, 0], noise=False) > 0.5:
            break
    return fun