/
index.html
667 lines (584 loc) · 32.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="keywords" content="Genetic Algorithm, GA, metaheuristic, AI, tutorial">
<meta name="description" content="A tutorial for genetic algorithms">
<title>GA tutorial</title>
<link rel="stylesheet" href="style.css">
<!-- LATEX equation interpreter -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/katex.min.css" integrity="sha384-D+9gmBxUQogRLqvARvNLmA9hS2x//eK1FhVb9PiU86gmcrBrJAQT8okdJ4LMp2uv" crossorigin="anonymous">
<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/katex.min.js" integrity="sha384-483A6DwYfKeDa0Q52fJmxFXkcPCFfnXMoXblOkJ4JcA8zATN6Tm78UNL72AKk+0O" crossorigin="anonymous"></script>
<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/contrib/auto-render.min.js" integrity="sha384-yACMu8JWxKzSp/C1YV86pzGiQ/l1YUfE8oPuahJQxzehAjEt2GiQuy/BIvl9KyeF" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
</head>
<body>
<header>
<h1> Genetic Algorithms tutorial </h1>
<center>Possibly one of the simplest tutorials on GA</center>
</header>
<div class="content" >
<!-- INTRODUCTION -->
<section>
<h2>Problem formulation</h2>
<p>
Let us consider the simple problem of maximising the sum of a bit array:
</p>
<div class="flex center">
\(\max \qquad f=\sum_{i=1}^{N} x_{i} \)
</div>
<div class="flex center">
\( \qquad \qquad x_i \in \{0,1\} \)
</div>
<p>
The solution is obviously \( \mathbf{x}=[1, 1, 1, 1, ...] \).
</p>
<p>
Let us see how to use genetic algorithms to find this solution.
</p>
</section>
<!-- INITIATION -->
<section>
<h2>Initiate population</h2>
<p>
First, let's generate a set of random bit arrays as our initial solutions. We will call these individual candidate solutions the population. Click on Initiate to do this.<br><br>
</p>
<div class="flex center">
<button id="initiateButton" onclick="initiate()">Initiate</button>
</div>
<div id="initiate" class="initiate"> </div>
<script>
var npop=4;
var genomelength=5;
// Initiate random bit arrays
var genomes=new Array(npop).fill().map(genome => new Array(genomelength).fill().map(x=>Math.round(Math.random())) );
var pop=[];
// Creating the Individual class
var Individual=function(genome,id,name="Individual"){
this.genome=genome;
this.fitness=genome.reduce((a, b)=> a + b, 0);;
this.id=id;
this.update= function(elemId,text="",styles=new Array(genome.length).fill().map(x=>"")){
const popbox= document.getElementById(elemId);
genomebox=document.createElement("div");
genomebox.classList.add("genomebox");
genomeName=document.createElement('div');
genomeName.innerText=text;
genomeName.classList.add("genomeName");
genomebox.appendChild(genomeName);
genomedrawing=document.createElement('div');
genomedrawing.id=elemId+name+id;
genomedrawing.classList.add("genome");
genomedrawing.draggable=true;
genomebox.appendChild(genomedrawing);
popbox.appendChild(genomebox);
for (let index = 0; index < genome.length; index++) {
let thisbit = document.createElement('div');
thisbit.id=elemId+name+id+'bit'+index;
thisbit.innerText=genome[index].toString();
thisbit.classList.add("bit");
thisbit.style=styles[index]; // styles override
if(genome[index]==1){
thisbit.classList.add("style-for-1");
}else{
thisbit.classList.add("style-for-0");
}
genomedrawing.appendChild(thisbit);
}
}
}
var initiate= function (){
document.getElementById("initiate").innerHTML="";
genomes=new Array(npop).fill().map(genome => new Array(genomelength).fill().map(x=>Math.round(Math.random())) );
for (let i = 0; i < npop; i++) {
pop[i] = new Individual(genomes[i],i);
pop[i].update('initiate','Individual '+(i+1));
}
}
</script>
</section>
<!-- SELECTION -->
<section>
<h2>Selection</h2>
In this step, we will randomly pick two parent individuals from the initial population.
We will apply selection pressure by picking the better of the two.
This is analogous to "survival of the fittest" in Darwinian evolution.
There are other ways to apply selection pressure, but picking the best of two is called "tournament selection" since it mimics a typical knockout tournament.
Click all the <code>Pick randomly</code> buttons and then click <code>Select</code> to see who win this tournament.
<div class="flex-box selectionbox" >
<div class="column containercol">
<div class="container selection" id="selection1">
<button id='pick1button' onclick="pick(1)">Pick randomly</button>
<div id="select1"></div>
</div>
<div class="container selection" id="selection2">
<button id='pick2button' onclick="pick(2)">Pick randomly</button>
<div id="select2"></div>
</div>
</div>
<div class="column svgcol" >
<svg height="100%" width="100%">
<line x1="0%" y1="30%" x2="50%" y2="30%" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="0%" y1="70%" x2="50%" y2="70%" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="50%" y1="30%" x2="50%" y2="70%" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="50%" y1="50%" x2="100%" y2="50%" style="stroke:rgb(255,0,0);stroke-width:2" />
</svg>
</div>
<div class="column containercol" >
<div class="container parent" id="parentbox1">
<button id='select1button' onclick="select(1)">Select Parent 1</button>
<div id='parent1'></div>
</div>
</div>
</div>
<div class="flex-box selectionbox" >
<div class="column containercol">
<div class="container selection" id="selection3">
<button id='pick3button' onclick="pick(3)">Pick randomly</button>
<div id="select3"></div>
</div>
<div class="container selection" id="selection4">
<button id='pick4button' onclick="pick(4)">Pick randomly</button>
<div id="select4"></div>
</div>
</div>
<div class="column svgcol" >
<svg height="100%" width="100%">
<line x1="0%" y1="30%" x2="50%" y2="30%" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="0%" y1="70%" x2="50%" y2="70%" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="50%" y1="30%" x2="50%" y2="70%" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="50%" y1="50%" x2="100%" y2="50%" style="stroke:rgb(255,0,0);stroke-width:2" />
</svg>
</div>
<div class="column containercol" >
<div class="container parent" id="parentbox2">
<button id='select2button' onclick="select(2)">Select Parent 2</button>
<div id='parent2'></div>
</div>
</div>
</div>
<script> // Defining the selection operation
var parent1, parent2, pickedIndividuals=[1,2,3,4];
var pick = function(id){
let boxId='select'+id;
let boxdiv=document.getElementById(boxId);
boxdiv.innerHTML="";
let picked =Math.round(Math.random()*(pop.length-1));
pop[picked].update(boxId,'Individual '+(picked+1));
pickedIndividuals[id-1]=picked;
}
var select= function(id){
let parentId='parent'+id;
let cxparentId='cxparent'+id;
let rpparentId='rpparent'+id;
let parentdiv=document.getElementById('parent'+id);
let cxparentdiv=document.getElementById('cxparent'+id);
let rpparentdiv=document.getElementById('rpparent'+id);
parentdiv.innerHTML="";
cxparentdiv.innerHTML="";
rpparentdiv.innerHTML="";
let parent1styles=new Array(genomelength).fill().map(x=>"background-color:rgba(255,0,0,0.4)");
let parent2styles=new Array(genomelength).fill().map(x=>"background-color:rgba(0,0,255,0.4)");
let selected;
if (id==1){
if (pop[pickedIndividuals[0]].fitness>pop[pickedIndividuals[1]].fitness){selected=1 ; }else{ selected=2;}
parent1=pickedIndividuals[selected-1];
pop[parent1].update('parent'+id,'Individual '+(parent1+1));
pop[parent1].update('cxparent'+id,"",parent1styles);
pop[parent1].update('rpparent'+id,'Individual '+(parent1+1));
}
if (id==2){
if (pop[pickedIndividuals[2]].fitness>pop[pickedIndividuals[3]].fitness){selected=3 ; }else{ selected=4;}
parent2=pickedIndividuals[selected-1];
pop[parent2].update('parent'+id,'Individual '+(parent2+1));
pop[parent2].update('cxparent'+id,"",parent2styles);
pop[parent2].update('rpparent'+id,'Individual '+(parent2+1));
}
}
</script>
</section>
<!-- CROSS OVER -->
<section>
<h2>Crossover or Recombination</h2>
The selected individuals in the previous step are allowed to crossover or reproduce.
There are various ways one can perform the crossover.
In this demonstration, we illustrate three methods, namely, uniform, single-point and two-point crossover.
Pick one and click crossover. You may click crossover as many times as you want.
Each time a new pair of children will be generated.
<div class="flex-box selectionbox" >
<div class="column containercol" >
<div class="crossover container" id="selection1">
<div>Parent 1</div>
<div id="cxparent1"></div>
</div>
<div class="crossover container" id="selection2">
<div>Parent 2</div>
<div id="cxparent2"></div>
</div>
</div>
<div class="svgcol">
<svg height="100%" width="100%">
<line x1="0%" y1="30%" x2="100%" y2="70%" style="stroke:rgb(255,0,0);stroke-width:2" />
<line x1="0%" y1="70%" x2="100%" y2="30%" style="stroke:rgb(255,0,0);stroke-width:2" />
</svg>
</div>
<div class="column containercol" >
<div class="container offspring" id="child1box">
<div>Child 1</div>
<div id="child1"></div>
</div>
<div class="container offspring" id="child2box">
<div>Child 2</div>
<div id="child2"></div>
</div>
</div>
</div>
<center>
<button id='crossoverButton' onclick="crossover()">Crossover</button>
<form>
<input type="radio" name="choice" checked="checked" value="Uniform" id="choice-Uniform" onclick="updateCrossOverType()">
<label for="choice-Uniform">Uniform</label>
<input type="radio" name="choice" value="SinglePt" id="choice-SinglePt" onclick="updateCrossOverType()">
<label for="choice-SinglePt">Single point</label>
<input type="radio" name="choice" value="TwoPt" id="choice-TwoPt" onclick="updateCrossOverType()">
<label for="choice-TwoPt">Two point</label>
<br>
<!-- <button id="btn">Show Selected Value</button> -->
</form>
<div id="CrossoverHelp"></div>
</center>
<script> // Defining the crossover operation
var crossoverType;
var child1,child2;
var crossoverrate=0.5;
const updateCrossOverType = function(){
// radio button
const rbs = document.querySelectorAll('input[name="choice"]');
for (const rb of rbs) {
if (rb.checked) {
crossoverType = rb.value; // updates crossoverType to whats selected in the radiobutton
crossoverHelpDiv=document.getElementById("CrossoverHelp");
if (crossoverType=="Uniform"){
crossoverHelpDiv.innerText="Chooses each bit from either of the parents with equal likelihood.";
}else if(crossoverType=="SinglePt"){
crossoverHelpDiv.innerText="Swaps after a randomly chosen crossover point.";
}else if(crossoverType=="TwoPt"){
crossoverHelpDiv.innerText="Cuts at two random points and swaps between them.";
}
break;
}
}
// end radio button
}
const crossover=function(){
updateCrossOverType();
parent1genome=pop[parent1].genome;
parent2genome=pop[parent2].genome;
parent1styles=new Array(genomelength).fill().map(x=>"background-color:rgba(255,0,0,0.4)");
parent2styles=new Array(genomelength).fill().map(x=>"background-color:rgba(0,0,255,0.4)");
let child1genome=[],child2genome=[];
let child1styles=[],child2styles=[];
switch (crossoverType) {
case "Uniform":
for (let i = 0; i < parent1genome.length; i++) {
if(Math.random()<crossoverrate){
child1genome[i]=parent2genome[i];
child2genome[i]=parent1genome[i];
child1styles[i]=parent2styles[i];
child2styles[i]=parent1styles[i];
}else{
child1genome[i]=parent1genome[i];
child2genome[i]=parent2genome[i];
child1styles[i]=parent1styles[i];
child2styles[i]=parent2styles[i];
}
}
break;
case "SinglePt":
let cxPt=Math.round(Math.random()*(genomelength-1-1)+1);
// cxPt can be from second to last elem
for (let i = 0; i < parent1genome.length; i++) {
if(i<cxPt){
child1genome[i]=parent2genome[i];
child2genome[i]=parent1genome[i];
child1styles[i]=parent2styles[i];
child2styles[i]=parent1styles[i];
}else{
child1genome[i]=parent1genome[i];
child2genome[i]=parent2genome[i];
child1styles[i]=parent1styles[i];
child2styles[i]=parent2styles[i];
}
}
break;
case "TwoPt":
let cxPt1=Math.round(Math.random()*((genomelength-1)-1-1)); // from second to last but one
let cxPt2=Math.round(Math.random()*((genomelength-1)-cxPt1-1-1)+cxPt1+2); // from cxpt1+1 to last but one
// cxPt can be from second to last elem
for (let i = 0; i < parent1genome.length; i++) {
if(cxPt1<=i && i<=cxPt2){
child1genome[i]=parent2genome[i];
child2genome[i]=parent1genome[i];
child1styles[i]=parent2styles[i];
child2styles[i]=parent1styles[i];
}else{
child1genome[i]=parent1genome[i];
child2genome[i]=parent2genome[i];
child1styles[i]=parent1styles[i];
child2styles[i]=parent2styles[i];
}
}
break;
default:
// single pt
for (let i = 0; i < parent1genome.length; i++) {
if(i<cxPt){
child1genome[i]=parent2genome[i];
child2genome[i]=parent1genome[i];
child1styles[i]=parent2styles[i];
child2styles[i]=parent1styles[i];
}else{
child1genome[i]=parent1genome[i];
child2genome[i]=parent2genome[i];
child1styles[i]=parent1styles[i];
child2styles[i]=parent2styles[i];
}
}
break;
}
child1=new Individual(child1genome,1);
child2=new Individual(child2genome,2);
let childdiv,cxchilddiv;
childdiv=document.getElementById('child1');
cxchilddiv=document.getElementById('childtomutate1');
childdiv.innerHTML="";
cxchilddiv.innerHTML="";
childdiv=document.getElementById('child2');
cxchilddiv=document.getElementById('childtomutate2');
childdiv.innerHTML="";
cxchilddiv.innerHTML="";
child1.update('child1',"",child1styles);
child2.update('child2',"",child2styles);
child1.update('childtomutate1');
child2.update('childtomutate2');
}
</script>
</section>
<!-- MUTATION -->
<section>
<h2>Mutation</h2>
In this step, the recombined solutions of the crossover operation are mutated. In this implementation, each bit is flipped (0 becomes 1 and 1 becomes 0) with a probability (<code>mutationrate</code>) of 1/(array length). This is achieved using a random number generator.
For each bit, a random number is generated uniformly between 0 and 1. The bit is flipped if the random number is less than the <code>mutationrate</code>. Click mutate as many times as you want.
<div class="flex-box mutatebox" >
<div class="column containercol" >
<div class="container offspring" id="childtomutatebox1">
<div>Child 1</div>
<div id="childtomutate1"></div>
</div>
<div class="container mutant" id="mutantbox1">
<div>Mutant 1</div>
<div id="mutant1"></div>
</div>
<button id='mutate1Button' onclick="mutate(1)">Mutate</button>
</div>
<div class="column containercol" >
<div class="container offspring" id="childtomutatebox1">
<div>Child 2</div>
<div id="childtomutate2"></div>
</div>
<div class="container mutant" id="mutantbox2">
<div>Mutant 2</div>
<div id="mutant2"></div>
</div>
<button id='mutate2Button' onclick="mutate(2)">Mutate</button>
</div>
</div>
<script> // Defining the mutation operation
var mutant1,mutant2;
var mutationrate=1/genomelength;
var mutant1genome,mutant2genome;
var mutate=function(id){
let mutantdiv,mutanttoreplacediv;
if(id==1){
mutant1genome=child1.genome;
for (let i = 0; i < mutant1genome.length; i++) {
if(Math.random()<mutationrate){
mutant1genome[i]=1-mutant1genome[i];
}
}
mutant1=new Individual(mutant1genome,1);
mutantdiv=document.getElementById('mutant1');
rpmutantdiv=document.getElementById('rpmutant1');
mutantdiv.innerHTML="";
rpmutantdiv.innerHTML="";
mutant1.update('mutant1');
mutant1.update('rpmutant1');
}
if(id==2){
mutant2genome=child2.genome;
for (let i = 0; i < mutant2genome.length; i++) {
if(Math.random()<mutationrate){
mutant2genome[i]=1-mutant2genome[i];
}
}
mutant2=new Individual(mutant2genome,2);
mutantdiv=document.getElementById('mutant2');
rpmutantdiv=document.getElementById('rpmutant2');
mutantdiv.innerHTML="";
rpmutantdiv.innerHTML="";
mutant2.update('mutant2');
mutant2.update('rpmutant2');
}
return 0;
}
</script>
<div></div>
</section>
<!-- REPLACEMENT -->
<section>
<h2> Replacement</h2>
In this step, a decision will be made as to whether the mutated offspring will replace the parents. Again there are several ways to perform this.
In this implementation, a greedy acceptance is used, i.e., only if the offspring is strictly better (has more ones), it replaces the parent.
Once you click replace button, the population will be updated.
<div class="flex-row" >
<div class="column containercol" >
<div class="container mutant" id="childtoreplacebox1">
<div>Mutant 1</div>
<div id="rpmutant1"></div>
</div>
<div class="container offspring" id="mutantbox1">
<div>Parent 1</div>
<div id="rpparent1"></div>
</div>
<button id='replace1Button' onclick="replace(1)">Replace parent <br> with mutant if better</button>
</div>
<div class="column containercol" >
<div class="container mutant" id="childtoreplacebox1">
<div>Mutant 2</div>
<div id="rpmutant2"></div>
</div>
<div class="container offspring" id="mutantbox2">
<div>Parent 2</div>
<div id="rpparent2"></div>
</div>
<button id='replace2Button' onclick="replace(2)">Replace parent <br>with mutant if better</button>
</div>
</div>
<script>
// Defining the replace function
var replace=function(id){
if(id==1 && mutant1.fitness>=pop[parent1].fitness){
pop[parent1]=new Individual(mutant1genome,parent1);
}
if(id==2 && mutant2.fitness>=pop[parent2].fitness){
pop[parent2]=new Individual(mutant2genome,parent2);
}
}
</script>
</section>
<!-- BEST SOLUTION UPDATE -->
<section>
<h2>Best found solution</h2>
After completing each generation, you can check the best solution obtained so far. The update best solution button may be clicked anytime during the algorithmic process.
<div id="bestSolutionDiv" class="flex-box"></div>
<br>
<br>
<div class="flex center">
<button class="largeBtn" id="updateBestButton" onclick="updateBest()" >Update best solution</button>
</div>
<br>
<br>
</section>
<!-- REPEAT -->
<section>
<h2>Repeat</h2>
That must have been a lot of clicks, wasn't it? Now, you may conveniently repeat all these steps with a single click on the following button. You may also refresh the page, scroll down and start clicking repeat immediately.
Pressing the button will check if the solutions are initiated and if not done, does it automatically for you.
<center>
<button id="repeatButton" onclick="repeat()" >Repeat</button>
<br><br>
<div id="generationNo">Generation: 1</div>
</center>
<script>
var bestInd, bestFitness, generationNo=1;;
var updateBest=function(){
if (pop[0] instanceof Individual){
}else{// if population is not initiated, initiating it
document.getElementById('initiateButton').click();
}
document.getElementById("initiate").innerHTML="";
for (let i = 0; i < npop; i++) {
pop[i].update('initiate','Individual '+(i+1)); // update the population displayed
}
bestSolutionDiv=document.getElementById('bestSolutionDiv');
bestSolutionDiv.innerHTML="";
bestInd=0;
for (let ind = 1; ind < pop.length; ind++) {
if(pop[ind].fitness>=pop[bestInd].fitness){
bestInd=ind;
bestFitness=pop[bestInd].fitness;
}
}
pop[bestInd].update('bestSolutionDiv');
}
var repeat=function(){
if (pop[0] instanceof Individual){
}else{ // if population is not initiated, initiating it
document.getElementById('initiateButton').click();
}
// pick individuals for selection
document.getElementById('pick1button').click();
document.getElementById('pick2button').click();
document.getElementById('pick3button').click();
document.getElementById('pick4button').click();
// select individuals in a tournament
document.getElementById('select1button').click();
document.getElementById('select2button').click();
// perform cross-over
document.getElementById('crossoverButton').click();
// perform mutation
document.getElementById('mutate1Button').click();
document.getElementById('mutate2Button').click();
// replace children into parents
document.getElementById('replace1Button').click();
document.getElementById('replace2Button').click();
// update best solution
document.getElementById('updateBestButton').click();
generationNo+=1;
document.getElementById("generationNo").innerText="Generation: "+generationNo;
}
</script>
<br>
<br>
Now if you repeatedly click the Repeat button, eventually the best solution will be all ones. <br><br>
Hope you understood how genetic algorithms work from this simple example. Cheers for trying out this tutorial!
<br>
<br>
<br><br>
<h2>Bonus tip</h2>
You can play with this program using the console. For example, to update the population size or bit length, update the variables <code>npop</code> or <code>genomelength</code>.
To do this, open console by right-click > inspect, click on Console, and then enter the following:
<br><br>
<pre>
<code>npop = 12</code>
<code>genomelength = 6 </code>
</pre>
<br><br>
Then click on Initiate. You should see that the initial population has 12 individuals, each with 6 bits.
<br> <br>
Feel free to explore!
<br><br><br><br>
<br><br><br><br>
</section>
</div>
<footer>
MIT License 2.0 - Vivek T. Ramamoorthy<br>
<a href="https://VivekTRamamoorthy.github.io" target="_blank">Homepage</a>
| <a href="https://github.com/VivekTRamamoorthy" target="_blank">Source</a>
</footer>
</body>
</html>