示例#1
0
def run(run_index,
        model,
        folder="data_dumps",
        hp_opt="IS0",
        sample_domain=1000):
    '''
    This function will run CO optimization using one of several coregionalization methods.

        1. Pearson R Intrinsic Coregionalization Method (PRICM).  This approach
           will dynamically calculate the Pearson R value for the off-diagonals
           in the ICM.  Diagonals are kept as 1.0.
        2. Intrinsic Coregionalization Method (ICM).  This approach will use a
           lower triangular matrix (L) of hyperparameters to generate the
           coregionalization matrix B = LL^T.

    Further, we can parameterize the hyperparameters in many ways:

        1. IS0 - Only parameterize hyperparameters using values sampled at IS0.
        2. Full - Parameterize hyperparameters using all sampled data.
        3. Overlap - Parameterize hyperparameters using data that overlaps all IS.

    **Parameters**

        run_index: *int*
            This is simply used for a naming convention.
        model: *str*
            The model to be used (PRICM or ICM).
        folder: *str, optional*
            What to name the folder where the data will go.
        hp_opt: *str, optional*
            With what data should the hyperparameters be parameterized.
            Options: IS0, full, overlap
        sample_domain: *int, optional*
            How many data points to sample from the domain.
    '''

    hp_opt = hp_opt.lower()
    allowed_hp_opt = ["is0", "full", "overlap"]
    assert hp_opt in allowed_hp_opt, "Error, hp_opt (%s) not in %s" % (
        hp_opt, ", ".join(allowed_hp_opt))

    # Generate the main object
    sim = Optimizer()

    # Assign simulation properties
    sim.hyperparameter_objective = MLE
    ###################################################################################################
    # File names
    sim.fname_out = None
    sim.fname_historical = None

    sim.logger_fname = "%s/%d_%s_%s.log" % (folder, run_index, model, hp_opt)
    if os.path.exists(sim.logger_fname):
        os.system("rm %s" % sim.logger_fname)
    os.system("touch %s" % sim.logger_fname)
    sim.obj_vs_cost_fname = None
    sim.mu_fname = None
    sim.sig_fname = None
    sim.combos_fname = None
    sim.hp_fname = None
    sim.acquisition_fname = None
    sim.save_extra_files = True

    # Information sources, in order from expensive to cheap
    rosenbrock = lambda x1, x2: (1.0 - x1)**2 + 100.0 * (x2 - x1**2)**2 - 456.3
    sim.IS = [
        lambda x1, x2: -1.0 * rosenbrock(x1, x2), lambda x1, x2: -1.0 *
        (rosenbrock(x1, x2) + 10.0 * np.sin(10.0 * x1 + 5.0 * x2))
    ]
    sim.costs = [1000.0, 1.0]

    sim.save_extra_files = False
    ########################################
    sim.numerical = True
    sim.historical_nsample = 5
    sim.domain = [(-2.0, 2.0), (-2.0, 2.0)]
    sim.sample_n_from_domain = sample_domain
    ########################################

    sim.n_start = 10  # The number of starting MLE samples
    # sim.reopt = 20
    sim.reopt = float('inf')  # Never re-opt hyperparams
    sim.ramp_opt = None
    sim.parallel = False

    # Parameters for debugging and overwritting
    sim.debug = False
    sim.verbose = True
    sim.overwrite = True  # If True, warning, else Error

    sim.acquisition = getNextSample_misokg

    # Functional forms of our mean and covariance
    sim.mean = lambda X, Y, theta: np.array([-456.3 for _ in Y])

    def cov_miso(X0, Y, theta, split=False):
        Kx = squared(np.array(X0), [theta.l1], theta.sig_1)
        Kx_l = squared(np.array(X0), [theta.l2], theta.sig_2)
        return np.block([[Kx, Kx], [Kx, Kx + Kx_l]])

    def cov_pricm(X0, Y, theta, split=False):
        Kx = squared(np.array(X0), [theta.l1], theta.sig_1)
        Kx = Kx + 1E-6 * np.eye(Kx.shape[0])

        if model.lower() == "pricm":
            Ks = np.array([
                np.array([
                    theta.rho[str(sorted([i, j]))] for j in range(theta.n_IS)
                ]) for i in range(theta.n_IS)
            ])
        elif model.lower() == "icm":
            L = np.array([
                np.array([
                    theta.rho[str(sorted([i, j]))] if i >= j else 0.0
                    for j in range(theta.n_IS)
                ]) for i in range(theta.n_IS)
            ])
            # Force it to be positive semi-definite
            Ks = L.dot(L.T)

        if split:
            return Ks, Kx
        else:
            return np.kron(Ks, Kx)

    sim.theta.bounds = {}
    sim.theta.sig_1, sim.theta.bounds['sig_1'] = None, (1E-2,
                                                        lambda _, Y: np.var(Y))
    sim.theta.l1, sim.theta.bounds['l1'] = None, (1E-1, 1)

    if model == "miso":
        sim.cov = cov_miso
        sim.theta.sig_2, sim.theta.bounds['sig_2'] = None, (
            1E-2, lambda _, Y: np.var(Y))
        sim.theta.l2, sim.theta.bounds['l2'] = None, (1E-1, 1)
        sim.theta.rho = {
            str(sorted([i, j])): 1.0
            for i in range(len(sim.IS)) for j in range(i, len(sim.IS))
        }
    else:
        sim.cov = cov_pricm
        sim.theta.rho = {"[0, 0]": None, "[0, 1]": None, "[1, 1]": None}
        if model.lower() == "icm":
            sim.theta.rho = {
                str(sorted([i, j])): None
                for i in range(len(sim.IS)) for j in range(i, len(sim.IS))
            }
        elif model.lower() == "pricm":
            sim.theta.rho = {
                str(sorted([i, j])): 1.0
                for i in range(len(sim.IS)) for j in range(i, len(sim.IS))
            }
            sim.dynamic_pc = True
        else:
            raise Exception("Invalid model.  Use MISO, ICM, or PRICM")

    for k in sim.theta.rho.keys():
        sim.theta.bounds['rho %s' % k] = (0.1, 1.0)
        a, b = eval(k)
        if a != b:
            sim.theta.bounds['rho %s' % k] = (0.01, 1.0 - 1E-6)

    sim.theta.set_hp_names()

    # Define how we update hyperparameters
    hp_opt = hp_opt.lower()
    if hp_opt == "is0":
        sim.update_hp_only_with_IS0 = True
        sim.update_hp_only_with_overlapped = False
    elif hp_opt == "overlap":
        sim.update_hp_only_with_IS0 = False
        sim.update_hp_only_with_overlapped = True
    elif hp_opt == "full":
        sim.update_hp_only_with_IS0 = False
        sim.update_hp_only_with_overlapped = False
    else:
        raise Exception("Unknown hp_opt (%s)." % hp_opt)

    # These should be False by default, but ensure they are
    sim.theta.normalize_L = False
    sim.theta.normalize_Ks = False
    sim.preconditioned = False

    # Assign our likelihood function.
    sim.loglike = g_loglike
    ###################################################################################################

    # Start simulation
    sim.iteration_kill_switch = None
    sim.cost_kill_switch = 100000
    sim.run()
