def run_aposmm(sim_max): sim_specs = { 'sim_f': sim_f, 'in': ['x'], 'out': [('f', float), ('grad', float, ndim)] } gen_out = [('x', float, ndim), ('x_on_cube', float, ndim), ('sim_id', int), ('local_min', bool), ('local_pt', bool)] gen_specs = { 'gen_f': gen_f, 'in': [], 'out': gen_out, 'user': { 'initial_sample_size': 100, 'localopt_method': 'LD_MMA', # 'opt_return_codes': [0], # 'nu': 1e-6, # 'mu': 1e-6, 'xtol_rel': 1e-6, 'ftol_rel': 1e-6, # 'run_max_eval':10000, # 'dist_to_bound_multiple': 0.5, 'max_active_runs': 6, 'lb': self._bounds[:, 0], # This is only for sampling. TAO_NM doesn't honor constraints. 'ub': self._bounds[:, 1] } } alloc_specs = { 'alloc_f': alloc_f, 'out': [('given_back', bool)], 'user': {} } persis_info = add_unique_random_streams({}, nworkers + 1) exit_criteria = {'sim_max': sim_max} # Perform the run # H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info, # alloc_specs, libE_specs) return libE(sim_specs, gen_specs, exit_criteria, persis_info, alloc_specs, libE_specs)
} } alloc_specs = { 'alloc_f': give_sim_work_first, # Allocation function 'out': [('allocated', bool)], # Output fields (included in History) 'user': { 'stop_on_NaNs': True, # Should alloc preempt evals 'batch_mode': True, # Wait until all sim evals are done 'num_active_gens': 1, # Only allow one active generator 'stop_partial_fvec_eval': True } # Should alloc preempt evals } # end_alloc_specs_rst_tag persis_info = add_unique_random_streams(persis_info, nworkers + 1) persis_info_safe = deepcopy(persis_info) exit_criteria = {'sim_max': budget, 'elapsed_wallclock_time': 300} # Perform the run H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info, alloc_specs, libE_specs) if is_master: assert flag == 0 save_libE_output(H, persis_info, __file__, nworkers) # Perform the run but not stopping on NaNs alloc_specs['user'].pop('stop_on_NaNs') persis_info = deepcopy(persis_info_safe) H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info,
nworkers = 4 libE_specs = {'nworkers': nworkers, 'comms': 'local'} gen_specs = {'gen_f': gen_random_sample, # Our generator function 'out': [('x', float, (1,))], # gen_f output (name, type, size). 'user': {'lower': np.array([-3]), # random sampling lower bound 'upper': np.array([3]), # random sampling upper bound 'gen_batch_size': 5 # number of values gen_f will generate per call } } sim_specs = {'sim_f': sim_find_sine, # Our simulator function 'in': ['x'], # Input field names. 'x' from gen_f output 'out': [('y', float)]} # sim_f output. 'y' = sine('x') persis_info = add_unique_random_streams({}, nworkers+1) # Intitialize manager/workers random streams exit_criteria = {'sim_max': 80} # Stop libEnsemble after 80 simulations H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info, libE_specs=libE_specs) # Some (optional) statements to visualize our History array print([i for i in H.dtype.fields]) print(H) colors = ['b', 'g', 'r', 'y', 'm', 'c', 'k', 'w'] for i in range(1, nworkers + 1): worker_xy = np.extract(H['sim_worker'] == i, H) x = [entry.tolist()[0] for entry in worker_xy['x']]
def optimize(self): """Minimize the objective """ obj_val = self.obj_w.get_obj() num_parameters = self.obj_w.num_parameters lb = np.array([(l if l is not None else -2 * np.pi) for (l, u) in self.variable_bounds]) ub = np.array([(u if u is not None else 2 * np.pi) for (l, u) in self.variable_bounds]) def sim_func(H, gen_info, sim_specs, libE_info): del libE_info # Ignored parameter batch = len(H['x']) O = np.zeros(batch, dtype=sim_specs['out']) for i, x in enumerate(H['x']): O['f'][i] = obj_val(x) return O, gen_info script_name = os.path.splitext(os.path.basename(__file__))[0] #State the objective function, its arguments, output, and necessary parameters (and their sizes) sim_specs = { 'sim_f': sim_func, # This is the function whose output is being minimized 'in': ['x'], # These keys will be given to the above function 'out': [ ('f', float ), # This is the output from the function being minimized ], } gen_out = [ ('x', float, num_parameters), ('x_on_cube', float, num_parameters), ('sim_id', int), ('local_pt', bool), ('local_min', bool), ] np.random.seed(0) # State the generating function, its arguments, output, and necessary parameters. # This is the default dictionary, we update it below gen_specs = { 'gen_f': gen_f, 'in': [ 'x', 'f', 'local_pt', 'sim_id', 'returned', 'x_on_cube', 'local_min' ], #'mu':0.1, # limit on dist_to_bound: everything closer to bound than mu is thrown out 'out': gen_out, 'user': { 'lb': lb, 'ub': ub, 'initial_sample_size': nworkers - 1, # num points sampled before starting opt runs, one per worker 'localopt_method': self.optimizer_name, 'sample_points': np.atleast_2d(self.initial_point), 'run_max_eval': 100, 'num_pts_first_pass': nworkers - 1, 'max_active_runs': 2, 'periodic': self.obj_w.is_periodic, } } gen_specs['user'].update(self.gen_specs_user) persis_info = add_unique_random_streams({}, nworkers + 1) alloc_specs = { 'alloc_f': alloc_f, 'out': [('given_back', bool)], 'user': {} } H, persis_info, flag = libE(sim_specs, gen_specs, self.exit_criteria, persis_info=persis_info, libE_specs=libE_specs, alloc_specs=alloc_specs) if MPI.COMM_WORLD.Get_rank() == 0: self.res['num_optimizer_evals'] = len(H['f']) min_index = np.argmin(H['f']) self.res['min_val'] = H['f'][min_index] self.res['opt_params'] = H['x'][min_index] self.res['H'] = H self.res['persis_info'] = persis_info return self.res
def optimize_obj(obj_val, num_parameters, ub=None, lb=None, sim_max=None): def sim_func(H, gen_info, sim_specs, libE_info): del libE_info # Ignored parameter batch = len(H['x']) O = np.zeros(batch, dtype=sim_specs['out']) for i, x in enumerate(H['x']): O['f'][i] = obj_val(x) return O, gen_info script_name = os.path.splitext(os.path.basename(__file__))[0] #State the objective function, its arguments, output, and necessary parameters (and their sizes) sim_specs = { 'sim_f': sim_func, # This is the function whose output is being minimized 'in': ['x'], # These keys will be given to the above function 'out': [ ('f', float), # This is the output from the function being minimized ], } gen_out = [ ('x', float, num_parameters), ('x_on_cube', float, num_parameters), ('sim_id', int), ('local_pt', bool), ('local_min', bool), ] np.random.seed(0) # State the generating function, its arguments, output, and necessary parameters. gen_specs = { 'gen_f': gen_f, 'in': ['x', 'f', 'local_pt', 'sim_id', 'returned', 'x_on_cube', 'local_min'], #'mu':0.1, # limit on dist_to_bound: everything closer to bound than mu is thrown out 'out': gen_out, 'user': { 'lb': lb, 'ub': ub, 'initial_sample_size': 20, # num points sampled before starting opt runs, one per worker # 'localopt_method': 'scipy_COBYLA', # 'scipy_kwargs': {'tol': 1e-10, 'options': {'disp':True, 'maxiter': 100}}, 'localopt_method': 'LN_COBYLA', 'sample_points': np.atleast_2d(np.random.uniform(lb, ub, (20, len(lb)))), 'run_max_eval': 100, 'ftol_rel': 1e-10, 'xtol_rel': 1e-10, 'num_pts_first_pass': nworkers - 1, 'max_active_runs': 2, 'periodic': True, } } # Tell libEnsemble when to stop exit_criteria = {'sim_max': sim_max} persis_info = add_unique_random_streams({}, nworkers + 1) alloc_specs = { 'alloc_f': alloc_f, 'out': [('given_back', bool)], 'user': {} } H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info=persis_info, libE_specs=libE_specs, alloc_specs=alloc_specs) if MPI.COMM_WORLD.Get_rank() == 0: return (H, persis_info)
def _configure_persistant_info(self): # _configure_specs must have been already called self.persis_info = add_unique_random_streams({}, self.nworkers + 1)
libE_specs = {'nworkers': nworkers, 'comms': 'local'} gen_specs = { 'gen_f': gen_random_sample, # Our generator function 'out': [('x', float, (1, ))], # gen_f output (name, type, size) 'user': { 'lower': np.array([-3]), # lower boundary for random sampling 'upper': np.array([3]), # upper boundary for random sampling 'gen_batch_size': 5 # number of x's gen_f generates per call } } sim_specs = { 'sim_f': sim_find_sine, # Our simulator function 'in': ['x'], # Input field names. 'x' from gen_f output 'out': [('y', float)] } # sim_f output. 'y' = sine('x') persis_info = add_unique_random_streams({}, nworkers + 1) # Worker numbers start at 1 exit_criteria = {'sim_max': 80} # Stop libEnsemble after 80 simulations H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info, libE_specs=libE_specs) print([i for i in H.dtype.fields]) # (optional) to visualize our history array print(H)
def _configure_persistant_info(self): self.persis_info = add_unique_random_streams({}, self.nworkers + 1)