Exemple #1
0
def main():
    """ Main function. """
    domain_vars = [{
        'name': 'hubble_constant',
        'type': 'float',
        'min': 60,
        'max': 80
    }, {
        'name': 'omega_m',
        'type': 'float',
        'min': 0,
        'max': 1
    }, {
        'name': 'omega_l',
        'type': 'float',
        'min': 0,
        'max': 1
    }]
    config_params = {'domain': domain_vars}
    config = load_config(config_params)
    max_capital = 2 * 60 * 60  # Optimisation budget in seconds

    # Optimise
    opt_pt, opt_val, history = maximise_function(snls_objective,
                                                 config.domain,
                                                 max_capital,
                                                 capital_type='realtime',
                                                 config=config)
Exemple #2
0
def main():
  """ Main function. """
  domain_bounds = [[-5, 10], [0, 15]]
  max_capital = 100
  opt_val, opt_pt, _ = maximise_function(branin, domain_bounds, max_capital)
  print('Optimum Value in %d evals: %0.4f'%(max_capital, opt_val))
  print('Optimum Point: %s'%(opt_pt))
Exemple #3
0
def main():
    """ Main function. """
    disc_euc_items = list(np.random.random((1000, 3)))
    domain_vars = [
        {
            'type': 'discrete_euclidean',
            'items': disc_euc_items
        },
        {
            'type': 'float',
            'min': 0,
            'max': 11,
            'dim': 2
        },
        {
            'type': 'int',
            'min': 0,
            'max': 114
        },
    ]
    config_params = {'domain': domain_vars}
    config = load_config(config_params)
    max_num_evals = 100
    opt_val, opt_pt, history = maximise_function(hartmann6_3,
                                                 config.domain,
                                                 max_num_evals,
                                                 config=config)
    print(opt_pt, opt_val)
Exemple #4
0
def main():
    """ Main function. """
    domain_bounds = [[-5, 10], [0, 15]]
    max_capital = 100
    opt_val, opt_pt = maximise_function(branin,
                                        max_capital,
                                        domain_bounds=domain_bounds,
                                        hp_tune_criterion='post_sampling',
                                        hp_tune_method='slice')
    print('Optimum Value in %d evals: %0.4f' % (max_capital, opt_val))
    print('Optimum Point: %s' % (opt_pt))
Exemple #5
0
def single_fidelity():
    """Main function"""
    domain_bounds = [[-20, 20], [-20, 20]]
    max_capital = 200
    time_start = time.time()
    opt_val, opt_pt, history = maximise_function(
        straw_hat, domain_bounds, max_capital)  #,opt_method='rand')
    time_end = time.time()
    print('totally cost', time_end - time_start)
    curr_opt_points = history.curr_opt_points
    curr_opt_vals = history.curr_opt_vals

    # plot sample points
    fig = plt.figure()
    ax = fig.gca(projection='3d')

    # Make function surface.
    X1 = np.arange(-20, 20, 0.25)
    X2 = np.arange(-20, 20, 0.25)
    X1, X2 = np.meshgrid(X1, X2)
    x = [X1, X2]
    z = straw_hat(x)

    # Make sample points
    n = len(curr_opt_vals)
    smaple_z = curr_opt_vals
    sample_x = np.zeros(n)
    sample_y = np.zeros(n)
    for idx in range(n):
        sample_x[idx] = curr_opt_points[idx][0]
        sample_y[idx] = curr_opt_points[idx][1]

    # Plot the surface.
    #surf = ax.plot_surface(X1, X2, z, cmap=cm.coolwarm,linewidth=0, antialiased=False)

    # Plot a basic wireframe.
    ax.plot_wireframe(X1, X2, z, rstride=10, cstride=10)
    # Customize the z axis.
    ax.set_zlim(-1.01, 1.01)
    ax.zaxis.set_major_locator(LinearLocator(10))
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

    # Add a color bar which maps values to colors.
    # fig.colorbar(surf, shrink=0.5, aspect=5)
    ax.plot(sample_x, sample_y, smaple_z, label='parametric curve', c='red')
    ax.legend()
    plt.show()
