OPTMIZATION Practical FILE
OPTMIZATION Practical FILE
TECHNOLOGY, DWARKA
LAB File
EAEPE21
ECAM-2
Submitted to:
Submitted by:
Prof. Aarti Jain
GARVIT
2020UEA6591
INDEX
Code:
% Lower and upper bounds
Lb = [-2 -2];
Ub = [2 2];
nd = length(Lb);
%Initializing parameters and settings
T_init = 1.0;
T_min = 1e-10;
F_min = -1e+100;
max_rej = 250;
max_run = 150;
max_accept = 15;
k = 1;
alpha = 0.95;
Enorm = 1e-2;
guess = Lb+(Ub-Lb).*rand(size(Lb));
%initializing the counter i,j etc
i = 0; j = 0; accept = 0; totaleval = 0;
%Initializing various values
T = T_init;
E_init = f_obj(guess);
E_old = E_init; E_new = E_old;
best = guess;
function z=f_obj(u)
%Rosenbrock's function with f+=0 at (1,1)
z=(u(1)-1)^2+100*(u(2)-u(1)^2)^2;
end
Output:
EXPERIMENT 2
Aim: Write a MATLAB program to compute the optimal output using simulated
annealing algorithm
Code:
%Started Simulated Annealling
while((T>T_min) && (j <= max_rej) && (E_new > F_min))
i = i+1;
if(i >= max_run) || (accept >= max_accept)
T = alpha*T;
totaleval = totaleval + i;
i = 1; accept = 1;
end
Output:
EXPERIMENT 3
Aim: Using Optimization ToolBox, apply Genetic Algorithm on Rosen-brock’s 2D
equation
Code:
% initialize values
x0 = [5;0]; %initial population
a = 100; %number of iterations
% objective function
function f = objectiveFcn(x,a)
f = a*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
end
% constraints function
function [c,ceq] = constraintFcn(x)
c(1) = x(1)^2 + x(2)^2 - 5;
c(2) = 3 - x(1)^2 - x(2)^2;
ceq = []; % No equality constraints
end
Code:
% func - function to be optimized
% lb - lower bound of the solution space
% ub - upper bound of the solution space
% dim - dimension of the solution space
% nFireflies - number of fireflies in the population
% maxGen - maximum number of generations
% Define the optimization function
func = @(x) (x(1) - 5)^2 + (x(2) - 3)^2;
% Set the lower and upper bounds of the solution space
lb = -5
ub = 5
% Set the number of fireflies and dimensions
nFireflies = 100
dim = 2
% Set the maximum number of generations
maxGen = 50
% Initialize the fireflies
fireflies = lb + (ub - lb)*rand(nFireflies, dim)
% Evaluate the fitness of the fireflies
fit = zeros(nFireflies,1);
for i = 1 : nFireflies
fit(i) = func(fireflies(i,:));
end
% Find the best firefly
[best_fit, bestIdx] = min(fit)
best_solution = fireflies(bestIdx, :)
% Set the algorithm parameters
beta0 = 1
gamma = 1
alpha = 0.2
Output:
EXPERIMENT 5
Aim: Write a MATLAB program to compute the optimal output using firefly
algorithm
Code:
% Iterate through generations
for g = 1 : maxGen
for i = 1 : nFireflies
for j = i+1 : nFireflies
% Calculate the distance between fireflies i and j
dist = norm(fireflies(i,:) - fireflies(j,:));
Code:
% fitness_func: the fitness function to be optimized
% n_vars: the number of variables in the problem
% lb: lower bounds for each variable
% ub: upper bounds for each variable
% max_iters: maximum number of iterations
% swarm_size: number of particles in the swarm
% inertia_weight: inertia weight parameter
% cognitive_weight: cognitive weight parameter
% social_weight: social weight parameter
fitness_func = @(x) sum(x.^2);
n_vars = 2
lb = -10
ub = 10
max_iters = 100
swarm_size = 50
inertia_weight = 0.729
cognitive_weight = 1.49445
social_weight = 1.49445
% Initialize the swarm particles
swarm = lb +(ub - lb)*rand(swarm_size, n_vars)
velocities = zeros(swarm_size, n_vars);
fitnesses = zeros(swarm_size, 1);
pbests = swarm;
pbest_fitnesses = inf(swarm_size, 1);
gbest = zeros(1, n_vars);
gbest_fitness = inf;
Output:
EXPERIMENT 7
Aim: Write a MATLAB program to compute the optimal output using particle swarm
optimization algorithm
Code:
%For each particle calculate the fitness value
for i = 1:max_iters
% Evaluate fitness for each particle
for j = 1:swarm_size
fitnesses(j) = fitness_func(swarm(j,:));
%if the fitness value is better than the pbest before,
%set the current value as new pbest
if fitnesses(j) < pbest_fitnesses(j)
pbests(j,:) = swarm(j,:);
pbest_fitnesses(j) = fitnesses(j);
end
if fitnesses(j) < gbest_fitness
gbest = swarm(j,:);
gbest_fitness = fitnesses(j);
end
end
% Update velocities and positions for each particle
for j = 1:swarm_size
r1 = rand(1, n_vars);
r2 = rand(1, n_vars);
velocities(j,:) = inertia_weight * velocities(j,:) + cognitive_weight * r1 .* (pbests(j,:) -
swarm(j,:)) + social_weight * r2 .* (gbest - swarm(j,:));
swarm(j,:) = swarm(j,:) + velocities(j,:);
end
end
% Return the best solution and best fitness found
fprintf('Best solution and best fitness is:')
best_fitness = gbest_fitness;
best_solution = gbest;
disp(best_fitness);
disp(best_solution);
end
Output: