If you have already read about these fascinating creatures in the previous post about The BAT Algorithm, let’s implement the MATLAB Code for the BAT Algorithm in your favorite computing environment. This code initializes a population of bats and updates their positions and velocities based on the BAT algorithm’s rules. The sphere function is used as the objective function to evaluate the quality of each bat’s position. The algorithm returns the best position and its corresponding fitness value after a specified number of iterations.

% BAT Algorithm in MATLAB

% Parameters
num_bats = 30;
dim = 10;
num_iterations = 100;
freq_min = 0;
freq_max = 2;
A = 0.5;
r0 = 0.5;
alpha = 0.9;
gamma = 0.9;
lb = -10;
ub = 10;

% Initialize bat positions and velocities
positions = lb + (ub-lb).*rand(num_bats, dim);
velocities = zeros(num_bats, dim);
frequencies = zeros(num_bats, 1);
loudness = A * ones(num_bats, 1);
pulse_rate = r0 * ones(num_bats, 1);

% Evaluate initial fitness
fitness = arrayfun(@(i) sphere_func(positions(i, :)), 1:num_bats);
[best_fitness, idx] = min(fitness);
best_position = positions(idx, :);

for iteration = 1:num_iterations
    avg_loudness = mean(loudness);
    avg_pulse_rate = mean(pulse_rate);
    
    % Update bats
    for i = 1:num_bats
        beta = rand();
        frequencies(i) = freq_min + (freq_max - freq_min) * beta;
        velocities(i, :) = velocities(i, :) + (positions(i, :) - best_position) * frequencies(i);
        new_position = positions(i, :) + velocities(i, :);
        
        % Boundary check
        new_position = max(min(new_position, ub), lb);
        
        % Local search
        if rand() > pulse_rate(i)
            epsilon = rand() * 2 - 1; % Random value between -1 and 1
            new_position = positions(i, :) + epsilon * avg_loudness;
        end
        
        % Evaluate new solution
        new_fitness = sphere_func(new_position);
        
        % Greedy mechanism to update if new solution is better and random value is less than loudness
        if new_fitness < fitness(i) && rand() < loudness(i)
            positions(i, :) = new_position;
            fitness(i) = new_fitness;
        end
        
        % Update global best
        if fitness(i) < best_fitness
            best_position = positions(i, :);
            best_fitness = fitness(i);
        end
        
        loudness(i) = loudness(i) * alpha;
        pulse_rate(i) = r0 * (1 - exp(-gamma * iteration));
    end
    
    % Print the best fitness value in each iteration
    fprintf('Iteration %d: Best Fitness = %f\n', iteration, best_fitness);
end

fprintf('\nOptimized Solution: ');
disp(best_position);
fprintf('Best Fitness Value: %f\n', best_fitness);

function result = sphere_func(x)
    % x is a vector of input values
    % result is the value of the sphere function at x
    
    % Compute the sum of squares of elements in x
    result = sum(x.^2);
end

If you’re looking for the Python implementation of the BAT algorithm. Please click here. Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *