├── .github └── FUNDING.yml ├── .gitignore ├── Cross.m ├── DecodeChromosome.m ├── DecodePopulation.m ├── EvaluateIndividual.m ├── EvaluatePopulation.m ├── Experiments.m ├── FunctionOptimization.m ├── InitializePopulation.m ├── InsertBestIndividual.m ├── LICENSE.md ├── Mutate.m ├── README.md ├── TournamentSelect.m ├── UnitTestCross.m ├── UnitTestDecodeChromosome.m ├── UnitTestDecodePopulation.m ├── UnitTestInitializePopulation.m └── UnitTestInsertBestIndividual.m /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['https://www.buymeacoffee.com/alpsayin'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.asv 2 | *~ -------------------------------------------------------------------------------- /Cross.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function newChromosomePair = Cross( chromosome1, chromosome2) 10 | 11 | nGenes = size(chromosome1,2) ; 12 | crossoverPoint = 1 + fix(rand*(nGenes-1)); 13 | assert(crossoverPoint>0 && crossoverPoint<=nGenes); 14 | newChromosomePair(1, :) = [chromosome1(1:crossoverPoint) chromosome2(crossoverPoint+1:end)]; 15 | newChromosomePair(2, :) = [chromosome2(1:crossoverPoint) chromosome1(crossoverPoint+1:end)]; 16 | 17 | % % Deprecated - to be deleted in the next iteration 18 | % for j = 1: nGenes 19 | % if (j < crossoverPoint) 20 | % newChromosomePair(1,j) = chromosome1(j); 21 | % newChromosomePair(2,j) = chromosome2(j); 22 | % else 23 | % newChromosomePair(1,j) = chromosome2(j); 24 | % newChromosomePair(2,j) = chromosome1(j); 25 | % end 26 | % end 27 | 28 | -------------------------------------------------------------------------------- /DecodeChromosome.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function x = DecodeChromosome (chromosome, numberOfVariables, variableRange) 10 | 11 | x = zeros(1,numberOfVariables); 12 | numberOfBits = size(chromosome,2) / numberOfVariables; 13 | 14 | for index = 1:numberOfVariables 15 | x(index) = 0.0 ; 16 | geneRangeStart = (((index-1)*numberOfBits)+1); 17 | geneRangeEnd = index*numberOfBits; 18 | x(index) = sum(chromosome( geneRangeStart:geneRangeEnd ).*(2.^-(1:numberOfBits))); 19 | % % Deprecated - to be deleted in the next iteration 20 | % for jj = 1:numberOfBits 21 | % x(index) = x(index) + chromosome(jj + (index-1)*numberOfBits )*(2^(-jj)) ; 22 | % end 23 | x(index) = -variableRange + 2*variableRange*x(index)/(1-2^(-numberOfBits)); 24 | end 25 | -------------------------------------------------------------------------------- /DecodePopulation.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function x = DecodePopulation(population, numberOfVariables, variableRange) 10 | 11 | populationSize = size(population, 1); 12 | x = zeros(populationSize, numberOfVariables); 13 | numberOfBits = size(population,2) / numberOfVariables; 14 | 15 | for index = 1:numberOfVariables 16 | geneRangeStart = (((index-1)*numberOfBits)+1); 17 | geneRangeEnd = index*numberOfBits; 18 | x(:,index) = sum(population( :, geneRangeStart:geneRangeEnd ).*repmat((2.^-(1:numberOfBits)),populationSize,1), 2); 19 | % % Deprecated - to be deleted in the next iteration 20 | % for jj = 1:numberOfBits 21 | % x(index) = x(index) + chromosome(jj + (index-1)*numberOfBits )*(2^(-jj)) ; 22 | % end 23 | x(:,index) = -variableRange + 2*variableRange*x(:,index)/(1-2^(-numberOfBits)); 24 | end 25 | -------------------------------------------------------------------------------- /EvaluateIndividual.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function fitnessValue = EvaluateIndividual(x) 10 | 11 | %fitnessValue = (exp(-x(1)^2 -x(2)^2) + sqrt(5)*(sin(x(2)*x(1)*x(1))^2)+ 2*(cos(2*x(1)+ 3*x(2))^2))/( 1+x(1)^2 +x(2)^2);% 12 | g = ( 1 + ((x(1) + x(2) + 1)^2)*(19 -14*x(1) + 3*x(1)^2 -14*x(2) + 6*x(1)*x(2) + 3*x(2)^2) ) * (30 + ((2*x(1) - 3*x(2))^2)*(18 - 32*x(1) + 12*x(1)^2 + 48*x(2) - 36*x(1)*x(2) + 27*x(2)^2) ); 13 | fitnessValue = 1/g; 14 | -------------------------------------------------------------------------------- /EvaluatePopulation.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function fitnessValue = EvaluatePopulation(x, runParallel) 10 | 11 | if ~exist('runParallel','var') 12 | runParallel = false; 13 | end 14 | 15 | fitnessValue = zeros(size(x,1),1); 16 | if runParallel 17 | parfor index = 1:size(x,1) 18 | fitnessValue(index) = EvaluateIndividual(x(index,:)); 19 | end 20 | else 21 | for index = 1:size(x,1) 22 | fitnessValue(index) = EvaluateIndividual(x(index,:)); 23 | end 24 | end -------------------------------------------------------------------------------- /Experiments.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | clear 10 | close all 11 | 12 | numberOfRuns = 20; 13 | 14 | minimum = zeros(1,numberOfRuns); 15 | 16 | for averageRun=1:numberOfRuns 17 | FunctionOptimization; 18 | minimum(1,averageRun) = 1/maximumFitness; 19 | end 20 | 21 | clc 22 | average = mean(minimum) 23 | standardDeviation = std(minimum) 24 | -------------------------------------------------------------------------------- /FunctionOptimization.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | %% CLEAN-UP 10 | clear;close all;clc; 11 | tic 12 | 13 | %% PARAMETERS 14 | populationSize = 150 ; 15 | numberOfGenes = 40; 16 | crossoverProbability = 0.8 ; 17 | mutationProbability = 0.0625; 18 | tournamentSelectionParameter = 0.5; 19 | variableRange = 10.0; 20 | numberOfGenerations = 250; 21 | numberOfVariables = 2; 22 | tournamentSize = 10; 23 | numberOfReplications = 2; 24 | verbose = true; 25 | draw_plots = true; 26 | % UNLESS THE FITNESS FUNCTION IS REALLY DIFFICULT TO COMPUTE, IT'S FASTER 27 | % NOT TO USE PARALLEL COMPUTATION 28 | runparallel = false; 29 | 30 | 31 | %% VARIABLES 32 | fitness = zeros(populationSize, 1); 33 | 34 | %% PLOTTING SETUP 35 | if draw_plots 36 | fitnessFigureHandle = figure; 37 | hold on; 38 | set(fitnessFigureHandle,'Position',[50,50,500,200]); 39 | set(fitnessFigureHandle,'DoubleBuffer','on'); 40 | axis([1 numberOfGenerations -variableRange variableRange]); 41 | bestPlotHandle = plot(1:numberOfGenerations, zeros(1,numberOfGenerations)); 42 | textHandle = text(30,2.6, sprintf('best: %4.3f', 0.0)); 43 | hold off; 44 | drawnow; 45 | 46 | surfaceFigureHandle= figure; 47 | hold on; 48 | set(surfaceFigureHandle,'DoubleBuffer','on'); 49 | delta=0.1; 50 | limit = fix(2*variableRange/delta)+1 ; 51 | [xValues, yValues] = meshgrid(-variableRange: delta:variableRange,-variableRange: delta:variableRange); 52 | zValues= zeros(limit,limit); 53 | for j = 1: limit 54 | for k = 1: limit 55 | zValues(j,k) = EvaluateIndividual([xValues(j,k) yValues(j,k)]); 56 | end 57 | end 58 | surfl(xValues,yValues,zValues) 59 | colormap gray; 60 | shading interp; 61 | view ([-7 -9 10]); 62 | decodedPopulation = zeros(populationSize,numberOfVariables); 63 | populationPlotHandle = plot3(decodedPopulation(:,1),decodedPopulation(:,2),fitness(:),'kp'); 64 | hold off; 65 | drawnow; 66 | end 67 | %% INITIATE POPULATION 68 | population = InitializePopulation(populationSize, numberOfGenes) ; 69 | 70 | %% RUN GENERATIONS 71 | for iGeneration = 1: numberOfGenerations 72 | 73 | %% FIND MAXIMUM FITNESS OF POPULATION 74 | 75 | decodedPopulation = DecodePopulation(population, numberOfVariables, variableRange); 76 | fitness = EvaluatePopulation(decodedPopulation, runparallel); 77 | [maximumFitness, bestIndividualIndex] = max(fitness); 78 | xBest = decodedPopulation(bestIndividualIndex,:); 79 | 80 | % % Deprecated - to be deleted in the next iteration 81 | % maximumFitness = 0.0; 82 | % for i = 1: populationSize 83 | % chromosome = population(i,:); 84 | % x = DecodeChromosome(chromosome, numberOfVariables, variableRange) ; 85 | % decodedPopulation(i,:)= x; 86 | % fitness(i) = EvaluateIndividual(x); 87 | % if ( fitness(i)> maximumFitness) 88 | % maximumFitness = fitness(i); 89 | % bestIndividualIndex = i; 90 | % xBest=x ; 91 | % end 92 | % end 93 | 94 | % Print out 95 | if verbose 96 | fprintf('Maximum Fitness: %d\n',maximumFitness); 97 | fprintf('Best Solution: %d\n',xBest); 98 | end 99 | 100 | %% COPY POPULATION 101 | newPopulation = population; 102 | 103 | %% NEW GENERATION 104 | for i = 1:tournamentSize:populationSize 105 | %% TOURNAMENT SELECTION 106 | i1 = TournamentSelect(fitness,tournamentSelectionParameter,tournamentSize); 107 | i2 = TournamentSelect(fitness,tournamentSelectionParameter,tournamentSize); 108 | chromosome1 = population(i1,:); 109 | chromosome2 = population(i2,:); 110 | 111 | %% CROSS-OVER 112 | r = rand; 113 | if ( r < crossoverProbability) 114 | newChromosomePair = Cross(chromosome1, chromosome2); 115 | newPopulation(i,:) = newChromosomePair(1,:); 116 | newPopulation(i+1,:) = newChromosomePair(2,:); 117 | else 118 | newPopulation(i,:) = chromosome1; 119 | newPopulation(i+1,:) = chromosome2; 120 | end 121 | end 122 | 123 | %% MUTATE 124 | newPopulation = Mutate(newPopulation, mutationProbability); 125 | 126 | %% PRESERVATION OF PREVIOUS BEST SOLUTION 127 | bestChromosome = population(bestIndividualIndex,:); 128 | newPopulation = InsertBestIndividual(newPopulation, bestChromosome, numberOfReplications); 129 | 130 | %% COPY THE NEW POPULATION ONTO CURRENT POPULATION 131 | population = newPopulation; 132 | 133 | %% PLOT CURRENT SITUATION 134 | if draw_plots 135 | plotvector = get(bestPlotHandle,'YData'); 136 | plotvector(iGeneration)= maximumFitness; 137 | set(bestPlotHandle,'YData',plotvector); 138 | set(textHandle,'String', sprintf('best: %4.3f',maximumFitness)); 139 | set(populationPlotHandle,'XData', decodedPopulation(:,1),'YData',decodedPopulation(:,2),'ZData', fitness(:)); 140 | drawnow; 141 | end 142 | end 143 | 144 | % Print out 145 | fprintf('Maximum Fitness: %d\n',maximumFitness); 146 | fprintf('Best Solution: %d\n',xBest); 147 | toc -------------------------------------------------------------------------------- /InitializePopulation.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function population = InitializePopulation(populationSize, numberOfGenes) 10 | 11 | population = (rand(populationSize, numberOfGenes)<0.5).*1; 12 | 13 | return 14 | 15 | %% DEPRECATED - to be deleted in the next iteration 16 | 17 | population = zeros(populationSize, numberOfGenes); 18 | for ii = 1: populationSize 19 | for jj = 1: numberOfGenes 20 | s = rand; 21 | if (s < 0.5) 22 | population(ii,jj)=0; 23 | else 24 | population(ii,jj)=1; 25 | end 26 | end 27 | end -------------------------------------------------------------------------------- /InsertBestIndividual.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function newPopulation = InsertBestIndividual( population, bestChromosome, nc) 10 | 11 | % Replace a random individual with the best chromosome 12 | newPopulation = population; 13 | randIndexes = ceil(rand(nc,1).*size(population,1)); 14 | newPopulation(randIndexes,:) = repmat(bestChromosome,nc,1); 15 | 16 | return 17 | 18 | % Alternatively we can grow the population but generally people don't want growing population 19 | newPopulation = zeros(size(population,1)+nc, size(population,2)); 20 | newPopulation(1:size(population,1)) = population; 21 | newPopulation(size(population,1)+1:end) = repmat(bestChromosome,nc,1); 22 | 23 | % Deprecated - to be deleted in the next iteration 24 | % for k = 1:nc 25 | % newPopulation(k,:) = bestChromosome; 26 | % end 27 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2012 Alp Sayin, and individual Contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Mutate.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function tempPopulation = Mutate(tempPopulation, mutationProbability) 10 | 11 | indexes = rand(size(tempPopulation)) number of individuals in a population 16 | + numberOfGenes -> number of bits per chromosome 17 | + crossoverProbability -> probability that a crossover will happen between two individuals 18 | + mutationProbability -> probability that a mutation will occur in an individual 19 | + tournamentSelectionParameter -> parameter that's used to calculate the probabilities for individuals to be chosen in a tournament -> 'p*(1-p)^k' where k denotes the k'th worst individual in the tournament pool 20 | + variableRange -> the range in which the genes will be decoded into. basically minimum and maximum values of the parameters 21 | + numberOfGenerations -> number of iterations to run genetic algorithm 22 | + numberOfVariables -> number of variables stored in one chromosome 23 | + tournamentSize -> this value determines the number of individuals to be taken into a tournament. an individual of this pool is then chosen for mating with a probability calculated from tournamentSelectionParameter 24 | + numberOfReplications -> after a generation is run, this number of best individuals are copied back into the population to ensure the solution quality does not degrade 25 | + verbose -> if true; progress is printed 26 | + draw_plots -> if true; progress is plotted 27 | 28 | Unit Tests 29 | ---------- 30 | They are simply there to test the individual methods/steps of the genetic algorithm. Can be used for debugging. 31 | 32 | Licensing Stuff 33 | --------------- 34 | Please dont remove my name from the codes. 35 | -------------------------------------------------------------------------------- /TournamentSelect.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | function iSelected = TournamentSelect( fitnessValues, tournamentSelectionParameter, tournamentSize) 10 | 11 | populationSize = size(fitnessValues,1) ; 12 | 13 | %select 'tournamentSize' candidates for tournament 14 | candidates = 1 + fix(rand(1,tournamentSize)*populationSize); 15 | candidateFitnesses = fitnessValues(candidates); 16 | [~, sortedIndexes] = sort(candidateFitnesses,1,'descend'); 17 | selectionProbabilityMatrix = tournamentSelectionParameter*((1-tournamentSelectionParameter).^(0:tournamentSize-2)'); 18 | r = rand; 19 | iSelected = candidates(sortedIndexes(r>selectionProbabilityMatrix)); 20 | if isempty(iSelected) 21 | iSelected = candidates(sortedIndexes(end)); 22 | else 23 | iSelected = iSelected(1); 24 | end 25 | -------------------------------------------------------------------------------- /UnitTestCross.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | clear 10 | close all 11 | 12 | chromosome1 = zeros(1,10); 13 | chromosome2 = ones(1,10); 14 | 15 | v = [1 10 -2 2]; 16 | subplot(2,2,1) 17 | stem(chromosome1) 18 | axis(v) 19 | title('chromosome1 (before cross)') 20 | subplot(2,2,3) 21 | stem(chromosome2) 22 | axis(v) 23 | title('chromosome2 (before cross)') 24 | 25 | newChromosomePair = Cross(chromosome1, chromosome2); 26 | 27 | subplot(2,2,2) 28 | stem(newChromosomePair(1,:)) 29 | axis(v) 30 | title('chromosome1 (after cross)') 31 | subplot(2,2,4) 32 | stem(newChromosomePair(2,:)) 33 | axis(v) 34 | title('chromosome2 (after cross)') 35 | -------------------------------------------------------------------------------- /UnitTestDecodeChromosome.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | chromosome= [0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1]; 10 | numberOfVariables= 4; 11 | variableRange= 2; 12 | 13 | x = DecodeChromosome(chromosome, numberOfVariables, variableRange) 14 | -------------------------------------------------------------------------------- /UnitTestDecodePopulation.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | 10 | population = [0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 11 | 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1]; 12 | numberOfVariables = 4; 13 | variableRange = 2; 14 | 15 | x = DecodePopulation(population, numberOfVariables, variableRange) 16 | -------------------------------------------------------------------------------- /UnitTestInitializePopulation.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | clear 10 | close all 11 | 12 | populationSize = 5; 13 | numberOfGenes = 10; 14 | population = InitializePopulation(populationSize, numberOfGenes); 15 | 16 | for ii=1:populationSize 17 | subplot(populationSize,1,ii) 18 | bar(population(ii,:)) 19 | xlabel('Genes') 20 | ylabel(sprintf('#%d', ii)) 21 | end 22 | -------------------------------------------------------------------------------- /UnitTestInsertBestIndividual.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % 3 | % Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com 4 | % Matlab Genetic Algorithm 5 | % Spring 2012 6 | % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | clear 10 | close all 11 | 12 | populationSize = 5; 13 | numberOfGenes = 10; 14 | population = ones(populationSize, numberOfGenes); 15 | 16 | for ii=1:populationSize 17 | subplot(populationSize,1,ii) 18 | stem(population(ii,:)) 19 | end 20 | 21 | 22 | bestChromosome = zeros(1,numberOfGenes); 23 | 24 | newPopulation = InsertBestIndividual(population, bestChromosome, 2); 25 | 26 | figure 27 | for ii=1:populationSize 28 | subplot(populationSize,1,ii) 29 | stem(newPopulation(ii,:)) 30 | end 31 | 32 | --------------------------------------------------------------------------------