Skip to content

Commit b727bfb

Browse files
Merge pull request #2 from Abhishek-photon/Abhishek-photon-patch-2
Add files via upload
2 parents a46408e + 1fcc20d commit b727bfb

File tree

8 files changed

+184
-0
lines changed

8 files changed

+184
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function y = Mutate(x,mu,sigma)
2+
flag = rand(size(x)) < mu;
3+
r = rand(size(x));
4+
y=x;
5+
y(flag) = x(flag) + sigma*r(flag);
6+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function i = RouletteWheelSelection(p)
2+
r= rand*sum(p);
3+
c=cumsum(p);
4+
i=find(r<=c,1,'first');
5+
end
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
function out = RunGA(problem, params)
2+
3+
% Problem
4+
CostFunction = problem.CostFunction;
5+
nVar = problem.nVar;
6+
varsize = [1,nVar];
7+
upperbound = problem.upperbound;
8+
lowerbound = problem.lowerbound;
9+
10+
% Params
11+
MaxIt = params.MaxIt;
12+
nPop = params.nPop;
13+
beta = params.beta;
14+
pC = params.pC;
15+
nC = round(pC*nPop/2)*2;
16+
mu = params.mu;
17+
sigma = params.sigma;
18+
gamma = params.gamma;
19+
20+
% Template for Empty Individuals
21+
empty_individual.Position = [];
22+
empty_individual.Cost = [];
23+
24+
% Best Solution Ever Found
25+
bestsol.Cost = inf;
26+
27+
% Initialization
28+
pop = repmat(empty_individual, nPop, 1);
29+
for i = 1:nPop
30+
31+
% Generate Random Solution
32+
pop(i).Position = unifrnd(lowerbound,upperbound,varsize);
33+
34+
% Evaluate Solution
35+
pop(i).Cost = CostFunction(pop(i).Position);
36+
37+
% Compare Solution to Best Solution Ever Found
38+
if pop(i).Cost < bestsol.Cost
39+
bestsol = pop(i);
40+
end
41+
42+
end
43+
44+
% Best Cost of Iterations
45+
bestcost = nan(MaxIt, 1);
46+
47+
% Main Loop
48+
for it = 1:MaxIt
49+
50+
% Selection Probabilities
51+
c = [pop.Cost];
52+
avgc = mean(c);
53+
if avgc ~= 0
54+
c = c/avgc;
55+
end
56+
probs = exp(-beta*c);
57+
58+
% Initialize Offsprings Population
59+
popc = repmat(empty_individual, nC/2, 2);
60+
61+
% Crossover
62+
for k = 1:nC/2
63+
64+
% Select Parents
65+
p1 = pop(RouletteWheelSelection(probs));
66+
p2 = pop(RouletteWheelSelection(probs));
67+
68+
% Perform Crossover
69+
[popc(k, 1).Position, popc(k, 2).Position] = ...
70+
UniformCrossover(p1.Position, p2.Position,gamma);
71+
72+
end
73+
74+
% Convert popc to Single-Column Matrix
75+
popc = popc(:);
76+
77+
% Mutation
78+
for l = 1:nC
79+
80+
% Perform Mutation
81+
popc(l).Position = Mutate(popc(l).Position, mu,sigma);
82+
83+
% Check bounds
84+
popc(l).position = max(popc(l).position,lowerbound);
85+
popc(l).position = min(popc(l).position,lowerbound);
86+
87+
% Evaluation
88+
popc(l).Cost = CostFunction(popc(l).Position);
89+
90+
% Compare Solution to Best Solution Ever Found
91+
if popc(l).Cost < bestsol.Cost
92+
bestsol = popc(l);
93+
end
94+
95+
end
96+
97+
% Merge and Sort Populations
98+
pop = SortPopulation([pop; popc]);
99+
100+
% Remove Extra Individuals
101+
pop = pop(1:nPop);
102+
103+
% Update Best Cost of Iteration
104+
bestcost(it) = bestsol.Cost;
105+
106+
% Display Itertion Information
107+
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(bestcost(it))]);
108+
109+
end
110+
111+
112+
% Results
113+
out.pop = pop;
114+
out.bestsol = bestsol;
115+
out.bestcost = bestcost;
116+
117+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function pop =SortPopulation(pop)
2+
[~,so] = sort([pop.Cost]);
3+
pop = pop(so);
4+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function [y1 , y2] = UniformCrossover(x1,x2,gamma)
2+
alpha =unifrnd(-gamma, 1+gamma, size(x1));
3+
4+
y1 = alpha.*x1 + (1-alpha).*x2;
5+
y2 = alpha.*x2 + (1-alpha).*x1;
6+
7+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
%% Problem Definition
6+
7+
problem.CostFunction = @(x) sphere(x);
8+
problem.nVar = 5;
9+
problem.upperbound = 10;
10+
problem.lowerbound = -10;
11+
12+
13+
%% GA Parameters
14+
15+
params.MaxIt = 1000;
16+
params.nPop = 1000;
17+
18+
params.beta = 1;
19+
params.pC = 1;
20+
params.mu = 0.02;
21+
params.sigma = 0.1;
22+
params.gamma = 0.1;
23+
%% Run GA
24+
25+
out = Run_GA(problem, params);
26+
27+
28+
%% Results
29+
30+
figure;
31+
semilogy(out.bestcost, 'LineWidth', 2);
32+
xlabel('Iterations');
33+
ylabel('Best Cost');
34+
grid on;
35+
36+
37+
38+
39+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function z= minone(x)
2+
z = sum(x.^2);
3+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function z = sphere(x)
2+
z=sum(x.^2);
3+
end

0 commit comments

Comments
 (0)