Exemple #6
0
def main():
  """ Main function. """
  # First Specify all parameters
  disc_num_items_x1 = [4, 10, 23, 45, 78, 87.1, 91.8, 99, 75.7, 28.1, 3.141593]
  domain_vars = [
    {'name': 'x0', 'type': 'discrete_numeric', 'items': '0:0.05:1', 'dim': 2},
    {'name': 'x1', 'type': 'discrete_numeric', 'items': disc_num_items_x1},
    {'name': 'x2', 'type': 'float',  'min': 10, 'max': 16},
    ]
  domain_constraints = [
    {'name': 'dc1', 'constraint': 'sum(x0) + (x2 - 10)/6.0 <= 2.1'},
    {'name': 'dc2', 'constraint': dc2_constraint}
    ]
  disc_items_z2 = ['a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef', 'abcdefg', 'abcdefg',
                   'abcdefgh', 'abcdefghi']
  fidel_vars = [{'name': 'z1', 'type': 'discrete', 'items': disc_items_z2, 'dim': 2},
                {'name': 'z2', 'type': 'float', 'min': 21.3, 'max': 243.9},
               ]
  fidel_to_opt = [["abcdefghi", "abcdefghi"], 200.1]
  # Budget of evaluations
  max_num_evals = 100 # Optimisation budget (max number of evaluations)
  max_mf_capital = max_num_evals * mf_cost(fidel_to_opt) # Multi-fideltiy capital

  # First do the MF version
  config_params = {'domain': domain_vars, 'fidel_space': fidel_vars,
                   'domain_constraints': domain_constraints,
                   'fidel_to_opt': fidel_to_opt}
  config = load_config(config_params)
  # Optimise
  mf_opt_val, mf_opt_pt, history = maximise_multifidelity_function(mf_objective,
                                     config.fidel_space, config.domain,
                                     config.fidel_to_opt, mf_cost, 
                                     max_mf_capital, config=config)
  print(mf_opt_pt, mf_opt_val)

  # Non-MF version
  config_params = {'domain': domain_vars, 'domain_constraints': domain_constraints}
  config = load_config(config_params)
  max_capital = 100 # Optimisation budget (max number of evaluations)
  # Optimise
  opt_val, opt_pt, history = maximise_function(objective, config.domain,
                                               max_num_evals, config=config)
  print(opt_pt, opt_val)
Exemple #7
0
def main():
    """ Main function. """
    size = 1000
    dim = 3
    disc_euc_items = list(np.random.random((size, dim)))
    domain_vars = [
        {
            'type': 'discrete_euclidean',
            'items': disc_euc_items
        },
    ]
    config_params = {'domain': domain_vars}
    config = load_config(config_params)
    max_num_evals = 100
    opt_pt, opt_val, history = maximise_function(objective,
                                                 config.domain,
                                                 max_num_evals,
                                                 config=config)
    print(opt_pt, opt_val)
Exemple #8
0
def main():
  """ Main function. """
  size = 1000
  dim = 3
  disc_euc_items = list(np.random.random((size, dim)))
  domain_vars = [
    {'type': 'discrete_euclidean', 'items': disc_euc_items},
    ]
  config_params = {'domain': domain_vars}
  config = load_config(config_params)
  max_num_evals = 100

  # specify optimisation method
#   opt_method = 'bo' # Bayesian optimisation
  opt_method = 'ea' # evolutionary algorithm
#   opt_method = 'rand' # random search
  opt_val, opt_pt, history = maximise_function(objective, config.domain, max_num_evals,
                                               config=config, opt_method=opt_method)
  print(opt_pt, opt_val)
