def __call__(self, function): scanner_options = { 'sade': dict(gen=self.gen, variant=self.variant, variant_adptv=self.variant_adptv, ftol=self.ftol, xtol=self.xtol, memory=self.memory, seed=self.seed), 'gaco': dict(gen=self.gen, ker=self.ker, q=self.q, oracle=self.oracle, acc=self.acc, threshold=self.threshold, n_gen_mark=self.n_gen_mark, impstop=self.impstop, evalstop=self.evalstop, focus=self.focus, memory=self.memory, seed=self.seed), 'maco': dict(gen=self.gen, ker=self.ker, q=self.q, threshold=self.threshold, n_gen_mark=self.n_gen_mark, evalstop=self.evalstop, focus=self.focus, memory=self.memory, seed=self.seed), 'gwo': dict(gen=self.gen, seed=self.seed), 'bee_colony': dict(gen=self.gen, limit=self.limit, seed=self.seed), 'de': dict(gen=self.gen, F=self.F, CR=self.CR, variant=self.variant, ftol=self.ftol, xtol=self.xtol, seed=self.seed), 'sea': dict(gen=self.gen, seed=self.seed), 'sga': dict(gen=self.gen, cr=self.cr, eta_c=self.eta_c, m=self.m, param_m=self.param_m, param_s=self.param_s, crossover=self.crossover, mutation=self.mutation, selection=self.selection, seed=self.seed), 'de1220': dict(gen=self.gen, allowed_variants=self.allowed_variants, variant_adptv=self.variant_adptv, ftol=self.ftol, xtol=self.xtol, memory=self.memory, seed=self.seed), 'cmaes': dict(gen=self.gen, cc=self.cc, cs=self.cs, c1=self.c1, cmu=self.cmu, sigma0=self.sigma0, ftol=self.ftol, xtol=self.xtol, memory=self.memory, force_bounds=self.force_bounds, seed=self.seed), 'moead': dict(gen=self.gen, weight_generation=self.weight_generation, decomposition=self.decomposition, neighbours=self.neighbours, CR=self.CR, F=self.F, eta_m=self.eta_m, realb=self.realb, limit=self.limit, preserve_diversity=self.preserve_diversity, seed=self.seed), 'compass_search': dict(max_fevals=self.max_fevals, start_range=self.start_range, stop_range=self.stop_range, reduction_coeff=self.reduction_coeff), 'simulated_annealing': dict(Ts=self.Ts, Tf=self.Tf, n_T_adj=self.n_T_adj, n_range_adj=self.n_range_adj, bin_size=self.bin_size, start_range=self.start_range, seed=self.seed), 'pso': dict(gen=self.gen, omega=self.omega, eta1=self.eta1, eta2=self.eta2, max_vel=self.max_vel, variant=self.variant, neighb_type=self.neighb_type, neighb_param=self.neighb_param, memory=self.memory, seed=self.seed), 'pso_gen': dict(gen=self.gen, omega=self.omega, eta1=self.eta1, eta2=self.eta2, max_vel=self.max_vel, variant=self.variant, neighb_type=self.neighb_type, neighb_param=self.neighb_param, memory=self.memory, seed=self.seed), 'nsga2': dict(gen=self.gen, cr=self.cr, eta_c=self.eta_c, m=self.m, eta_m=self.eta_m, seed=self.seed), 'nspso': dict(gen=self.gen, omega=self.omega, c1=self.c1, c2=self.c2, chi=self.chi, v_coeff=self.v_coeff, leader_selection_range=self.leader_selection_range, diversity_mechanism=self.diversity_mechanism, memory=self.memory, seed=self.seed), 'mbh': dict(algo=self.algo, stop=self.stop, perturb=self.perturb, seed=self.seed), 'cstrs_self_adaptive': dict(iters=self.iters, algo=self.algo, seed=self.seed), 'ihs': dict(gen=self.gen, phmcr=self.phmcr, ppar_min=self.ppar_min, ppar_max=self.ppar_max, bw_min=self.bw_min, bw_max=self.bw_max, seed=self.seed), 'xnes': dict(gen=self.gen, eta_mu=self.eta_mu, eta_sigma=self.eta_sigma, eta_b=self.eta_b, sigma0=self.sigma0, ftol=self.ftol, xtol=self.xtol, memory=self.memory, force_bounds=self.force_bounds, seed=self.seed) } if self.log_data: xl = [] yl = [] log_data = self.log_data # class interf_function: def __init__(self, dim): self.dim = dim def fitness(self, x): x = np.expand_dims(x, axis=0) y = function(x) # x = x[0] y = y.tolist() if log_data: xl.append(x) yl.append(y) # print (x, y[0]) return y[0] if function.is_differentiable(): def gradient(self, x): x = np.expand_dims(x, axis=0) g = function(x) g = g.tolist() return g[0] def get_bounds(self): lb = [] ub = [] bounds = function.get_ranges() # warning # check for infinities for i in range(len(bounds)): lb.append(bounds[i, 0]) ub.append(bounds[i, 1]) r = (np.array(lb), np.array(ub)) return r # I need to call pygmo functions directly prob = pg.problem(interf_function(function)) # print (prob.get_thread_safety()) if self.scanner == "sade": # I need a dictionary with algorithms and options algo = pg.algorithm(pg.sade(**scanner_options[self.scanner])) elif self.scanner == "gaco": algo = pg.algorithm(pg.gaco(**scanner_options[self.scanner])) # elif self.scanner == "maco": # is not implemented though in webpage # looks it is # algo = pg.algorithm(pg.maco(**scanner_options[self.scanner])) elif self.scanner == "gwo": algo = pg.algorithm(pg.gwo(**scanner_options[self.scanner])) elif self.scanner == "bee_colony": algo = pg.algorithm(pg.bee_colony(**scanner_options[self.scanner])) elif self.scanner == "de": algo = pg.algorithm(pg.de(**scanner_options[self.scanner])) elif self.scanner == "sea": algo = pg.algorithm(pg.sea(**scanner_options[self.scanner])) elif self.scanner == "sga": algo = pg.algorithm(pg.sga(**scanner_options[self.scanner])) elif self.scanner == "de1220": algo = pg.algorithm(pg.de1220(**scanner_options[self.scanner])) elif self.scanner == "cmaes": algo = pg.algorithm(pg.cmaes(**scanner_options[self.scanner])) # elif self.scanner == "moead": #multiobjective algorithm # algo = pg.algorithm(pg.moead(**scanner_options[self.scanner])) elif self.scanner == "compass_search": algo = pg.algorithm( pg.compass_search(**scanner_options[self.scanner])) elif self.scanner == 'simulated_annealing': algo = pg.algorithm( pg.simulated_annealing(**scanner_options[self.scanner])) elif self.scanner == 'pso': algo = pg.algorithm(pg.pso(**scanner_options[self.scanner])) elif self.scanner == 'pso_gen': algo = pg.algorithm(pg.pso_gen(**scanner_options[self.scanner])) # elif self.scanner == 'nsga2': #multiobjective algorithm # algo = pg.algorithm(pg.nsga2(**scanner_options[self.scanner])) # elif self.scanner == 'nspso': is not implemented though in webpage # looks it is # algo = pg.algorithm(pg.nspso(**scanner_options[self.scanner])) elif self.scanner == 'mbh': if scanner_options[self.scanner]['algo'] == 'de': algo = pg.algorithm( pg.mbh(pg.algorithm(pg.de(**scanner_options['de'])))) # elif self.scanner == 'ihs': #does not work # algo = pg.algorithm(ihs(**scanner_options[self.scanner])) # elif self.scanner == 'xnes': #does not work # algo = pg.algorithm(xnes(**scanner_options[self.scanner])) # uda = algo.extract(xnes) else: print( 'The ' + self.scanner + ' algorithm is not implemented. The ' 'list of algorithms available is', algorithms) sys.exit() # add verbosing flag if self.verbose > 1: algo.set_verbosity(self.verbose) pop = pg.population(prob, self.size) if self.verbose > 9: print('prob', prob) opt = algo.evolve(pop) if self.verbose > 9: print('algo', algo) # best_x = np.expand_dims(opt.champion_x, axis=0) # best_fitness = np.expand_dims(opt.get_f()[opt.best_idx()], axis=0) best_x = np.expand_dims(opt.champion_x, axis=0) best_fitness = np.expand_dims(opt.champion_f, axis=0) if self.verbose > 0: print('best fit:', best_x, best_fitness) if self.log_data: x = np.squeeze(xl, axis=(1, )) y = np.squeeze(yl, axis=(2, )) if self.log_data: return (x, y) else: return (best_x, best_fitness)
new_x = quantization_bins[x_ind] predict_prob = clf.predict_proba(new_x.reshape((1, -1))) else: predict_prob = clf.predict_proba(x.reshape((1, -1))) loss = - predict_prob[0][self.target] if print_extra: print (predict_prob[0]) return [loss] def get_bounds(self): return ([self.min_limit]*self.dim, [self.max_limit]*self.dim) if __name__ == "__main__": quantization = True for iteration in range(1): f, axarr = plt.subplots(2, 5, figsize=(13,5)) axarr = axarr.reshape((-1)) for target in range(10): prob = pg.problem(adverserial_example(dim=X_train.shape[1], target=target, quantization=quantization, min_limit=min_limit, max_limit=max_limit)) algo = pg.algorithm(pg.sea(gen = 1)) # algo = pg.algorithm(pg.sga(gen = 200)) pop = pg.population(prob,2) pop = algo.evolve(pop) axarr[target].imshow(pop.champion_x.reshape((int(np.sqrt(X_train.shape[1])), int(np.sqrt(X_train.shape[1]))))) axarr[target].set_title("Target: {}, Prob: {}".format(target, str(-np.round(pop.champion_f[0], 2)))) axarr[target].set_xticks([]) axarr[target].set_yticks([]) plt.savefig("{} - Data: {} - Quant: {} - Iteration: {}".format(algorithm.upper(), dataset_used, quantization, iteration)) plt.close()
assert (algorithm == 'knn') dist_adv, indices_adv = clf.kneighbors(X=[x], n_neighbors=5, return_distance=True) return dist_adv, indices_adv if __name__ == "__main__": quantization = True for iteration in range(1): f, axarr = plt.subplots(2, 5, figsize=(13,5)) axarr = axarr.reshape((-1)) for target in range(10): print ("#####################################################################################") print ("TARGET: ", target) print ("#####################################################################################") prob_instance = adverserial_example(dim=X_train.shape[1], target=target, quantization=quantization, min_limit=min_limit, max_limit=max_limit) prob = pg.problem(prob_instance) algo = pg.algorithm(pg.sea(gen = 5000)) # algo = pg.algorithm(pg.sga(gen = 1000)) pop = pg.population(prob,30) pop = algo.evolve(pop) print ("Best: {}".format(pop.champion_f[0])) prob_instance.fitness(pop.champion_x, print_extra=True) # print (prob_instance.evaluation(pop.champion_x)) axarr[target].imshow(pop.champion_x.reshape((int(np.sqrt(X_train.shape[1])), int(np.sqrt(X_train.shape[1]))))) axarr[target].set_title("Target: {}, Prob: {}".format(target, str(-np.round(pop.champion_f[0], 2)))) # axarr[target].set_title("Target: {}".format(target)) axarr[target].set_xticks([]) axarr[target].set_yticks([]) plt.savefig("{} - Data: {} - Quant: {} - Iteration: {}".format(algorithm.upper(), dataset_used, quantization, iteration)) plt.close() # plt.show()
arc.push_back(isl) for i in range(2): addIsland(arc=archi, algo=pg.algorithm( pg.cstrs_self_adaptive(iters=1000, algo=pg.de(gen=50)))) addIsland(arc=archi, algo=pg.algorithm( pg.cstrs_self_adaptive(iters=1000, algo=pg.pso(gen=50)))) addIsland(arc=archi, algo=pg.algorithm( pg.cstrs_self_adaptive(iters=1000, algo=pg.simulated_annealing()))) addIsland(arc=archi, algo=pg.algorithm( pg.cstrs_self_adaptive(iters=1000, algo=pg.sea(gen=50)))) bpool = ThreadPool(processes=3) def logger(): #while True: print(archi.get_champions_x()) print(sorted(list(map(lambda a: a[0], archi.get_champions_f())))) #print(archi.get_champions_f()) jx = pd.DataFrame(archi.get_champions_x()).to_json(orient='values') jf = pd.DataFrame(archi.get_champions_f()).to_json(orient='values') with open("jx.json", "w") as jxf: jxf.write(jx) with open("jf.json", "w") as jff: