def solve(self, termination_condition=MaxIterTerminationCondition(DEF_MAX_ITER), snapshot_rate=1): ''' Solves for the maximal / minimal point ''' pass
def test_gaussian_uniform_alloc(self, num_candidates=25): # get candidates np.random.seed(1000) pred_means = np.random.rand(num_candidates) pred_covs = np.random.rand(num_candidates) candidates = [] for i in range(num_candidates): candidates.append(GaussianRV(pred_means[i], pred_covs[i])) # get true maximum true_max = np.max(pred_means) true_max_indices = np.where(pred_means == true_max) # solve using uniform allocation obj = RandomContinuousObjective() ua = GaussianUniformAllocationMean(obj, candidates) result = ua.solve( termination_condition=MaxIterTerminationCondition(MAX_ITERS), snapshot_rate=SNAPSHOT_RATE) # check result (not guaranteed to work in finite iterations but whatever) self.assertTrue(len(result.best_candidates) == 1) self.assertTrue(np.abs(result.best_candidates[0].mu - true_max) < 1e-4) self.assertTrue(result.best_pred_ind[-1] == true_max_indices[0])
def test_thompson_sampling(self, num_candidates=NUM_CANDIDATES): # get candidates np.random.seed(1000) pred_means = np.random.rand(num_candidates) candidates = [] for i in range(num_candidates): candidates.append(BernoulliRV(pred_means[i])) # get true maximum true_max = np.max(pred_means) true_max_indices = np.where(pred_means == true_max) # solve using uniform allocation obj = RandomBinaryObjective() ts = ThompsonSampling(obj, candidates) result = ts.solve( termination_condition=MaxIterTerminationCondition(MAX_ITERS), snapshot_rate=SNAPSHOT_RATE) # check result (not guaranteed to work in finite iterations but whatever) self.assertTrue(len(result.best_candidates) == 1) self.assertTrue(np.abs(result.best_candidates[0].p - true_max) < 1e-4) self.assertTrue(result.best_pred_ind[-1] == true_max_indices[0])
def top_K_solve(self, K, termination_condition = MaxIterTerminationCondition(DEF_MAX_ITER), snapshot_rate = 1): """ Solves for the top K maximal / minimal points """ # partition the input space if K == 1: candidate_bins = [self.candidates_] else: candidate_bins = self.partition(K) # maximize over each bin top_K_results = [] for k in range(K): top_K_results.append(self.discrete_maximize(candidate_bins[k], termination_condition, snapshot_rate)) return top_K_results
def expected_quality(grasp_rv, graspable_rv, params_rv, quality_config): """ Compute robustness, or the expected grasp quality wrt given random variables. Parameters ---------- grasp_rv : :obj:`ParallelJawGraspPoseGaussianRV` random variable for gripper pose obj_rv : :obj:`GraspableObjectPoseGaussianRV` random variable for object pose params_rv : :obj:`ParamsGaussianRV` random variable for a set of grasp quality parameters quality_config : :obj:`GraspQualityConfig` parameters for grasp quality computation Returns ------- float mean quality float variance of quality samples """ # set up random variable q_rv = QuasiStaticGraspQualityRV(grasp_rv, graspable_rv, params_rv, quality_config) candidates = [q_rv] # brute force with uniform allocation snapshot_rate = quality_config['sampling_snapshot_rate'] num_samples = quality_config['num_quality_samples'] objective = RandomContinuousObjective() ua = GaussianUniformAllocationMean(objective, candidates) ua_result = ua.solve( termination_condition=MaxIterTerminationCondition(num_samples), snapshot_rate=snapshot_rate) # convert to estimated prob success final_model = ua_result.models[-1] mn_q = final_model.means std_q = final_model.sample_vars return mn_q[0], std_q[0]
def discrete_maximize( self, candidates, termination_condition=MaxIterTerminationCondition(DEF_MAX_ITER), snapshot_rate=1): """ Maximizes a function over a discrete set of variables by iteratively predicting the best point (using some model and policy). Parameters --------- candidates : list of arbitrary objects that can be evaluted by the objective the list of candidates to optimize over termination_condition : :obj:`TerminationCondition` called on each iteration to determine whether or not to terminate snapshot_rate : int how often to store the state of the optimizer Returns ------ result : :obj:`AdaptiveSamplingResult` the result of the optimization """ # check input if len(candidates) == 0: raise ValueError('No candidates specified') if not isinstance(self.model_, DiscreteModel): logging.error('Illegal model specified') raise ValueError( 'Illegitimate model used in DiscreteAdaptiveSampler') # init vars terminate = False k = 0 # cur iter num_candidates = len(candidates) self.reset_model(candidates) # update model with new candidates # logging times = [] iters = [] iter_indices = [] iter_vals = [] iter_models = [] start_time = time.clock() next_ind_val = 0 while not terminate: # get next point to sample next_ind = self.selection_policy_.choose_next() # evaluate the function at the given point (can be nondeterministic) prev_ind_val = next_ind_val next_ind_val = self.objective_.evaluate(candidates[next_ind]) # snapshot the model and whatnot if (k % snapshot_rate) == 0: logging.debug('Iteration %d' % (k)) # log time and stuff checkpt = time.clock() times.append(checkpt - start_time) iters.append(k) iter_indices.append(next_ind) iter_vals.append(next_ind_val) iter_models.append(self.model_.snapshot()) # update the model (e.g. posterior update, grasp pruning) self.model_.update(next_ind, next_ind_val) # check termination condiation k = k + 1 terminate = termination_condition(k, cur_val=next_ind_val, prev_val=prev_ind_val, model=self.model_) # log final values checkpt = time.clock() times.append(checkpt - start_time) iters.append(k) iter_indices.append(next_ind) iter_vals.append(next_ind_val) iter_models.append(self.model_.snapshot()) # log total runtime end_time = time.clock() total_duration = end_time - start_time # log results and return best_indices, best_pred_means, best_pred_vars = self.model_.max_prediction( ) best_candidates = [] num_best = best_indices.shape[0] for i in range(num_best): best_candidates.append(candidates[best_indices[i]]) return AdaptiveSamplingResult(best_candidates, best_pred_means, best_pred_vars, total_duration, times, iters, iter_indices, iter_vals, iter_models)
def solve(self, termination_condition=MaxIterTerminationCondition(DEF_MAX_ITER), snapshot_rate=1): """ Call discrete maxmization function with all candidates """ return self.discrete_maximize(self.candidates_, termination_condition, snapshot_rate)