Exemple #9
0
def main():
  """ Main function. """
  # First Specify all parameters
  domain_vars = [{'name': 'x', 'type': 'float', 'min': 0, 'max': 1, 'dim': 3}]
  domain_constraints = [
    {'name': 'quadrant', 'constraint': 'np.linalg.norm(x[0:2]) <= 0.5'},
    ]
  fidel_vars = [{'name': 'z', 'type': 'float',  'min': 0, 'max': 10}]
  fidel_space_constraints = [{'name': 'fsc1', 'constraint': fsc1_constraint}]
  fidel_to_opt = [9.1]
  print('fsc1_constraint(fidel_to_opt)', fsc1_constraint(fidel_to_opt))
  # Budget of evaluations
  max_num_evals = 100 # Optimisation budget (max number of evaluations)
  max_mf_capital = max_num_evals * mf_cost(fidel_to_opt) # Multi-fideltiy capital

  # Non-MF version
  config_params = {'domain': domain_vars, 'domain_constraints': domain_constraints}
  config = load_config(config_params)
  max_capital = 100 # Optimisation budget (max number of evaluations)
  # Optimise
  opt_val, opt_pt, history = maximise_function(objective, config.domain,
                                               max_num_evals, config=config)
  print(opt_pt, opt_val)

  # MF version
  config_params = {'domain': domain_vars, 'fidel_space': fidel_vars,
                   'domain_constraints': domain_constraints,
                   'fidel_space_constraints': fidel_space_constraints,
                   'fidel_to_opt': fidel_to_opt}
  config = load_config(config_params)
  # Optimise
  mf_opt_val, mf_opt_pt, history = maximise_multifidelity_function(mf_objective,
                                     config.fidel_space, config.domain,
                                     config.fidel_to_opt, mf_cost, 
                                     max_mf_capital, config=config)
  print(mf_opt_pt, mf_opt_val)
