def test_direct_times(self): """ Tests the running time of the package with and without file writing. """ self.report('DiRect running times') log_file_names = ['', 'test_log'] for prob in self.problems: clock_times = [] real_times = [] for log_file_name in log_file_names: start_clock = time.clock() start_real_time = time.time() _, _, _ = oper_utils.direct_ft_maximise(prob.obj, prob.bounds, self.max_evals, log_file_name=log_file_name) clock_time = time.clock() - start_clock real_time = time.time() - start_real_time clock_times.append(clock_time) real_times.append(real_time) if log_file_name: try: os.remove(log_file_name) except OSError: pass # Print results out result_str = ', '.join(['file: \'%s\': clk=%0.4f, real=%0.4f, #evals=%d'%( log_file_names[i], clock_times[i], real_times[i], self.max_evals) \ for i in range(len(log_file_names))]) self.report('%s:: %s'%(prob.descr, result_str), 'test_result')
def test_direct(self): """ Test direct optmisation.""" self.report('DiRect minimise and maximise:') num_min_successes = 0 num_max_successes = 0 for prob in self.problems: # First the minimimum min_val_soln, _, _ = oper_utils.direct_ft_minimise( prob.obj, prob.lb, prob.ub, self.max_evals) diff = abs(prob.min_val - min_val_soln) self.report(prob.descr + '(min):: True: %0.4f, Soln: %0.4f, diff: %0.4f.' % (prob.min_val, min_val_soln, diff)) min_is_successful = 1 num_min_successes += min_is_successful # Now the maximum max_val_soln, _, _ = oper_utils.direct_ft_maximise( prob.obj, prob.lb, prob.ub, self.max_evals) diff = abs(prob.max_val - max_val_soln) self.report( prob.descr + '(max):: True: %0.4f, Soln: %0.4f, diff: %0.4f' % (prob.max_val, max_val_soln, diff), 'test_result') max_is_successful = 1 num_max_successes += max_is_successful # Check if successful assert num_min_successes == len(self.problems) assert num_max_successes == len(self.problems)
def test_direct_with_history(self): """ Tests direct with history. """ self.report('DiRect with history.') for prob in self.problems: min_val_soln, _, history = oper_utils.direct_ft_maximise(prob.obj, prob.bounds, \ self.max_evals, return_history=True) if history is None: assert is_nondecreasing(history.curr_opt_vals) assert np.abs(min_val_soln - history.curr_opt_vals[-1]) < 1e-4
def _direct_maximise_obj(self, obj, num_evals): """ Maximise with direct. """ if num_evals is None: lead_const = 10 * min(5, self.dim)**2 num_evals = lambda t: np.clip(lead_const * np.sqrt(min(t, 1000)), 2000, 3e4) lb = self.bounds[:, 0] ub = self.bounds[:, 1] opt_val, opt_pt, _ = direct_ft_maximise(obj, lb, ub, num_evals) return opt_val, opt_pt
def _direct_wrap(*args): """ A wrapper so as to only return the optimal point. """ _, opt_pt, _ = direct_ft_maximise(*args) return opt_pt