-
-
Notifications
You must be signed in to change notification settings - Fork 181
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function y = Mutate(x,mu,sigma) | ||
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 |
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 |
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; | ||
|
||
|
||
|
||
|
||
|
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,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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok sir, will do the same ASAP. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function z = sphere(x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add comments explaining this function. |
||
z=sum(x.^2); | ||
end |
There was a problem hiding this comment.
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.