Skip to content

Commit

Permalink
Merge pull request huawei-noah#52 from huawei-noah/juliusz
Browse files Browse the repository at this point in the history
Restore pre-merge version.
  • Loading branch information
AntGro authored Aug 10, 2023
2 parents 914d920 + 90b7ade commit 68250c6
Show file tree
Hide file tree
Showing 9 changed files with 6 additions and 196 deletions.
29 changes: 0 additions & 29 deletions RDUCB/config/synthetic_functions/hartmann614/gpmw.yml

This file was deleted.

28 changes: 0 additions & 28 deletions RDUCB/config/synthetic_functions/rosenbrock20/gpmw.yml

This file was deleted.

29 changes: 0 additions & 29 deletions RDUCB/config/synthetic_functions/stybtang250/gpmw.yml

This file was deleted.

85 changes: 0 additions & 85 deletions RDUCB/hdbo/acquisition_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,88 +467,3 @@ def optimize(self, f, f_df):
fbest = fval

return xbest, fbest


from joblib import Parallel, delayed


class MWAcquisitionOptimizer(AcquisitionOptimizer):
def __init__(self, eta_factor, T, optimal_Y, domain, X, mlflow_logging, max_eval, acq_opt_restarts=1):
super(MWAcquisitionOptimizer, self).__init__(domain)
self.eta_factor = eta_factor
self.T = T
self.optimal_Y = optimal_Y
self.domain = domain
self.mlflow_logging = mlflow_logging
self.max_eval = max_eval
self.acq_opt_restarts = acq_opt_restarts

self.dimension = domain.dimension
self.edges_set = None

self.evaluated_points = []
for x in X:
self.evaluated_points.append(x)

self.K = [len(domain) for domain in self.domain.combined_domain]
self.weights = [np.ones(self.K[i])/self.K[i] for i in range(self.dimension)]

self._total_cost = 0

def optimize(self, f=None, df=None, f_df=None):
inital_total_cost = self._total_cost

evals = []
for i in range(self.acq_opt_restarts):
self._total_cost = inital_total_cost
x_best, fmin = self._optimize_helper(f)
total_cost = self._total_cost
evals.append((fmin, x_best, total_cost))

fmin, x_best, total_cost = min(evals, key=lambda x: x[0])
self._total_cost = total_cost

return x_best, fmin

def _optimize_helper(self, f):

if not self.evaluated_points:
x = np.array([np.array(self.domain.combined_domain[j])[np.random.choice(self.K[j], 1)] for j in range(self.dimension)]).squeeze()
else:
x = self.evaluated_points[-1]

# ugly walkaround to avoid serialising objects that cause bugs in parallel jobs
gp_model = f[tuple(f.keys())[0]].model
gp_model_detached_attributes = {}
for attribute in {"fn", "cfn", "additional_args"}:
gp_model_detached_attributes[attribute] = getattr(gp_model, attribute)
setattr(gp_model, attribute, None)

self.weights = Parallel(n_jobs=-1)(delayed(weights_update)(self.weights[i], self.eta_factor, self.T, self.optimal_Y, self.K[i], f, x, self.domain.combined_domain[i], i) for i in range(self.dimension))


x_sample = np.array([self.domain.combined_domain[i][np.argmax(np.random.multinomial(1, self.weights[i]))] for i in range(self.dimension)]).squeeze()
self.evaluated_points.append(x_sample)
f_sample = f(x_sample)

# restoring gp model to its previous state
for attribute in {"fn", "cfn", "additional_args"}:
setattr(gp_model, attribute, gp_model_detached_attributes[attribute])

return x_sample.reshape(1, -1), f_sample

def weights_update(curr_weight, eta_factor, T, optimal_Y, K, f, x, combined_domain, i):
#print(f"Thread {i} starting")
x = x.copy()
x_list = []
for k in range(K):
x[i] = combined_domain[k]
x_list.append(x.copy())

y_hat = np.minimum(optimal_Y, np.array(-f(np.array(x_list))).squeeze())

curr_weight = curr_weight * np.exp(-np.sqrt(eta_factor*np.log(K)/T)*(optimal_Y-y_hat))
curr_weight = curr_weight / np.sum(curr_weight)

