Skip to content

Commit 95e1c4a

Browse files
Run the main.m which the main script of algorithm
1 parent b640385 commit 95e1c4a

File tree

10 files changed

+196
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)