Exemple #10
0
def main():
    """ Main function. """
    # Load configuration file
    objective, config_file, mf_cost = _CHOOSER_DICT[PROBLEM]
    config = load_config_file(config_file)

    # Specify optimisation method -----------------------------------------------------
    opt_method = 'bo'
    # opt_method = 'ga'
    # opt_method = 'rand'

    # Optimise
    max_capital = 60
    domain, domain_orderings = config.domain, config.domain_orderings
    if PROBLEM in ['3d', '5d']:
        # Create function caller.
        # Note there is no function passed in to the Function Caller object.
        func_caller = CPFunctionCaller(None,
                                       domain,
                                       domain_orderings=domain_orderings)

        if opt_method == 'bo':
            opt = gp_bandit.CPGPBandit(func_caller, ask_tell_mode=True)
        elif opt_method == 'ga':
            opt = cp_ga_optimiser.CPGAOptimiser(func_caller,
                                                ask_tell_mode=True)
        elif opt_method == 'rand':
            opt = random_optimiser.CPRandomOptimiser(func_caller,
                                                     ask_tell_mode=True)
        opt.initialise()

        # Optimize using the ask-tell interface
        # User continually asks for the next point to evaluate, then tells the optimizer the
        # new result to perform Bayesian optimisation.
        best_x, best_y = None, float('-inf')
        for _ in range(max_capital):
            x = opt.ask()
            y = objective(x)
            opt.tell([(x, y)])
            print('x: %s, y: %s' % (x, y))
            if y > best_y:
                best_x, best_y = x, y
        print("Optimal Value: %s, Optimal Point: %s" % (best_y, best_x))

        # Compare results with the maximise_function API
        print("-------------")
        print("Compare with maximise_function API:")
        opt_val, opt_pt, history = maximise_function(objective,
                                                     config.domain,
                                                     max_capital,
                                                     opt_method=opt_method,
                                                     config=config)

    elif PROBLEM == '3d_euc':
        # Create function caller.
        # Note there is no function passed in to the Function Caller object.
        domain = domain.list_of_domains[0]
        func_caller = EuclideanFunctionCaller(None, domain)

        if opt_method == 'bo':
            opt = gp_bandit.EuclideanGPBandit(func_caller, ask_tell_mode=True)
        elif opt_method == 'ga':
            raise ValueError("Invalid opt_method %s" % (opt_method))
        opt.initialise()

        # Optimize using the ask-tell interface
        # User continually asks for the next point to evaluate, then tells the optimizer the
        # new result to perform Bayesian optimisation.
        best_x, best_y = None, float('-inf')
        for _ in range(max_capital):
            # Optionally, you can add an integer argument `n_points` to ask to have it return
            # `n_points` number of points. These points will be returned as a list.
            # No argument for `n_points` returns a single point from ask.
            x = opt.ask()
            y = objective(x)
            opt.tell([(x, y)])
            print('x: %s, y: %s' % (x, y))
            if y > best_y:
                best_x, best_y = x, y
        print("Optimal Value: %s, Optimal Point: %s" % (best_y, best_x))

        # Compare results with the maximise_function API
        print("-------------")
        print("Compare with maximise_function API:")
        opt_val, opt_pt, history = maximise_function(objective,
                                                     config.domain,
                                                     max_capital,
                                                     opt_method=opt_method,
                                                     config=config)

    else:
        # Create function caller.
        # Note there is no function passed in to the Function Caller object.
        (ask_tell_fidel_space, ask_tell_domain, _, ask_tell_mf_cost, ask_tell_fidel_to_opt, ask_tell_config, _) = \
          preprocess_multifidelity_arguments(config.fidel_space, domain, [objective],
                                             mf_cost, config.fidel_to_opt, config)
        func_caller = CPFunctionCaller(
            None,
            ask_tell_domain,
            domain_orderings=domain_orderings,
            fidel_space=ask_tell_fidel_space,
            fidel_cost_func=ask_tell_mf_cost,
            fidel_to_opt=ask_tell_fidel_to_opt,
            fidel_space_orderings=config.fidel_space_orderings,
            config=ask_tell_config)
        if opt_method == 'bo':
            opt = gp_bandit.CPGPBandit(func_caller,
                                       is_mf=True,
                                       ask_tell_mode=True)
        else:
            raise ValueError("Invalid opt_method %s" % (opt_method))
        opt.initialise()

        # Optimize using the ask-tell interface
        # User continually asks for the next point to evaluate, then tells the optimizer the
        # new result to perform Bayesian optimisation.
        best_z, best_x, best_y = None, None, float('-inf')
        for _ in range(max_capital):
            point = opt.ask()
            z, x = point[0], point[1]
            y = objective(z, x)
            opt.tell([(z, x, y)])
            print('z: %s, x: %s, y: %s' % (z, x, y))
            if y > best_y:
                best_z, best_x, best_y = z, x, y
        print("Optimal Value: %s, Optimal Point: %s" % (best_y, best_x))

        # Compare results with the maximise_multifidelity_function API
        print("-------------")
        print("Compare with maximise_multifidelity_function API:")
        opt_val, opt_pt, history = maximise_multifidelity_function(
            objective,
            config.fidel_space,
            config.domain,
            config.fidel_to_opt,
            mf_cost,
            max_capital,
            opt_method=opt_method,
            config=config)

    print('opt_pt: %s' % (str(opt_pt)))
    print('opt_val: %s' % (str(opt_val)))
Exemple #11
0
def main():
    """ Main function. """
    # First Specify all parameters
    domain_vars = [
        {
            'type': 'int',
            'min': 224,
            'max': 324,
            'dim': 1
        },
        {
            'type': 'float',
            'min': 0,
            'max': 10,
            'dim': 2
        },
        {
            'type': 'float',
            'min': 0,
            'max': 1,
            'dim': 1
        },
        {
            'type': 'int',
            'min': 0,
            'max': 92,
            'dim': 2
        },
    ]
    fidel_vars = [
        {
            'type': 'float',
            'min': 1234.9,
            'max': 9467.18,
            'dim': 2
        },
        {
            'type': 'discrete',
            'items': ['a', 'bc', 'def', 'ghij']
        },
        {
            'type': 'int',
            'min': 123,
            'max': 234,
            'dim': 1
        },
    ]
    fidel_to_opt = [[9467.18, 9452.8], "def", [234]]
    # Budget of evaluations
    max_num_evals = 100  # Optimisation budget (max number of evaluations)
    max_mf_capital = max_num_evals * mf_cost(
        fidel_to_opt)  # Multi-fideltiy capital

    # First do the MF version
    config_params = {
        'domain': domain_vars,
        'fidel_space': fidel_vars,
        'fidel_to_opt': fidel_to_opt
    }
    config = load_config(config_params)
    # Optimise
    mf_opt_val, mf_opt_pt, history = maximise_multifidelity_function(
        mf_objective,
        config.fidel_space,
        config.domain,
        config.fidel_to_opt,
        mf_cost,
        max_mf_capital,
        config=config)
    print(mf_opt_pt, mf_opt_val)

    # Non-MF version
    config_params = {'domain': domain_vars}
    config = load_config(config_params)
    max_capital = 100  # Optimisation budget (max number of evaluations)
    # Optimise
    opt_val, opt_pt, history = maximise_function(objective,
                                                 config.domain,
                                                 max_num_evals,
                                                 config=config)
    print(opt_pt, opt_val)
