def tune_fit(cxpb, mu, alpha, cxmode): #--setup the parameter space nx = 5 BOUNDS = {} for i in range(1, nx + 1): BOUNDS['x' + str(i)] = ['float', -100, 100] #--setup the ES algorithm es = ES(mode='min', bounds=BOUNDS, fit=sphere, lambda_=80, mu=mu, mutpb=0.1, alpha=alpha, cxmode=cxmode, cxpb=cxpb, ncores=1, seed=1) #--Evolute the ES object and obtains y_best #--turn off verbose for less algorithm print-out when tuning x_best, y_best, es_hist = es.evolute(ngen=100, verbose=0) return y_best #returns the best score
def test_es(): #Define the fitness function def FIT(individual): """Sphere test objective function. F(x) = sum_{i=1}^d xi^2 d=1,2,3,... Range: [-100,100] Minima: 0 """ y = sum(x**2 for x in individual) return y #Setup the parameter space (d=5) nx = 5 BOUNDS = {} for i in range(1, nx + 1): BOUNDS['x' + str(i)] = ['float', -100, 100] es = ES(mode='min', bounds=BOUNDS, fit=FIT, lambda_=80, mu=40, mutpb=0.25, cxmode='blend', cxpb=0.7, ncores=1, seed=1) x_best, y_best, es_hist = es.evolute(ngen=100, verbose=0)
def tune_fit(cxpb, mu, alpha, cxmode, mutpb): #--setup the ES algorithm es = ES(mode='min', bounds=BOUNDS, fit=BEAM, lambda_=80, mu=mu, mutpb=mutpb, alpha=alpha, cxmode=cxmode, cxpb=cxpb, ncores=1, seed=1) #--Evolute the ES object and obtains y_best #--turn off verbose for less algorithm print-out when tuning x_best, y_best, es_hist = es.evolute(ngen=100, verbose=0) return y_best #returns the best score
#Define the fitness function def FIT(individual): """Sphere test objective function. F(x) = sum_{i=1}^d xi^2 d=1,2,3,... Range: [-100,100] Minima: 0 """ y = sum(x**2 for x in individual) return y #Setup the parameter space (d=5) nx = 5 BOUNDS = {} for i in range(1, nx + 1): BOUNDS['x' + str(i)] = ['float', -100, 100] es = ES(mode='min', bounds=BOUNDS, fit=FIT, lambda_=80, mu=40, mutpb=0.25, cxmode='blend', cxpb=0.7, ncores=1, seed=1) x_best, y_best, es_hist = es.evolute(ngen=100, verbose=1)
bounds['x4'] = ['float', 10, 200] ######################## # Setup and evolute HHO ######################## hho = HHO(mode='min', bounds=bounds, fit=Vessel, nhawks=50, int_transform='minmax', ncores=1, seed=1) x_hho, y_hho, hho_hist=hho.evolute(ngen=200, verbose=False) assert Vessel(x_hho) == y_hho ######################## # Setup and evolute ES ######################## es = ES(mode='min', fit=Vessel, cxmode='cx2point', bounds=bounds, lambda_=60, mu=30, cxpb=0.7, mutpb=0.2, seed=1) x_es, y_es, es_hist=es.evolute(ngen=200, verbose=False) assert Vessel(x_es) == y_es ######################## # Setup and evolute PESA ######################## pesa=PESA(mode='min', bounds=bounds, fit=Vessel, npop=60, mu=30, alpha_init=0.01, alpha_end=1.0, cxpb=0.7, mutpb=0.2, alpha_backdoor=0.05) x_pesa, y_pesa, pesa_hist=pesa.evolute(ngen=200, verbose=False) assert Vessel(x_pesa) == y_pesa ######################## # Setup and evolute BAT ######################## bat=BAT(mode='min', bounds=bounds, fit=Vessel, nbats=50, fmin = 0 , fmax = 1, A=0.5, r0=0.5, levy = True, seed = 1, ncores=1)
#--------------------------------- # GA #--------------------------------- ga = ES(mode='min', bounds=space, fit=ACKLEY, lambda_=50, mu=25, mutpb=0.15, alpha=0.5, cxmode='blend', cxpb=0.85, ncores=1, seed=1) x_ga, y_ga, ga_hist = ga.evolute(ngen=150, verbose=0) #--------------------------------- # GWO #--------------------------------- gwo = GWO(mode='min', fit=ACKLEY, bounds=space, nwolves=50, ncores=1, seed=1) x_gwo, y_gwo, gwo_hist = gwo.evolute(ngen=150, verbose=0) #--------------------------------- # WOA #--------------------------------- woa = WOA(mode='min', bounds=space, fit=ACKLEY, nwhales=50, a0=1.5,
seed=1) x_pesa, y_pesa, pesa_hist=pesa.evolute(ngen=300, x0=x0, verbose=False) #Run GA #use same optimized hyperparameters from PESA (or repeat tuning again for GA) ga=ES(mode='min', bounds=bounds, fit=Vessel, cxmode='cx2point', lambda_=sorted_res['npop'].iloc[0], mu=int(sorted_res['frac'].iloc[0]*sorted_res['npop'].iloc[0]), mutpb=sorted_res['mutpb'].iloc[0], cxpb=sorted_res['cxpb'].iloc[0], ncores=1, seed=1) #filter initial guess for GA #lambda_=int(sorted_res['npop'].iloc[0]) #x0_ga=x0[:lambda_] x_ga, y_ga, ga_hist=ga.evolute(ngen=300, x0=None, verbose=0) #or use x0_ga if you like #(random guess seems to be better) #************************************************************* # Part VI: Post-processing #************************************************************* #plot results plt.figure() plt.plot(pesa_hist, label='PESA') plt.plot(ga_hist, label='GA') plt.xlabel('Generation') plt.ylabel('Fitness') plt.legend() plt.savefig('ex2_ans21_fitness.png',format='png', dpi=300, bbox_inches="tight") #print best results