示例#2
0
def run_misokg(run_index, sffx="misokg", SAMPLE_DOMAIN=1000):

    FOLDER = "RNS%d" % SAMPLE_DOMAIN

    scaled = False
    dpc = False
    invert_dpc = False
    scaled = False
    use_I = False
    use_J = False
    use_miso = False

    if sffx == "misokg":
        use_miso = True
    elif sffx == "bdpc":
        dpc = True
    elif sffx == "bidpc":
        dpc = True
        invert_dpc = True
    elif sffx == "bvl":
        pass
    elif sffx == "bsvl":
        scaled = True
    elif sffx == "bI":
        use_I = True
    elif sffx == "bu":
        use_J = True
    else:
        raise Exception("This sffx (%s) is not accounted for." % sffx)

    # Generate the main object
    sim = Optimizer()

    # Assign simulation properties
    #if use_MAP:
    #    sim.hyperparameter_objective = MAP
    #else:
    sim.hyperparameter_objective = MLE
    ###################################################################################################
    # File names
    sim.fname_out = None
    sim.fname_historical = None

    sim.logger_fname = "%s/%d_%s.log" % (FOLDER, run_index, sffx)
    if os.path.exists(sim.logger_fname):
        os.system("rm %s" % sim.logger_fname)
    os.system("touch %s" % sim.logger_fname)

    sim.obj_vs_cost_fname = None
    sim.mu_fname = None
    sim.sig_fname = None
    sim.combos_fname = None
    sim.hp_fname = None
    sim.acquisition_fname = None
    sim.save_extra_files = True

    # Information sources, in order from expensive to cheap
    rosenbrock = lambda x1, x2: (1.0 - x1)**2 + 100.0 * (x2 - x1**2)**2 - 456.3
    sim.IS = [
        lambda x1, x2: -1.0 * rosenbrock(x1, x2), lambda x1, x2: -1.0 *
        (rosenbrock(x1, x2) + 0.1 * np.sin(10.0 * x1 + 5.0 * x2))
    ]
    #sim.IS = [
    #    lambda x1, x2: (1.0 - x1)**2 + 100.0 * (x2 - x1**2)**2 - 456.3 + np.random.normal()
    #    lambda x1, x2: (1.0 - x1)**2 + 100.0 * (x2 - x1**2)**2 - 456.3 + 2.0 * np.sin(10.0 * x1 + 5.0 * x2)
    #]
    sim.costs = [1000.0, 1.0]

    ########################################
    sim.numerical = True
    sim.historical_nsample = 5
    sim.domain = [(-2.0, 2.0), (-2.0, 2.0)]
    sim.sample_n_from_domain = SAMPLE_DOMAIN
    ########################################

    sim.n_start = 10  # The number of starting MLE samples
    sim.reopt = 20
    sim.ramp_opt = None
    sim.parallel = False

    # Parameters for debugging and overwritting
    sim.debug = False
    sim.verbose = True
    sim.overwrite = True  # If True, warning, else Error

    sim.acquisition = getNextSample_misokg

    # Functional forms of our mean and covariance
    sim.mean = lambda X, Y, theta: np.array([-456.3 for _ in Y])

    def cov_miso(X0, Y, theta):
        Kx = squared(np.array(X0)[:, 1:], [theta.l1, theta.l2], theta.sig_1)
        Kx_l = squared(np.array(X0)[:, 1:], [theta.l3, theta.l4], theta.sig_2)
        return np.block([[Kx, Kx], [Kx, Kx + Kx_l]])

    def cov_bonilla(X0, Y, theta):
        Kx = squared(np.array(X0)[:, 1:], [theta.l1, theta.l2], theta.sig_1)
        Kx = Kx + 1E-6 * np.eye(Kx.shape[0])

        if use_J:
            Ks = np.ones((theta.n_IS, theta.n_IS)) * (1.0 - 1E-6) + np.eye(
                theta.n_IS) * 1E-6
        elif use_I:
            Ks = np.eye(theta.n_IS)
        elif dpc and invert_dpc:
            Ks = np.array([
                np.array([
                    1.0 if i != j else theta.rho["[0, %d]" % i]**(-2.0)
                    for j in range(theta.n_IS)
                ]) for i in range(theta.n_IS)
            ])
        elif dpc:
            Ks = np.array([
                np.array([
                    theta.rho[str(sorted([i, j]))] for j in range(theta.n_IS)
                ]) for i in range(theta.n_IS)
            ])
        else:
            L = np.array([
                np.array([
                    theta.rho[str(sorted([i, j]))] if i >= j else 0.0
                    for j in range(theta.n_IS)
                ]) for i in range(theta.n_IS)
            ])
            # Force it to be positive semi-definite
            Ks = L.dot(L.T)
            if theta.n_IS == 2:
                e = np.diag(np.array([theta.e1, theta.e2]))
            elif theta.n_IS == 3:
                e = np.diag(np.array([theta.e1, theta.e2, theta.e3]))
            else:
                raise Exception("HOW?")
            Ks = e.dot(Ks.dot(e))

        return np.kron(Ks, Kx)

    sim.theta.bounds = {}
    sim.theta.sig_1, sim.theta.bounds['sig_1'] = None, (1E-2,
                                                        lambda _, Y: np.var(Y))
    sim.theta.l1, sim.theta.bounds['l1'] = None, (1E-1, 1)
    sim.theta.l2, sim.theta.bounds['l2'] = None, (1E-1, 1)

    if use_miso:
        sim.cov = cov_miso
        sim.theta.sig_2, sim.theta.bounds['sig_2'] = None, (
            1E-2, lambda _, Y: np.var(Y))
        sim.theta.l3, sim.theta.bounds['l3'] = None, (1E-1, 1)
        sim.theta.l4, sim.theta.bounds['l4'] = None, (1E-1, 1)
        sim.theta.rho = {
            str(sorted([i, j])): 1.0
            for i in range(len(sim.IS)) for j in range(i, len(sim.IS))
        }
    else:
        sim.cov = cov_bonilla

        if scaled:
            sim.theta.e1, sim.theta.bounds['e1'] = None, (1E-1, 1.0)
            sim.theta.e2, sim.theta.bounds['e2'] = None, (1E-1, 1.0)
        else:
            sim.theta.e1, sim.theta.bounds['e1'] = 1.0, (1E-1, 1.0)
            sim.theta.e2, sim.theta.bounds['e2'] = 1.0, (1E-1, 1.0)

        sim.theta.rho = {"[0, 0]": None, "[0, 1]": None, "[1, 1]": None}
        if dpc or use_I or use_J:
            sim.theta.rho = {
                str(sorted([i, j])): 1.0
                for i in range(len(sim.IS)) for j in range(i, len(sim.IS))
            }
            sim.dynamic_pc = dpc

    for k in sim.theta.rho.keys():
        sim.theta.bounds['rho %s' % k] = (0.1, 1.0)
        a, b = eval(k)
        if a != b:
            sim.theta.bounds['rho %s' % k] = (0.01, 1.0 - 1E-6)

    sim.theta.set_hp_names()

    sim.update_hp_only_with_IS0 = False
    sim.update_hp_only_with_overlapped = False
    sim.theta.normalize_L = False
    sim.theta.normalize_Ks = False

    ###################################################################################################

    # Start simulation
    sim.iteration_kill_switch = 200
    sim.cost_kill_switch = 10000
    #sim.cost_kill_switch = sim.iteration_kill_switch * sim.costs[0]
    sim.run()