Exemple #12
0
def main():
    """ Main function. """
    domain_vars = [{
        'name': 'hubble_constant',
        'type': 'float',
        'min': 60,
        'max': 80
    }, {
        'name': 'omega_m',
        'type': 'float',
        'min': 0,
        'max': 1
    }, {
        'name': 'omega_l',
        'type': 'float',
        'min': 0,
        'max': 1
    }]
    fidel_vars = [{
        'name': 'log10_resolution',
        'type': 'float',
        'min': 2,
        'max': 5
    }, {
        'name': 'num_obs_to_use',
        'type': 'int',
        'min': 50,
        'max': 192
    }]
    fidel_to_opt = [5, 192]
    max_capital = 2 * 60 * 60  # Optimisation budget in seconds

    # A parallel set up where we will evaluate the function in three different threads.
    num_workers = 3

    # Optimise without multi-fidelity
    config_params = {'domain': domain_vars}
    config = load_config(config_params)
    opt_val, opt_pt, history = maximise_function(snls_objective,
                                                 config.domain,
                                                 max_capital,
                                                 num_workers=num_workers,
                                                 capital_type='realtime',
                                                 config=config)
    print(opt_pt, opt_val)

    # Optimise with multi-fidelity
    config_params = {
        'domain': domain_vars,
        'fidel_space': fidel_vars,
        'fidel_to_opt': fidel_to_opt
    }
    config = load_config(config_params)
    # Optimise
    mf_opt_val, mf_opt_pt, history = maximise_multifidelity_function(
        snls_mf_objective,
        config.fidel_space,
        config.domain,
        config.fidel_to_opt,
        snls_mf_cost,
        max_capital,
        config=config)
    print(mf_opt_pt, mf_opt_val)
Exemple #13
0
def main():
    """ Main function. """
    # Load configuration file
    objective, config_file, mf_cost = _CHOOSER_DICT[PROBLEM]
    config = load_config_file(config_file)

    # Specify optimisation method -----------------------------------------------------
    #   opt_method = 'bo'
    opt_method = 'ga'
    #   opt_method = 'rand'

    # Specify options
    options = Namespace(
        build_new_model_every=5,  # update the model every 5 iterations
        report_results_every=4,  # report progress every 4 iterations
        report_model_on_each_build=True,  # report model when you build it.
    )

    # Specifying GP priors -------------------------------------------------------------
    # Dragonfly allows specifying a mean for the GP prior - if there is prior knowledge
    # on the rough behaviour of the function to be optimised, this is one way that
    # information can be incorporated into the model.
    if USE_CONDUCTIVITY_PRIOR_MEAN:
        if PROBLEM in ['3d', '3d_euc']:
            options.gp_prior_mean = conductivity_prior_mean_3d
        elif PROBLEM == '3d_mf':
            options.gp_prior_mean = conductivity_prior_mean_3d_mf
        elif PROBLEM == '5d':
            options.gp_prior_mean = conductivity_prior_mean_5d
        # The _unproc indicates that the mean function is "unprocessed". Dragonfly converts
        # the domain specified given in the configuration to an internal order which may
        # have reordered the variables. The _unproc tells that the function
        # should be called in the original format.

    # Saving and loading data ----------------------------------------------------------
    # You can save and load progress in Dragonfly. This allows you to resume an
    # optimisation routine if it crashes from where we left off.
    # Other related options include:
    #   - progress_load_from: loads progress from this file but does not save it.
    #   - progress_save_to: loads progress from this file but does not save it.
    #   - progress_report_on_each_save: reports that the progress was saved (default True)
    if SAVE_AND_LOAD_PROGRESS:
        options.progress_load_from_and_save_to = 'progress.p'
        options.progress_save_every = 5
        # progress_load_from and progress_load_from_and_save_to can be a list of file names
        # in which case we will load from all the files.
        # e.g options.progress_load_from_and_save_to = ['progress1.p', 'progress2.p']

    # Optimise
    max_capital = 60
    if PROBLEM in ['3d', '3d_euc', '5d']:
        opt_val, opt_pt, history = maximise_function(objective,
                                                     config.domain,
                                                     max_capital,
                                                     opt_method=opt_method,
                                                     config=config,
                                                     options=options)
    else:
        opt_val, opt_pt, history = maximise_multifidelity_function(
            objective,
            config.fidel_space,
            config.domain,
            config.fidel_to_opt,
            mf_cost,
            max_capital,
            opt_method=opt_method,
            config=config,
            options=options)

    print('opt_pt: %s' % (str(opt_pt)))
    print('opt_val: %s' % (str(opt_val)))
