SlideShare a Scribd company logo
1 of 21
CHECKBOARD USING GENETIC
ALGORITHM
• We are given an n by n checkboard in which
every field can have a different colour from a
set of four colours.
• Goal is to achieve a checkboard in a way that there
are no neighbours with the same colour (not diagonal)
1 2 3 4 5 6 7 8 9 10
1
2
3
4
5
6
7
8
9
10
1 2 3 4 5 6 7 8 9 10
1
2
3
4
5
6
7
8
9
10
The size of a population populationSize = 4
The size of the matrix we'll be using as a solution matrixSize = 4
Build a data structure to contain the initial population population =
zeros( matrixSize, matrixSize, populationSize );
• The checkboard contain 4 colors which shows by number 0 to 3.
it is a node-edge problem so the maximum fitness can easily be computed
optimalFitness = 2 * ( ( matrixSize - 1 ) * matrixSize )=2*3*4=24;
Performance
Initialize:
for( i = 1:populationSize )
population( :, :, i ) = fix( 4 * rand( matrixSize, matrixSize ) );
end
Initial population
population(:,:,1)
1 3 0 0
3 3 2 0
2 3 0 1
2 3 0 2
population(:,:,2)
3 0 0 0
2 1 0 1
0 1 2 2
2 2 3 2
population(:,:,3)
2 0 1 0
3 0 0 3
2 1 1 1
0 2 3 2
population(:,:,4)
3 2 3 1
1 0 2 3
3 1 3 0
0 3 3 0
Fitness function calculation
population(:,:,1)
1 3 0 0
3 3 2 0
2 3 0 1
2 3 0 2
1 1 0
0 1 1
1 1 1
1 1 1
Row Column
1 + 1 + 0
0 + 1 + 1
1 + 1 + 1
1 + 1 + 1
1 + 1 + 0
0 + 0 + 0
1 + 1 + 0
0 + 1 + 1
10 6
10 + 6 = 16Fitness value
Fitness of the initial population
fitness( 1 ) = checkers( population( :, :, 1 ) ) = 16
fitness( 2 ) = checkers( population( :, :, 2 ) ) = 17
fitness( 3 ) = checkers( population( :, :, 3 ) ) = 20
fitness( 4 ) = checkers( population( :, :, 4 ) ) = 21
optimalFitness = 2 × N × (N-1)
here N is the size of matrix (N=4) optimalFitness=2×4×3=24
what's the best fitness in this
population?
bestCurrentFitness = max( fitness ) = 21;
meanCurrentFitness = mean( fitness) = 18.5;
• optimalFitness = 24
bestCurrentFitness = 21 > bestFitness = 0
bestFitness = bestCurrentFitness
And displaye checkboard
0.5 1 1.5 2 2.5 3 3.5 4 4.5
0.5
1
1.5
2
2.5
3
3.5
4
4.5
Generate newPopulation
newPopulation(:,:,1)
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
newPopulation(:,:,2)
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
newPopulation(:,:,3)
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
newPopulation(:,:,4)
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Number of chromosome which bestindividual is memorized to do
crossovers a3 = fix (populationSize*rand(1))+1;
Number of chromosome which bestindividual is memorized after crossovers
a4 = fix (populationSize*rand(1))+1;
a3 = 4
a4 = 4
Generation = 1
i = 1 and i +1 = 2
mom = select( population, fitness );
1 3 0 0
3 3 2 0
2 3 0 1
2 3 0 2
dad = select( population, fitness );
3 0 0 0
2 1 0 1
0 1 2 2
2 2 3 2
dad
3 0 0 0
2 1 0 1
0 1 2 2
2 2 3 2
mom
1 3 0 0
3 3 2 0
2 3 0 1
2 3 0 2
Crossover for i = 1 and i +1 = 2 and rand(1) = 0.68 > 0.5
Mutation for i = 1 and i +1 = 2
child1
1 3 0 0
2 3 2 0
0 1 0 1
2 2 3 2
newPopulation( :, :, 1 ) =
mutate(child1 )
1 3 0 0
2 3 2 0
0 1 0 1
2 2 3 2
For each gene 1% probability exists to occur mutation.
child2
3 0 0 0
3 1 0 1
2 3 2 2
2 3 0 2
newPopulation( :, :, 2 ) =
mutate(child2 )
3 0 0 0
3 1 0 1
2 3 2 2
2 3 0 2
i = 3 and i +1 = 4
mom = select( population, fitness );
3 2 3 1
1 0 2 3
3 1 3 0
0 3 3 0
dad = select( population, fitness );
3 0 0 0
2 1 0 1
0 1 2 2
2 2 3 2
dad
3 0 0 0
2 1 0 1
0 1 2 2
2 2 3 2
mom
3 2 3 1
1 0 2 3
3 1 3 0
0 3 3 0
Crossover for i = 3 and i +1 = 4 and rand(1) = 0.38 < 0.5
Number of the Column for Crossover
a5 = fix (matrixSize*rand(1))+1 = 2;
Mutation for i = 3 and i +1 = 4
child1
3 0 3 1
1 1 2 3
3 1 3 0
0 2 3 0
newPopulation( :, :, 3 ) =
mutate(child1 )
3 0 3 1
1 1 2 3
3 1 3 0
0 2 3 0
For each gene 1% probability exists to occur mutation.
newPopulation( :, :, 4 ) =
bestindividual( population, fitness)
3 2 3 1
1 0 2 3
3 1 3 0
0 3 3 0
Population = newPopulation
population(:,:,1)
1 3 0 0
2 3 2 0
0 1 0 1
2 2 3 2
population(:,:,2)
3 0 0 0
3 1 0 1
2 3 2 2
2 3 0 2
population(:,:,3)
3 0 3 1
1 1 2 3
3 1 3 0
0 2 3 0
population(:,:,4)
3 2 3 1
1 0 2 3
3 1 3 0
0 3 3 0
Fitness changes
Old
fitness( 1 ) = checkers( population( :, :, 1 ) ) = 16
fitness( 2 ) = checkers( population( :, :, 2 ) ) = 17
fitness( 3 ) = checkers( population( :, :, 3 ) ) = 20
fitness( 4 ) = checkers( population( :, :, 4 ) ) = 21
New
fitness( 1 ) = checkers( population( :, :, 1 ) ) = 20
fitness( 2 ) = checkers( population( :, :, 2 ) ) = 16
fitness( 3 ) = checkers( population( :, :, 3 ) ) = 20
fitness( 4 ) = checkers( population( :, :, 4 ) ) = 21
After 47 generation the optimal fitness obtains
0.5 1 1.5 2 2.5 3 3.5 4 4.5
0.5
1
1.5
2
2.5
3
3.5
4
4.5
0 5 10 15 20 25 30 35 40 45 50
16
17
18
19
20
21
22
23
24
Generations
Fitness
Best Fitness
Mean Fitness
Performance of Select Function
population(:,:,1)
1 3 0 0
2 3 2 0
0 1 0 1
2 2 3 2
population(:,:,2)
3 0 0 0
3 1 0 1
2 3 2 2
2 3 0 2
population(:,:,3)
3 0 3 1
1 1 2 3
3 1 3 0
0 2 3 0
population(:,:,4)
3 2 3 1
1 0 2 3
3 1 3 0
0 3 3 0
fitness( 1 ) = checkers( population( :, :, 1 ) ) = 20
fitness( 2 ) = checkers( population( :, :, 2 ) ) = 16
fitness( 3 ) = checkers( population( :, :, 3 ) ) = 20
fitness( 4 ) = checkers( population( :, :, 4 ) ) = 21
Performance of Select Function
function selected = select( population, fitness )
[m, n] = size( fitness ); % m = n = 4
s = sum( fitness ); % s = 77
roulette = s*rand( 1 ); % roulette = 48.7868
currentSum = 0;
picked = n; % n = 4
for i = 1:n
s1 = currentSum + fitness(i)
if s1 < roulette
picked = I
end
currentSum = currentSum + fitness(i);
end
selected = population( :, :, picked );
i currentSum fitness(i) s1 if s1 < roulette picked
1 0 20 20 Yes 1
2 20 16 36 Yes 2
3 36 20 56 No 2
4 56 21 77 No 2

