Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real coded Genetic Algorithm #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions algorithms/Genetic-Algorithm/real_coded_method/Mutate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function y = Mutate(x,mu,sigma)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comments explaining. do the same for other uncommented files.
If a wiki link available, give that too.

flag = rand(size(x)) < mu;
r = rand(size(x));
y=x;
y(flag) = x(flag) + sigma*r(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
117 changes: 117 additions & 0 deletions algorithms/Genetic-Algorithm/real_coded_method/Run_GA.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
function out = RunGA(problem, params)

% Problem
CostFunction = problem.CostFunction;
nVar = problem.nVar;
varsize = [1,nVar];
upperbound = problem.upperbound;
lowerbound = problem.lowerbound;

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

% 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 = unifrnd(lowerbound,upperbound,varsize);

% 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] = ...
UniformCrossover(p1.Position, p2.Position,gamma);

end

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

% Mutation
for l = 1:nC

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

% Check bounds
popc(l).position = max(popc(l).position,lowerbound);
popc(l).position = min(popc(l).position,lowerbound);

% 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,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,gamma)
alpha =unifrnd(-gamma, 1+gamma, size(x1));

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

end
39 changes: 39 additions & 0 deletions algorithms/Genetic-Algorithm/real_coded_method/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
clc;
clear;
close all;

%% Problem Definition

problem.CostFunction = @(x) sphere(x);
problem.nVar = 5;
problem.upperbound = 10;
problem.lowerbound = -10;


%% GA Parameters

params.MaxIt = 1000;
params.nPop = 1000;

params.beta = 1;
params.pC = 1;
params.mu = 0.02;
params.sigma = 0.1;
params.gamma = 0.1;
%% Run GA

out = Run_GA(problem, params);


%% Results

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





3 changes: 3 additions & 0 deletions algorithms/Genetic-Algorithm/real_coded_method/minone.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function z= minone(x)
z = sum(x.^2);
end
2 changes: 2 additions & 0 deletions algorithms/Genetic-Algorithm/real_coded_method/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Run the main.m file to execute whole algorithm.
This is a similar minimization algorith but here real parameters are used instead of binary digits.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please briefly explain here the task you are trying to do with GA. GA is an approach to solving a variety of problems. Also, rename readme.txt to readme.md and use markdown formatting.
Perhaps also give a wiki link to the problem you are solving here so that readers can get a deeper understanding if they wish to. Write about what is the difference between real coded vs other approaches. Give a brief outline of the various steps.
Have a look at this repos readme for some inspiration.

https://github.com/OpenMined/PyGrid

You don't have to go all out, remember this is an educational repo. You are expected to educate, explain; not just give code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sir, will do the same ASAP.

3 changes: 3 additions & 0 deletions algorithms/Genetic-Algorithm/real_coded_method/sphere.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function z = sphere(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comments explaining this function.

z=sum(x.^2);
end