def get_best_nest(nest, newnest, fitness, n, dim, objf): # Evaluating all new solutions tempnest = numpy.zeros((n, dim)) tempnest = numpy.copy(nest) bench = Benchmark() info = bench.get_info(objf, dim) ub = info['upper'] lb = info['lower'] optimum = info['best'] for j in range(0, n): #for j=1:size(nest,1), fun_fitness = bench.get_function(objf) fnew = fun_fitness(newnest[j, :]) if fnew <= fitness[j]: fitness[j] = fnew tempnest[j, :] = newnest[j, :] # Find the current best fmin = min(fitness) K = numpy.argmin(fitness) bestlocal = tempnest[K, :] return fmin, bestlocal, tempnest, fitness
def __init__(self, dim): super(NonContinuousRotatedRastrigin, self).__init__('Non-continuous Rotated Rastrigin', dim, -100, 100) self.func = Benchmark().get_function(13)
def __init__(self, dim): super(CECRastrigin, self).__init__('CEC Rastrigin', dim, -100, 100) self.func = Benchmark().get_function(11)
def __init__(self, dim): super(RotatedGriewank, self).__init__('Rotated Griewank', dim, -100, 100) self.func = Benchmark().get_function(10)
def CS(objf, n, MaxGeneration): dim = 30 bench = Benchmark() info = bench.get_info(objf, dim) # dim = info['dimension'] ub = info['upper'] lb = info['lower'] optimum = info['best'] #lb=-1 #ub=1 #n=50 N_IterTotal = MaxGeneration # Discovery rate of alien eggs/solutions pa = 0.25 nd = dim # Lb=[lb]*nd # Ub=[ub]*nd convergence = [] # RInitialize nests randomely nest = numpy.random.rand(n, dim) * (ub - lb) + lb new_nest = numpy.zeros((n, dim)) new_nest = numpy.copy(nest) bestnest = [0] * dim fitness = numpy.zeros(n) fitness.fill(float("inf")) s = solution() print("CS is optimizing " + str(objf)) timerStart = time.time() s.startTime = time.strftime("%Y-%m-%d-%H-%M-%S") fmin, bestnest, nest, fitness = get_best_nest(nest, new_nest, fitness, n, dim, objf) convergence = [] # Main loop counter for iter in range(0, N_IterTotal): # Generate new solutions (but keep the current best) new_nest = get_cuckoos(nest, bestnest, lb, ub, n, dim) # Evaluate new solutions and find best fnew, best, nest, fitness = get_best_nest(nest, new_nest, fitness, n, dim, objf) new_nest = empty_nests(new_nest, pa, n, dim) # Evaluate new solutions and find best fnew, best, nest, fitness = get_best_nest(nest, new_nest, fitness, n, dim, objf) if fnew < fmin: fmin = fnew bestnest = best if (iter % 100 == 0): print([ 'At iteration ' + str(iter) + ' the best fitness is ' + str(fmin) + ": CS" + " :" + str(objf) ]) if (iter % 1000 == 0): convergence.append(fmin) convergence.append(Lightn[0]) convergence.append(Lightn[int(n / 2)]) convergence.append(Lightn[n - 1]) convergence.append(numpy.sum(Lightn) / n) convergence.append(numpy.std(Lightn)) timerEnd = time.time() s.endTime = time.strftime("%Y-%m-%d-%H-%M-%S") s.executionTime = timerEnd - timerStart s.convergence = convergence s.optimizer = "CS" s.objfname = "F" + str(objf) return s
def __init__(self, dim): super(CompositionFunction8, self).__init__('Composition Function 8', dim, -100, 100) self.func = Benchmark().get_function(28)
def __init__(self, dim): super(RotatedExpandedGriewankPlusRosenbrock, self).__init__('Rotated Expanded Griewank Plus Rosenbrock', dim, -100, 100) self.func = Benchmark().get_function(19)
def __init__(self, dim): super(RotatedKatsuura, self).__init__('Rotated Katsuura', dim, -100, 100) self.func = Benchmark().get_function(16)
def __init__(self, dim): super(DifferentPowers, self).__init__('Different Powers', dim, -100, 100) self.func = Benchmark().get_function(5)
def __init__(self, dim): super(RotatedDiscus, self).__init__('Rotated Discus', dim, -100, 100) self.func = Benchmark().get_function(4)
def __init__(self, dim): super(BentCigar, self).__init__('Bent Cigar', dim, -100, 100) self.func = Benchmark().get_function(3)
def __init__(self, dim): super(RotatedElliptic, self).__init__('Rotated Elliptic', dim, -100, 100) self.func = Benchmark().get_function(2)
def __init__(self, dim): super(CECSphere, self).__init__('CEC Sphere', dim, -100, 100) self.func = Benchmark().get_function(1)
if de_strategy == 'current_to_pbest_1' or de_strategy == 'rand_to_pbest_1': p_best_rate = args.p_best_rate archive_rate = args.archive_rate num_children = 0 if de_alg == 'plus_de' or de_alg == 'wi_de': num_children = int(np.floor(pop_size * args.children_size_rate)) num_children = max(num_children, 1) subset_size = 0 if de_alg == 'sts_de': subset_size = int(np.floor(pop_size * args.subset_size_rate)) subset_size = max(subset_size, 2) remaining_evals = 100 * dim fbench = Benchmark() info = fbench.get_info(func_id, dim) fun = fbench.get_function(func_id) lower_bounds = np.full(dim, info['lower']) upper_bounds = np.full(dim, info['upper']) de = None if de_alg == 'syn_de': de = SynchronousDE(fun, dim, lower_bounds, upper_bounds, remaining_evals, pop_size, de_strategy, de_sf, de_cr, p_best_rate, archive_rate) elif de_alg == 'asy_de': de = AsynchronousDE(fun, dim, lower_bounds, upper_bounds, remaining_evals, pop_size, de_strategy, de_sf, de_cr, p_best_rate, archive_rate) elif de_alg == 'wi_de': de = WIDE(fun, dim, lower_bounds, upper_bounds, remaining_evals, pop_size, de_strategy, de_sf, de_cr, p_best_rate, archive_rate, num_children) elif de_alg == 'plus_de': de = MuPlusLambdaDE(fun, dim, lower_bounds, upper_bounds, remaining_evals, pop_size, de_strategy, de_sf, de_cr, p_best_rate, archive_rate, num_children) elif de_alg == 'sts_de':
def __init__(self, dim): super(CECSchwefel, self).__init__('CEC Schwefel', dim, -100, 100) self.func = Benchmark().get_function(14)
def __init__(self, dim): super(RotatedSchwefel, self).__init__('Rotated Schwefel', dim, -100, 100) self.func = Benchmark().get_function(15)
def __init__(self, dim): super(RotatedRosenbrock, self).__init__('Rotated Rosenbrock', dim, -100, 100) self.func = Benchmark().get_function(6)
def __init__(self, dim): super(RotatedLunacekBiRastrigin, self).__init__('Rotated Lunacek Bi-Rastrigin', dim, -100, 100) self.func = Benchmark().get_function(18)
def __init__(self, dim): super(RotatedSchaffersF7, self).__init__('Rotated Schaffers F7', dim, -100, 100) self.func = Benchmark().get_function(7)
def __init__(self, dim): super(RotatedExpandedScafferF6, self).__init__('Rotated Expanded Scaffer F6', dim, -100, 100) self.func = Benchmark().get_function(20)
def __init__(self, dim): super(RotatedAckley, self).__init__('Rotated Ackley', dim, -100, 100) self.func = Benchmark().get_function(8)
def PFA(objf, n, MaxGeneration): dim = 30 bench = Benchmark() info = bench.get_info(objf, dim) # dim = info['dimension'] ub = info['upper'] lb = info['lower'] optimum = info['best'] #General parameters #n=50 #number of fireflies # dim=10000 #dim #lb=-50 #ub=50 #MaxGeneration=500 #FFA parameters alpha = 0.50 # Randomness 0--1 (highly random) betamin = 0.50 # minimum value of beta gamma = 1 # Absorption coefficient zn = numpy.ones(n) zn.fill(float("inf")) #ns(i,:)=Lb+(Ub-Lb).*rand(1,d); ns = numpy.random.uniform(0, 1, (n, dim)) * (ub - lb) + lb Lightn = numpy.ones(n) Lightn.fill(float("inf")) Lightnprev = numpy.ones(n) Lightnprev.fill(float("inf")) #[ns,Lightn]=init_ffa(n,d,Lb,Ub,u0) convergence = [] s = solution() print("PFA is optimizing F" + str(objf)) timerStart = time.time() s.startTime = time.strftime("%Y-%m-%d-%H-%M-%S") # Main loop for k in range(0, MaxGeneration): # start iterations #% This line of reducing alpha is optional #alpha=alpha_new(alpha,MaxGeneration); Lightnprev = Lightn #% Evaluate new solutions (for all n fireflies) fun_fitness = bench.get_function(objf) for i in range(0, n): zn[i] = fun_fitness(ns[i, :]) Lightn[i] = zn[i] # Ranking fireflies by their light intensity/objectives Lightn = numpy.sort(zn) Index = numpy.argsort(zn) ns = ns[Index, :] #Find the current best nso = ns Lighto = Lightn nbest = ns[0, :] Lightbest = Lightn[0] #% For output only fbest = Lightbest #% Move all fireflies to the better locations # [ns]=ffa_move(n,d,ns,Lightn,nso,Lighto,nbest,... # Lightbest,alpha,betamin,gamma,Lb,Ub); scale = numpy.ones(dim) * abs(ub - lb) for i in range(0, n): # The attractiveness parameter beta=exp(-gamma*r) for j in range(0, n): # r=numpy.sqrt(numpy.sum((ns[i,:]-ns[j,:])**2)); # r2=numpy.sqrt(numpy.sum((ns[i,:]-ns[0,:])**2)); r = numpy.sum((ns[i, :] - ns[j, :])) r2 = numpy.sum((ns[0, :] - ns[j, :])) #r=1 # Update moves if Lightn[i] > Lighto[j]: # Brighter and more attractive # PropFA parameters per = ((k / MaxGeneration) * 100) / 75 per2 = numpy.heaviside(per - 1, 0.5) ratA = (numpy.absolute(Lightn[i]) - numpy.absolute( Lightnprev[i])) / max(numpy.absolute(Lightn[i]), numpy.absolute(Lightnprev[i])) ratB = (numpy.absolute(Lightn[j]) - numpy.absolute( Lightn[i])) / max(numpy.absolute(Lightn[j]), numpy.absolute(Lightn[i])) ratC = (numpy.absolute(fbest) - numpy.absolute( Lightn[i])) / max(numpy.absolute(fbest), numpy.absolute(Lightn[i])) ratAvg = (ratA + ratB + ratC) / 3 scale2 = numpy.absolute(ub - lb) if (Lightnprev[i] == Lightn[i]): alpha = 10 else: r3 = numpy.sum((ns[0, :] - ns[n - 1, :])) alpha = (r2 / 10000) * ratAvg * numpy.exp(-k * per2) if (Lightnprev[i] == Lightn[i]): gamma = 1 else: gamma = (ratB / ratC) beta0 = 1 beta = (beta0 - betamin) * numpy.exp( -gamma * r**2) + betamin beta2 = (beta0 - betamin) * numpy.exp( -gamma * r2**2) + betamin tmpf = alpha * (numpy.random.rand(dim) - 0.5) * scale2 #ns[i,:]=ns[i,:]*(1-beta)+nso[j,:]*beta+tmpf ns[i, :] = ns[i, :] + (beta * (nso[j, :] - ns[i, :])) + ( beta2 * (nso[0, :] - ns[i, :])) + tmpf #ns=numpy.clip(ns, lb, ub) IterationNumber = k BestQuality = fbest if (k % 1 == 0): print([ 'At iteration ' + str(k) + ' the best fitness is ' + str(BestQuality) + ": PFA" + " :" + str(objf) ]) if (k % 1000 == 0): convergence.append(fbest) # ######5################# End main loop convergence.append(Lightn[0]) convergence.append(Lightn[int(n / 2)]) convergence.append(Lightn[n - 1]) convergence.append(numpy.sum(Lightn) / n) convergence.append(numpy.std(Lightn)) timerEnd = time.time() s.endTime = time.strftime("%Y-%m-%d-%H-%M-%S") s.executionTime = timerEnd - timerStart s.convergence = convergence s.optimizer = "PFA" s.objfname = "F" + str(objf) return s
def __init__(self, dim): super(RotatedWeierstrass, self).__init__('Rotated Weierstrass', dim, -100, 100) self.func = Benchmark().get_function(9)