'lengthscale_bounds': (1e-4, 1000.0) }, 'population_size': 200, 'num_generations': 200, 'termination_conditions': True, 'space': space, 'objective_names': objective_names, 'n_initial': 5, 'n_epochs': 4, 'save_surrogate_eval': True, 'save': True, 'file_path': 'results/zdt3.h5', 'resample_fraction': 1.00, } best = dmosopt.run(dmosopt_params, verbose=True) if best is not None: import matplotlib.pyplot as plt bestx, besty = best x, y = dmosopt.sopt_dict['dmosopt_zdt3'].optimizer_dict[0].get_evals() besty_dict = dict(besty) # plot results plt.plot(y[:, 0], y[:, 1], 'b.', label='Evaluated points') plt.plot(besty_dict['y1'], besty_dict['y2'], 'r.', label='Best solutions') y_true = zdt3_pareto() plt.plot(y_true[:, 0],
def main(config_path, target_features_path, target_features_namespace, optimize_file_dir, optimize_file_name, nprocs_per_worker, n_epochs, n_initial, initial_maxiter, initial_method, optimizer_method, population_size, num_generations, resample_fraction, mutation_rate, collective_mode, spawn_startup_wait, verbose): network_args = click.get_current_context().args network_config = {} for arg in network_args: kv = arg.split("=") if len(kv) > 1: k,v = kv network_config[k.replace('--', '').replace('-', '_')] = v else: k = kv[0] network_config[k.replace('--', '').replace('-', '_')] = True run_ts = datetime.datetime.today().strftime('%Y%m%d_%H%M') if optimize_file_name is None: optimize_file_name=f"dmosopt.optimize_network_{run_ts}.h5" operational_config = read_from_yaml(config_path) operational_config['run_ts'] = run_ts if target_features_path is not None: operational_config['target_features_path'] = target_features_path if target_features_namespace is not None: operational_config['target_features_namespace'] = target_features_namespace network_config.update(operational_config.get('kwargs', {})) env = Env(**network_config) objective_names = operational_config['objective_names'] param_config_name = operational_config['param_config_name'] target_populations = operational_config['target_populations'] opt_param_config = optimization_params(env.netclamp_config.optimize_parameters, target_populations, param_config_name) opt_targets = opt_param_config.opt_targets param_names = opt_param_config.param_names param_tuples = opt_param_config.param_tuples hyperprm_space = { param_pattern: [param_tuple.param_range[0], param_tuple.param_range[1]] for param_pattern, param_tuple in zip(param_names, param_tuples) } init_objfun = 'init_network_objfun' init_params = { 'operational_config': operational_config, 'opt_targets': opt_targets, 'param_tuples': [ param_tuple._asdict() for param_tuple in param_tuples ], 'param_names': param_names } init_params.update(network_config.items()) nworkers = env.comm.size-1 if resample_fraction is None: resample_fraction = float(nworkers) / float(population_size) if resample_fraction > 1.0: resample_fraction = 1.0 if resample_fraction < 0.1: resample_fraction = 0.1 # Create an optimizer feature_dtypes = [(feature_name, np.float32) for feature_name in objective_names] constraint_names = [f'{target_pop_name} positive rate' for target_pop_name in target_populations ] dmosopt_params = {'opt_id': 'dentate.optimize_network', 'obj_fun_init_name': init_objfun, 'obj_fun_init_module': 'dentate.optimize_network', 'obj_fun_init_args': init_params, 'reduce_fun_name': 'compute_objectives', 'reduce_fun_module': 'dentate.optimize_network', 'reduce_fun_args': (operational_config, opt_targets), 'problem_parameters': {}, 'space': hyperprm_space, 'objective_names': objective_names, 'feature_dtypes': feature_dtypes, 'constraint_names': constraint_names, 'n_initial': n_initial, 'initial_maxiter': initial_maxiter, 'initial_method': initial_method, 'optimizer': optimizer_method, 'n_epochs': n_epochs, 'population_size': population_size, 'num_generations': num_generations, 'resample_fraction': resample_fraction, 'mutation_rate': mutation_rate, 'file_path': f'{optimize_file_dir}/{optimize_file_name}', 'termination_conditions': True, 'save_surrogate_eval': True, 'save': True, 'save_eval': 5 } #dmosopt_params['broker_fun_name'] = 'dmosopt_broker_init' #dmosopt_params['broker_module_name'] = 'dentate.optimize_network' best = dmosopt.run(dmosopt_params, spawn_workers=True, sequential_spawn=False, spawn_startup_wait=spawn_startup_wait, nprocs_per_worker=nprocs_per_worker, collective_mode=collective_mode, verbose=True, worker_debug=True) if best is not None: if optimize_file_dir is not None: results_file_id = 'DG_optimize_network_%s' % run_ts yaml_file_path = '%s/optimize_network.%s.yaml' % (optimize_file_dir, str(results_file_id)) prms = best[0] prms_dict = dict(prms) n_res = prms[0][1].shape[0] results_config_dict = {} for i in range(n_res): result_param_list = [] for param_pattern, param_tuple in zip(param_names, param_tuples): result_param_list.append([param_pattern, float(prms_dict[param_pattern][i])]) results_config_dict[i] = result_param_list write_to_yaml(yaml_file_path, results_config_dict)
def optimize_run(env, population, param_config_name, selectivity_config_name, init_objfun, problem_regime, nprocs_per_worker=1, n_epochs=10, n_initial=30, initial_maxiter=50, initial_method="slh", optimizer_method="nsga2", surrogate_method='vgp', population_size=200, num_generations=200, resample_fraction=None, mutation_rate=None, param_type='synaptic', init_params={}, results_file=None, cooperative_init=False, spawn_startup_wait=None, verbose=False): opt_param_config = optimization_params(env.netclamp_config.optimize_parameters, [population], param_config_name, param_type) opt_targets = opt_param_config.opt_targets param_names = opt_param_config.param_names param_tuples = opt_param_config.param_tuples hyperprm_space = { param_pattern: [param_tuple.param_range[0], param_tuple.param_range[1]] for param_pattern, param_tuple in zip(param_names, param_tuples) } if results_file is None: if env.results_path is not None: file_path = f'{env.results_path}/dmosopt.optimize_selectivity.{env.results_file_id}.h5' else: file_path = f'dmosopt.optimize_selectivity.{env.results_file_id}.h5' else: file_path = '%s/%s' % (env.results_path, results_file) problem_ids = None reduce_fun_name = None if ProblemRegime[problem_regime] == ProblemRegime.every: reduce_fun_name = "opt_reduce_every" problem_ids = init_params.get('cell_index_set', None) elif ProblemRegime[problem_regime] == ProblemRegime.mean: reduce_fun_name = "opt_reduce_mean" elif ProblemRegime[problem_regime] == ProblemRegime.max: reduce_fun_name = "opt_reduce_max" else: raise RuntimeError(f'optimize_run: unknown problem regime {problem_regime}') n_trials = init_params.get('n_trials', 1) nworkers = env.comm.size-1 if resample_fraction is None: resample_fraction = float(nworkers) / float(population_size) if resample_fraction > 1.0: resample_fraction = 1.0 if resample_fraction < 0.1: resample_fraction = 0.1 objective_names = ['residual_infld', 'residual_state'] feature_names = ['mean_peak_rate', 'mean_trough_rate', 'max_infld_rate', 'min_infld_rate', 'mean_infld_rate', 'mean_outfld_rate', 'mean_peak_state', 'mean_trough_state', 'mean_outfld_state'] N_objectives = 2 feature_dtypes = [(feature_name, np.float32) for feature_name in feature_names] feature_dtypes.append(('trial_objs', np.float32, (N_objectives, n_trials))) feature_dtypes.append(('trial_mean_infld_rate', (np.float32, (1, n_trials)))) feature_dtypes.append(('trial_mean_outfld_rate', (np.float32, (1, n_trials)))) constraint_names = ['positive_rate'] dmosopt_params = {'opt_id': 'dentate.optimize_selectivity', 'problem_ids': problem_ids, 'obj_fun_init_name': init_objfun, 'obj_fun_init_module': 'dentate.optimize_selectivity', 'obj_fun_init_args': init_params, 'reduce_fun_name': reduce_fun_name, 'reduce_fun_module': 'dentate.optimization', 'problem_parameters': {}, 'space': hyperprm_space, 'objective_names': objective_names, 'feature_dtypes': feature_dtypes, 'constraint_names': constraint_names, 'n_initial': n_initial, 'n_epochs': n_epochs, 'population_size': population_size, 'num_generations': num_generations, 'resample_fraction': resample_fraction, 'mutation_rate': mutation_rate, 'initial_maxiter': initial_maxiter, 'initial_method': initial_method, 'optimizer': optimizer_method, 'surrogate_method': surrogate_method, 'file_path': file_path, 'save': True, 'save_eval' : 5, } opt_results = dmosopt.run(dmosopt_params, verbose=verbose, collective_mode="sendrecv", spawn_workers=True, nprocs_per_worker=nprocs_per_worker, spawn_startup_wait=spawn_startup_wait ) if opt_results is not None: if ProblemRegime[problem_regime] == ProblemRegime.every: gid_results_config_dict = {} for gid, opt_result in viewitems(opt_results): params_dict = dict(opt_result[0]) result_value = opt_result[1] results_config_tuples = [] for param_pattern, param_tuple in zip(param_names, param_tuples): results_config_tuples.append((param_pattern, params_dict[param_pattern])) gid_results_config_dict[int(gid)] = results_config_tuples logger.info('Optimized parameters and objective function: ' f'{pprint.pformat(gid_results_config_dict)} @' f'{result_value}') return gid_results_config_dict else: params_dict = dict(opt_results[0]) result_value = opt_results[1] results_config_tuples = [] for param_pattern, param_tuple in zip(param_names, param_tuples): results_config_tuples.append((param_pattern, params_dict[param_pattern])) logger.info('Optimized parameters and objective function: ' f'{pprint.pformat(results_config_tuples)} @' f'{result_value}') return results_config_tuples else: return None
'train_size': train_size, 'test_size': test_size, 'presentation_time': presentation_time, 'pause_time': pause_time, 'skip_time': skip_time, 'n_outputs': n_outputs, 'n_exc': n_exc, 'n_inh': n_inh, 'dt': dt } dmosopt_params = { 'opt_id': 'dmosopt_srf_autoenc_nordland', 'obj_fun_init_name': 'init_obj_fun', 'obj_fun_init_args': init_args, 'obj_fun_init_module': 'optimize_srf_autoenc_nordland', 'problem_parameters': problem_parameters, 'optimize': 'nsga2', 'space': space, 'n_iter': 10, 'surogate_method': 'siv', 'objective_names': objective_names, 'n_initial': 100, 'resample_fraction': 1.0, 'save': True, 'file_path': True, 'file_path': 'dmosopt.srf_autoenc_nordland.h5', } best = dmosopt.run(dmosopt_params, spawn_workers=False, verbose=True)