Skip to content

Commit

Permalink
updated Binary Genetic Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
grantmwilliams committed Apr 8, 2015
1 parent e851c5d commit 005a928
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 87 deletions.
20 changes: 0 additions & 20 deletions Binary Genetic Algorithm/binTooDec.m

This file was deleted.

36 changes: 36 additions & 0 deletions Binary Genetic Algorithm/decode.m
@@ -0,0 +1,36 @@
function xy = decode(Pop,popSize)
% Convert from Binary to Decimal
cPop1 = Pop(1:end, 1:end/2);
cPop2= Pop(1:end, end/2+1:end);

% Sub Function for Binary to Decimal Conversion
temp1 = 0; temp2=0;
d = zeros(1, length(Pop));
e = zeros(1,length(Pop));

for j = 1:popSize
A = cPop1(j, 1:end);
for i =1: length(A)
temp1 = A(i)*2^(length(A)-i) + temp1;
end % End i loop
d(j) = temp1;
temp1 = 0;
end % End j loop

for jj = 1:popSize
B = cPop2(jj, 1:end);
for ii =1:length(B)
temp2 = B(ii)*2^(length(B)-ii) + temp2;
end % End ii loop
e(jj) = temp2;
temp2=0;
end % End jj loop

%-----------------
e = e';
d = d';
%----------------------
xy = [d,e]; % Unnormalized answer

end % End function

8 changes: 8 additions & 0 deletions Binary Genetic Algorithm/getFitness.m
@@ -0,0 +1,8 @@
function [F] = getFitness(xy)
g = ( 1 + ((xy(:,1) + xy(:,2) + 1).^2).*(19 -14*xy(:,1) + ...
3*xy(:,1).^2 -14*xy(:,2) + 6*xy(:,1).*xy(:,2) + 3*xy(:,2).^2) )...
.* (30 + ((2*xy(:,1) - 3*xy(:,2)).^2).*(18 - 32*xy(:,1) + ...
12*xy(:,1).^2 + 48*xy(:,2) - 36*xy(:,1).*xy(:,2) + 27*xy(:,2).^2));
F = g.^(-1);
end

94 changes: 94 additions & 0 deletions Binary Genetic Algorithm/grantsGA.m
@@ -0,0 +1,94 @@
%% Standard Genetic Algorithm
clear;close all;clc;
format long g
%% Parameters
popSize = 100; % Population Size
genome = 10; % Genome Size
mutRate = .01; % Mutation Rate
S = 4; % Tournament Size
limit = 100; % Number of Generations
best = -1e9; % Initialize Best
F = zeros(limit,1); % Initialize Fitness
variableRange = [-3,3];
numberOfVariables = 2;
%% Initialize Population
Pop = round(rand(popSize,genome));

%% Initial Fitness
xy = decode(Pop,popSize); % Convert to Binary
xy = normalizeXY(xy,variableRange); % Normalize to x1 <= XY <= x2
initialFitness = getFitness(xy); % Fitness function goes here
initialConditions = [initialFitness xy];
%% Begin Main Algorithm
tic
for Gen = 1:limit
%% Fitness

xy = decode(Pop,popSize); % Convert to Binary
xy = normalizeXY(xy,variableRange); % Normalize to x1 <= XY <= x2
F = getFitness(xy); %Fitness function goes here

[current,currentIdx] = max(F);

if current > best
best = current;
bestGenome = xy(currentIdx,:);
end

%% Print Stats
fprintf('Gen: %d Best Fitness: %d\n', Gen, max(F));

%% Tournament Selection
T = round(rand(2*popSize,S)*(popSize-1)+1); % Tournaments
[~,idx] = max(F(T),[],2); % Index of Winners
W = T(sub2ind(size(T),(1:2*popSize)',idx)); % Winners

%% 2 Point Crossover

Pop2 = Pop(W(1:2:end),:); % Set Pop2 = Pop Winners 1
P2A = Pop(W(2:2:end),:); % Assemble Pop2 Winners 2

% Split Pop2 for x and y genomes
xPop2 = Pop2(:,1:genome/2);
yPop2 = Pop2(:,genome/2 + 1:end);

% Split P2A for x and y genomes
xP2A = P2A(:,1:genome/2);
yP2A = P2A(:,genome/2+2:end);

% For x genome
Ref = ones(popSize,1)*(1:genome/2); % Reference Matrix
CP = sort(round(rand(popSize,2)*(genome/2-1)+1),2); % Crossover Points
xidx = CP(:,1)*ones(1,genome/2)<Ref & CP(:,2)*ones(1,genome/2)>Ref; % Logical Index
xPop2(xidx) = xP2A(xidx); % Recombine Winners


% For y genome
Ref = ones(popSize,1)*(1:genome/2); % Reference Matrix
CP = sort(round(rand(popSize,2)*(genome/2-1)+1),2); % Crossover Points
yidx = CP(:,1)*ones(1,genome/2)<Ref & CP(:,2)*ones(1,genome/2)>Ref; % Logical Index
yPop2(yidx) = yP2A(yidx); % Recombine Winners

Pop2 = horzcat(xPop2,yPop2);
P2A = horzcat(xP2A,yP2A);

%% Mutation (bitflip)
idx = rand(size(Pop2))<mutRate; % Index for Mutations
Pop2(idx) = Pop2(idx)*-1+1; % Bit Flip Occurs

%% Reset Poplulations
Pop = Pop2;

end % End main loop
%% Final Fitness stuff
finalFitness = getFitness(xy); % Fitness function goes here
finalConditions = [finalFitness xy];

%% Prints Best Stats
disp('-----------------------------------------');
fprintf('Best Fitness: ');
disp(best);
disp('Best Genome: ');
disp(bestGenome);

toc
21 changes: 21 additions & 0 deletions Binary Genetic Algorithm/normalizeXY.m
@@ -0,0 +1,21 @@
function xy = normalizeXY(xy,variableRange)
% Normalizes the range of xy to variableRange(1) <= xy <= variableRange(2)

%% Initialize Vector
R1 = max(max(xy)) - min(min(xy));
for i = 1:length(xy);
A = xy(i,:); % Grab One Row of xy

%% Normalize to 0 <= A <= 1

A = (A-min(min(xy)))/R1;
%% Scale to X <= A <= Y
R2 = variableRange(2)-variableRange(1);
A = A*R2 + variableRange(1);

%% Replace Row of xy with Normalized Version
xy(i,:) = A;
end

end

67 changes: 0 additions & 67 deletions Binary Genetic Algorithm/standardGeneticAlgorithm.m

This file was deleted.

0 comments on commit 005a928

Please sign in to comment.