def partition_dag(self): """ Returns a tuple of: 1. the # of partitions formed (int) 2. the parallel time (longest path, int) 3. partition time (seconds, float) 4. a list of partitions (Partition) """ # trigger the PSO algorithm G = self._dag lb = [0.99] * self._leng ub = [3.01] * self._leng stt = time.time() if (self._deadline is None): xopt, fopt = pso(self.objective_func, lb, ub, swarmsize=self._swarm_size) else: xopt, fopt = pso(self.objective_func, lb, ub, ieqcons=[self.constrain_func], swarmsize=self._swarm_size) curr_lpl, num_parts, parts, g_dict = self._partition_G(G, xopt) edt = time.time() #print "PSO scheduler took {0} seconds".format(edt - stt) st_gid = len(self._drop_list) + 1 + num_parts for n in G.nodes(data=True): if (not n[1].has_key('gid')): n[1]['gid'] = st_gid part = Partition(st_gid, self._max_dop) part.add_node(n[0], n[1].get('weight', 1)) g_dict[st_gid] = part parts.append(part) # will it get rejected? num_parts += 1 self._parts = parts #print "call counts ", self._call_counts return (num_parts, curr_lpl, edt - stt, parts)
def swarm(self): params = self.params_class(**self.params_kwargs) lb, ub = params.create_vector_bounds() def objective_function(vector): params.receive_vector(vector=vector) instance_generation_function = 'player' return - score_params(params=params, objective=self.objective, opponents_information=self.opponents_information, weights=self.weights, sample_count=self.sample_count, instance_generation_function=instance_generation_function ) # TODO remove check once v 0.7 is pip installable # There is a multiprocessing version (0.7) of pyswarm available at # https://github.com/tisimst/pyswarm, just pass processes=X # Pip installs version 0.6 if pyswarm.__version__ == "0.7": xopt, fopt = pyswarm.pso(objective_function, lb, ub, swarmsize=self.population, maxiter=self.generations, debug=self.debug, phip=self.phip, phig=self.phig, omega=self.omega, processes=self.processes) else: xopt, fopt = pyswarm.pso(objective_function, lb, ub, swarmsize=self.population, maxiter=self.generations, debug=self.debug, phip=self.phip, phig=self.phig, omega=self.omega) return xopt, fopt
def optimise(): testCases = testcollection.readTests() #grouping.redefineGroupingConditions(dataselect.conditionBreakHigh) #grouping.redefineGroupingConditions(dataselect.conditionDoubleTopsFiltered) ### Objective Function. ### An input for pyswarm.pso. Returns a float utility score from testing with a set of weights. def testWithWeights(weights): arrLen = len(parametersToOptimise) weightDict = {} for i in range(0,arrLen): weightDict[parametersToOptimise[i]] = weights[i] result = testAlgorithmsForAverageScore(testCases, weightDict) print(str(result) + ' <- ' + str(weights)) # print as we go. return (result - 1)*100 ### Uncomment this to test the running time of weight optimisation. # testWeightOptimisationRunningTime(testWithWeights) ub = [1]*5 # Upper bound = 1 lb = [0]*5 # Lower bound = 0 xopt, fopt = pyswarm.pso(testWithWeights, lb, ub, maxiter=20) # If we reach this point, it means the weight optimisation process is complete. print('Final Weights:') print(xopt) print('Score = ' + str(fopt))
def optimize(self): self._favor_context = True with nostdout(): self._x, _ = pso( self._funcwrapped, self._lower, self._upper, maxiter=MAX_IT, ) self._favor_context = False self.lsearch()
def optimize(self, control): """Run PSO optimization on the automata using custom PSO configuration.""" xopt, fopt = pso( func=self._classifier_error, lb=self.automata.vector_lb, ub=self.automata.vector_ub, maxiter=control["maxit"], swarmsize=control["s"], debug=control["trace"]) print('xopt (optymalny znaleziony wektor) = \n', xopt) print('fopt (najmniejsza znaleziona wartość dla wektora) = ', fopt) self.automata.vector = xopt
def run_pso_multiple_times(): swarm_size = 100 indi_results = [] for max_iter in [100,150,200,300,400,500]: print("Current number of Iteration", str(max_iter)) start_time = time.time() xopt, fopt = pso(banana,lb, ub,f_ieqcons=con,swarmsize=swarm_size,maxiter=max_iter) rmse = calculate_mse(xopt) finish_time = time.time() - start_time pso_param = [swarm_size, max_iter, finish_time, rmse] indi_results.append(pso_param + xopt.tolist()) indi_results = np.array(indi_results) np.savetxt("pso.csv", indi_results, delimiter=",")
def main(): filename=sys.argv[1] data=readfile(filename) row,col=data.shape #print max(data[row-4]) newbigger=[] for rownum in xrange(row): newbigger.append(data[rownum][col-1]) midnum=caculatemidnum(newbigger) data=data/midnum if row>0: file_result=open('result.txt','w') file_result.write(repr('para_a').rjust(10)+repr('para_b').rjust(10)+repr('para_c').rjust(10)+repr('para_d').rjust(10)+repr('para_e').rjust(10)+repr('para_f').rjust(10)+repr('para_g').rjust(10)+repr('error').rjust(10)+'\n') for rownum in xrange(row): '''print data[rownum].min() print data[rownum].max() print data[rownum].mean()''' args=data[rownum] length=len(args) iteration=constraint(args,length) print('iterations={}'.format(iteration)) print("please wait a moment....") minNum=args.min() maxNum=args.max() meanNum=args.mean() lb=[-maxNum,0,-meanNum,-1,-maxNum,-1,-maxNum] ub=[1,0.5,maxNum,1.1,maxNum+meanNum,1.1,maxNum+meanNum] initialData=[0,0.01,meanNum,-1,maxNum,0.7,maxNum] initialData2=[minNum,0.03,meanNum+4,0.1,maxNum,-1,maxNum] xopt4,fopt4=pso(myfunction,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration) print('The optimum is at:') print(' {}'.format(xopt4)) print('Optimal function values:') #print(' R-square : {}'.format(fopt4)) print('ajusted R-squared:{}'.format(-fopt4)) file_result.write(repr(xopt4[0]).rjust(10)+repr(xopt4[1]).rjust(10)+repr(xopt4[2]).rjust(10)+repr(xopt4[3]).rjust(10)+repr(xopt4[4]).rjust(10)+repr(xopt4[5]).rjust(10)+repr(xopt4[6]).rjust(10)+repr(fopt4).rjust(10)+'\n') x=np.linspace(1,length,10000) y=xopt4[0]+xopt4[1]*x+xopt4[2]/((1+np.exp(-xopt4[3]*(x-xopt4[4])))*(1+np.exp(-xopt4[5]*(x-xopt4[6])))) plt.figure(rownum) for i in xrange(length): plt.plot(i+1,args[i],'blue',linestyle='dashed',marker='.') plt.plot(x,y,'r',linewidth=2) plt.xlabel("circle(Time)") plt.ylabel("fluorescence") plt.legend() plt.show() if row>0: file_result.close()
def optimize(self): self._favor_context = True while (self._nbcall < MAX_FN_CALL): with nostdout(): x, v = pso( self._funcwrapped, self._lower, self._upper, maxiter=MAX_IT, ) if self._xmini is None: self._xmini = x if self._fmini is None: self._fmini = v if v < self._fmini: self._fmini = v self._xmini = x
def select_params_with_pso(ks): for k in ks: MODEL_PARAM['k'] = k kwargs = { 'trn_xs': trn_xs, 'trn_ys': trn_ys, 'tst_xs': tst_xs, 'tst_ys': tst_ys, 'model_name': MODEL_NAME, 'model_param': MODEL_PARAM, 'model_repeat': MODEL_REPEAT, } # print '@@@@@@@@@@@@', MODEL_PARAM['k'] xopt, fopt = pso(objective, lb, ub, maxiter=200, swarmsize=80, debug=False, processes=8, kwargs=kwargs) # print xopt, fopt # print 'result:' print k, fopt, for wi in xopt: print '%.8f' % wi, print '' sys.stdout.flush()
def gprSwarmOptim_helper(train,target,max_evals,lb,ub,swarmsize=100): """ optimizer for hyperOptim. Runs a GP and if it fails (which happens if two points have the same input but differen output) tries a random forest. Then estimates the optimum using PSO. :param train: :param target: :param max_evals: :param lb: :param ub: :param swarmsize: :return: """ try: gp = GaussianProcess(regr='quadratic', corr='squared_exponential',nugget=1e-2, optimizer='Welch') gp.fit(train,target) except: gp=RandomForestRegressor(n_estimators=(50+2*len(train))) gp.fit(train,target) xopt = pso(lambda x:-1.0*gp.predict([x]), lb, ub,swarmsize=swarmsize) return (xopt[0],-1*xopt[1])
def deterministic(self): # create upper and lower bound matrices: setpointList = self.model.setpoints upperbounds = numpy.zeros((self.horizon, len(setpointList))) upperbounds.fill(self.upperbound) lowerbounds = numpy.zeros((self.horizon, len(setpointList))) lowerbounds.fill(self.lowerbound) def cost(setpoints): # get weather vector: self.determine = self.create_weather_vectors(1)[0] # create a dataframe using the weather vector and setpoint matrix: weather = pandas.DataFrame(self.determine, columns=self.generator.predictor_variables) sp = pandas.DataFrame(setpoints, columns=setpointList) data = pandas.concat([weather, sp], axis=1) prediction = self.model.predict(data) total_cost = numpy.sum(prediction) return total_cost # run the optimiztion: return pso(cost, lowerbounds, upperbounds)
# -*- coding: utf-8 -*- """ Created on Tue Jun 30 16:51:01 2015 @author: 8 """ from pyswarm import pso import numpy as np from math import pi, exp, sqrt # def ackley(chrom): chrom = np.array(chrom) sum1 = np.sum(chrom**2) sum2 = np.sum(np.cos(2 * pi * chrom)) n = len(chrom) f = -20 * exp(-.2 * sqrt(sum1 / n)) - exp(sum2 / n) + 20 + exp(1) return f dim = 4 x, f = pso(ackley, [-30] * dim, [30] * dim, maxiter=10000, minfunc=1e-15, minstep=1e-16) print f
def optimize(self): while self._nbcall < MAX_FN_CALL: with nostdout(): xopt, fopt = pso( self._funcwrapped, self._lower, self._upper, maxiter=MAX_IT)
cons_water_upper = 150 - X[3] cons_age_upper = 30 - X[-1] return [cons_str_lower, cons_water_upper, cons_age_upper] # Log info logging.info(objective.__doc__) logging.info(constraints.__doc__) # Run PSO 5 times with some generic hyper-parameters X_opts = [] n_runs = 5 for n in range(n_runs): logging.info('Run %d' % (n)) X_opt, _ = pso(objective, lower_bounds, upper_bounds, f_ieqcons=constraints, swarmsize=100, maxiter=200) X_opts.append(X_opt) X_opts = np.asarray(X_opts) y_hat_opts = regressor.predict(X_opts).reshape(-1, 1) data_opt = np.concatenate([X_opts, y_hat_opts], axis=1) df_predict = pd.DataFrame(columns=df.columns, data=data_opt) logging.info('Particle swarm optimization completed...\n\n') # Display Results for n in range(5): print('Run: %d' % n) print(df_predict.loc[n], '\n\n') # Compare with unseen data
"KappaP11G", "KappaP22D", "KappaP33D", "LambdaD", "RSigmaST", "RSigmaMT", "RSigmaLT", "Sigma22F", "Sigma33F", "ThetaP2F", "ThetaP3F", "KappaP22F", "KappaP33F", "LambdaF" ] llh_best = np.inf list_parameters = [] list_factors = [] list_llh = [] for i in range(iterations): # res = minimize(kalman_afns, initial_guess, args=other_args, method="SLSQP", bounds=bnds, constraints=cons, # options=opt) res, fopt = pso(kalman_afns, args=other_args, lb=lbnds, ub=ubnds, maxiter=50, debug=False, swarmsize=100, minstep=1e-3) llh, df_factor_ts = kalman_afns(res, delta_t, tenors, rates_dict, True) df_parameters = pd.DataFrame(np.reshape(res, (1, 20)), columns=parameter_columns, index=[i]) df_parameters = df_parameters.assign(loglh=[llh]) if llh < llh_best: # df_factors_ts = df_factors_ts.assign(it=[i]*df_factors_ts.shape[0]) llh_best = llh print("Best: %s" % llh_best) df_factor_results = df_factor_ts df_parameters_best = df_parameters list_parameters.append(df_parameters_best)
fout.close() def minfunc(pars): if model.mark3LogPrior(pars) != -np.inf: ll = model.mark6LogLikelihood(pars, incJitter=incJitterEquad) return -ll else: return 1e80 p0 = model.initParameters(fixpstart=False, startEfacAtOne=False) #maxpars = fmin(minfunc, p0, ftol=1e-8, maxiter=10000, maxfun=1000, disp=True) #x = minimize(minfunc, p0, method='CG', tol=1e-3) #maxpars = x.x maxpars, maxf = pyswarm.pso(minfunc, model.pmin, model.pmax, swarmsize=500, \ omega=0.5, phip=0.5, phig=0.5, maxiter=1000, debug=False, \ minfunc=1e-3) pars = np.loadtxt(outdir + '/chains/{0}'.format(model.psr[0].name)+ \ '/pars.txt', dtype='S42') fout = open(outdir + '/noisefiles/{0}_noise.txt'.format(model.psr[0].name), 'w') for ii,pp in enumerate(pars): fout.write('{0} {1}\n'.format(pp, maxpars[ii])) fout.close() print psr, maxpars, model.mark6LogLikelihood(maxpars, incJitter=incJitterEquad) # setup full model if args.pname[0] != 'all': model = PALmodels.PTAmodels(h5filename, pulsars=args.pname)
individual[21]) + "," + str( individual[22]) + "," + str( individual[23]) + "," + str( individual[24]) + "," + str( individual[25]) + " ]" print "------------------------" return y #---------------------------------------------------------------------- ##PySwarm print('*' * 65) print('Example minimization of 4th-order banana function (no constraints)') #xopt1, fopt1 = pso(example_fitness, min_ind, max_ind) xopt1, fopt1 = pso(example_fitness, min_ind, max_ind, maxiter=200) #xopt1, fopt1 = pso(example_fitness, min_ind, max_ind, ieqcons=[], # f_ieqcons=None, args=(), kwargs={}, # swarmsize=100, omega=0.5, phip=0.5, phig=0.5, maxiter=100, # minstep=1e-8, minfunc=1e-8, debug=False) # https://pythonhosted.org/pyswarm/ print('The optimum is at:') print(' {}'.format(xopt1)) print('Optimal function value:') print(' myfunc: {}'.format(fopt1)) #---------------------------------------------------------------------- ##PySwarm #print ('*'*65) #print ('Example minimization of 4th-order banana function (with constraint)') #def mycon(x): # x1 = x[0]
opt_prob.addObj('f') #opt_prob.addCon('g1','i') #possibility to add constraintes #opt_prob.addCon('g2','i') ############################################################################## ############################################################################## # solving the problem with the function in scipy.optimize ############################################################################## ############################################################################## if algo == 1: xopt, fopt = pso(monerreur, lb, up, swarmsize=swarmsize, minfunc=1e-18, minstep=1e-8, debug=1, phip=0.5, phig=0.5, maxiter=maxiter) ############################################################################## ############################################################################## # solving the problem with yopt ############################################################################## ############################################################################## swarmsize = comm.bcast(swarmsize, root=0) maxiter = comm.bcast(maxiter, root=0) if algo == 2: # Solve Problem (No-Parallelization) alpso_none = ALPSO() #pll_type='SPM')
def particle_search(min_var, max_var, deltas, initial_structure_obj, lateral_pressure, init_filter=float('inf'), side='p', const_chk=(True, True, True, True), pso_options=(100, 0.5, 0.5, 0.5, 100, 1e-8, 1e-8), fat_dict=None, fat_press=None, slamming_press=0): ''' Searchin using Particle Swarm Search (http://pythonhosted.org/pyswarm/) :param min_var: :param max_var: :param deltas: :param initial_structure_obj: :param lateral_pressure: :param side: :param const_chk: :param pso_options: :return: ''' args = (initial_structure_obj, lateral_pressure, init_filter, side, const_chk, min_var[6], min_var[7], fat_dict, fat_press, slamming_press) # Multiprocessing is not efficient if the swarm size is below 500 (approximatly). if pso_options[0] < 500: xopt, fopt = pso(calc_weight_pso, min_var[0:6], max_var[0:6], f_ieqcons=any_constraints_all_number, swarmsize=pso_options[0], omega=pso_options[1], phip=pso_options[2], phig=pso_options[3], maxiter=pso_options[4], minstep=pso_options[5], minfunc=pso_options[6], args=args) else: xopt, fopt = pso(calc_weight_pso, min_var[0:6], max_var[0:6], f_ieqcons=any_constraints_all_number, swarmsize=pso_options[0], omega=pso_options[1], phip=pso_options[2], phig=pso_options[3], maxiter=pso_options[4], minstep=pso_options[5], minfunc=pso_options[6], args=args, processes=cpu_count()) ass_var = xopt np.append(ass_var, [args[5], args[6]]) new_structure_obj = create_new_structure_obj( initial_structure_obj, [round(item, 5) for item in ass_var]) new_calc_obj = create_new_calc_obj(initial_structure_obj, [round(item, 5) for item in ass_var])[0] args = list(args) args[0] = new_structure_obj return new_structure_obj, new_calc_obj, fat_dict, True if any_constraints_all_number( xopt, *args) == 0 else False
#def con(x): # x1 = x[0] # x2 = x[1] # return [-(x1 + 0.25)**2 + 0.75*x2] lb = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ub = [ 300, 5, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 ] xopt, fopt = pso(transposer, lb, ub) # Optimum should be around x=[0.5, 0.76] with banana(x)=4.5 and con(x)=0 # import kivy module import kivy # this restrict the kivy version i.e # below this kivy version you cannot # use the app or software kivy.require("1.9.1") # base Class of your App inherits from the App class. # app:always refers to the instance of your application from kivy.app import App
f = 0 for j in range(0, 7): f = f + 100 * (xx[j + 1] - xx[j]**2)**2 + (1.0 - xx[j])**2 return f comparetol = 1e-4 maxevals = 80 delta = .1 nsamp = round(maxevals / 3) # compute optimum/optimizer by PSO from pyswarm import pso # https://pythonhosted.org/pyswarm/ xopt0, fopt0 = pso(fun, lb, ub, swarmsize=200, minfunc=1e-12, maxiter=10000) use_linear_constraints = 0 use_nl_constraints = 0 elif benchmark == "ackley": nvars = 2 lb = -5 * ones((nvars, 1)).flatten("c") ub = -lb fun = lambda x: array([ -20.0 * exp(-.2 * sqrt(0.5 * (x[0]**2 + x[1]**2))) - exp(0.5 * ( cos(2.0 * pi * x[0]) + cos(2.0 * pi * x[1]))) + exp(1.0) + 20.0 ]) comparetol = 1e-4
from pyswarm import pso def banana(x): x1 = x[0] x2 = x[1] return x1**4 - 2*x2*x1**2 + x2**2 + x1**2 - 2*x1 + 5 def con(x): x1 = x[0] x2 = x[1] return [-(x1 + 0.25)**2 + 0.75*x2] lb = [-10, -10] ub = [10, 10] xopt, fopt = pso(banana, lb, ub, debug=True) print xopt, fopt
loss = log_loss(testY, preds, labels=classes) return loss if __name__ == '__main__': # Load dataset train_texts, train_targets, validation_texts, validation_targets = get_dataset_partitions( ) # Convert dataset to feature representation build_featureset(train_texts, train_targets, validation_texts, validation_targets) # Dictate upper and lower bounds of a particle lower_bound = [0.01, 0.1] upper_bound = [1.2, 2] # Run PSO xopt, fopt = pso(loss, lower_bound, upper_bound, swarmsize=5, maxiter=5, omega=0.75, phip=0.75, phig=0.75, debug=True) print("OPTIMAL HYPERPARAMETER SETUP: {}\nMINIMUM LOSS OBTAINED: {}".format( xopt, fopt))
def fit(self, X, y): #X_train_inner, X_test_inner, y_train_inner, y_test_inner = model_selection.train_test_split(X, y, test_size = 0.2, random_state = 40, shuffle = True) X = np.array(X) y = np.array(y) # Define Objective function def obj(x): params = { self.hyperparameters[0]: int(x[0]), self.hyperparameters[1]: int(x[1]), self.hyperparameters[2]: x[2], self.hyperparameters[3]: x[3], self.hyperparameters[4]: x[4], self.hyperparameters[5]: int(x[5]) } kf = model_selection.KFold(n_splits=self.cv) kf.get_n_splits(X) rmse = [] for train_index, test_index in kf.split(np.array(X)): X_train_inner, X_test_inner = np.array( X)[train_index], np.array(X)[test_index] y_train_inner, y_test_inner = np.array( y)[train_index], np.array(y)[test_index] model = lgb.LGBMRegressor(**params).fit( X_train_inner, y_train_inner) rmse.append( metrics.mean_squared_error(y_test_inner, model.predict(X_test_inner))) rmse = np.mean(rmse) return rmse # Perform Model Optimization xopt, fopt = pyswarm.pso(func=obj, lb=self.lb, ub=self.ub, swarmsize=self.pop, phip=self.phip, phig=self.phig, omega=self.omega, maxiter=self.maxiter, minstep=self.minstep, minfunc=self.minfunc, debug=self.debug) # Fit the best model hyperpara_optimized = { self.hyperparameters[0]: int(xopt[0]), self.hyperparameters[1]: int(xopt[1]), self.hyperparameters[2]: xopt[2], self.hyperparameters[3]: xopt[3], self.hyperparameters[4]: xopt[4], self.hyperparameters[5]: int(xopt[5]) } self.fitted_model = lgb.LGBMRegressor(**hyperpara_optimized).fit(X, y) return self
def main(): filename = sys.argv[1] data = readfile(filename) row, col = data.shape #print max(data[row-4]) newbigger = [] for rownum in xrange(row): newbigger.append(data[rownum][col - 1]) midnum = caculatemidnum(newbigger) data = data / midnum if row > 0: file_result = open('result.txt', 'w') file_result.write( repr('para_a').rjust(10) + repr('para_b').rjust(10) + repr('para_c').rjust(10) + repr('para_d').rjust(10) + repr('para_e').rjust(10) + repr('para_f').rjust(10) + repr('para_g').rjust(10) + repr('error').rjust(10) + '\n') for rownum in xrange(row): '''print data[rownum].min() print data[rownum].max() print data[rownum].mean()''' args = data[rownum] length = len(args) iteration = constraint(args, length) print('iterations={}'.format(iteration)) print("please wait a moment....") minNum = args.min() maxNum = args.max() meanNum = args.mean() lb = [-maxNum, 0, -meanNum, -1, -maxNum, -1, -maxNum] ub = [1, 0.5, maxNum, 1.1, maxNum + meanNum, 1.1, maxNum + meanNum] initialData = [0, 0.01, meanNum, -1, maxNum, 0.7, maxNum] initialData2 = [minNum, 0.03, meanNum + 4, 0.1, maxNum, -1, maxNum] xopt4, fopt4 = pso(myfunction, lb, ub, args=args, initialData=initialData, initialData2=initialData2, swarmsize=250, maxiter=iteration) print('The optimum is at:') print(' {}'.format(xopt4)) print('Optimal function values:') #print(' R-square : {}'.format(fopt4)) print('ajusted R-squared:{}'.format(-fopt4)) file_result.write( repr(xopt4[0]).rjust(10) + repr(xopt4[1]).rjust(10) + repr(xopt4[2]).rjust(10) + repr(xopt4[3]).rjust(10) + repr(xopt4[4]).rjust(10) + repr(xopt4[5]).rjust(10) + repr(xopt4[6]).rjust(10) + repr(fopt4).rjust(10) + '\n') x = np.linspace(1, length, 10000) y = xopt4[0] + xopt4[1] * x + xopt4[2] / ( (1 + np.exp(-xopt4[3] * (x - xopt4[4]))) * (1 + np.exp(-xopt4[5] * (x - xopt4[6])))) plt.figure(rownum) for i in xrange(length): plt.plot(i + 1, args[i], 'blue', linestyle='dashed', marker='.') plt.plot(x, y, 'r', linewidth=2) plt.xlabel("circle(Time)") plt.ylabel("fluorescence") plt.legend() plt.show() if row > 0: file_result.close()
"KappaP33D", "LambdaD", "RSigmaST", "RSigmaMT", "RSigmaLT", "Sigma22F", "Sigma33F", "ThetaP2F", "ThetaP3F", "KappaP22F", "KappaP33F", "LambdaF"] llh_best = np.inf curr_rates_dict = {"eur": rates_dict["eur"].truncate(before=begin_estimation_window, after=fc_date), "gbp": rates_dict["gbp"].truncate(before=begin_estimation_window, after=fc_date), "fxgbpeur": rates_dict["fxgbpeur"].truncate(before=begin_estimation_window, after=fc_date)} # other arguments to transfer to kalman_afns other_args = (delta_t, tenors, curr_rates_dict, False) for i in range(iterations): res, fopt = pso(kalman_afns, args=other_args, lb=lbnds, ub=ubnds, maxiter=pso_maxiter, debug=False, swarmsize=n_swarm, minstep=pso_minstep) llh, df_factor_ts = kalman_afns(res, delta_t, tenors, curr_rates_dict, True) df_parameters = pd.DataFrame(np.reshape(res, (1, 20)), columns=parameter_columns, index=[fc_date]) df_parameters = df_parameters.assign(loglh=[llh]) if llh < llh_best: df_factor_ts["fcdate"] = fc_date df_factor_ts.set_index('fcdate', append=True, inplace=True) df_factors_best = df_factor_ts df_parameters_best = df_parameters llh_best = llh print((i + 1) / iterations * 100, "%") print("---Elapsed time: %s seconds ---" % (time.time() - start_time)) list_factors_best.append(df_factors_best) list_parameters_best.append(df_parameters_best) print(llh_best)
def pyswarm_library(): """PSO using Pysmwarm library""" # PARAMETERS DEFINITION pop_size = 500 iteration = 1000 w = 0.5 c1 = 0.5 c2 = 0.5 minstep = 1e-3 minfunc = 1e-3 debug = False # Default parameters print("Use default parameters:\n" "\t\t\tpop_size =", pop_size, "\n", "\t\t\titeration =", iteration, "\n", "\t\t\tw =", w, "\n", "\t\t\tc1 =", c1, "\n", "\t\t\tc2 =", c2, "\n", "\t\t\tminstep =", minstep, "\n", "\t\t\tminfunc =", minfunc, "\n", "\t\t\tdebug =", debug, "\n\n", "or set personal ones? [D for default|P for personal]") # Set personal parameters param = input() if param == "P": print("Size of population = [int]") pop_size = eval(input()) print("Number of interation = [int]") iteration = eval(input()) print("Inertial weight = [float]") w = eval(input()) print("Cognitive coefficient = [float]") c1 = eval(input()) print("Social coefficient [float]") c2 = eval(input()) print( "The minimum stepsize of swarm’s best position before the search terminates = [int]" ) minstep = eval(input()) print( "Minimum change of swarm’s best objective value before the search terminates = [int]" ) minfunc = eval(input()) print("Active debyg = [boolean]") debug = eval(input()) def constraints(x): """Pick the constraints from benchmarck_function file""" for k in range(len(x)): locals()[f"x{k+1}"] = x[k] constraints = eval("bf." + opt_func + ".inequal_constraints") positive_constraints = [] for k in range(len(constraints)): # Library use >= notation positive_constraints.extend([eval(constraints[k]) * -1]) return positive_constraints lb = [] ub = [] # Pick the bounds of the fitting function for k in range(len(eval("bf." + opt_func + ".bounds"))): lb.extend([eval("bf." + opt_func + ".bounds")[k][0]]) ub.extend([eval("bf." + opt_func + ".bounds")[k][1]]) # Compute gbest with library function start_time = time.time() gbest_pos, gbest = pso(eval("bf." + opt_func + ".function"), lb, ub, f_ieqcons=constraints, swarmsize=pop_size, omega=w, phip=c1, phig=c2, maxiter=iteration, minstep=minstep, minfunc=minfunc, debug=debug) end_time = time.time() print("ELAPSED TIME:", f"{end_time-start_time:.0f}", "sec for", runs, "runs") return gbest, gbest_pos
def preparation(): project_path = 'C:\\Users\\FrancescoBaldi\\switchdrive\\Work in progress\\Paper 0\\Ecos2015PaperExtension\\' path_files = project_path + os.sep sys.path.append(path_files) filenames = input.filenames(project_path) # Note: this is just a test CONSTANTS = kk.constantsSetting() CONSTANTS["filenames"] = filenames processed_temp = pd.read_hdf(CONSTANTS["filenames"]["dataset_output"], 'processed') dataset_raw = pd.read_hdf(filenames["dataset_raw"], 'table') data = pd.DataFrame(index=processed_temp.index) indexes_2_save_processed = { "Passengers_calc", "Demands:Electricity:HVAC:Edot", "T_air", "AE1:Cyl:FuelPh_in:mdot", "AE2:Cyl:FuelPh_in:mdot", "AE3:Cyl:FuelPh_in:mdot", "AE4:Cyl:FuelPh_in:mdot", "ME1:Cyl:FuelPh_in:mdot", "ME2:Cyl:FuelPh_in:mdot", "ME3:Cyl:FuelPh_in:mdot", "ME4:Cyl:FuelPh_in:mdot", "ME2:HRSG:Steam_in:mdot", "ME3:HRSG:Steam_in:mdot", "AE1:HRSG:Steam_in:mdot", "AE2:HRSG:Steam_in:mdot", "AE3:HRSG:Steam_in:mdot", "AE4:HRSG:Steam_in:mdot", "CoolingSystems:HTcollector24:HTWater_out:mdot", "CoolingSystems:HTcollector13:HTWater_out:mdot", "CoolingSystems:HTcollector24:HTWater_out:T", "CoolingSystems:HTcollector13:HTWater_out:T", "ME1:on", "ME2:on", "ME3:on", "ME4:on", "AE1:on", "AE2:on", "AE3:on", "AE4:on", } indexes_2_save_raw = { "SBO 1 OUTLET PRESSUR:7313:bar:Average:900", "SBO 2 OUTLET PRESSUR:7333:bar:Average:900", "Boiler_Port", "Boiler_starbord" } for idx in indexes_2_save_processed: data[idx] = processed_temp[idx] for idx in indexes_2_save_raw: data[idx] = dataset_raw[idx] # Prepare the input constant_input = (data, CONSTANTS, "optimization") # Set the boundaries lb = [0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 0.2, 343, 0.5, 1e4, 2000] ub = [1000, 1000, 1.0, 1.0, 1.0, 1.0, 1.0, 353, 0.9, 1e7, 8000] # param[0] = Constant demand to the HTHR systems # param[1] = Constant steam demand # param[2] = a1 factor of the HVAC re-heater (HVAC,RH) # param[3] = a1 factor of the hot water heater (HWH) # param[4] = a1 factor of the HVAC pre-heater (HVAC,PH) # param[5] = a1 factor of the Galley (G) # param[6] = a1 factor of the Other users (TH, OT, HFOT) # param[7] = Fixed temperature of the HRHT # param[8] = Effectiveness of the HTHR heat exchangers # param[9] = Boiler drum steam storage capacity [MJ, energy] # param[10] = Boiler heat rate when in use [kW] # Launch the optimization xopt, fopt = pso(fitnessFunction, lb, ub, args=constant_input, debug=True) # Test 1 # xopt = [0.00000000e+00, 0.00000000e+00, 2.54803569e-01, 9.99957005e-01, 1.00000000e+00, 5.48135386e-01, 2.02497633e-01, 3.46800156e+02, 5.00000000e-01, 9.91841867e+06, 7.92913650e+03] # xopt = [1.11658780e+02, 1.01578823e+01, 2.58777430e-01, 7.33523799e-01, 1.00000000e+00, 7.31005444e-01, 2.00407605e-01, 3.43000000e+02, 5.00000000e-01, 1.00000000e+07, 2.83632523e+03] # fopt = 0.3632706766877296 fitnessFunction(xopt, data, CONSTANTS, "test") return (xopt, fopt)
def main(): filename = sys.argv[1] data = readfile(filename) row, col = data.shape # print max(data[row-4]) newbigger = [] for rownum in xrange(row): newbigger.append(data[rownum][col - 1]) midnum = caculatemidnum(newbigger) data = data / midnum if row > 0: file_result = open("result.txt", "w") file_result.write( repr("para_a").rjust(10) + repr("para_b").rjust(10) + repr("para_c").rjust(10) + repr("para_d").rjust(10) + repr("para_e").rjust(10) + repr("para_f").rjust(10) + repr("para_g").rjust(10) + repr("error").rjust(10) + "\n" ) for rownum in xrange(row): """print data[rownum].min() print data[rownum].max() print data[rownum].mean()""" print("please wait a moment....") args = data[rownum] length = len(args) minNum = args.min() maxNum = args.max() meanNum = args.mean() lb = [minNum - maxNum, 0, meanNum - maxNum, -1, minNum, -1, minNum] ub = [meanNum, 0.5, meanNum + maxNum, 1.1, meanNum + maxNum, 1.1, meanNum + maxNum] initialData = [minNum, 0.01, meanNum, -0.1, maxNum, 0.7, maxNum] xopt4, fopt4 = pso(myfunction, lb, ub, args=args, initialData=initialData) print("The optimum is at:") print(" {}".format(xopt4)) print("Optimal function values:") # print(' R-square : {}'.format(fopt4)) print("ajusted R-squared:{}".format(fopt4)) file_result.write( repr(xopt4[0]).rjust(10) + repr(xopt4[1]).rjust(10) + repr(xopt4[2]).rjust(10) + repr(xopt4[3]).rjust(10) + repr(xopt4[4]).rjust(10) + repr(xopt4[5]).rjust(10) + repr(xopt4[6]).rjust(10) + repr(fopt4).rjust(10) + "\n" ) x = np.linspace(1, length, 10000) y = ( xopt4[0] + xopt4[1] * x + xopt4[2] / ((1 + np.exp(-xopt4[3] * (x - xopt4[4]))) * (1 + np.exp(-xopt4[5] * (x - xopt4[6])))) ) plt.figure(rownum) for i in xrange(length): plt.plot(i + 1, args[i], "blue", linestyle="dashed", marker="*") plt.plot(x, y, "r", linewidth=2) plt.xlabel("circle(Time)") plt.ylabel("fluorescence") plt.legend() plt.show() if row > 0: file_result.close()
def optimize(self): with nostdout(): xopt, fopt = pso( self._funcwrapped, self._lower, self._upper, maxiter=MAX_IT)
def fit(self, X, y): # Get split indices kf = model_selection.StratifiedKFold(n_splits=self.cv, random_state=0) kf.get_n_splits(X) # initialize model list self.model_list = [] self.score = [] self.model_name = [] print('\n\n Tuning Models \n\n') split_index = 1 # Split into train test for train_index, test_index in kf.split(X, y): X_train, X_test = np.array(X)[train_index], np.array(X)[test_index] y_train, y_test = np.array(y).ravel()[train_index], np.array( y).ravel()[test_index] if self.sample == 'oversample': X_train, y_train = over_sampling.SMOTE( random_state=12).fit_resample(X_train, y_train) if self.sample == 'undersample': X_train, y_train = under_sampling.EditedNearestNeighbours( random_state=12).fit_resample(X_train, y_train) if self.sample == 'balance': X_train, y_train = combine.SMOTEENN( random_state=12).fit_resample(X_train, y_train) # Define objective function def obj(x): params = { 'n_estimators': int(x[0]), 'max_depth': int(x[1]), 'min_data_in_leaf': int(x[2]), 'learning_rate': x[3], 'subsample': x[4], 'feature_fraction': x[5], 'reg_lambda': x[6], 'num_leaves': int(x[7]) } # Fit required model: model_sel = lgb.LGBMClassifier(**params).fit( X_train, y_train, eval_set=[(X_test, y_test)], eval_metric='multi_logloss', verbose=False) # Evaluate rmse score = -metrics.accuracy_score(y_test, model_sel.predict(X_test)) return score if split_index == 1: ## Optimize pso = pyswarm.pso(func=obj, lb=[val[0] for val in self.bounds], ub=[val[1] for val in self.bounds], swarmsize=self.swarmsize, omega=self.omega, phip=self.phip, phig=self.phig, maxiter=self.maxiter, minstep=self.minstep, minfunc=self.minfunc, debug=self.debug) # Get tuned hyperparameters x = pso[0] params_tuned = { 'n_estimators': int(x[0]), 'max_depth': int(x[1]), 'min_data_in_leaf': int(x[2]), 'learning_rate': x[3], 'subsample': x[4], 'feature_fraction': x[5], 'reg_lambda': x[6], 'num_leaves': int(x[7]) } # Get Fitted, tuned model fitted_model = lgb.LGBMClassifier(**params_tuned).fit( X_train, y_train, eval_set=[(X_test, y_test)], eval_metric='multi_logloss', verbose=False) self.model_name.append('model' + str(split_index)) # Calculate Score score_temp = metrics.accuracy_score(y_test, fitted_model.predict(X_test)) self.score.append(score_temp) print('Metric: ', score_temp, '\n\n') # Append model to final list of models self.model_list.append(fitted_model) # Print fold errors print(self.score) split_index = split_index + 1 model_rank = pd.DataFrame(data=list(zip(self.model_list, self.score)), index=self.model_name, columns=['model', 'score']) self.top_n_model_list = model_rank.sort_values( 'score')[:self.top_n].model.values.tolist() print('\n All Models trained: \n', model_rank['score']) #print('\n \n Models Selected by voting: \n \n', model_rank.sort_values('score')[:self.top_n]['score']) self.voting_classifier = ensemble.VotingClassifier( estimators=list(zip(self.model_name, self.model_list)), voting='soft').fit(X, y) self.fitted_status = True return self
def optimize(self): while (self._nbcall < MAX_FN_CALL): with nostdout(): xopt, fopt = pso( self._funcwrapped, self._lower, self._upper, maxiter=MAX_IT)
for comp, h_score in zip(d, h): h_dict[comp] = h_score #print("h_dict",h,h_dict,matrix[0]) p_d = 1.0 all_s = "" for activity_vec, e in zip(matrix, error_vec): p_e_d = 1 s = "" for comp in d: if activity_vec[comp] == 1: p_e_d = p_e_d * h_dict[comp] s = s + "h" + str(comp) if e == 1: p_e_d = 1 - p_e_d s = "1-" + s all_s = all_s + "(" + s p_d = p_d * p_e_d #print(all_s) return -p_d lb = [0 for _ in d] # ub=[1 for _ in d] #ub=[1 for x,ind in enumerate(matrix[0]) if ind ] xopt, fopt = pso(probabilty_TF, lb, ub, swarmsize=1000, maxiter=10000000) print(-fopt) # print -probabilty_TF([0.47,0.19]) # print -probabilty_TF([0.41,0.5])
for i in range(2): lb.append(-2.048) ub.append(2.048) elif fu == "DJF3": for i in range(4): lb.append(-5.12) ub.append(5.12) elif fu == "GP": for i in range(2): lb.append(-2) ub.append(2) elif fu == "R": for i in range(4): lb.append(-2.048) ub.append(2.048) elif fu == "Z": for i in range(4): lb.append(-5) ub.append(10) elif fu == "SH": for i in range(6): lb.append(-500) ub.append(500) # xopt, fopt = pso(locals()[fu], lb, ub, maxiter=300, minfunc=1e-8, minstep=1e-8) xopt, fopt = pso(locals()[fu], lb, ub, \ maxiter=9000, minfunc=1e-16, minstep=1e-16, \ swarmsize=460, omega=0.9, phip=0.7, phig=0.2) print "Result: " + str(fopt) print "Parameters:\n" + str(xopt)
sleep(0.05) print x,y,z #X.append(x) #Y.append(y) #Z.append(z) return z print f([0,0]) print f([-3.263,-3.186]) print f([-3.186,-3.263]) print "busqueda PSO" lb = [0.045, 2200] ub = [0.085, 3800] xopt, fopt = pso(f_robot, lb, ub, swarmsize=5, minstep=30) X = [] Y = [] Z = [] print len(X) print xopt print fopt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for i in range(len(X)): xs = X[i] ys = Y[i]
print(D.love) print(d.name) elif test == 10: # pso def myfun(x): x1 = x[0] x2 = x[1] return x1**2 + x2**2 def mycon(x): x1 = x[0] x2 = x[1] return [x1**2 - 10, math.exp(x2) - 6e6] from pyswarm import pso g, fg = pso(myfun, [2, 6], [100, 100], f_ieqcons=mycon) print(f'Extreme point is {g} and corresponding value is {fg}') elif test == 9: # decorator and partial function from functools import wraps def decorator(func): @wraps(func) def new_func(*args): print('Hi, xiongdi') print('Jianghu') return (func(*args) + 10) return new_func
print "\n Your initial parameters are {0}\n".format(x0) print "\n Running a quick profile on the likelihood to estimate evaluation speed...\n" cProfile.run('ln_prob(x0)') ################################## # Now, we sample or maximize..... ################################## if args.sample_or_maximize == 'maximize': def swarm_prob(x): return -ln_prob(x) xopt, fopt = pso(swarm_prob, pmin, pmax, swarmsize=100, omega=0.5, phip=0.5, phig=0.5, maxiter=1000, debug=True) print "\n Printing out the ML noise values for {0}...\n".format(psr.name) fil = open("{0}_MLnoise_nmode{1}.txt".format(psr.name,nmode),'w') for ii in range(len(xopt)): print "{0} = {1}".format(parameters[ii],xopt[ii]) print >>fil, "{0} {1}".format(parameters[ii],xopt[ii]) print "\n Values saved in {0}_MLnoise_nmode{1}.txt".format(psr.name,nmode) else: cov_diag = np.array([0.5,0.5,0.5,0.5]) cov_diag = np.append(cov_diag,0.2*np.ones(len(backends))) cov_diag = np.append(cov_diag,0.5*np.ones(len(backends))) print "\n Now, we sample... \n"
y_train.append(l[-1]) del l[-1] x_train.append(l) y_train = np.array(y_train, dtype='f') x_train = np.array(x_train, dtype='f') #Parameters to optimize: # eta range: [0,1] [0] # gamma range: [0,10] [1] # max_depth range: [1,10] [2] # min_child_weight range: [0,10] [3] # subsample range: (0,1] [4] # colsample_bytree range: (0,1] [5] # num_round sem range ,por opcao, [0,300] [6] # scale_pos_weight sem range ,por opcao, [0,10] [7] # max_delta_step range: [0,10] [8] # Lower-bound: lb = [0, 0, 1, 0, 0, 0.1, 0, 0, 0] # Upper-bound: ub = [1, 10, 10, 10, 1, 1, 300, 10, 10] pso(objective_function, lb, ub, swarmsize=200, omega=0.6, phip=0.6, phig=0.4, maxiter=1000, debug=False)
def main(events_fpath, tables_dir, geom_file=None, start_index=None, stop_index=None): """Perform scans and minimization for events. Parameters ---------- events_fpath : string Path to events HDF5 file tables_dir : string Path to directory containing the retro tables geom_file : string File containing detector geometry start_index : None or int Event index (as ordered in events file) to start on. Specify 0 or `None` to start with the first event. I.e., iterate over `range(start_index, stop_index)`. stop_index : None or int Event index (as ordered in events file) to stop before. I.e., iterate over `range(start_index, stop_index)`. """ # pylint: disable=no-member print('Instantiate a hypo class...') discrete_hypo = DiscreteHypo( hypo_kernels=[const_energy_loss_muon, point_cascade]) # Load events print('Loading events...') events = Events(events_fpath) print(' %d events found' % len(events)) # Load detector geometry array print('Loading detector geometry from "%s"...' % retro.expand(geom_file)) detector_geometry = np.load(retro.expand(geom_file)) # Load tables print('Loading DOM tables...') dom_tables = DOMTimePolarTables(tables_dir=tables_dir, hash_val=None, geom=detector_geometry, use_directionality=False) dom_tables.load_tables() tdi_table = None #print('Loading TDI table...') #tdi_table = TDICartTable( # tables_dir=tables_dir, # use_directionality=False, # #proto_tile_hash='0e28683a74ebea92', # 14^3 tiles, 1 m gridsize, +/- 700 m in x and y, -800 to +600 in z # #proto_tile_hash='8c4770c8371a4025', # single tile, 10 m gridsize +/- 700 m in x and y, -800 to +600 in z # proto_tile_hash='fd29bc306d29bc83', # single tile, QE used; 10 m gridsize +/- 700 m in x and y, -800 to +600 in z # scale=1, #) neg_llh_func_kwargs = dict(dom_tables=dom_tables, tdi_table=tdi_table, noise_charge=NOISE_CHARGE) # Iterate through events for idx, event in enumerate(events[start_index:stop_index]): primary_interaction_str = retro.get_primary_interaction_str(event) print('Working on event #%i / event UID %d' % (idx, event.uid)) print(' %s, %.1f GeV' % (primary_interaction_str, event.neutrino.energy)) print(' track :', event.track) print(' cascade :', event.cascade) truth_params = retro.event_to_hypo_params(event) pinfo_gen_truth = discrete_hypo.get_pinfo_gen(truth_params) neg_llh_truth = get_neg_llh(pinfo_gen=pinfo_gen_truth, event=event, **neg_llh_func_kwargs) print('llh at truth = %.2f' % neg_llh_truth) if MIN_DIMS: print('Will minimize following dimension(s): %s' % MIN_DIMS) min_results = None variable_dims = [] fixed_dims = [] for dim in retro.HYPO_PARAMS_T._fields: if dim in MIN_DIMS: variable_dims.append(dim) else: fixed_dims.append(dim) lower_bounds = [] upper_bounds = [] for dim in MIN_DIMS: if (MIN_USE_RELATIVE_BOUNDS and getattr(REL_BOUNDS, dim) is not None): nom_val = getattr(truth_params, dim) lower = nom_val + getattr(REL_BOUNDS, dim)[0] upper = nom_val + getattr(REL_BOUNDS, dim)[1] else: lower, upper = getattr(ABS_BOUNDS, dim) lower_bounds.append(lower) upper_bounds.append(upper) def get_neg_llh_partial(args): """Minimizer callable that only includes free params as args""" pass xopt1, fopt1 = pso(get_neg_llh_partial, lower_bounds, upper_bounds, kwargs=neg_llh_func_kwargs, minstep=1e-5, minfunc=1e-1, debug=True) if SCAN_DIM_SETS: print('Will scan following sets of dimensions: %s' % str(SCAN_DIM_SETS)) time_to_scan = 0 num_likelihoods = 0 t0 = time.time() for dims in SCAN_DIM_SETS: print('Scanning dimension(s): %s...' % str(dims)) if isinstance(dims, string_types): dims = [dims] nominal_params = deepcopy(truth_params) scan_values = [] for dim in dims: if (SCAN_USE_RELATIVE_BOUNDS and getattr(REL_BOUNDS, dim) is not None): nom_val = getattr(nominal_params, dim) lower = nom_val + getattr(REL_BOUNDS, dim)[0] upper = nom_val + getattr(REL_BOUNDS, dim)[1] else: lower, upper = getattr(ABS_BOUNDS, dim) scan_values.append( np.linspace(lower, upper, NUM_SCAN_POINTS)) ts0 = time.time() neg_llh = scan(hypo_obj=discrete_hypo, event=event, neg_llh_func=get_neg_llh, dims=dims, scan_values=scan_values, nominal_params=nominal_params, neg_llh_func_kwargs=neg_llh_func_kwargs) ts1 = time.time() time_to_scan += ts1 - ts0 num_likelihoods += len(neg_llh) datadump = OrderedDict([ ('filename', event.filename), ('event', event.event), ('uid', event.uid), ('neutrino_energy', event.neutrino.energy), ('primary_interaction', primary_interaction_str), ('dims', dims), ('scan_values', scan_values), ('neg_llh', neg_llh), ('truth', tuple(getattr(truth_params, d) for d in dims)), ('LLH_USE_NOHIT', LLH_USE_NOHIT), ('CASCADE_E_SCALE', CASCADE_E_SCALE), ('TRACK_E_SCALE', TRACK_E_SCALE), ('NOISE_CHARGE', NOISE_CHARGE), ]) fname = ('scan_results_event_%d_uid_%d_dims_%s.pkl' % (event.event, event.uid, '_'.join(dims))) fpath = join(RESULTS_DIR, fname) pickle.dump(datadump, file(fpath, 'wb'), pickle.HIGHEST_PROTOCOL) print('saved scan to "%s"' % fpath) plot_1d_scan(dir=RESULTS_DIR, event=event.event, uid=event.uid) print('') print('Time to scan ({:d} likelihoods): {} s'.format( num_likelihoods, np.round(time_to_scan, 3))) print('Time to scan, dump, and plot: {} s'.format( np.round(time.time() - t0, 3))) print('')
# pickle.dump(simulations, open(os.getcwd() + '/test005_1000_1000_50_' + str(starting_num) + '.obj', 'wb')) # Extract simulations list_of_simulations = [pickle.load(open(os.getcwd() + '/test005_1000_1000_50_' + str(15 + num*50) + '.obj', 'rb')) for num in np.arange(10)] # PSO betas = np.array([sim[1] for simulations in list_of_simulations for sim in simulations]) indices = np.array([sim[1] for simulations in list_of_simulations for sim in simulations]) training_hours = np.arange(1000) Ec = np.reshape(ubem.city_electricity_scaled[training_hours], (ubem.modeling_hours,)) all_buildings = create_all_buildings(ubem=ubem, total_simulations=1, sim_num=6) for i in np.arange(2): cost, pos = pso(ubem=ubem, all_buildings=all_buildings, betas=betas, Ec=Ec, n_particles=10, iters=10, global_pso=False) # for r in np.arange(betas.shape[0]): # print(r) # for r_1 in np.arange(r+1, betas.shape[0]): # print(np.linalg.norm(betas[r] - betas[r_1])) # Extract errors from log files # all_errors = get_simulation_errors() # all_errors = np.array(all_errors).flatten() # # ax = sns.distplot(all_errors, kde=False, hist=True, color='#274a5c', hist_kws={"alpha":1}) # ax.axvline(np.mean(all_errors), color='black', linestyle='-') # plt.xlabel('Mean Absolute Percentage Error') # plt.ylabel('Count') # plt.savefig('/Users/jonathanroth/PycharmProjects/UBEM/Error_Distribution.pdf')
times = [] for executions in range(0, 100): print(str(executions) + "%") start_time = time.time() km = KMeans() min_clusters = 2 max_clusters = 7 lb = [min_clusters, ] lb.extend([0 for i in range(0, km.dimens * max_clusters)]) up = [max_clusters, ] up.extend([10 for i in range(0, km.dimens * max_clusters)]) xopt, fopt = pso(km.execute, lb, up, swarmsize=128, maxiter=8, omega=0.125) end_time = time.time() elapsed_time = end_time - start_time times.append(elapsed_time) precisions.append(fopt) print(times) print(precisions)
# MERGE # ds = [res, res2] # res3 = {} # for k in res.keys(): # res3[k] = list(np.concatenate(np.array(list(d[k] for d in ds)))) # PREPARE DATA res = pickle.load(open('/Users/jonathanroth/PycharmProjects/UBEM/5sims_1000T_500B.obj', 'rb')) error_vec = [np.mean(err) for err in res['error_vec']] betas = res['betas_vec'] all_buildings = np.concatenate(res['prepared_buildings_vec'], axis=0) training_hours = res['training_hours_vec'][0] Ec = np.reshape(ubem.city_electricity_scaled[training_hours], (ubem.modeling_hours,)) # RUN PARTICLE SWARM OPTIMIZER cost, pos = pso(all_buildings=all_buildings, betas=betas, n_particles=25, iters=3, global_pso=True) random_results = random_betas_new(all_buildings=all_buildings, betas=betas, iters=20) # check_error(pos=pos, one_mc_simulation=False, sim_num=0, sim_buildings=500) # # TEST PSO WITH DUMMY EXAMPLE # def rosenbrock_with_args(x, a, b, c=0): # print('shape of x: ', x.shape) # f = (a - x[:, 0]) ** 2 + b * (x[:, 1] - x[:, 0] ** 2) ** 2 + c # print('shape of f: ', f.shape) # return f # x_max = 10 * 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) # cost, pos = optimizer.optimize(rosenbrock_with_args, 1000, a=1, b=100, c=0)
import numpy as np from pyswarm import pso ############################################################################### print('*'*65) print('Example minimization of 4th-order banana function (no constraints)') def myfunc(x): x1 = x[0] x2 = x[1] return x1**4 - 2*x2*x1**2 + x2**2 + x1**2 - 2*x1 + 5 lb = [-3, -1] ub = [2, 6] xopt1, fopt1 = pso(myfunc, lb, ub) print('The optimum is at:') print(' {}'.format(xopt1)) print('Optimal function value:') print(' myfunc: {}'.format(fopt1)) ############################################################################### print('*'*65) print('Example minimization of 4th-order banana function (with constraint)') def mycon(x): x1 = x[0] x2 = x[1] return [-(x1 + 0.25)**2 + 0.75*x2]
def pso_evaluate(self,lb,ub,args={}): """ Optimization using Particle swarm optimization (PSO) algorithm. One function would be considered as the minimization function (with priority). Other cost functions, would be considered as constraints. All functions return mean squared distance between sample and simulated curves Optional inputs for pso: swarmsize : int The number of particles in the swarm (Default: 100) omega : scalar Particle velocity scaling factor (Default: 0.5) phip : scalar Scaling factor to search away from the particles best known position (Default: 0.5) phig : scalar Scaling factor to search away from the swarms best known position (Default: 0.5) maxiter : int The maximum number of iterations for the swarm to search (Default: 100) minstep : scalar The minimum stepsize of swarms best position before the search terminates (Default: 1e-8) minfunc : scalar The minimum change of swarms best objective value before the search terminates (Default: 1e-8) debug : boolean If True, progress statements will be displayed every iteration (Default: False) :param lb: Lower bound values :param ub: Upper bound values :param args: Optional arguments for pso function :return: optimized values """ self.scale = False self.pso_flag = True self.func = self.vclamp_cost func_flag = False cons = list() # Initialize pso optional arguments if 'swarmsize' in args: swarmsize = args['swarmsize'] else: swarmsize = 100 if 'maxiter' in args: maxiter = args['maxiter'] else: maxiter = 100 if 'minstep' in args: minstep = args['minstep'] else: minstep = 1e-8 if 'minfunc' in args: minfunc = args['minfunc'] else: minfunc = 1e-8 if 'debug' in args: debug = args['debug'] else: debug = False # Define minimum acceptable cost value for constrains if 'func' in args: self.func = args['func'] else: self.func = self.vclamp_cost if 'IV_dist' in args: self.IV_dist = args['IV_dist'] else: self.IV_dist = 1e-20 if 'POV_dist' in args: self.POV_dist = args['POV_dist'] else: self.POV_dist = 25e-4 if 'I_dist' in args: self.I_dist = args['I_dist'] else: self.I_dist = 1e-8 if 'V_dist' in args: self.V_dist = args['V_dist'] else: self.V_dist = 1e-20 # Define minimization function and constraints if 'VClamp' in self.sampleData: self.func = self.vclamp_cost func_flag = True if 'IClamp' in self.sampleData: if func_flag == False: self.func = self.iclamp_cost func_flag = True else: cons.append(self.iclamp_cost) if 'IV' in self.sampleData: if func_flag == False: self.func = self.iv_cost func_flag = True else: cons.append(self.iv_cost) if 'POV' in self.sampleData: if func_flag == False: self.func = self.pov_cost func_flag = True else: cons.append(self.pov_cost) xopt, fopt = pso(func=self.func, lb=lb, ub=ub, ieqcons=cons, debug=debug, swarmsize=swarmsize, maxiter=maxiter, minfunc=minfunc, minstep=minstep) return xopt,fopt
def main(): global Xmax global Xmin filename=sys.argv[1] function=sys.argv[2] data=readfile(filename) row,col=data.shape #print max(data[row-4]) newbigger=[] for rownum in xrange(row): newbigger.append(data[rownum][col-1]) midnum=caculatemidnum(newbigger) data=data/midnum if row>0: file_result=open('./result/result_select_2.txt','w') file_result.write(repr('para_a').rjust(40)+repr('para_b').rjust(40)+repr('para_c').rjust(40)+repr('para_d').rjust(40)+repr('para_e').rjust(40)+repr('para_f').rjust(40)+repr('para_g').rjust(40)+repr('error').rjust(40)+'\n') for rownum in xrange(row): '''print data[rownum].min() print data[rownum].max() print data[rownum].mean()''' args=data[rownum] length=len(args) maxPosition=getX(args,length) if maxPosition<30: Xmin=0 Xmax=maxPosition+40-(maxPosition-Xmin) elif maxPosition>length-10: Xmax=length-1 Xmin=maxPosition-40+(Xmax-maxPosition) else: Xmax=maxPosition+10 Xmin=maxPosition-30 iteration=constraint(args,Xmin,Xmax) print('iterations={}'.format(iteration)) print('maxPosition={}'.format(maxPosition)) minNum=args.min() maxNum=args.max() meanNum=args.mean() general=meanNum+maxNum radio=getArrange(args,length)/10 radiomax=radio*maxNum radiomean=radio*meanNum lb=[-1,0,minNum,-1,0,-1,0] ub=[1,0.5,general+radiomean-maxNum,1.1,general+radiomax,1.1,general+radiomax] initialData=[0,0.01,meanNum,-1,maxNum,0.7,maxNum] initialData2=[0,0.03,meanNum,0.1,maxNum,-1,radiomax] print("please wait a moment....") if function=="SSE": xopt4,fopt4=pso(SSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration) print('Optimal function values:') print('SSE:{}'.format(fopt4)) elif function=="RMSE": xopt4,fopt4=pso(RMSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration) print('Optimal function values:') print('RMSE:{}'.format(-fopt4)) elif function=="RScore": xopt4,fopt4=pso(RScore,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration) print('Optimal function values:') print(' R-squared:{}'.format(-fopt4)) elif function=="RERS": xopt4,fopt4=pso(RMSE_RS,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration) print('Optimal function values:') print('1/2*RMSE+1/2*R-squared:{}'.format(-fopt4)) else: xopt4,fopt4=pso(AdjustRS,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration) print('Optimal function values:') print('ajusted R-squared:{}'.format(-fopt4)) print('The optimum is at:') print(' {}'.format(xopt4)) file_result.write(repr(xopt4[0]).rjust(40)+repr(xopt4[1]).rjust(40)+repr(xopt4[2]).rjust(40)+repr(xopt4[3]).rjust(40)+repr(xopt4[4]).rjust(40)+repr(xopt4[5]).rjust(40)+repr(xopt4[6]).rjust(40)+repr(fopt4).rjust(40)+'\n') x=np.linspace(Xmin-2,Xmax+2,10000) y=xopt4[0]+xopt4[1]*x+xopt4[2]/((1+np.exp(-xopt4[3]*(x-xopt4[4])))*(1+np.exp(-xopt4[5]*(x-xopt4[6])))) plt.figure(rownum) for i in xrange(Xmin,Xmax): plt.plot(i+1,args[i],'blue',linestyle='dashed',marker='.') plt.plot(x,y,'r',linewidth=2) plt.plot(maxPosition,args[maxPosition-1],'red',linestyle='dashed',marker='*') plt.xlabel("circle(Time)") plt.ylabel("fluorescence") plt.legend() plt.show() if row>0: file_result.close() print("***********************attention************************************") print('please go to the dir of result to save result_select_2.txt\n')
from pyswarm import pso def banana(x): x1 = x[0] x2 = x[1] return x1**4 - 2*x2*x1**2 + x2**2 + x1**2 - 2*x1 + 5 def con(x): x1 = x[0] x2 = x[1] return [-(x1 + 0.25)**2 + 0.75*x2] lb = [-3, -1] ub = [2, 6] xopt, fopt = pso(banana, lb, ub, f_ieqcons=con) print(xopt) print(fopt) # Optimum should be around x=[0.5, 0.76] with banana(x)=4.5 and con(x)=0
def main(): global length filename = sys.argv[1] function = sys.argv[2] data = readfile(filename) row, col = data.shape newbigger = [] for rownum in xrange(row): newbigger.append(data[rownum][col - 1]) midnum = caculatemidnum(newbigger) data = data / midnum if row > 0: os.system("clear") file_result = open('./result/result_only.txt', 'w') file_result.write( repr('para_a').rjust(40) + repr('para_b').rjust(40) + repr('para_c').rjust(40) + repr('para_d').rjust(40) + repr('para_e').rjust(40) + repr('para_f').rjust(40) + repr('para_g').rjust(40) + repr('error').rjust(40) + '\n') for rownum in xrange(row): '''print data[rownum].min() print data[rownum].max() print data[rownum].mean()''' args = data[rownum] array = data[rownum] length = len(args) iteration = constraint(args, 0, length - 1) print('iterations={}'.format(iteration)) minNum = args.min() maxNum = args.max() #general=maxNum meanNum = args.mean() radio = getX(args, length) Xmin, Xmax = getMinMax(length, radio) quick_sort(array, 0, length - 1) initial_a = array[2] initial_g = array[length - 3] #arrange=getArrangea(args,length) #radiomax=radio*maxNum #radiomean=radio*meanNum lb = [minNum, 0, meanNum, -1, Xmin, -1, Xmin] #ub=[initial_a,0.5,initial_g,1.1,Xmax,1.1,Xmax] ub = [meanNum, 0.5, maxNum, 1.1, Xmax, 1.1, Xmax] #ub=[meanNum,0.5,maxNum,1.1,Xmax,1.1,Xmax] #ub=[minNum+5,0.5,general+radiomax,1.1,general+radiomax,1.1,general+radiomax] initialData = [ initial_a, 0.01, initial_g - initial_a, -1, radio, 0.7, radio ] initialData2 = [ initial_a + 1, 0.03, initial_g - initial_a + 2, 0.1, radio, -1, radio ] print("please wait a moment....") if function == "SSE": xopt4, fopt4 = pso(SSE, lb, ub, args=args, initialData=initialData, initialData2=initialData2, swarmsize=250, maxiter=iteration, minstep=1e-12, minfunc=1e-12) print('Optimal function values:') print('SSE:{}'.format(fopt4)) elif function == "RMSE": xopt4, fopt4 = pso(RMSE, lb, ub, args=args, initialData=initialData, initialData2=initialData2, swarmsize=250, maxiter=iteration, minstep=1e-12, minfunc=1e-12) print('Optimal function values:') print('RMSE:{}'.format(fopt4)) elif function == "RScore": xopt4, fopt4 = pso(RScore, lb, ub, args=args, initialData=initialData, initialData2=initialData2, swarmsize=250, maxiter=iteration, minstep=1e-12, minfunc=1e-12) print('Optimal function values:') print(' R-squared:{}'.format(-fopt4)) elif function == "RERS": xopt4, fopt4 = pso(RMSE_RS, lb, ub, args=args, initialData=initialData, initialData2=initialData2, swarmsize=250, maxiter=iteration, minstep=1e-12, minfunc=1e-12) print('Optimal function values:') print('1/2*RMSE+1/2*R-squared:{}'.format(-fopt4)) else: xopt4, fopt4 = pso(AdjustRS, lb, ub, args=args, initialData=initialData, initialData2=initialData2, swarmsize=250, maxiter=iteration, minstep=1e-12, minfunc=1e-12) print('Optimal function values:') print('ajusted R-squared:{}'.format(-fopt4)) file_result.write( repr(xopt4[0]).rjust(40) + repr(xopt4[1]).rjust(40) + repr(xopt4[2]).rjust(40) + repr(xopt4[3]).rjust(40) + repr(xopt4[4]).rjust(40) + repr(xopt4[5]).rjust(40) + repr(xopt4[6]).rjust(40) + repr(fopt4).rjust(40) + '\n') x = np.linspace(1, length, 10000) y = xopt4[0] + xopt4[1] * x + xopt4[2] / ( (1 + np.exp(-xopt4[3] * (x - xopt4[4]))) * (1 + np.exp(-xopt4[5] * (x - xopt4[6])))) plt.figure(rownum) for i in xrange(length): plt.plot(i + 1, args[i], 'blue', linestyle='dashed', marker='.') plt.plot(x, y, 'r', linewidth=2) plt.xlabel("circle(Time)") plt.ylabel("fluorescence") plt.legend() plt.show() printfresult(xopt4, args, 0, length) if row > 0: file_result.close() print( "***********************attention************************************" ) print('please go to dir of result to save result_only.txt file ')
def main(): global length filename=sys.argv[1] function=sys.argv[2] method=sys.argv[3] data=readfile(filename) row,col=data.shape #print max(data[row-4]) newbigger=[] for rownum in xrange(row): newbigger.append(data[rownum][col-1]) midnum=caculatemidnum(newbigger) data=data/midnum if row>0: os.system("clear") file_result_itera=open('./result/result_all_itera.txt','w') file_result_itera.close() file_result=open('./result/result_all.txt','w') file_result.write(repr('para_a').rjust(40)+repr('para_b').rjust(40)+repr('para_c').rjust(40)+repr('para_d').rjust(40)+repr('para_e').rjust(40)+repr('para_f').rjust(40)+repr('para_g').rjust(40)+repr('error').rjust(40)+'\n') for rownum in xrange(row): '''print data[rownum].min() print data[rownum].max() print data[rownum].mean()''' args=data[rownum] array=data[rownum] length=len(args) iteration=constraint(args,0,length-1) print('iterations={}'.format(iteration)) minNum=args.min() maxNum=args.max() meanNum=args.mean() #general=maxNum radio=getX(args,length) Xmin,Xmax=getMinMax(length,radio) quick_sort(array,0,length-1) initial_a=array[2] initial_g=array[length-3] lb=[minNum,0,meanNum,-1,Xmin,-1,Xmin] ub=[meanNum,0.5,maxNum,1.1,Xmax,1.1,Xmax] initialData=[initial_a,0.01,initial_g-initial_a,-1,radio,0.7,radio] initialData2=[initial_a+1,0.03,initial_g-initial_a+2,0.1,radio,-1,radio] print("please wait a moment....") if function=="SSE": xopt4,fopt4=pso(SSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('SSE:{}'.format(fopt4)) elif function=="RMSE": xopt4,fopt4=pso(RMSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('RMSE:{}'.format(-fopt4)) elif function=="RScore": xopt4,fopt4=pso(RScore,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print(' R-squared:{}'.format(-fopt4)) elif function=="RERS": xopt4,fopt4=pso(RMSE_RS,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('1/2*RMSE+1/2*R-squared:{}'.format(-fopt4)) else: xopt4,fopt4=pso(AdjustRS,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('ajusted R-squared:{}'.format(-fopt4)) file_result.write(repr(xopt4[0]).rjust(40)+repr(xopt4[1]).rjust(40)+repr(xopt4[2]).rjust(40)+repr(xopt4[3]).rjust(40)+repr(xopt4[4]).rjust(40)+repr(xopt4[5]).rjust(40)+repr(xopt4[6]).rjust(40)+repr(fopt4).rjust(40)+'\n') print('The optimum is at:') print(' {}'.format(xopt4)) print('please wait a moment again,it\'s next to minimize the function') fitfunction(xopt4,length,args,method=method) if row>0: file_result.close() print("***********************attention************************************") print('please go to dir of result to save result_all.txt file and result_all_itera.txt')
fun = lambda x: sum([ 100 * (x[i + 1] - x[i]**2)**2 + (1 - x[i])**2 for i in range(NumVars - 1) ]) cons = [{'type': 'ineq', 'fun': lambda x: 9 - x[i]**2} for i in range(NumVars)] x0 = tuple([0 for _ in range(NumVars)]) sol1 = minimize(fun, x0, method='COBYLA', constraints=cons) sol2 = minimize(fun, x0, method='SLSQP', constraints=cons) print sol1 print sol2 # solve with pso print "PSO:" from pyswarm import pso lb = [-3 for i in range(NumVars)] ub = [3 for i in range(NumVars)] cns = [cn['fun'] for cn in cons] print pso(fun, lb, ub, ieqcons=cns) """ Relaxation method: Solution of a Semidefinite Program: Solver: CVXOPT Status: Optimal Initialization Time: 750.234924078 seconds Run Time: 8.43369 seconds Primal Objective Value: 1.67774267808e-08 Dual Objective Value: 1.10015692778e-08 Feasible solution for moments of order 2 scipy.optimize: fun: 4.4963584556077389 maxcv: 0.0 message: 'Maximum number of function evaluations has been exceeded.'
con_index = [] final_index_limit = [21, 2, 4, 4, 3, 4, 3, 5, 7, 7, 7, 3, 2, 2] start_index=0 con_index = [] for index in final_index_limit: temp = [] temp.append(sum(x[start_index:start_index+index]) - 1) temp = temp*index con_index=con_index + temp start_index = index return con_index lb = [0]*74 ub = [1]*74 xopt, fopt = pso(banana,lb, ub,f_ieqcons=con,swarmsize=740,maxiter=1000) print("--- %s seconds ---" % (time.time() - start_time)) # In[ ]: ##Rating fopt # In[ ]: print("----------------------Difference Between Ratings---------------------") for i in range(10): rating = 0 count = 0
# -*- coding: utf-8 -*- """ Created on Tue Jun 30 16:51:01 2015 @author: 8 """ from pyswarm import pso import numpy as np from math import pi, exp, sqrt # def ackley(chrom): chrom = np.array(chrom) sum1 = np.sum(chrom**2) sum2 = np.sum(np.cos(2*pi*chrom)) n = len(chrom) f = -20 * exp(-.2 * sqrt(sum1/n)) - exp(sum2/n) + 20 + exp(1) return f dim = 4 x, f = pso(ackley, [-30]*dim, [30]*dim, maxiter=10000, minfunc=1e-15, minstep=1e-16) print f
excel_to_csv(calibration_range_file) Range = csv_to_data(os.path.join(output_folder,"Range.csv")) lb= np.zeros(len(CB)) ub= np.zeros(len(CB)) for i in range(len(CB)): lb[i] = Range[CB[i]-1,2] ub[i] = Range[CB[i]-1,3] if RB==1: # Runoff print("Starting Runoff Callibration") #lb = [0.65, 0.65, 0.19, 0.77, 1.92, 0.65, 0.65]; #ub = [1.35, 1.35, 23.08, 73.08, 96.15, 1.35, 1.35]; xopt, fopt = pso(MMMF_Cal, lb, ub,args=(), kwargs={},swarmsize=2, omega=0.5, phip=0.5, phig=0.5, maxiter=4, minstep=1e-8,minfunc=1e-8, debug=False) #print (xopt) #print (fopt) log_file.write(xopt,fopt) if RB ==2: # Soil Loss # GC, VSc, DRc # lb = [0.65, 0.00009, 0.02]; # ub = [1.35, 0.00035, 2]; print("Starting soil loss Callibration") xopt, fopt = pso(MMMF_Cal, lb, ub,args=(), kwargs={}, swarmsize=2, omega=0.5, phip=0.5, phig=0.5, maxiter=4, minstep=1e-8, minfunc=1e-8, debug=False) #print (xopt) #print (fopt) log_file.write(str(xopt)) Parameter=['MS','BD','LP', 'EHD', 'per_incp','Et/Eo','CC','GC','PH','NV','D','VSc','VSs','VSz','Kc','Kz','Ks','DRc','DRz','DRs']
def optimizer(): return pyswarm.pso(pso_gsa_fitness, index_lower_bounds, index_upper_bounds, kwargs=kwargs, swarmsize=20)
svm.train(reader.training_attributes, reader.training_labels) train_rate = svm.test(reader.training_attributes, reader.training_labels) test_rate = svm.test(reader.testing_attributes, reader.testing_labels) # output summmary statistics print("") print("C, Gamma : %s" % params) print("Testing Data Set Performance Summary") print("Training Rate : {}%".format(round(train_rate * 100, 2))) print("Testing Rate : {}%".format(round(test_rate * 100, 2))) results.append([list(params), test_rate]) log = -np.log(test_rate) print(log) return log lb = [1, 0.000001] ub = [200, 0.2] xopt, fopt = pso(optimize, lb, ub, swarmsize=100, maxiter=100) print(xopt, fopt) with open('data/' + str(dataset_number) + '_bar/svm_optimize_results.csv', 'wb') as csvfile: writer = csv.writer(csvfile, delimiter=',') for row in results: writer.writerow(row) writer.writerow([xopt, optimize(xopt)])
# Save best if totalfitness < -best_fitness: best_fitness = -totalfitness best_solution = parameter_matrix print(best_fitness) print(best_solution) list_of_best_fitness.append(best_fitness) return totalfitness lb = [3, 2, 3, 2, 3, 2, 3, 2] ub = [23, 29, 23, 29, 23, 29, 23, 29] for runs in range(1): best_solution = np.zeros(len(lb)) best_fitness = 0 list_of_best_fitness = [] xopt, fopt = pso(objfnc, lb, ub, maxiter=20, swarmsize=10) np.savetxt('PSO_Supply2_Best.out', best_solution, delimiter=',') print("DONE! xopt:{} ,fopt:{}".format(xopt, fopt)) # # Save the list of best fitness to a text file # print(len(list_of_best_fitness)) # with open("list_of_bests_PSO.tsv", "a+") as write_f: # for value in list_of_best_fitness: # s = str(value) # write_f.write(s[:s.index('.')] + "\t") # write_f.write("\n")
def main(): global length filename=sys.argv[1] function=sys.argv[2] data=readfile(filename) row,col=data.shape newbigger=[] for rownum in xrange(row): newbigger.append(data[rownum][col-1]) midnum=caculatemidnum(newbigger) data=data/midnum if row>0: os.system("clear") file_result=open('./result/result_only.txt','w') file_result.write(repr('para_a').rjust(40)+repr('para_b').rjust(40)+repr('para_c').rjust(40)+repr('para_d').rjust(40)+repr('para_e').rjust(40)+repr('para_f').rjust(40)+repr('para_g').rjust(40)+repr('error').rjust(40)+'\n') for rownum in xrange(row): '''print data[rownum].min() print data[rownum].max() print data[rownum].mean()''' args=data[rownum] array=data[rownum] length=len(args) iteration=constraint(args,0,length-1) print('iterations={}'.format(iteration)) minNum=args.min() maxNum=args.max() #general=maxNum meanNum=args.mean() radio=getX(args,length) Xmin,Xmax=getMinMax(length,radio) quick_sort(array,0,length-1) initial_a=array[2] initial_g=array[length-3] #arrange=getArrangea(args,length) #radiomax=radio*maxNum #radiomean=radio*meanNum lb=[minNum,0,meanNum,-1,Xmin,-1,Xmin] #ub=[initial_a,0.5,initial_g,1.1,Xmax,1.1,Xmax] ub=[meanNum,0.5,maxNum,1.1,Xmax,1.1,Xmax] #ub=[meanNum,0.5,maxNum,1.1,Xmax,1.1,Xmax] #ub=[minNum+5,0.5,general+radiomax,1.1,general+radiomax,1.1,general+radiomax] initialData=[initial_a,0.01,initial_g-initial_a,-1,radio,0.7,radio] initialData2=[initial_a+1,0.03,initial_g-initial_a+2,0.1,radio,-1,radio] print("please wait a moment....") if function=="SSE": xopt4,fopt4=pso(SSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('SSE:{}'.format(fopt4)) elif function=="RMSE": xopt4,fopt4=pso(RMSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('RMSE:{}'.format(fopt4)) elif function=="RScore": xopt4,fopt4=pso(RScore,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print(' R-squared:{}'.format(-fopt4)) elif function=="RERS": xopt4,fopt4=pso(RMSE_RS,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('1/2*RMSE+1/2*R-squared:{}'.format(-fopt4)) else: xopt4,fopt4=pso(AdjustRS,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('ajusted R-squared:{}'.format(-fopt4)) file_result.write(repr(xopt4[0]).rjust(40)+repr(xopt4[1]).rjust(40)+repr(xopt4[2]).rjust(40)+repr(xopt4[3]).rjust(40)+repr(xopt4[4]).rjust(40)+repr(xopt4[5]).rjust(40)+repr(xopt4[6]).rjust(40)+repr(fopt4).rjust(40)+'\n') x=np.linspace(1,length,10000) y=xopt4[0]+xopt4[1]*x+xopt4[2]/((1+np.exp(-xopt4[3]*(x-xopt4[4])))*(1+np.exp(-xopt4[5]*(x-xopt4[6])))) plt.figure(rownum) for i in xrange(length): plt.plot(i+1,args[i],'blue',linestyle='dashed',marker='.') plt.plot(x,y,'r',linewidth=2) plt.xlabel("circle(Time)") plt.ylabel("fluorescence") plt.legend() plt.show() printfresult(xopt4,args,0,length) if row>0: file_result.close() print("***********************attention************************************") print('please go to dir of result to save result_only.txt file ')
action = select_action(observation, weights) observation, reward, done, _ = env.step(action) reward = -20 if done and t < 499 else reward cum_reward += reward if done: break return -cum_reward weights_dim = 19 lb = np.full(weights_dim, -10) ub = np.full(weights_dim, 10) # print lb, ub xopt, fopt = pso(func=evaluate_weights, lb=lb, ub=ub, maxiter=10, debug=True, swarmsize=100) print xopt, fopt def test(weights): observation = env.reset() cum_reward = 0 for t in range(5000): env.render() action = select_action(observation, weights) observation, reward, done, _ = env.step(action) reward = -20 if done and t < 499 else reward cum_reward += reward
def main(): global length filename=sys.argv[1] function=sys.argv[2] data=readfile(filename) row,col=data.shape #print max(data[row-4]) newbigger=[] for rownum in xrange(row): newbigger.append(data[rownum][col-1]) midnum=caculatemidnum(newbigger) data=data/midnum if row>0: #file_result_itera=open('./result/result_all_itera.txt','w') #file_result_itera.close() file_result=open('./result/result_all_low.txt','w') file_result.write(repr('para_a').rjust(40)+repr('para_b').rjust(40)+repr('para_c').rjust(40)+repr('para_d').rjust(40)+repr('para_e').rjust(40)+repr('para_f').rjust(40)+repr('para_g').rjust(40)+repr('error').rjust(40)+'\n') for rownum in xrange(row): '''print data[rownum].min() print data[rownum].max() print data[rownum].mean()''' args=data[rownum] length=len(args) iteration=constraint(args,0,length-1) print('iterations={}'.format(iteration)) minNum=args.min() maxNum=args.max() meanNum=args.mean() #general=meanNum+maxNum #radio=getArrange(args,length)/10 #arrange=getArrangea(args,length) #radiomax=radio*maxNum #radiomean=radio*meanNum lb=[-1,0,minNum,-1,0,-1,0] ub=[1,0.5,2*meanNum,1.1,maxNum,1.1,maxNum] #ub=[1,0.5,general+radiomean-maxNum,1.1,general+radiomax,1.1,general+radiomax] initialData=[0,0.01,meanNum,-1,maxNum,0.7,maxNum] initialData2=[0,0.03,meanNum,0.1,meanNum,-1,meanNum] print("please wait a moment....") if function=="SSE": xopt4,fopt4=pso(SSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('SSE:{}'.format(fopt4)) elif function=="RMSE": xopt4,fopt4=pso(SSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('RMSE:{}'.format(-fopt4)) elif function=="RScore": xopt4,fopt4=pso(SSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print(' R-squared:{}'.format(-fopt4)) elif function=="REandRS": xopt4,fopt4=pso(SSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('1/2*RMSE+1/2*R-squared:{}'.format(-fopt4)) else: xopt4,fopt4=pso(SSE,lb,ub,args=args,initialData=initialData,initialData2=initialData2,swarmsize=250,maxiter=iteration,minstep=1e-12,minfunc=1e-12) print('Optimal function values:') print('ajusted R-squared:{}'.format(-fopt4)) #fitfunction(xopt4,length,args) file_result.write(repr(xopt4[0]).rjust(40)+repr(xopt4[1]).rjust(40)+repr(xopt4[2]).rjust(40)+repr(xopt4[3]).rjust(40)+repr(xopt4[4]).rjust(40)+repr(xopt4[5]).rjust(40)+repr(xopt4[6]).rjust(40)+repr(fopt4).rjust(40)+'\n') print('The optimum is at:') print(' {}'.format(xopt4)) print('please wait a moment again,it\'s next to minimize the function') #xopt4=minimizefunction(xopt4,length,args) #fitfunction(xopt4,length,args) print("after iteration is at:") print(" {}".format(xopt4)) file_result.write(repr(xopt4[0]).rjust(40)+repr(xopt4[1]).rjust(40)+repr(xopt4[2]).rjust(40)+repr(xopt4[3]).rjust(40)+repr(xopt4[4]).rjust(40)+repr(xopt4[5]).rjust(40)+repr(xopt4[6]).rjust(40)+repr(fopt4).rjust(40)+'\n') fitfunction(xopt4,length,args) '''x=np.linspace(1,length,10000) y=xopt4[0]+xopt4[1]*x+xopt4[2]/((1+np.exp(-xopt4[3]*(x-xopt4[4])))*(1+np.exp(-xopt4[5]*(x-xopt4[6])))) plt.figure(rownum) for i in xrange(length): plt.plot(i+1,args[i],'blue',linestyle='dashed',marker='.') plt.plot(x,y,'r',linewidth=2) plt.xlabel("circle(Time)") plt.ylabel("fluorescence") plt.legend() plt.show()''' if row>0: file_result.close() print("***********************attention************************************") print('please go to dir of result to save result_all_high.txt file ')
####################################################################################### ################################## OPTIMISATION ####################################### ####################################################################################### if (initVmax >= 252 or initVmin <= 216): print('Peak voltage of ' + str(round(initVmax, 3)) + 'V detected, proceeding to optimise with Volt-VAr.') lb = [249, 251, 0.8] ub = [250, 253, 1] xopt, fopt = pso(fitnessFunction, lb, ub, f_ieqcons=constraintFunction, swarmsize=24, maxiter=25, omega=0.9, phip=0.5, phig=0.3, minfunc=0.01, debug=True) optCurve = pfAuto.generateCurve2(pvSize, vNom, xopt) for line in PV: pfAuto.setVVcurve(optCurve, line) pfAuto.runSim() pfAuto.deratePV2(PV, user) pfAuto.runSim()