Exemple #1
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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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)