More Related Content

Similar to Genetic algorithm

Introduction to machine learning algorithms
Introduction to machine learning algorithmsIntroduction to machine learning algorithms
Introduction to machine learning algorithmsbigdata trunk
 
lagrange and newton divided differences.ppt
lagrange and newton divided differences.pptlagrange and newton divided differences.ppt
lagrange and newton divided differences.pptThirumoorthy64
 
3D Math Without Presenter Notes
3D Math Without Presenter Notes3D Math Without Presenter Notes
3D Math Without Presenter NotesJanie Clayton
 
Theory to consider an inaccurate testing and how to determine the prior proba...
Theory to consider an inaccurate testing and how to determine the prior proba...Theory to consider an inaccurate testing and how to determine the prior proba...
Theory to consider an inaccurate testing and how to determine the prior proba...Toshiyuki Shimono
 
Oct8 - 131 slid
Oct8 - 131 slidOct8 - 131 slid
Oct8 - 131 slidTak Lee
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202Mahmoud Samir Fayed
 
Descriptiva-Semana8
Descriptiva-Semana8 Descriptiva-Semana8
Descriptiva-Semana8 Jorge Obando
 
Diversity mechanisms for evolutionary populations in Search-Based Software En...
Diversity mechanisms for evolutionary populations in Search-Based Software En...Diversity mechanisms for evolutionary populations in Search-Based Software En...
Diversity mechanisms for evolutionary populations in Search-Based Software En...Annibale Panichella
 
