Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function [y1, y2] = Crossover(x1,x2)
m=randi([1,3]);
switch m
case 1
[y1, y2] = SinglePointCrossover(x1,x2);
case 2
[y1, y2] = DoublePointCrossover(x1,x2);
otherwise
[y1, y2] = UniformCrossover(x1,x2);
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function [y1,y2] = DoublePointCrossover(x1,x2)
nVar = numel(x1);

q = randperm(nVar);
j1 = min(q(1),q(2));
j2 = max(q(1),q(2));

y1 = [x1(1:j1) x2(j1+1:j2) x1(j2+1:end)];
y2 = [x2(1:j1) x1(j1+1:j2) x2(j2+1:end)];

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function y = Mutate(x,mu)
flag = rand(size(x)) < mu;
y=x;
y(flag) = 1-x(flag);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function i = RouletteWheelSelection(p)
r= rand*sum(p);
c=cumsum(p);
i=find(r<=c,1,'first');
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
function out = RunGA(problem, params)

% Problem
CostFunction = problem.CostFunction;
nVar = problem.nVar;

% Params
MaxIt = params.MaxIt;
nPop = params.nPop;
beta = params.beta;
pC = params.pC;
nC = round(pC*nPop/2)*2;
mu = params.mu;

% Template for Empty Individuals
empty_individual.Position = [];
empty_individual.Cost = [];

% Best Solution Ever Found
bestsol.Cost = inf;

% Initialization
pop = repmat(empty_individual, nPop, 1);
for i = 1:nPop

% Generate Random Solution
pop(i).Position = randi([0, 1], 1, nVar);

% Evaluate Solution
pop(i).Cost = CostFunction(pop(i).Position);

% Compare Solution to Best Solution Ever Found
if pop(i).Cost < bestsol.Cost
bestsol = pop(i);
end

end

% Best Cost of Iterations
bestcost = nan(MaxIt, 1);

% Main Loop
for it = 1:MaxIt

% Selection Probabilities
c = [pop.Cost];
avgc = mean(c);
if avgc ~= 0
c = c/avgc;
end
probs = exp(-beta*c);

% Initialize Offsprings Population
popc = repmat(empty_individual, nC/2, 2);

% Crossover
for k = 1:nC/2

% Select Parents
p1 = pop(RouletteWheelSelection(probs));
p2 = pop(RouletteWheelSelection(probs));

% Perform Crossover
[popc(k, 1).Position, popc(k, 2).Position] = ...
Crossover(p1.Position, p2.Position);

end

% Convert popc to Single-Column Matrix
popc = popc(:);

% Mutation
for l = 1:nC

% Perform Mutation
popc(l).Position = Mutate(popc(l).Position, mu);

% Evaluation
popc(l).Cost = CostFunction(popc(l).Position);

% Compare Solution to Best Solution Ever Found
if popc(l).Cost < bestsol.Cost
bestsol = popc(l);
end

end

% Merge and Sort Populations
pop = SortPopulation([pop; popc]);

% Remove Extra Individuals
pop = pop(1:nPop);

% Update Best Cost of Iteration
bestcost(it) = bestsol.Cost;

% Display Itertion Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(bestcost(it))]);

end


% Results
out.pop = pop;
out.bestsol = bestsol;
out.bestcost = bestcost;

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function [y1, y2] = SinglePointCrossover(x1,x2)
nVar = numel(x1);
j = randi([1, nVar-1]);
y1 = [x1(1:j) x2(j+1:end)];
y2 = [x2(1:j) x1(j+1:end)];
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function pop =SortPopulation(pop)
[~,so] = sort([pop.Cost]);
pop = pop(so);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function [y1 , y2] = UniformCrossover(x1,x2)
alpha =randi([0,1], size(x1));

y1 = alpha.*x1 + (1-alpha).*x2;
y2 = alpha.*x2 + (1-alpha).*x1;

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
clc;
clear;
close all;

%% Problem Definition

problem.CostFunction = @(x) minone(x);
problem.nVar = 100;


%% GA Parameters

params.MaxIt = 150;
params.nPop = 100;

params.beta = 1;
params.pC = 1;
params.mu = 0.02;

%% Run GA

out = Run_GA(problem, params);


%% Results

figure;
plot(out.bestcost, 'LineWidth', 2);
xlabel('Iterations');
ylabel('Best Cost');
grid on;





Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function z= minone(x)
z = sum(x.^2);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a minimization agorithm for a single variable function using genetic algorithm