diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/Crossover.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/Crossover.m new file mode 100644 index 0000000..165ef2d --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/Crossover.m @@ -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 \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/DoublePointCrossover.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/DoublePointCrossover.m new file mode 100644 index 0000000..eef07cd --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/DoublePointCrossover.m @@ -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 \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/Mutate.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/Mutate.m new file mode 100644 index 0000000..6a4f93d --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/Mutate.m @@ -0,0 +1,5 @@ +function y = Mutate(x,mu) + flag = rand(size(x)) < mu; + y=x; + y(flag) = 1-x(flag); +end \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/RouletteWheelSelection.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/RouletteWheelSelection.m new file mode 100644 index 0000000..4b08610 --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/RouletteWheelSelection.m @@ -0,0 +1,5 @@ +function i = RouletteWheelSelection(p) + r= rand*sum(p); + c=cumsum(p); + i=find(r<=c,1,'first'); +end \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/Run_GA.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/Run_GA.m new file mode 100644 index 0000000..cb762e5 --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/Run_GA.m @@ -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 \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/SinglePointCrossover.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/SinglePointCrossover.m new file mode 100644 index 0000000..dba1505 --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/SinglePointCrossover.m @@ -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 \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/SortPopulation.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/SortPopulation.m new file mode 100644 index 0000000..11fb8e3 --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/SortPopulation.m @@ -0,0 +1,4 @@ +function pop =SortPopulation(pop) + [~,so] = sort([pop.Cost]); + pop = pop(so); +end \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/UniformCrossover.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/UniformCrossover.m new file mode 100644 index 0000000..d5b81a9 --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/UniformCrossover.m @@ -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 \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/main.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/main.m new file mode 100644 index 0000000..9900821 --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/main.m @@ -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; + + + + + diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/minone.m b/algorithms/Genetic-Algorithm/Minimization of polynomial function/minone.m new file mode 100644 index 0000000..941f623 --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/minone.m @@ -0,0 +1,3 @@ +function z= minone(x) + z = sum(x.^2); +end \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/Minimization of polynomial function/readme.txt b/algorithms/Genetic-Algorithm/Minimization of polynomial function/readme.txt new file mode 100644 index 0000000..2a0f848 --- /dev/null +++ b/algorithms/Genetic-Algorithm/Minimization of polynomial function/readme.txt @@ -0,0 +1 @@ +this is a minimization agorithm for a single variable function using genetic algorithm