Exemple #14
0
def main():
    """ Main function. """
    # First Specify all parameters
    domain_vars = [
        {
            'name': 'rw',
            'type': 'float',
            'min': 0.05,
            'max': 0.15,
            'dim': 1
        },
        {
            'name': 'L_Kw',
            'type': 'float',
            'min': 0,
            'max': 1,
            'dim': 2
        },
        {
            'name': 'Tu',
            'type': 'int',
            'min': 63070,
            'max': 115600,
            'dim': ''
        },
        {
            'name': 'Tl',
            'type': 'float',
            'min': 63.1,
            'max': 116
        },
        {
            'name': 'Hu_Hl',
            'type': 'int',
            'min': 0,
            'max': 240,
            'dim': 2
        },
        {
            'name': 'r',
            'type': 'float',
            'min': 100,
            'max': 50000
        },
    ]
    domain_constraints = [{
        'constraint': 'np.sqrt(rw[0]) + L_Kw[1] <= 0.9'
    }, {
        'constraint': 'r/100.0 + Hu_Hl[1] < 200'
    }]
    fidel_vars = [
        {
            'name': 'fidel_0',
            'type': 'float',
            'min': 0.05,
            'max': 0.25
        },
        {
            'name': 'fidel_1',
            'type': 'discrete_numeric',
            'items': "0.1:0.05:1.01"
        },
    ]
    fidel_space_constraints = [{
        'name': 'fsc1',
        'constraint': 'fidel_0 + fidel_1 <= 0.9'
    }]
    fidel_to_opt = [0.1, 0.75]
    # Budget of evaluations
    max_num_evals = 100  # Optimisation budget (max number of evaluations)
    max_mf_capital = max_num_evals * mf_cost(
        fidel_to_opt)  # Multi-fideltiy capital

    # First do the MF version
    config_params = {
        'domain': domain_vars,
        'fidel_space': fidel_vars,
        'domain_constraints': domain_constraints,
        'fidel_space_constraints': fidel_space_constraints,
        'fidel_to_opt': fidel_to_opt
    }
    config = load_config(config_params)
    # Optimise
    mf_opt_pt, mf_opt_val, history = maximise_multifidelity_function(
        mf_objective,
        config.fidel_space,
        config.domain,
        config.fidel_to_opt,
        mf_cost,
        max_mf_capital,
        config=config)
    print(mf_opt_pt, mf_opt_val)

    # Non-MF version
    config_params = {
        'domain': domain_vars,
        'domain_constraints': domain_constraints
    }
    config = load_config(config_params)
    max_capital = 100  # Optimisation budget (max number of evaluations)
    # Optimise
    opt_pt, opt_val, history = maximise_function(objective,
                                                 config.domain,
                                                 max_num_evals,
                                                 config=config)
    print(opt_pt, opt_val)