Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
110
Task/Evolutionary-algorithm/MATLAB/evolutionary-algorithm-3.m
Normal file
110
Task/Evolutionary-algorithm/MATLAB/evolutionary-algorithm-3.m
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
%% Genetic Algorithm -- Solves For A User Input String
|
||||
|
||||
% #### PLEASE NOTE: you can change the selection and crossover type in the
|
||||
% parameters and see how the algorithm changes. ####
|
||||
|
||||
clear;close all;clc; %Clears variables, closes windows, and clears the command window
|
||||
tic % Begins the timer
|
||||
|
||||
%% Select Target String
|
||||
target = 'METHINKS IT IS LIKE A WEASEL';
|
||||
% *Can Be Any String With Any Values and Any Length!*
|
||||
% but for this example we use 'METHINKS IT IS LIKE A WEASEL'
|
||||
|
||||
%% Parameters
|
||||
popSize = 1000; % Population Size (100-10000 generally produce good results)
|
||||
genome = length(target); % Genome Size
|
||||
mutRate = .01; % Mutation Rate (5%-25% produce good results)
|
||||
S = 4; % Tournament Size (2-6 produce good results)
|
||||
best = Inf; % Initialize Best (arbitrarily large)
|
||||
MaxVal = max(double(target)); % Max Integer Value Needed
|
||||
ideal = double(target); % Convert Target to Integers
|
||||
|
||||
selection = 0; % 0: Tournament
|
||||
% 1: 50% Truncation
|
||||
|
||||
crossover = 1; % 0: Uniform crossover
|
||||
% 1: 1 point crossover
|
||||
% 2: 2 point crossover
|
||||
%% Initialize Population
|
||||
Pop = round(rand(popSize,genome)*(MaxVal-1)+1); % Creates Population With Corrected Genome Length
|
||||
|
||||
for Gen = 1:1e6 % A Very Large Number Was Chosen, But Shouldn't Be Needed
|
||||
|
||||
%% Fitness
|
||||
|
||||
% The fitness function starts by converting the characters into integers and then
|
||||
% subtracting each element of each member of the population from each element of
|
||||
% the target string. The function then takes the absolute value of
|
||||
% the differences and sums each row and stores the function as a mx1 matrix.
|
||||
|
||||
F = sum(abs(bsxfun(@minus,Pop,ideal)),2);
|
||||
|
||||
|
||||
|
||||
% Finding Best Members for Score Keeping and Printing Reasons
|
||||
[current,currentGenome] = min(F); % current is the minimum value of the fitness array F
|
||||
% currentGenome is the index of that value in the F array
|
||||
|
||||
% Stores New Best Values and Prints New Best Scores
|
||||
if current < best
|
||||
best = current;
|
||||
bestGenome = Pop(currentGenome,:); % Uses that index to find best value
|
||||
|
||||
fprintf('Gen: %d | Fitness: %d | ',Gen, best); % Formatted printing of generation and fitness
|
||||
disp(char(bestGenome)); % Best genome so far
|
||||
elseif best == 0
|
||||
break % Stops program when we are done
|
||||
end
|
||||
|
||||
%% Selection
|
||||
|
||||
% TOURNAMENT
|
||||
if selection == 0
|
||||
T = round(rand(2*popSize,S)*(popSize-1)+1); % Tournaments
|
||||
[~,idx] = min(F(T),[],2); % Index to Determine Winners
|
||||
W = T(sub2ind(size(T),(1:2*popSize)',idx)); % Winners
|
||||
|
||||
% 50% TRUNCATION
|
||||
elseif selection == 1
|
||||
[~,V] = sort(F,'descend'); % Sort Fitness in Ascending Order
|
||||
V = V(popSize/2+1:end); % Winner Pool
|
||||
W = V(round(rand(2*popSize,1)*(popSize/2-1)+1))'; % Winners
|
||||
end
|
||||
|
||||
%% Crossover
|
||||
|
||||
% UNIFORM CROSSOVER
|
||||
if crossover == 0
|
||||
idx = logical(round(rand(size(Pop)))); % Index of Genome from Winner 2
|
||||
Pop2 = Pop(W(1:2:end),:); % Set Pop2 = Pop Winners 1
|
||||
P2A = Pop(W(2:2:end),:); % Assemble Pop2 Winners 2
|
||||
Pop2(idx) = P2A(idx); % Combine Winners 1 and 2
|
||||
|
||||
% 1-POINT CROSSOVER
|
||||
elseif crossover == 1
|
||||
Pop2 = Pop(W(1:2:end),:); % New Population From Pop 1 Winners
|
||||
P2A = Pop(W(2:2:end),:); % Assemble the New Population
|
||||
Ref = ones(popSize,1)*(1:genome); % The Reference Matrix
|
||||
idx = (round(rand(popSize,1)*(genome-1)+1)*ones(1,genome))>Ref; % Logical Indexing
|
||||
Pop2(idx) = P2A(idx); % Recombine Both Parts of Winners
|
||||
|
||||
% 2-POINT CROSSOVER
|
||||
elseif crossover == 2
|
||||
Pop2 = Pop(W(1:2:end),:); % New Pop is Winners of old Pop
|
||||
P2A = Pop(W(2:2:end),:); % Assemble Pop2 Winners 2
|
||||
Ref = ones(popSize,1)*(1:genome); % Ones Matrix
|
||||
CP = sort(round(rand(popSize,2)*(genome-1)+1),2); % Crossover Points
|
||||
idx = CP(:,1)*ones(1,genome)<Ref&CP(:,2)*ones(1,genome)>Ref; % Index
|
||||
Pop2(idx)=P2A(idx); % Recombine Winners
|
||||
end
|
||||
%% Mutation
|
||||
idx = rand(size(Pop2))<mutRate; % Index of Mutations
|
||||
Pop2(idx) = round(rand([1,sum(sum(idx))])*(MaxVal-1)+1); % Mutated Value
|
||||
|
||||
%% Reset Poplulations
|
||||
Pop = Pop2;
|
||||
|
||||
end
|
||||
|
||||
toc % Ends timer and prints elapsed time
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
Gen: 1 | Fitness: 465 | C<EFBFBD>I1%G+<%?R<EFBFBD>8>9<EFBFBD>JU#(E<EFBFBD>UO<EFBFBD>PHI
|
||||
Gen: 2 | Fitness: 429 | W=P6>D<EFBFBD>I)VU6$T 99,<EFBFBD> B<EFBFBD>BMP0JH
|
||||
Gen: 3 | Fitness: 366 | P<EFBFBD>;R08AS<EFBFBD>GJ<EFBFBD>IS&T38IE<EFBFBD>)SJERLJ
|
||||
Gen: 4 | Fitness: 322 | KI8M5LAS<EFBFBD>GJ<EFBFBD>IS<EFBFBD>SP<EFBFBD>@)D<EFBFBD>V@
|
||||
JCP
|
||||
Gen: 5 | Fitness: 295 | UAUR08AS<EFBFBD>GJ<EFBFBD>IS<EFBFBD>8HG*<EFBFBD>+<EFBFBD>=C?UB(
|
||||
Gen: 6 | Fitness: 259 | VCUQH35S<EFBFBD>HR4.L<EFBFBD>ISJQ%J<EFBFBD>OC*T=E
|
||||
Gen: 7 | Fitness: 226 | LFB8GPET(LODKQ<EFBFBD>KQ<K E*PEMA6I
|
||||
Gen: 8 | Fitness: 192 | EPKOLCIR<EFBFBD>QQ<EFBFBD>NF<EFBFBD>QG:B(D/U>BQGF
|
||||
Gen: 9 | Fitness: 159 | N8R7?SOU<EFBFBD>NO$OK O?K?!;<EFBFBD>MB?QHG
|
||||
Gen: 10 | Fitness: 146 | TGN@EQR4)PS%IS#TFJQ%A!U>BVLI
|
||||
Gen: 11 | Fitness: 120 | L?VMALJS%?R EK IILE<EFBFBD>6'RRERLJ
|
||||
Gen: 12 | Fitness: 102 | R@T9COMR<EFBFBD>NU CS*R?K?!; VD>LCL
|
||||
Gen: 13 | Fitness: 96 | NENMVOMR<EFBFBD>NU CS*R?K?!; VD>LCL
|
||||
Gen: 14 | Fitness: 82 | REJGNPMU<EFBFBD>KR CS JKI@+D<EFBFBD>UD?QHG
|
||||
Gen: 15 | Fitness: 75 | NETI=HPQ<EFBFBD>FT ID EFKE D"WD>QDQ
|
||||
Gen: 16 | Fitness: 70 | R@TKCOOT)@R$IS KKLE<EFBFBD>D"WC?UBJ
|
||||
Gen: 17 | Fitness: 61 | NESIKQRP<EFBFBD>NU CS<EFBFBD>MFKE ; SEETCP
|
||||
Gen: 18 | Fitness: 57 | LFSGLPTN<EFBFBD>NU GQ IIKE D"VD>LCL
|
||||
Gen: 19 | Fitness: 40 | NENKJLMS<EFBFBD>GS%IS#MFKE B UFATCL
|
||||
Gen: 21 | Fitness: 39 | NETIGPEU<EFBFBD>KR IS IIKD"? UFDQEK
|
||||
Gen: 22 | Fitness: 33 | NETGCOMT<EFBFBD>LU IS#MFKE B UFATCL
|
||||
Gen: 23 | Fitness: 32 | NETIKNPQ<EFBFBD>NU IS#IIKE B UFATCL
|
||||
Gen: 24 | Fitness: 27 | NETKJLMS<EFBFBD>LU IS MFKE B UFATCL
|
||||
Gen: 25 | Fitness: 23 | LETIKOMS LU IS IIKE D WEDQEK
|
||||
Gen: 26 | Fitness: 22 | NETIKMJS LU IS IIKE D WEDQEK
|
||||
Gen: 27 | Fitness: 20 | LETIKOMS LU IS KILE B"WFATCL
|
||||
Gen: 28 | Fitness: 19 | NESGJQJS<EFBFBD>GU IS KIKE B WFATEK
|
||||
Gen: 29 | Fitness: 16 | NETIHPMS KR IS KIKE B WFATEK
|
||||
Gen: 30 | Fitness: 15 | NESHLPKS KU IS KIKE B WFATEK
|
||||
Gen: 31 | Fitness: 13 | NETGGNKS KU IS KIKE C WFATEK
|
||||
Gen: 32 | Fitness: 12 | NETHGNJS IU IS JIKE B WFATCL
|
||||
Gen: 33 | Fitness: 11 | NETIJPKS IU IS KIKE B WFATEK
|
||||
Gen: 35 | Fitness: 8 | LEUIHNJS IT IS JIKE A WEATEL
|
||||
Gen: 37 | Fitness: 7 | NETIHNJS IS IS LIKE B WFASEL
|
||||
Gen: 38 | Fitness: 6 | NETHGNJS IT IS LIKE A WFASEK
|
||||
Gen: 39 | Fitness: 4 | METGHNKS IT IS LIKE B WEATEL
|
||||
Gen: 42 | Fitness: 3 | NETHINKS IT IS KIKE B WEASEL
|
||||
Gen: 43 | Fitness: 2 | NETHINKS IT IS LIKE A WFASEL
|
||||
Gen: 44 | Fitness: 1 | METHHNKS IT IS LIKE A WEASEL
|
||||
Gen: 46 | Fitness: 0 | METHINKS IT IS LIKE A WEASEL
|
||||
Elapsed time is 0.099618 seconds.
|
||||
Loading…
Add table
Add a link
Reference in a new issue