def launch_cmaes_pure(center, sigma, nbeval=10000, display=True, ma_func=myEvalFn.sphere): res = purecma.fmin(ma_func, center, sigma, maxfevals=nbeval) return res[1].result[1]
def launch_cmaes_pure_nn(center, sigma, nbeval=10000, display=True, ma_func=my_Function): res = 0 aux = center nbiter = 0 unit_eval = 100 while(res != -500)and nbiter < nbeval/unit_eval: print(-res) aux,cma = purecma.fmin(ma_func,aux,sigma,maxfevals=unit_eval,verb_disp=0) res = cma.best.f nbiter += 1 return -res,aux
def compare(self, func, mu_0, min_f, choice_prior, verbose): tol1 = 1e-30 tol2 = 1e-30 dim = len(mu_0) mu_0 = np.array(mu_0) n = 4 + int(3 * log(dim)) # mu_0 = np.array([400,400]) k_0 = 4 v_0 = n + 2 factor = 1 psi = np.eye(dim) list_param = [mu_0, k_0, v_0, psi, factor] np.random.seed(3) list_x_star_1, val1 = self.compute_min(list_param, n, tol1, tol2, func, 'StrategyOne', choice_prior, verbose) np.random.seed(3) list_x_star_2, val2 = self.compute_min(list_param, n, tol1, tol2, func, 'StrategyTwo', choice_prior, verbose) random.seed(3) res = pcma.fmin(func, mu_0, 1, verb_disp=1000) data = res[1].logger._data cma_esvalues = [f for f in data['fit']] cma_esvalues = cma_esvalues[1:] best_seen = cma_esvalues[0] cma_bestvalues = [] for elem in cma_esvalues: if elem < best_seen: cma_bestvalues.append(elem) best_seen = elem else: cma_bestvalues.append(best_seen) val1 = np.abs([i - min_f for i in val1]) val2 = np.abs([i - min_f for i in val2]) cma_bestvalues = np.abs([i - min_f for i in cma_bestvalues]) plt.figure(figsize=(6, 4.5)) plt.semilogy(np.arange(len(val2)), val2, label='B-CMA-ES S2') plt.semilogy(val1, label='B-CMA-ES S1') plt.semilogy(cma_bestvalues, label='CMAES') plt.title(func.__name__ + ' convergence using ' + choice_prior + ' prior') plt.xlabel('iteration steps') plt.ylabel('error') plt.legend() plt.savefig(self.path_plot + '\\convergence_' + func.__name__ + '_' + choice_prior + '.png') plt.show()
[(x) for x in zip(data_test_s, label_test)]) # ------Readout--------------- states_train, states_test, _label_train, _label_test = [], [], [], [] for train in states_train_list: states_train.append(train[0]) _label_train.append(train[1]) for test in states_test_list: states_test.append(test[0]) _label_test.append(test[1]) states_train = (MinMaxScaler().fit_transform(np.asarray(states_train))).T states_test = (MinMaxScaler().fit_transform(np.asarray(states_test))).T score_train, score_test = readout.readout_sk(states_train, states_test, np.asarray(_label_train), np.asarray(_label_test), solver="lbfgs", multi_class="multinomial") # ----------show results----------- print('parameters %s' % parameter) print('Train score: ', score_train) print('Test score: ', score_test) return 1 - score_test ########################################## # -------CMA-ES parameters search--------------- if __name__ == '__main__': core = 10 pool = Pool(core) res = purecma.fmin(parameters_search, [0.5, 0.5, 0.5], 1, verb_disp=100)
def launch_cmaes_pure(center, sigma, nbeval=10000, display=True, ma_func=sphere): aux = purecma.fmin(ma_func,center,sigma,maxfevals=nbeval)[0] return ma_func(aux)
from brian2 import * from multiprocessing import Pool from cma import purecma def aaa(inputs): N = 1 print(inputs, id(N)) return inputs + 1 def parameters_search(parameter): # ---- check parameters >0 ----- if (np.array(parameter) < 0).any(): return np.random.randint(10, 100) states_train_list = pool.map(aaa, [1, 2, 3, 4, 5]) print(states_train_list) return np.random.randint(5, 100) ########################################## # -------CMA-ES parameters search--------------- if __name__ == '__main__': core = 2 pool = Pool(core) res = purecma.fmin(parameters_search, [30, 1, 1], 1, verb_disp=100)
def compare(func, mu_0, min_f, nb_seed, save_fig): multivariate = True tol = 0.005 # CMAES standard population size n = 4 + int(3 * math.log(len(mu_0))) k_0 = 4 v_0 = n + 2 factor = 1 psi = np.eye(2) list_param = [mu_0, k_0, v_0, psi, factor] Function_evaluations = np.zeros(0) bcma_v = np.zeros(0) cma_v = np.zeros(0) iteration_max = 500 for s in range(nb_seed): np.random.seed(s * 3) list_x_star, values = compute_min(list_param, n, tol, multivariate, func) bcma_bestvalues = [] for i in range(0, len(values), n): bcma_bestvalues.append(values[i] - min_f) if i < len(values) - 1: bcma_bestvalues.append(values[-1] - min_f) random.seed(s) res = pcma.fmin(func, [mu_0[0], mu_0[1]], 1, verb_disp=0) data = res[1].logger._data cma_esvalues = [f for f in data['fit']] cma_esvalues = cma_esvalues[1:] best_seen = cma_esvalues[0] cma_bestvalues = [] for elem in cma_esvalues: if elem < best_seen: cma_bestvalues.append(elem) best_seen = elem else: cma_bestvalues.append(best_seen) cma_bestvalues = np.abs([i - min_f for i in cma_bestvalues]) max_length = max(min(len(bcma_bestvalues), len(cma_bestvalues)), 30) for i in range(max_length): Function_evaluations = np.append(Function_evaluations, i) cma_v = np.append(cma_v, cma_bestvalues[i] \ if i < len(cma_bestvalues) else cma_v[-1]) bcma_v = np.append(bcma_v, bcma_bestvalues[i] \ if i < len(bcma_bestvalues) else bcma_v[-1]) if max_length < iteration_max: iteration_max = max_length df1 = pd.DataFrame() df2 = pd.DataFrame() df1['Function_evaluations'] = Function_evaluations df1['Method'] = ['CMA-ES'] * len(Function_evaluations) df2['Function_evaluations'] = Function_evaluations df2['Method'] = ['BCMA-ES'] * len(Function_evaluations) df1['Error'] = cma_v df2['Error'] = bcma_v df = pd.concat([df1, df2], axis=0) df = df[df['Function_evaluations'] <= iteration_max] f, ax = plt.subplots(figsize=(8, 5.2)) ax.set(yscale="log") if not save_fig: plt.title('Convergence comparison between CMA-ES and BCMA-ES\n' + func.__name__) sns.lineplot(x="Function_evaluations", y="Error", data=df, hue="Method") if save_fig: print(f'saved figure {func.__name__}_convergence.png') plt.savefig(f'{func.__name__}_convergence.png') plt.show() return df
from brian2 import * from multiprocessing import Pool from cma import purecma def aaa(inputs): N = 1 print(inputs, id(N)) return inputs+1 def parameters_search(parameter): # ---- check parameters >0 ----- if (np.array(parameter)<0).any(): return np.random.randint(10,100) states_train_list = pool.map(aaa, [1,2,3,4,5]) print(states_train_list) return np.random.randint(5,100) ########################################## # -------CMA-ES parameters search--------------- if __name__ == '__main__': core = 2 pool = Pool(core) res = purecma.fmin(parameters_search, [30,1,1], 1, verb_disp=100)
def doPcma(): gaussHolder.reset() gaussHolder.indexLabel.configure(text="Gaussian 1") angles, _ = pcma.fmin(pc.operate, initialAngles, 0.1) arm.setAngles(angles) arm.draw()