def _get_test_points(dz, dx, z_bounds, x_bounds, n=5): """ Gets test points. """ single_nz = np.random.random(dz) single_nx = np.random.random(dx) mult_nz = np.random.random((n, dz)) mult_nx = np.random.random((n, dx)) single_z = map_to_bounds(single_nz, z_bounds) single_x = map_to_bounds(single_nx, x_bounds) mult_z = map_to_bounds(mult_nz, z_bounds) mult_x = map_to_bounds(mult_nx, x_bounds) return (single_nz, single_nx, single_z, single_x, mult_nz, mult_nx, mult_z, mult_x)
def _child_set_up(self): """ Child set up. """ self.domain_obj = domains.EuclideanDomain([[0, 2.3], [3.4, 8.9], [0.12, 1.0]]) self.points = [ map_to_bounds(np.random.random((self.domain_obj.dim, )), self.domain_obj.bounds) for _ in range(5) ] self.non_points = [ map_to_bounds(np.random.random((self.domain_obj.dim, )), np.array([[3.5, 9.8], [-1.0, 1.1], [2.3, 4.5]])) for _ in range(5) ]
def sample_cts_dscr_hps_for_rand_exp_sampling_in_add_model(num_evals, cts_hp_bounds, \ dim, dscr_hp_vals, add_group_size_idx_in_dscr_hp_vals, tuning_objective): # IMPORTANT: We are assuming that the add_group_size is the first """ Samples the hyper-paramers for an additive model. """ agsidhp = add_group_size_idx_in_dscr_hp_vals sample_cts_hps = [] sample_dscr_hps = [] sample_other_gp_params = [] sample_obj_vals = [] for _ in range(num_evals): group_size = np.random.choice(dscr_hp_vals[agsidhp]) rand_perm = list(np.random.permutation(dim)) groupings = [ rand_perm[i:i + group_size] for i in range(0, dim, group_size) ] curr_other_gp_params = Namespace(add_gp_groupings=groupings) curr_dscr_hps = random_sample_from_discrete_domain(dscr_hp_vals) curr_dscr_hps[agsidhp] = group_size curr_cts_hps = map_to_bounds(np.random.random((len(cts_hp_bounds), )), cts_hp_bounds) curr_obj_val = tuning_objective(curr_cts_hps, curr_dscr_hps, curr_other_gp_params) # Now add to the lists sample_cts_hps.append(curr_cts_hps) sample_dscr_hps.append(curr_dscr_hps) sample_other_gp_params.append(curr_other_gp_params) sample_obj_vals.append(curr_obj_val) sample_probs = np.exp(sample_obj_vals) sample_probs = sample_probs / sample_probs.sum() return sample_cts_hps, sample_dscr_hps, sample_other_gp_params, sample_probs
def random_sample(obj, bounds, max_evals, vectorised=True): """ Optimises a function by randomly sampling and choosing its maximum. """ dim = len(bounds) rand_pts = map_to_bounds(np.random.random((int(max_evals), dim)), bounds) if vectorised: obj_vals = obj(rand_pts) else: obj_vals = np.array([obj(x) for x in rand_pts]) return rand_pts, obj_vals
def test_mapping_to_cube_and_bound(self): """ Test map_to_cube and map_to_bounds. """ self.report('map_to_cube and map_to_bounds') bounds = np.array([[1, 3], [2, 4], [5, 6]]) x = np.array([1.7, 3.1, 5.5]) X = np.array([[1.7, 3.1, 5.5], [2.1, 2.9, 5.0]]) y = np.array([0.35, 0.55, 0.5]) Y = np.array([[0.35, 0.55, 0.5], [0.55, 0.45, 0]]) # Map to cube y_ = general_utils.map_to_cube(x, bounds) Y_ = general_utils.map_to_cube(X, bounds) # Map to Bounds x_ = general_utils.map_to_bounds(y, bounds) X_ = general_utils.map_to_bounds(Y, bounds) # Check if correct. assert np.linalg.norm(y - y_) < 1e-5 assert np.linalg.norm(Y - Y_) < 1e-5 assert np.linalg.norm(x - x_) < 1e-5 assert np.linalg.norm(X - X_) < 1e-5
def get_euclidean_initial_points(init_method, num_samples, domain_bounds): """ Gets the initial set of points for a Euclidean space depending on the init_method. """ dim = len(domain_bounds) if init_method == 'rand': ret = random_sampling_cts(dim, num_samples) elif init_method == 'rand_kmeans': ret = random_sampling_kmeans_cts(dim, num_samples) elif init_method == 'latin_hc': ret = latin_hc_sampling(dim, num_samples) else: raise ValueError('Unknown init method %s.' % (init_method)) return map_to_bounds(ret, domain_bounds)
def random_sample_cts_dscr(obj, cts_bounds, dscr_vals, max_evals, vectorised=True): """ Sample from a joint continuous and discrete space. """ dim = len(cts_bounds) cts_rand_pts = map_to_bounds(np.random.random((int(max_evals), dim)), cts_bounds) dscr_rand_pts = random_sample_from_discrete_domain(dscr_vals, max_evals) if vectorised: obj_vals = obj(cts_rand_pts, dscr_rand_pts) else: obj_vals = np.array( [obj(cx, dx) for (cx, dx) in zip(cts_rand_pts, dscr_rand_pts)]) return cts_rand_pts, dscr_rand_pts, obj_vals
def perform_initial_queries(self): """ Perform initial queries. """ # If we already have some pre_eval points then do this. if (hasattr(self.options, 'pre_eval_points') and isinstance(self.options.pre_eval_points, np.ndarray)): self.pre_eval_vals = self.options.pre_eval_vals self.pre_eval_points = self.options.pre_eval_points self.curr_opt_val = self.pre_eval_vals.max() self.curr_opt_pt = self.pre_eval_points[ self.pre_eval_vals.argmax()] if self.options.pre_eval_true_vals is not None: self.pre_eval_true_vals = self.options.pre_eval_true_vals self.curr_true_opt_val = self.pre_eval_true_vals.max() self.curr_true_opt_pt = self.pre_eval_points[ self.pre_eval_true_vals.argmax()] return if self.options.num_init_evals <= 0: num_init_evals = min(6 * self.domain_dim, 25) else: num_init_evals = int(self.options.num_init_evals) num_init_evals = max(self.num_workers, num_init_evals) # Get Initial points in unit cube. if self.options.init_method == 'latin': init_points = gpb_utils.latin_hc_sampling(self.domain_dim, num_init_evals) elif self.options.init_method == 'random': init_points = gpb_utils.random_sampling(self.domain_dim, num_init_evals) elif self.options.init_method == 'randomkmeans': init_points = gpb_utils.random_sampling_kmeans( self.domain_dim, num_init_evals) else: raise ValueError('Unknonw init method: %s.' % (self.options.init_method)) # Map them to domain_bounds init_points = map_to_bounds(init_points, self.domain_bounds) for init_step in range(num_init_evals): self.step_idx += 1 self._wait_for_a_free_worker() self._dispatch_single_evaluation_to_worker_manager( init_points[init_step])
def _child_get_candidate_fidels(self, domain_point, filter_by_cost=True, *args, **kwargs): """ Returns candidate fidelities at domain_point. If filter_by_cost is true returns only those for which cost is larger than fidel_to_opt. """ if self.fidel_space.dim == 1: norm_candidates = np.linspace(0, 1, 100).reshape((-1, 1)) elif self.fidel_space.dim == 2: num_per_dim = 25 norm_candidates = (np.indices((num_per_dim, num_per_dim)).reshape(2, -1).T + 0.5) \ / float(num_per_dim) elif self.fidel_space.dim == 3: num_per_dim = 10 cand_1 = (np.indices( (num_per_dim, num_per_dim, num_per_dim)).reshape(3, -1).T + 0.5) / float(num_per_dim) cand_2 = np.random.random((1000, self.fidel_space.dim)) norm_candidates = np.vstack((cand_1, cand_2)) else: norm_candidates = np.random.random((4000, self.fidel_space.dim)) # Now unnormalise if necessary if self.domain_is_normalised: candidates = norm_candidates else: candidates = map_to_bounds(candidates, self.fidel_space.bounds) if filter_by_cost: fidel_costs = self.cost_multiple(candidates) filtered_idxs = np.where( np.array(fidel_costs) < self.cost_single(self.fidel_to_opt))[0] candidates = candidates[filtered_idxs, :] # Finally, always add the highest fidelity candidates = list(candidates) candidates.append(self.fidel_to_opt) return candidates
def get_unnormalised_coords(self, Z, X): """ Maps points in the cube to the original space. """ ret_X = None if X is None else map_to_bounds(X, self.domain_bounds) ret_Z = None if Z is None else map_to_bounds(Z, self.fidel_bounds) return ret_Z, ret_X
def get_raw_domain_coords(self, X): """ Maps points from the domain cube to the original space. """ if self.domain_is_normalised: return map_to_bounds(X, self.raw_domain.bounds) else: return X
def get_raw_fidel_coords(self, Z): """ Maps points from the fidelity space cube to the original space. """ if self.domain_is_normalised: return map_to_bounds(Z, self.raw_fidel_space.bounds) else: return Z
def get_unnormalised_coords(self, X): """ Maps points in the unit cube to the orignal space. """ return map_to_bounds(X, self.true_dom_bounds)
def _determine_next_query(self): """ Determines the next query. """ qinfo = Namespace(point=map_to_bounds( np.random.random(self.domain.dim), self.domain.bounds)) return qinfo