|
7 | 7 | 3) determine the minimizer via BOAlgorithm |
8 | 8 | """ |
9 | 9 |
|
| 10 | +import sys |
10 | 11 | import numpy as np |
11 | 12 | import matplotlib.pyplot as plt |
12 | 13 | import warnings |
|
17 | 18 | from hiopbbpy.problems import BraninProblem |
18 | 19 |
|
19 | 20 |
|
20 | | -### parameters |
21 | | -n_samples = 5 # number of the initial samples to train GP |
22 | | -theta = 1.e-2 # hyperparameter for GP kernel |
| 21 | +# Get user input for the number of repetitions from command-line arguments |
| 22 | +if len(sys.argv) != 2: |
| 23 | + num_repeat = 1 |
| 24 | +else: |
| 25 | + num_repeat = int(sys.argv[1]) |
23 | 26 |
|
24 | | -nx = 2 # dimension of the problem |
| 27 | +### parameters |
| 28 | +n_samples = 5 # number of the initial samples to train GP |
| 29 | +theta = 1.e-2 # hyperparameter for GP kernel |
| 30 | +nx = 2 # dimension of the problem |
25 | 31 | xlimits = np.array([[-5, 5], [-5, 5]]) # bounds on optimization variable |
26 | 32 |
|
27 | | -#problem = LpNormProblem(nx, xlimits) |
28 | | -problem = BraninProblem() |
29 | | - |
30 | | -print(problem.name, " problem") |
31 | | - |
32 | | -### initial training set |
33 | | -x_train = problem.sample(n_samples) |
34 | | -y_train = problem.evaluate(x_train) |
35 | | - |
36 | | -# Define the GP surrogate model |
37 | | -gp_model = smtKRG(theta, xlimits, nx) |
38 | | -gp_model.train(x_train, y_train) |
39 | | - |
40 | | -acquisition_types = ["LCB", "EI"] |
41 | | -for acquisition_type in acquisition_types: |
42 | | - print("acquisition type: ", acquisition_type) |
43 | | - |
44 | | - # Instantiate and run Bayesian Optimization |
45 | | - bo = BOAlgorithm(gp_model, x_train, y_train, acquisition_type = acquisition_type) #EI or LCB |
46 | | - bo.optimize(problem) |
47 | | - |
48 | | - # Retrieve optimal point |
49 | | - x_opt, y_opt = bo.getOptimalPoint() |
50 | | - print() |
| 33 | +### saved solutions |
| 34 | +saved_sol = {"LpNorm": {"LCB": 0.04618462, "EI": 0.44954611}, "Branin": {"LCB": 0.62655919, "EI": 1.9838798}} |
| 35 | + |
| 36 | +prob_type_l = ["LpNorm", "Branin"] |
| 37 | +acq_type_l = ["LCB", "EI"] |
| 38 | + |
| 39 | +mean_obj = {} |
| 40 | + |
| 41 | +retval = 0 |
| 42 | +for prob_type in prob_type_l: |
| 43 | + print() |
| 44 | + if prob_type == "LpNorm": |
| 45 | + problem = LpNormProblem(nx, xlimits) |
| 46 | + else: |
| 47 | + problem = BraninProblem() |
| 48 | + |
| 49 | + if prob_type not in mean_obj: |
| 50 | + mean_obj[prob_type] = {} |
| 51 | + |
| 52 | + for acq_type in acq_type_l: |
| 53 | + if acq_type not in mean_obj[prob_type]: |
| 54 | + mean_obj[prob_type][acq_type] = 0 |
| 55 | + |
| 56 | + print("Problem name: ", problem.name) |
| 57 | + print("Acquisition type: ", acq_type) |
| 58 | + |
| 59 | + for n_repeat in range(num_repeat): |
| 60 | + print("Run: ", n_repeat, "/", num_repeat) |
| 61 | + ### initial training set |
| 62 | + x_train = problem.sample(n_samples) |
| 63 | + y_train = problem.evaluate(x_train) |
| 64 | + |
| 65 | + ### Define the GP surrogate model |
| 66 | + gp_model = smtKRG(theta, xlimits, nx) |
| 67 | + gp_model.train(x_train, y_train) |
| 68 | + |
| 69 | + # Instantiate and run Bayesian Optimization |
| 70 | + bo = BOAlgorithm(gp_model, x_train, y_train, acquisition_type = acq_type) #EI or LCB |
| 71 | + bo.optimize(problem) |
| 72 | + |
| 73 | + # Retrieve optimal objec |
| 74 | + y_opt = bo.getOptimalObjective() |
| 75 | + |
| 76 | + mean_obj[prob_type][acq_type] += y_opt |
| 77 | + |
| 78 | +for prob_type in prob_type_l: |
| 79 | + for acq_type in acq_type_l: |
| 80 | + mean_obj[prob_type][acq_type] /= num_repeat |
| 81 | + print("Mean Opt.Obj for ", prob_type, "-", acq_type, mean_obj[prob_type][acq_type]) |
| 82 | + |
| 83 | + r_error = np.abs((mean_obj[prob_type][acq_type] - saved_sol[prob_type][acq_type])/saved_sol[prob_type][acq_type]) |
| 84 | + if r_error > 0.5: |
| 85 | + print("Relative Error > 0.5: ", r_error) |
| 86 | + print("Recorded Solution:", saved_sol[prob_type][acq_type]) |
| 87 | + retval = 1 |
| 88 | + |
| 89 | +sys.exit(retval) |
| 90 | + |
| 91 | + |
| 92 | + |
0 commit comments