def pso(): # instatiate the optimizer x_max = np.ones(n - 1) x_min = np.zeros(n - 1) bounds = (x_min, x_max) options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} q = 10 # window length clusterCenterNumber = 5 optimizer = GlobalBestPSO(n_particles=10, dimensions=n - 1, options=options, bounds=bounds) best_q = q best_clusterCenterNumber = clusterCenterNumber best_f = -1 # for _ in range(0): # for _ in range(0): # fcm = fuzzyCmeansClustering(q, clusterCenterNumber) # _, _ = optimizer.optimize(fcm.fuzzyCMeansClustering, 10) # anomaly_score_per_win = fcm.anomaly_score_per_win # f = max(anomaly_score_per_win) / np.mean(anomaly_score_per_win) # if f > best_f: # best_q = q # best_clusterCenterNumber = clusterCenterNumber # best_f = f # clusterCenterNumber += 1 # q += 1 fcm = fuzzyCmeansClustering(best_q, best_clusterCenterNumber) cost, pos = optimizer.optimize(fcm.fuzzyCMeansClustering, 3) # _ = fcm.anomaly_score_per_win # anomaly_score = fcm.cal_anomaly() anomaly_score = fcm.anomaly_score_per_win return best_q, best_clusterCenterNumber, anomaly_score
def pso_wrapper(seed_size, num_iters, batch_size): obj_func = gpyopt_objective d = 5 x_min = np.array([10**-5, 1, 1, 2, 1]) x_max = np.array([1, 5, n_features - 1, 99, 99]) bounds = (x_min, x_max) # Creating dicts to store pos_dict = {} cost_dict = {} for seed_i in range(seed_size): np.random.seed(seed_i) """ # Generating initial samples init_pos = np.random.random((initialsamplesize, d)) print(init_pos) """ # Running optimisation optimizer = GlobalBestPSO(n_particles=batch_size, dimensions=d, options=options, bounds=bounds, init_pos=None) best_cost, best_pos = optimizer.optimize(obj_func, num_iters + 1, seed_i) # Recording results pos_hist = optimizer.pos_history cost_hist = optimizer.cost_history pos_dict[seed_i] = pos_hist cost_dict[seed_i] = np.transpose(np.array([cost_hist])) return pos_dict, cost_dict
def swarm(self): circuit_swarm = GlobalBestPSO(n_particles=self.particles, dimensions=self.dimensions, options=self.options, bounds=self.bounds) cost, pos = circuit_swarm.optimize(self.swarm_cost, iters=self.loops, n_processes=self.processes) plot_cost_history(circuit_swarm.cost_history) plt.show() return cost, pos
def pso(c1, c2, w, k=2, p=2, n_particles=100, epochs=100, mode="gbest", verbose=0, visualize=0): options = {'c1': c1, 'c2': c2, 'w': w} # bound max_value = 1.0 * np.ones(DIMENSIONS) min_value = -1.0 * np.ones(DIMENSIONS) bounds = (min_value, max_value) # create pso optimizer if mode == "gbest": from pyswarms.single.global_best import GlobalBestPSO optimizer = GlobalBestPSO(n_particles=n_particles, dimensions=DIMENSIONS, options=options, bounds=bounds) elif mode == "lbest": from pyswarms.single.local_best import LocalBestPSO options.update({"k": k, "p": p}) optimizer = LocalBestPSO(n_particles=n_particles, dimensions=DIMENSIONS, options=options, bounds=bounds) else: raise NotImplementedError("Mode %s not support." % mode) # optimize cost, params = optimizer.optimize(f, epochs, verbose=verbose) if verbose: print("Accuracy: ", (predict(X_test, params) == Y_test).mean()) if visualize: import matplotlib.pyplot as plt from pyswarms.utils.plotters import plot_cost_history plot_cost_history(cost_history=optimizer.cost_history) plt.show() # for bayesian optimization return -cost
def pso_train(self, bounds, n_particles=10, options=None, iters=1000): """返回优化后的 cost=cwc_best, pos=(gamma_u, C_u, gamma_l, C_l), optimizer(pso优化器) :param bounds: 参数边界 :param n_particles: 粒子数 :param options: 粒子更新选项 :param iters: 迭代次数 :return: """ if options is None: options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} optimizer = GlobalBestPSO(n_particles=n_particles, dimensions=4, options=options, bounds=bounds) cost, pos = optimizer.optimize(self.pso_fitf, iters=iters) return cost, pos, optimizer
def pso(ubem, all_buildings, betas, Ec, n_particles=25, iters=300, global_pso=True): start = timeit.default_timer() num_buildings = all_buildings.shape[0] mc_simulations = len(betas) lb, ub = np.repeat(0, num_buildings), np.repeat(mc_simulations - 1, num_buildings) bounds = (lb, ub) # Use Global or Local optimizer if global_pso: options = { 'c1': 0.5, 'c2': 0.1, 'w': 0.1 } # options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} optimizer = GlobalBestPSO(n_particles=n_particles, dimensions=num_buildings, options=options, bounds=bounds) else: options = {'c1': 0.5, 'c2': 0.1, 'w': 0.1, 'k': 3, 'p': 1} optimizer = LocalBestPSO(n_particles=n_particles, dimensions=num_buildings, options=options, bounds=bounds) kwargs = { 'all_buildings': all_buildings, 'betas': np.array(betas), 'Ec': Ec, 'ubem': ubem } cost, pos = optimizer.optimize(objective_function, iters=iters, **kwargs) stop = timeit.default_timer() print( '--- Total time for PSO with ' + str(n_particles) + 'particles and ' + str(iters) + ' loops: ', stop - start) return (cost, pos)
def particleSwarmOptimization(request, nStudents): # hyperparameters and bounds x_max = 1 * np.ones(4) x_min = 0 * x_max bounds = (x_min, x_max) # instatiate the optimizer options = {'c1': .5, 'c2': .6, 'w': .8} optimizer = GlobalBestPSO(n_particles=10, dimensions=4, options=options, bounds=bounds) # now run the optimization bestcost, pos = optimizer.optimize(cost, 100, nStudents=nStudents) return pos
def pso_wrapper(seed_size, num_iters, batch_size): obj_func = cifar_utils.cifar_cnn_gpyopt d = 6 x_min = np.zeros((1, 6)).flatten() x_max = np.ones((1, 6)).flatten() bounds = (x_min, x_max) # Creating dicts to store pos_dict = {} cost_dict = {} for seed_i in range(seed_size): np.random.seed(seed_i) set_random_seed(seed_i) # Generating initial samples init_pos = np.random.random((batch_size, d)) init_pos[0] = x_init_dict[seed_i][0] init_pos[1] = x_init_dict[seed_i][1] if batch_size > 2: init_pos[2] = x_init_dict[seed_i][2] # Running optimisation_ optimizer = GlobalBestPSO(n_particles=batch_size, dimensions=d, options=options, bounds=bounds, init_pos=init_pos) best_cost, best_pos = optimizer.optimize(obj_func, num_iters + 1, seed_i) # Recording results pos_hist = optimizer.pos_history cost_hist = optimizer.cost_history pos_dict[seed_i] = pos_hist cost_dict[seed_i] = np.transpose(np.array([cost_hist])) return pos_dict, cost_dict
def fit(self, x,y , bound = None): ''' x = dias passados do dia inicial 1 y = numero de casos bound = intervalo de limite para procura de cada parametro, onde None = sem limite bound => (lista_min_bound, lista_max_bound) ''' df = np.array(y) options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} if bound==None: optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options=options) cost, pos = optimizer.optimize(self.__objectiveFunction, 1000, x = x,y=df) self.beta = pos[0] self.gamma = pos[1] self.x = x self.y = df self.rmse = cost self.optimize = optimizer else: optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options=options,bounds=bound) cost, pos = optimizer.optimize(self.__objectiveFunction, 1000, x = x,y=df) self.beta = pos[0] self.gamma = pos[1] self.x = x self.y = df self.rmse = cost self.optimize = optimizer
def particleSwarmOptimization(request, nStudents, excelData, isTransfer): beta = 0.35 if isTransfer else 0.125 # The following values represent the upper and lower bounds x_max = np.array([0.035, beta, 0.0005, 1]) x_min = np.array([0.01, 0.116, 0, 0]) bounds = (x_min, x_max) # instatiate the optimizer options = {'c1': .5, 'c2': .6, 'w': .8} # options = {'c1': 1, 'c2': 1, 'w': 1} optimizer = GlobalBestPSO(n_particles=10, dimensions=4, options=options, bounds=bounds) # now run the optimization bestcost, pos = optimizer.optimize(cost, 100, nStudents=nStudents, excelData=excelData) return pos
def fit(self, x, y, bound=None, name=''): ''' x = dias passados do dia inicial 1 y = numero de casos bound = intervalo de limite para procura de cada parametro, onde None = sem limite bound => (lista_min_bound, lista_max_bound) ''' df = np.array(y) options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} if bound == None: optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options=options) cost, pos = optimizer.optimize(self.__objectiveFunction, 1000, x=x, y=df) self.a = pos[0] self.b = pos[1] self.x = x self.y = df self.rmse = cost self.optimize = optimizer else: optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options=options, bounds=bound) cost, pos = optimizer.optimize(self.__objectiveFunction, 1000, x=x, y=df) self.a = pos[0] self.b = pos[1] self.x = x self.y = df self.rmse = cost self.optimize = optimizer input_variables = ['a', 'b'] file_address = 'optimised_coefficients/' filename = "ParametrosAjustados_Modelo_{}_{}_{}_Dias.txt".format( 'SIR', name, len(x)) if not os.path.exists(file_address): os.makedirs(file_address) file_optimised_parameters = open(file_address + filename, "w") file_optimised_parameters.close() with open(file_address + filename, "a") as file_optimised_parameters: for i in range(len(input_variables)): message = '{}:{:.4f}\n'.format(input_variables[i], pos[i]) file_optimised_parameters.write(message)
def fit(self, x, y, bound=([0, 1 / 21 - 0.0001], [1, 1 / 5 + 0.0001]), name=None): ''' x = dias passados do dia inicial 1 y = numero de casos bound = intervalo de limite para procura de cada parametro, onde None = sem limite bound => (lista_min_bound, lista_max_bound) ''' self.name = name self.y = y df = np.array(y) / self.N self.I0 = df[0] self.S0 = 1 - self.I0 self.R0 = 0 options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} if bound == None: optimizer = GlobalBestPSO(n_particles=50, dimensions=2, options=options) cost, pos = optimizer.optimize( self.objectiveFunction, 500, x=x, y=df, n_processes=self.numeroProcessadores) self.beta = pos[0] self.gamma = pos[1] self.x = x self.rmse = cost self.optimize = optimizer else: optimizer = GlobalBestPSO(n_particles=50, dimensions=2, options=options, bounds=bound) cost, pos = optimizer.optimize( self.objectiveFunction, 500, x=x, y=df, n_processes=self.numeroProcessadores) self.beta = pos[0] self.gamma = pos[1] self.x = x self.rmse = cost self.optimize = optimizer
def test_pso(test_func, seed_size, num_iters, batch_size): if test_func == 'branin': obj_func = branin_pso d = 2 x_max = 1 * np.ones(2) x_min = 0 * x_max bounds = (x_min, x_max) initialsamplesize = 3 elif test_func == 'egg': obj_func = egg_pso d = 2 x_max = 1 * np.ones(2) x_min = 0 * x_max bounds = (x_min, x_max) initialsamplesize = 3 elif test_func == 'hartmann': obj_func = hartmann_pso d = 6 x_max = 1 * np.ones(6) x_min = 0 * x_max bounds = (x_min, x_max) initialsamplesize = 9 elif test_func == 'mich': obj_func = hartmann_pso d = 10 x_max = np.pi * np.ones(10) x_min = 0 * x_max bounds = (x_min, x_max) initialsamplesize = 25 else: print("Function does not exist in repository") return 0 def obj_func_noise(x): noiseless = obj_func(x) iters = noiseless.shape[0] noisy_eval = noiseless + sigma0 * np.random.random((1,iters)) return noisy_eval[0] # Creating dicts to store pos_dict = {} cost_dict = {} for seed_i in range(seed_size): np.random.seed(seed_i) """ # Generating initial samples init_pos = np.random.random((initialsamplesize, d)) print(init_pos) """ # Running optimisation optimizer = GlobalBestPSO(n_particles = batch_size, dimensions=d, options=options, bounds=bounds, init_pos = None) best_cost, best_pos = optimizer.optimize(obj_func_noise, num_iters + 1, seed_i) # Recording results pos_hist = optimizer.pos_history cost_hist = optimizer.cost_history pos_dict[seed_i] = pos_hist cost_dict[seed_i] = np.transpose(np.array([cost_hist])) return pos_dict, cost_dict
'colsample_bytree', 'gamma', 'reg_lambda' ] n_dimensions = len(param_name) param_max_list = [1, 50, 5, 1, 1, 1, 10] param_min_list = [0, 1e-05, 0, 0, 0, 0, 0] #param_max_list = [1,5,1,1,1.5,50,10] #param_min_list = [0,0,0,0,0,1e-05,0] param_max = np.array(param_max_list) param_min = np.array(param_min_list) bounds = (param_min, param_max) # *** 2. set options #options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} # can use grid search to get a better value of those parameters #options = {'c1': 2.05, 'c2': 2.05, 'w': 0.7} # try this afterwards options = {'c1': args.c1, 'c2': args.c2, 'w': args.w} optimizer = GlobalBestPSO(n_particles=args.particles, dimensions=n_dimensions, options=options, bounds=bounds) cost, pos = optimizer.optimize(f_BDT, iters=args.iters, data=score_km_train_norm, labels=labels_train_np, param_list=param_name, n_processes=4) plot_cost_history(cost_history=optimizer.cost_history) plt.savefig("cost_history.png") plt.close() print("cost history saved as cost_history.png") parameter_name = param_name #parameter_name = ['max_depth','min_child_weight','subsample','colsample_bytree','gamma','reg_alpha','reg_lambda'] n_iters = len(optimizer.pos_history) #number of iterations PSO went through
import matplotlib.pyplot as plt def rosenbrock_with_args(x, a, b, c): int_x = np.array(x, dtype=int) f = (a - int_x[:, 0])**2 + c * (b - int_x[:, 1])**2 return f # instatiate the optimizer x_max = 100 * np.ones(2) x_min = -1 * x_max bounds = (x_min, x_max) options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options=options, bounds=bounds) # now run the optimization, pass a=1 and b=100 as a tuple assigned to args cost, pos = optimizer.optimize(rosenbrock_with_args, 50, a=6, b=17, c=4) # plot_cost_history(optimizer.cost_history) # m = Mesher(func=fx.sphere, # limits=[(-1, 1), (-1, 1)]) # # Adjust figure limits # d = Designer(limits=[(-1, 1), (-1, 1), (-0.1, 1)], # label=['x-axis', 'y-axis', 'z-axis']) # plot_contour(pos_history=optimizer.pos_history, # mesher=m, designer=d, mark=(0, 0)) # plt.show()
Z, rstride=1, cstride=1, cmap=plt.get_cmap('viridis'), **kwargs) return ax # %% x_max = (2, 3) x_min = (-2, -1) bounds = (x_min, x_max) options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} optimizer = GlobalBestPSO(n_particles=50, dimensions=2, options=options, bounds=bounds) # %% cost, pos = optimizer.optimize(rosenbrock_opt, 200) # %% history_pos = optimizer.pos_history # %% def gen_rosenbrock_pso_animation(history_pos): X, Y = np.arange(-2, 2, 0.05), np.arange(-1, 3, 0.05) X, Y = np.meshgrid(X, Y) Z = rosenbrock_fig(X, Y)
""" Created on 2021/04/25 @author: nicklee (Description) """ import numpy as np from pyswarms.single.global_best import GlobalBestPSO def rosenbrock(x): a, b, c = 1, 100, 0 # print((a - x[:,0])**2 + b*(x[:,1] - x[:,0])**2 + c) return (a - x[:, 0])**2 + b * (x[:, 1] - x[:, 0])**2 + c options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options=options) const, pos = optimizer.optimize(rosenbrock, 1000)
# Create bounds K_min, K_max = 1, 1.5 tau_min, tau_max = 1, 100 alpha1_min, alpha1_max = 0.50, 0.75 alpha2_min, alpha2_max = 0.65, 0.95 alpha3_min, alpha3_max = 0.50, 0.75 # L_min, L_max = 61**(1/0.95)*0.8, 61**(1/0.40)*1.2 L_min, L_max = 61**(1/0.95)*0.8, 750 bounds = (np.array([K_min, tau_min, alpha1_min, alpha2_min, alpha3_min, L_min]), np.array([K_max, tau_max, alpha1_max, alpha2_max, alpha3_max, L_max])) print(L_min, L_max) # Initialize swarm options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} kwargs = {"ref_data": data_fl , "Ts": 1 , "Tmax": t_fl[-1]} optimizer = GlobalBestPSO(n_particles=5, dimensions=6, options=options, bounds=bounds) cost, pos = optimizer.optimize(calculate_error_PSO_VarA, iters=50, **kwargs) plot_cost_history(cost_history=optimizer.cost_history) plt.show() alpha = create_list_alpha(pos[2], pos[3], pos[4], dates_converted) params = (data_fl, 1, t_fl[-1]) fopfdd = FOPFDD(pos[0], pos[1], alpha, pos[5]) t_mod2, data_mod2 = fopfdd.step_response(1, t_fl[-1], verbose=True) res = calculate_error_PSO_VarA(np.array([pos]), *params) plt.figure() plt.plot(t_fl, data_fl, label='data') plt.plot(t_mod2, data_mod2, label='model') plt.legend() plt.xlabel('Time [days]')
print("FINAL loss: ", curr_loss) print("ITER required: ", iter) np.savetxt( "final_params/{}_{}.csv".format( optimizer_name, datetime.now().strftime("%Y%m%d_%H%M%S")), params.detach().numpy()) else: if optimizer_name == "PSO": # Hyperparameters for PSO # c1: cognitive parameter, c2: social parameter, w: inertia parameter options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9} # Initialize optimizer optimizer = GlobalBestPSO(n_particles=20, dimensions=30, options=options) # Perform optimization cost, pos = optimizer.optimize(model.PSO_loss, iters=1000) print("FINAL params: ", pos) print("FINAL loss: ", cost) print("ITER required: ", iter) np.savetxt( "final_params/{}_{}.csv".format( optimizer_name, datetime.now().strftime("%Y%m%d_%H%M%S")), np.array(pos)) elif optimizer_name == "basinhopping": param0 = np.ones(30)
for i in range(n_particles): mx_hf,cor = Corr(mx,alpha[i],beta[i],att_field,location_name) j.append(-cor) print(j) return np.array(j) from pyswarms.single.global_best import GlobalBestPSO #using Whole Foods with x_max = 2* np.ones(2) x_min = 0* np.ones(2) bounds = (x_min, x_max) print(bounds) options = {'c1': 2, 'c2': 2, 'w': 0.9} #init_pos = pos optimizer = GlobalBestPSO(n_particles=10, dimensions=2,options=options, bounds=bounds) #kwargs={"mx": mx} cost, pos = optimizer.optimize(PSOfunction_Huff, 10) cost,pos """# T-Huff model ## considering the temporal visit changes <img src="https://geods.geography.wisc.edu/wp-content/uploads/2020/04/tgis12624_HourlyPlot.png" alt="Preiction vs. Actual" style="float: left; margin-right: 10px;" /> ## can input alpha, beta directly to get the correlation. """