Binomial distributions
Binomial distributionsBinomial distributions
Binomial distributionsUlster BOCES
 
3 Systems Jeopardy - Copy.ppt
3 Systems Jeopardy - Copy.ppt3 Systems Jeopardy - Copy.ppt
3 Systems Jeopardy - Copy.pptssusere48744
 
Introduction
IntroductionIntroduction
Introductionbutest
 
Chapter-1-04032021-111422pm (2).pptx
Chapter-1-04032021-111422pm (2).pptxChapter-1-04032021-111422pm (2).pptx
Chapter-1-04032021-111422pm (2).pptxabdulhannan992458
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithmBablu Shofi
 

Similar to Genetic algorithm (20)

N queen
N queenN queen
N queen
 
Matrices
Matrices Matrices
Matrices
 
Introduction to machine learning algorithms
Introduction to machine learning algorithmsIntroduction to machine learning algorithms
Introduction to machine learning algorithms
 
lagrange and newton divided differences.ppt
lagrange and newton divided differences.pptlagrange and newton divided differences.ppt
lagrange and newton divided differences.ppt
 
3D Math Without Presenter Notes
3D Math Without Presenter Notes3D Math Without Presenter Notes
3D Math Without Presenter Notes
 
Theory to consider an inaccurate testing and how to determine the prior proba...
Theory to consider an inaccurate testing and how to determine the prior proba...Theory to consider an inaccurate testing and how to determine the prior proba...
Theory to consider an inaccurate testing and how to determine the prior proba...
 
Oct8 - 131 slid
Oct8 - 131 slidOct8 - 131 slid
Oct8 - 131 slid
 
Practice Test 2 Solutions
Practice Test 2  SolutionsPractice Test 2  Solutions
Practice Test 2 Solutions
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Lr4 math cad
Lr4 math cadLr4 math cad
Lr4 math cad
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202
 
Descriptiva-Semana8
Descriptiva-Semana8 Descriptiva-Semana8
Descriptiva-Semana8
 
Diversity mechanisms for evolutionary populations in Search-Based Software En...
Diversity mechanisms for evolutionary populations in Search-Based Software En...Diversity mechanisms for evolutionary populations in Search-Based Software En...
Diversity mechanisms for evolutionary populations in Search-Based Software En...
 
Binomial distributions
Binomial distributionsBinomial distributions
Binomial distributions
 
3 Systems Jeopardy - Copy.ppt
3 Systems Jeopardy - Copy.ppt3 Systems Jeopardy - Copy.ppt
3 Systems Jeopardy - Copy.ppt
 
Introduction
IntroductionIntroduction
Introduction
 
Chapter 4 review
Chapter 4 reviewChapter 4 review
Chapter 4 review
 