#print(f"Thread {i} finished")
return curr_weight
11 changes: 0 additions & 11 deletions RDUCB/hdbo/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,3 @@ def make_fn_optimizer(self):
return None
def get_GraphFunction(self):
return OptimalGraphFunction

class GPMW(BayesianOptimization, metaclass=Algorithm):

__metaclass__ = Algorithm
def __init__(self, **kwargs):
self.random_graph = False
BayesianOptimization.__init__(self, **kwargs)

def FnOptimizer(self):
self.cycles = False
return function_optimizer.ParameterOnlyOptimizer
10 changes: 2 additions & 8 deletions RDUCB/hdbo/function_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,6 @@ def HypothesisType(self):
import operator
from functools import reduce

def get_ls_wrap(ord_dim_ls):
return ord_dim_ls

def get_ls_wrap_sum(ord_dim_ls):
return sum(ord_dim_ls)

class GraphFunction(object):
def __init__(self, graph, initial_kernel_params):
self.graph = graph
Expand Down Expand Up @@ -938,9 +932,9 @@ def __init__(self, graph, initial_kernel_params):
self.is_ard = initial_kernel_params['ard']

if self.is_ard:
self.ls_wrap = get_ls_wrap
self.ls_wrap = lambda ord_dim_ls: ord_dim_ls
else:
self.ls_wrap = get_ls_wrap_sum
self.ls_wrap = lambda ord_dim_ls: sum(ord_dim_ls)

# l_bfgs is now the default.
self.scipy_opt = scipy.optimize.fmin_tnc
Expand Down
4 changes: 2 additions & 2 deletions RDUCB/hdbo/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def args_parse():
del args_dict["algorithm_type"]

# turn it into lambda if string
#if type(args_dict["exploration_weight"]) == str:
# args_dict["exploration_weight"] = eval(args_dict["exploration_weight"])
if type(args_dict["exploration_weight"]) == str:
args_dict["exploration_weight"] = eval(args_dict["exploration_weight"])

return args_dict
4 changes: 1 addition & 3 deletions RDUCB/hdbo/myBOModular.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from GPyOpt.optimization.acquisition_optimizer import AcquisitionOptimizer
from myAcquisitionModular import MyAcquisitionModular
from myGPModel import MyGPModel
from acquisition_optimizer import MPAcquisitionOptimizer, BruteForceAcquisitionOptimizer, MWAcquisitionOptimizer
from acquisition_optimizer import MPAcquisitionOptimizer, BruteForceAcquisitionOptimizer
from GPyOpt.core.evaluators.sequential import Sequential
from GPyOpt.core.task.objective import SingleObjective
from GPyOpt.core.task.cost import CostModel
Expand Down Expand Up @@ -150,8 +150,6 @@ def __init__(self, domain, initial_design, graph_function,

if acquisition_optimizer_type == 'MP':
self.acquisition_optimizer = MPAcquisitionOptimizer(domain, self.model.graph_function, [], self.fn.mlflow_logging, max_eval=max_eval, acq_opt_restarts=acq_opt_restarts)
elif acquisition_optimizer_type == 'MW':
self.acquisition_optimizer = MWAcquisitionOptimizer(additional_args['eta_factor'], additional_args['T'], -self.fn.mlflow_logging.y_opt, domain, [], self.fn.mlflow_logging, max_eval, acq_opt_restarts)
elif acquisition_optimizer_type == 'bf':
self.acquisition_optimizer = BruteForceAcquisitionOptimizer(domain, [], self.fn.mlflow_logging, max_eval=max_eval, acq_opt_restarts=acq_opt_restarts)
elif acquisition_optimizer_type in {'lbfgs', "DIRECT"}:
Expand Down
2 changes: 1 addition & 1 deletion RDUCB/hdbo/myGPModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def predict_withGradients_with_kernel(self, X, kernel):


def exploration_weight(self):
return eval(f"({self.exploration_weight_function})(self.t)")
return self.exploration_weight_function(self.t)

# Update t when the model is updated
def updateModel(self, X_all, Y_all, X_new, Y_new, log=True):
Expand Down

0 comments on commit 68250c6

Please sign in to comment.