From a46408e455e90ce641d0902eab0c9a9b6bdba172 Mon Sep 17 00:00:00 2001 From: Abhishek Maletha <67141747+Abhishek-photon@users.noreply.github.com> Date: Fri, 2 Oct 2020 23:16:18 +0530 Subject: [PATCH 1/2] Create readme.txt --- algorithms/Genetic-Algorithm/real_coded_method/readme.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/readme.txt diff --git a/algorithms/Genetic-Algorithm/real_coded_method/readme.txt b/algorithms/Genetic-Algorithm/real_coded_method/readme.txt new file mode 100644 index 0000000..c010dc6 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/readme.txt @@ -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. From 1fcc20d5d05ecbd77dd10bd0c05793bf209e3814 Mon Sep 17 00:00:00 2001 From: Abhishek Maletha <67141747+Abhishek-photon@users.noreply.github.com> Date: Fri, 2 Oct 2020 23:17:30 +0530 Subject: [PATCH 2/2] Add files via upload --- .../real_coded_method/Mutate.m | 6 + .../RouletteWheelSelection.m | 5 + .../real_coded_method/Run_GA.m | 117 ++++++++++++++++++ .../real_coded_method/SortPopulation.m | 4 + .../real_coded_method/UniformCrossover.m | 7 ++ .../real_coded_method/main.m | 39 ++++++ .../real_coded_method/minone.m | 3 + .../real_coded_method/sphere.m | 3 + 8 files changed, 184 insertions(+) create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/Mutate.m create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/RouletteWheelSelection.m create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/Run_GA.m create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/SortPopulation.m create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/UniformCrossover.m create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/main.m create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/minone.m create mode 100644 algorithms/Genetic-Algorithm/real_coded_method/sphere.m diff --git a/algorithms/Genetic-Algorithm/real_coded_method/Mutate.m b/algorithms/Genetic-Algorithm/real_coded_method/Mutate.m new file mode 100644 index 0000000..ce4ec76 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/Mutate.m @@ -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 \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/real_coded_method/RouletteWheelSelection.m b/algorithms/Genetic-Algorithm/real_coded_method/RouletteWheelSelection.m new file mode 100644 index 0000000..4b08610 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/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/real_coded_method/Run_GA.m b/algorithms/Genetic-Algorithm/real_coded_method/Run_GA.m new file mode 100644 index 0000000..7047fd3 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/Run_GA.m @@ -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 \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/real_coded_method/SortPopulation.m b/algorithms/Genetic-Algorithm/real_coded_method/SortPopulation.m new file mode 100644 index 0000000..11fb8e3 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/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/real_coded_method/UniformCrossover.m b/algorithms/Genetic-Algorithm/real_coded_method/UniformCrossover.m new file mode 100644 index 0000000..cf62536 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/UniformCrossover.m @@ -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 \ No newline at end of file diff --git a/algorithms/Genetic-Algorithm/real_coded_method/main.m b/algorithms/Genetic-Algorithm/real_coded_method/main.m new file mode 100644 index 0000000..b7a7426 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/main.m @@ -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; + + + + + diff --git a/algorithms/Genetic-Algorithm/real_coded_method/minone.m b/algorithms/Genetic-Algorithm/real_coded_method/minone.m new file mode 100644 index 0000000..941f623 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/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/real_coded_method/sphere.m b/algorithms/Genetic-Algorithm/real_coded_method/sphere.m new file mode 100644 index 0000000..4e48857 --- /dev/null +++ b/algorithms/Genetic-Algorithm/real_coded_method/sphere.m @@ -0,0 +1,3 @@ +function z = sphere(x) + z=sum(x.^2); +end \ No newline at end of file