ML MODULE 2.pdf
ML MODULE 2.pdfML MODULE 2.pdf
ML MODULE 2.pdf
 
Chapter-1-04032021-111422pm (2).pptx
Chapter-1-04032021-111422pm (2).pptxChapter-1-04032021-111422pm (2).pptx
Chapter-1-04032021-111422pm (2).pptx
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 

Recently uploaded

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 

Recently uploaded (20)

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 

Genetic algorithm

  • 2. • We are given an n by n checkboard in which every field can have a different colour from a set of four colours. • Goal is to achieve a checkboard in a way that there are no neighbours with the same colour (not diagonal) 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
  • 3. The size of a population populationSize = 4 The size of the matrix we'll be using as a solution matrixSize = 4 Build a data structure to contain the initial population population = zeros( matrixSize, matrixSize, populationSize ); • The checkboard contain 4 colors which shows by number 0 to 3. it is a node-edge problem so the maximum fitness can easily be computed optimalFitness = 2 * ( ( matrixSize - 1 ) * matrixSize )=2*3*4=24; Performance Initialize: for( i = 1:populationSize ) population( :, :, i ) = fix( 4 * rand( matrixSize, matrixSize ) ); end
  • 4. Initial population population(:,:,1) 1 3 0 0 3 3 2 0 2 3 0 1 2 3 0 2 population(:,:,2) 3 0 0 0 2 1 0 1 0 1 2 2 2 2 3 2 population(:,:,3) 2 0 1 0 3 0 0 3 2 1 1 1 0 2 3 2 population(:,:,4) 3 2 3 1 1 0 2 3 3 1 3 0 0 3 3 0
  • 5. Fitness function calculation population(:,:,1) 1 3 0 0 3 3 2 0 2 3 0 1 2 3 0 2 1 1 0 0 1 1 1 1 1 1 1 1 Row Column 1 + 1 + 0 0 + 1 + 1 1 + 1 + 1 1 + 1 + 1 1 + 1 + 0 0 + 0 + 0 1 + 1 + 0 0 + 1 + 1 10 6 10 + 6 = 16Fitness value
  • 6. Fitness of the initial population fitness( 1 ) = checkers( population( :, :, 1 ) ) = 16 fitness( 2 ) = checkers( population( :, :, 2 ) ) = 17 fitness( 3 ) = checkers( population( :, :, 3 ) ) = 20 fitness( 4 ) = checkers( population( :, :, 4 ) ) = 21 optimalFitness = 2 × N × (N-1) here N is the size of matrix (N=4) optimalFitness=2×4×3=24
  • 7. what's the best fitness in this population? bestCurrentFitness = max( fitness ) = 21; meanCurrentFitness = mean( fitness) = 18.5; • optimalFitness = 24
  • 8. bestCurrentFitness = 21 > bestFitness = 0 bestFitness = bestCurrentFitness And displaye checkboard 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0.5 1 1.5 2 2.5 3 3.5 4 4.5
  • 9. Generate newPopulation newPopulation(:,:,1) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 newPopulation(:,:,2) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 newPopulation(:,:,3) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 newPopulation(:,:,4) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  • 10. Number of chromosome which bestindividual is memorized to do crossovers a3 = fix (populationSize*rand(1))+1; Number of chromosome which bestindividual is memorized after crossovers a4 = fix (populationSize*rand(1))+1; a3 = 4 a4 = 4 Generation = 1
  • 11. i = 1 and i +1 = 2 mom = select( population, fitness ); 1 3 0 0 3 3 2 0 2 3 0 1 2 3 0 2 dad = select( population, fitness ); 3 0 0 0 2 1 0 1 0 1 2 2 2 2 3 2
  • 12. dad 3 0 0 0 2 1 0 1 0 1 2 2 2 2 3 2 mom 1 3 0 0 3 3 2 0 2 3 0 1 2 3 0 2 Crossover for i = 1 and i +1 = 2 and rand(1) = 0.68 > 0.5
  • 13. Mutation for i = 1 and i +1 = 2 child1 1 3 0 0 2 3 2 0 0 1 0 1 2 2 3 2 newPopulation( :, :, 1 ) = mutate(child1 ) 1 3 0 0 2 3 2 0 0 1 0 1 2 2 3 2 For each gene 1% probability exists to occur mutation. child2 3 0 0 0 3 1 0 1 2 3 2 2 2 3 0 2 newPopulation( :, :, 2 ) = mutate(child2 ) 3 0 0 0 3 1 0 1 2 3 2 2 2 3 0 2
  • 14. i = 3 and i +1 = 4 mom = select( population, fitness ); 3 2 3 1 1 0 2 3 3 1 3 0 0 3 3 0 dad = select( population, fitness ); 3 0 0 0 2 1 0 1 0 1 2 2 2 2 3 2
  • 15. dad 3 0 0 0 2 1 0 1 0 1 2 2 2 2 3 2 mom 3 2 3 1 1 0 2 3 3 1 3 0 0 3 3 0 Crossover for i = 3 and i +1 = 4 and rand(1) = 0.38 < 0.5 Number of the Column for Crossover a5 = fix (matrixSize*rand(1))+1 = 2;
  • 16. Mutation for i = 3 and i +1 = 4 child1 3 0 3 1 1 1 2 3 3 1 3 0 0 2 3 0 newPopulation( :, :, 3 ) = mutate(child1 ) 3 0 3 1 1 1 2 3 3 1 3 0 0 2 3 0 For each gene 1% probability exists to occur mutation. newPopulation( :, :, 4 ) = bestindividual( population, fitness) 3 2 3 1 1 0 2 3 3 1 3 0 0 3 3 0
  • 17. Population = newPopulation population(:,:,1) 1 3 0 0 2 3 2 0 0 1 0 1 2 2 3 2 population(:,:,2) 3 0 0 0 3 1 0 1 2 3 2 2 2 3 0 2 population(:,:,3) 3 0 3 1 1 1 2 3 3 1 3 0 0 2 3 0 population(:,:,4) 3 2 3 1 1 0 2 3 3 1 3 0 0 3 3 0
  • 18. Fitness changes Old fitness( 1 ) = checkers( population( :, :, 1 ) ) = 16 fitness( 2 ) = checkers( population( :, :, 2 ) ) = 17 fitness( 3 ) = checkers( population( :, :, 3 ) ) = 20 fitness( 4 ) = checkers( population( :, :, 4 ) ) = 21 New fitness( 1 ) = checkers( population( :, :, 1 ) ) = 20 fitness( 2 ) = checkers( population( :, :, 2 ) ) = 16 fitness( 3 ) = checkers( population( :, :, 3 ) ) = 20 fitness( 4 ) = checkers( population( :, :, 4 ) ) = 21
  • 19. After 47 generation the optimal fitness obtains 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0 5 10 15 20 25 30 35 40 45 50 16 17 18 19 20 21 22 23 24 Generations Fitness Best Fitness Mean Fitness
  • 20. Performance of Select Function population(:,:,1) 1 3 0 0 2 3 2 0 0 1 0 1 2 2 3 2 population(:,:,2) 3 0 0 0 3 1 0 1 2 3 2 2 2 3 0 2 population(:,:,3) 3 0 3 1 1 1 2 3 3 1 3 0 0 2 3 0 population(:,:,4) 3 2 3 1 1 0 2 3 3 1 3 0 0 3 3 0 fitness( 1 ) = checkers( population( :, :, 1 ) ) = 20 fitness( 2 ) = checkers( population( :, :, 2 ) ) = 16 fitness( 3 ) = checkers( population( :, :, 3 ) ) = 20 fitness( 4 ) = checkers( population( :, :, 4 ) ) = 21
  • 21. Performance of Select Function function selected = select( population, fitness ) [m, n] = size( fitness ); % m = n = 4 s = sum( fitness ); % s = 77 roulette = s*rand( 1 ); % roulette = 48.7868 currentSum = 0; picked = n; % n = 4 for i = 1:n s1 = currentSum + fitness(i) if s1 < roulette picked = I end currentSum = currentSum + fitness(i); end selected = population( :, :, picked ); i currentSum fitness(i) s1 if s1 < roulette picked 1 0 20 20 Yes 1 2 20 16 36 Yes 2 3 36 20 56 No 2 4 56 21 77 No 2