-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic-algos.html
2815 lines (2616 loc) · 126 KB
/
genetic-algos.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Genetic Algorithms</title>
<link rel="stylesheet" href="reveal/dist/reset.css">
<link rel="stylesheet" href="reveal/dist/reveal.css">
<link rel="stylesheet" href="reveal/dist/theme/black.css" id="theme">
<link rel="stylesheet" href="genetic-algos.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Tight Genes:</h1>
<h2>Intro to Genetic Algorithms</h2>
<h3>by Dave Aronson</h3>
<h5>
<br/>
T.Rex-2023@Codosaur.us<br/>
twitter.com/DaveAronson<br/>
linkedin.com/in/DaveAronson
</h5>
<aside class="notes">
NOTE TO SELF: Timeslot is 60 mins, so aim for 45-50 of content.<br/>
CURRENT TIME: ~52:45 -- watch the ad-libs, but otherwise OK.
<br/><br/>
Howdy, Texas!
Before we kick things off,
a quick shout-out to . . .
</aside>
</section>
<section>
<img src="images/that-tx-sponsors.png" class="autofix">
<aside class="notes">
. . . our sponsors!
Without them,
this conference,
THAT Conference,
and many other conferences,
couldn't happen,
or at least wouldn't be so amazing.
So let's have
a big hand
and a round of applause for them!
(clap, clap, clap)
<br/><br/>
Now let's get on with <i>this</i> talk.
I'm Dave Aronson, the T. Rex of Codosaurus, and . . .
</aside>
</section>
<section>
<div class="main-text" style="font-size: 5em">🛫 ... 🛬</div>
<span class="slide-caption">Image: standard emoji</span>
<aside class="notes">
. . . I flew down here . . .
</aside>
</section>
<section>
<img src="images/chrome-offline-t-rex.png" height="35%"
style="position: absolute; top: 22%; right: 35%">
<img src="images/pterodactyl.png" height="60%"
style="position: absolute; top: 27%; left: 15%">
<span class="slide-caption">Images: https://pixabay.com/vectors/dinosaur-tyrannosaurus-t-rex-6273164/<br/>and https://pixabay.com/vectors/bird-flying-wings-dinosaur-ancient-44859/</span>
<aside class="notes">
. . . on my pet pterodactyl . . .
</aside>
</section>
<section>
<div class="main-text" style="font-size: 10em">👨🏽🏫</div>
<span class="slide-caption">Image: standard emoji</span>
<aside class="notes">
. . . to teach you about . . .
</aside>
</section>
<section>
<img src="images/genetic-testing.png" class="autofix" width="25%">
<div style="font-size: 2.4em">
Genetic
<br/>
<br/>
<br/>
Algorithms
</div>
<span class="slide-caption">Image: https://pixabay.com/vectors/genetic-testing-gene-panel-genetics-2316642</span>
<aside class="notes">
. . . Genetic Algorithms,
mainly . . .
</aside>
</section>
<section>
<div style="font-size: 1.2em; text-align: left">
<h2>Agenda</h2><br/>
<ul>
<li>what Genetic Algorithms are</li>
</ul>
</div>
<aside class="notes">
. . . what they are, . . .
</aside>
</section>
<section>
<div style="font-size: 1.2em; text-align: left">
<h2>Agenda</h2><br/>
<ul>
<li>what Genetic Algorithms are</li><br/>
<li>why use a Genetic Algorithm</li>
</ul>
</div>
<aside class="notes">
. . . when and why you might want to use one, and . . .
</aside>
</section>
<section>
<div style="font-size: 1.2em; text-align: left">
<h2>Agenda</h2><br/>
<ul>
<li>what Genetic Algorithms are</li><br/>
<li>why use a Genetic Algorithm</li><br/>
<li>how Genetic Algorithms work</li>
</ul>
</div>
<aside class="notes">
. . . how they work, with some demos,
which will actually be
the majority of the time
of this talk.
<br/><br/>
<!--
But first, a caveat.
I am not . . .
</aside>
</section>
<section>
<img src="images/expert.png" class="autofix">
<img src="images/nope-scope.png" class="autofix">
<span class="slide-caption">Image: https://pixabay.com/illustrations/smiley-nerd-glasses-pc-expert-1914523/</span>
<aside class="notes">
. . . an <i>expert</i> in Genetic Algorithms.
I haven't used them at work,
or for anything else serious.
But,
I have over 37 years of professional experience,
looked into the concept,
understood it
(or at least I think so!),
saw it seems to be a lot simpler
than its reputation implies,
and played with it a bit.
I found it very easy to
put together
what became the examples for this talk,
and a few other
evolutions and variations.
So, I put together a pitch for a talk to explain it,
got accepted,
actually <i>wrote</i> the talk,
and here I am!
<br/><br/>
-->
So what <i>are</i> Genetic Algorithms
in the first place?
They are . . .
</aside>
</section>
<section>
<img src="images/settings_icon-icons.com_49925.png" style="background-color: white">
<span class="slide-caption">Image: https://icon-icons.com/download/49925/PNG/512/</span>
<aside class="notes">
optimization heuristics,
</aside>
</section>
<section>
<div class="main-text" style="font-size: 8em">WAT?!</div>
<aside class="notes">
Okay, optimization means . . .
</aside>
</section>
<section>
<img src="images/army-reserve-captain-wanda-r-jewell-number-3-platform-from-redstone-arsenal-CROPPED.jpg" height="65%">
<div style="font-size: 1.5em">
Optimization: finding a better<br/>solution (ideally the best)
</div>
<span class="slide-caption">Image: https://picryl.com/media/army-reserve-captain-wanda-r-jewell-number-3-platform-from-redstone-arsenal-7f7d64</span>
<aside class="notes">
. . . finding a better solution
for something.
Ideally you get the best solution,
but you might not have time
to do all the calculations and such
to get all the way there.
So, just making it as better as you can,
still counts as <i>some</i> optimization.
So you could sum it up as <i>improvement</i>.
And a heuristic is a . . .
</aside>
</section>
<section>
<img src="images/shortcut-through-the-dunes.jpg" height="65%">
<div style="font-size: 1.5em">
Heuristic: shortcut to find <i>any</i> solution,
maybe "good enough"
</div>
<span class="slide-caption">Image: https://www.publicdomainpictures.net/en/view-image.php?image=205157</span>
<aside class="notes">
. . . shortcut to finding a solution to a problem.
Heuristics are used because they are
faster, simpler, cheaper,
or in some such way less resource-intensive,
than methods like
precise calculation or
brute-forcing your way through all possible values.
However, heuristics are not guaranteed to find
the best solution,
only <i>a</i> solution,
though <i>usually</i> there's an element of
it being "good enough",
for whatever value of "good enough"
we care to apply.
So . . .
</aside>
</section>
<section>
<div style="font-size: 2em">
<b><big>Optimization Heuristics:</big></b><br/>
shortcuts to find<br/>
"good enough" solutions<br/>
(ideally the best,<br/>
but OK if not).
</div>
<aside class="notes">
. . . optimization heuristics,
such as genetic algorithms,
are shortcuts
to find a good solution to a problem,
ideally the best solution,
but we'll usually have to settle for
something "good enough",
due to operational constraints such as time or money.
<br/><br/>
There are many kinds of optimization heuristics,
but genetic algorithms in particular,
as you may have guessed from the name,
are inspired by . . .
</aside>
</section>
<section>
<img src="images/Darwin-chart.PNG" class="autofix">
<span class="slide-caption">Image: https://commons.wikimedia.org/wiki/File:Darwin-chart.PNG</span>
<aside class="notes">
. . . real-world biological evolution,
primarily the principles of
survival of the fittest,
random combination of different sets of genes into one new set,
and random mutation.
<br/><br/>
The history goes back to 1950, when . . .
</aside>
</section>
<section>
<img src="images/turing.png" height="65%">
<div style="font-size: 3em">Alan Turing</div>
<span class="slide-caption">Image: https://cdn.britannica.com/81/191581-050-8C0A8CD3/Alan-Turing.jpg</span>
<aside class="notes">
. . . Alan Turing,
as in Turing Test, Turing Machine, Turing Completeness, and so on,
proposed a "learning machine"
in which the mechanism of learning
would be similar to evolution.
Nothing much came of that,
nor of genetic algorithms in general...
for a few decades.
But then they finally got a bit of traction.
The first commercial product based on genetic algorithms,
a mainframe toolkit for . . .
</aside>
</section>
<section>
<img src="images/industrial-process.jpg" class="autofix">
<span class="slide-caption">Image: https://pxhere.com/en/photo/267871</span>
<aside class="notes">
. . . industrial processes,
came out in the late 1980's,
from General Electric.
In 1989 a genetic algorithm toolkit called Evolver
came out for <i>PCs</i>.
These days,
MATLAB has a few genetic algorithm tools built-in,
and many languages have
genetic algorithm packages available.
But, the actual <i>uses</i> of genetic algorithms
remain generally obscure,
mostly used by companies in their internal
industrial processes, scheduling, logistics, and so on.
<br/><br/>
But once in a while, they get used for
something more interesting.
Most famously,
in 2005 . . .
</aside>
</section>
<section>
<img src="images/nasa-logo-web-rgb.png" class="autofix">
<span class="slide-caption">Image: https://www.nasa.gov/sites/default/files/thumbnails/image/nasa-logo-web-rgb.png</span>
<aside class="notes">
. . . NASA used a genetic algorithm to design an . . .
</aside>
</section>
<section>
<img src="images/st5-antenna.jpg" height="65%">
<div style="font-size: 1.5em">
ST5 satellite antenna,<br/>
with a quarter for scale
</div>
<span class="slide-caption">Image: https://www.jpl.nasa.gov/nmp/st5/IMAGES/st5-antenna.jpg</span>
<aside class="notes">
. . . antenna for the ST5 series of satellites,
launched in 2006.
(No, that's not just a paperclip
that got all bent up by someone
sitting bored and fidgeting,
in a meeting.)
The NASA Jet Propulsion Laboratory website says:
"Its unusual shape is expected
because most human antenna designers
would never think of such a design."
That is one of the great advantages of this approach.
These algorithms are much less hampered by
expectations of similarity to past solutions.
However, today we'll only be looking at
much simpler problem domains,
for the sake of making understandable demos.
<br/><br/>
So how do genetic algorithms work?
They consist of a simple series of steps:
</aside>
</section>
<section>
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<aside class="notes">
First, we create an initial population of candidates.
In Genetic Algorithm lingo, these are called "chromosomes",
but since most living beings contain
<i>many</i> chromosomes in each and every cell,
I don't like that term,
I think it leads to confusion,
so I'm just going to say "candidates".
I've also heard them called individuals, solutions,
or <i>phenotypes</i>,
to use a much more appropriate actual genetic term,
but most people don't know that word.
<br/><br/>
The next step is to . . .
</aside>
</section>
<section>
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="center cycle-stage cycle-stage-assess">Assess</span>
<aside class="notes">
. . . assess the "fitness" of each candidate,
according to whatever criteria we want to apply.
We do it here mainly because
it supplies the data
<i>usually</i> used in the next step,
which is to ask, are we . . .
</aside>
</section>
<section>
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="center cycle-stage cycle-stage-assess">Assess</span>
<span class="cycle-arrow-assess">⬅️</span>
<span class="cycle-stage cycle-stage-done">Done?</span>
<aside class="notes">
. . . done yet?
This is <i>usually</i> based on the fitness,
but <i>could</i> be based on other criteria,
and we'll discuss some of those later,
or a combination.
If we're not done, then next we . . .
</aside>
</section>
<section>
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="center cycle-stage cycle-stage-assess">Assess</span>
<span class="cycle-arrow-assess">⬅️</span>
<span class="cycle-stage cycle-stage-done">Done?</span>
<span class="cycle-arrow-done">⬇️</span>
<span class="cycle-stage cycle-stage-select">Select</span>
<aside class="notes">
. . . select some candidates to breed the next generation.
This is usually <i>also</i> based on the fitness,
to simulate <i>survival</i> of the fittest.
<!-- Remember, evolution is not about survival to a ripe old age,
it's only concerned with
survival to the point of passing along one's genes.
Survival of an elder
may contribute to survival of the young,
but that's a much more complicated discussion
of indirect evolutionary pressures. -->
<br/><br/>
After that, as you may have guessed,
we <i>use</i> the candidates we just selected . . .
</aside>
</section>
<section>
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="center cycle-stage cycle-stage-assess">Assess</span>
<span class="cycle-arrow-assess">⬅️</span>
<span class="cycle-stage cycle-stage-done">Done?</span>
<span class="cycle-arrow-done">⬇️</span>
<span class="cycle-stage cycle-stage-select">Select</span>
<span class="center cycle-arrow-select">➡️</span>
<span class="cycle-stage cycle-stage-breed">Breed</span>
<aside class="notes">
. . . to actually <i>breed</i>
a new population.
Some of the previous population,
especially the fittest ones,
<i>may</i> be carried over into this new population,
but usually not.
<br/><br/>
Next is a very important but easily forgotten step,
which is to . . .
</aside>
</section>
<section>
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="center cycle-stage cycle-stage-assess">Assess</span>
<span class="cycle-arrow-assess">⬅️</span>
<span class="cycle-stage cycle-stage-done">Done?</span>
<span class="cycle-arrow-done">⬇️</span>
<span class="cycle-stage cycle-stage-select">Select</span>
<span class="center cycle-arrow-select">➡️</span>
<span class="cycle-stage cycle-stage-breed">Breed</span>
<span class="cycle-arrow-breed">⬆️</span>
<span class="cycle-stage cycle-stage-mutate">Mutate</span>
<aside class="notes">
. . . <i>mutate</i> those new candidates,
so that we get some more diversity in the gene pool.
<br/><br/>
Finally, we . . .
</aside>
</section>
<section>
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="center cycle-stage cycle-stage-assess">Assess</span>
<span class="cycle-arrow-assess">⬅️</span>
<span class="cycle-stage cycle-stage-done">Done?</span>
<span class="cycle-arrow-done">⬇️</span>
<span class="cycle-stage cycle-stage-select">Select</span>
<span class="center cycle-arrow-select">➡️</span>
<span class="cycle-stage cycle-stage-breed">Breed</span>
<span class="cycle-arrow-breed">⬆️</span>
<span class="cycle-stage cycle-stage-mutate">Mutate</span>
<span class="cycle-arrow-mutate">⬅️</span>
<aside class="notes">
. . . go back to step 2, assessing their fitness.
<!-- (You might want to take a picture of this slide
and the next one,
because they're the big takeaways about
how genetic algorithms work at a high level.) -->
This sequence could be represented at a high level
with some rather simple code, like so:
</aside>
</section>
<section>
<img src="images/code/overview.png" class="autofix code">
<aside class="notes">
<!-- (I'll give you a moment to take pictures.) -->
Now let's take a closer look at what goes on in each step,
by working through an example.
</aside>
</section>
<section>
<div class="cycling-steps">
<span class="current-step center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="center cycle-stage cycle-stage-assess">Assess</span>
<span class="cycle-arrow-assess">⬅️</span>
<span class="cycle-stage cycle-stage-done">Done?</span>
<span class="cycle-arrow-done">⬇️</span>
<span class="cycle-stage cycle-stage-select">Select</span>
<span class="center cycle-arrow-select">➡️</span>
<span class="cycle-stage cycle-stage-breed">Breed</span>
<span class="cycle-arrow-breed">⬆️</span>
<span class="cycle-stage cycle-stage-mutate">Mutate</span>
<span class="cycle-arrow-mutate">⬅️</span>
</div>
<aside class="notes">
First we create an initial population of candidates.
But what is a candidate, and how do we create one?
These are different solutions to some problem,
usually represented as
different instances of the same data structure.
They could be
<!-- objects of the same class,
maps with the same keys,
arrays where the elements have meanings based on the index,
or --> anything <!-- else --> we want,
so long as we can
evaluate their fitness,
and combine <!-- two (or maybe more!) --> old ones
to make a new one.
The simplest common type of candidate is . . .
</aside>
</section>
<section>
<div style="font-size: .8em">
<b>
<code>
01001000<br/>
01100101<br/>
01101100<br/>
01101100<br/>
01101111<br/>
00100000<br/>
01110111<br/>
01101111<br/>
01110010<br/>
01101100<br/>
01100100<br/>
00100001<br/>
</code>
</b>
</div>
<aside class="notes">
. . . a simple string of bits.
This will do fine for candidates that consist of
a simple series of yes/no decisions.
This may sound simplistic,
but there is a huge class of problems that boil down to this,
called . . .
</aside>
</section>
<section>
<br/>
<img src="images/backpack.png" height="65%">
<span class="slide-caption">Image: https://publicdomainvectors.org/en/free-clipart/Rucksack-vector-image/9910.html</span>
<br/>Knapsack / Rucksack / Backpack / Whatever!
<aside class="notes">
. . . knapsack problems,
which is a category of
constrained resource allocation problems.
The canonical example is that you have a knapsack --
or rucksack, backpack, or whatever you call it --
and many things you want to carry in it,
but they won't all fit<!--,
or the total weight is more than you can carry,
or some such similar constraint,
or <i>combination</i> of constraints -->.
So you want to find the combination of items,
that will fit <!-- the constraints --> in the knapsack,
and has the maximum value.
That could be the literal cash value,
as we'll see in a moment,
or something more metaphorical,
like . . .
</aside>
</section>
<section>
<img src="images/survival-gear.jpg">
<span class="slide-caption">Image: https://www.pexels.com/photo/first-aid-and-surival-kits-5125690/</span>
<aside class="notes">
. . .
when <i>literally</i> putting items in a knapsack,
to go on a camping trip,
we would probably be much more concerned with each item's
usefulness for survival and comfort.
If we have only, say, three items,
that's only eight combinations,
so we can probably brute-force
our way through the solution space
pretty easily.
However, each time we add a possible item,
that doubles the number of combinations<!--,
just like adding a bit to the width of an integer type,
and for exactly the same reason -->.
So if we have just ten items to consider,
that's 1024 possible combinations,
and not all of them might even fit.
To look at a concrete example, suppose we know . . .
</aside>
</section>
<section>
<br/>
<div style="font-size: 9em">
🧑🏽🌾
🚚
</div>
<span class="slide-caption">Image: standard emoji</span>
<aside class="notes">
. . . a farmer,
with a smallish truck,
and he needs to decide what to take to market.
And on this farm he has . . .
</aside>
</section>
<section>
<div class="main-text" style="font-size: 4em">
🐄 🐄 🐄 🐄
</div>
<span class="slide-caption">Images: standard emoji</span>
<aside class="notes">
. . . some cows, EIEIO!
So among the things he can take to market are:
</aside>
</section>
<section>
<br/>
<div style="font-size: 4em">
🐄
🥛
🧀
🧈
🍨
🥩
<span style="background-image: url(images/leather.png)"> <span>
</div>
<span class="slide-caption">Images: standard emoji plus https://www.rawpixel.com/image/6130389/</span>
<aside class="notes">
. . . cows,
milk,
cheese,
butter,
ice cream,
meat,
and leather.
For the sake of simplicity,
we won't differentiate between
between price and profit,
nor dairy versus meat cows,
and <!-- it's very unrealistic but
we'll assume that -->
he can only take a set amount of each item,
and always has that amount on hand.
His truck has <i>room</i> to take all the items,
but it can only carry so much <i>weight</i>,
so that's our constraint.
His choices are as follows:
</aside>
</section>
<section>
<br/>
<table class="all-right">
<thead>
<tr>
<th class="text">What</th><th style="color:gray" class="text">Unit</th><th style="color:gray">Qty</th><th>Pounds</th><th>Value</th><!--<th>$/lb</th>-->
</tr>
</thead>
<tbody>
<tr>
<td class="text">Cow</td><td style="color:gray" class="text">cow</td><td style="color:gray">1</td><td>1,500</td><td>$2,000</td><!-- <td>$1.33</td> -->
</tr>
<tr>
<td class="text">Milk</td><td style="color:gray" class="text">1-gal jug</td><td style="color:gray">200</td><td>1,720</td><td>$800</td><!-- <td>$0.47</td> -->
</tr>
<tr>
<td class="text">Cheese</td><td style="color:gray" class="text">5-lb wheel</td><td style="color:gray">200</td><td>1,000</td><td>$12,000</td><!-- <td>$12.00</td> -->
</tr>
<tr>
<td class="text">Butter</td><td style="color:gray" class="text">1-lb block</td><td style="color:gray">1,000</td><td>1,000</td><td>$3,000</td><!-- <td>$3.00</td> -->
</tr>
<tr>
<td class="text">Ice Cream</td><td style="color:gray" class="text">1-gal tubs</td><td style="color:gray">200</td><td>1,000</td><td>$2,000</td><!-- <td>$2.00</td> -->
</tr>
<tr>
<td class="text">Meat</td><td style="color:gray" class="text">side</td><td style="color:gray">4</td><td>1,280</td><td>$8,000</td><!-- <td>$6.25</td> -->
</tr>
<tr>
<td class="text">Leather</td><td style="color:gray" class="text">hide</td><td style="color:gray">20</td><td>1,100</td><td>$6,000</td><!-- <td>$5.45</td> -->
</tr>
</tbody>
</table>
<aside class="notes">
You don't need to remember all that,
just realize that
it totals 8,600 pounds.
But, his truck's suspension
can only handle
two tons, or 4,000 pounds.
We <i>could</i> run through all the possible combinations,
<!-- see which ones total light enough,
and see which one of those totals the highest, -->
or use another kind of heuristic,
<!-- a different very common heuristic
of adding the most expensive thing that we can,
over and over until we can't, -->
but let's see what happens if we use a genetic algorithm.
First we need a way to represent each candidate.
In code, we could represent them as a class
in a language such as Ruby,
and create one randomly,
like so:
</aside>
</section>
<section>
<img src="images/code/class-truckload.png" class="autofix code">
<aside class="notes">
Whoa, that looks like we're just making a random number!
That's right, we're making a random number
<i>with seven bits</i>,
meaning that we have a random 1 or 0
for each of our seven possible items.
We could get as complex as we want in this function,
like dictating a minimum or maximum number of items,
but let's keep it simple.
<br/><br/>
To create an initial population,
we can just create a bunch of candidates
and stuff them into an array, . . .
</aside>
</section>
<section>
<img src="images/code/def-init-pop-1.png" class="autofix code">
<aside class="notes">
. . . like so.
(This could actually be done in much more idiomatic Ruby,
so don't scold me for that,
I'm just trying to keep it easily understandable by
people who don't know Ruby.)
So if we create a population of ten Truckloads,
we might wind up with a list like this:
</aside>
</section>
<section>
<table class="centered" style="font-size: 0.9em">
<thead>
<tr>
<th>Cow</th>
<th>Milk</th>
<th>Cheese</th>
<th>Butter</th>
<th>Ice Cream</th>
<th>Meat</th>
<th>Leather</th>
</tr>
</thead>
<tbody>
<tr><td>Y</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>Y</td><td>Y</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>Y</td><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td></tr>
<tr><td>N</td><td>Y</td><td>Y</td><td>N</td><td>Y</td><td>N</td><td>N</td></tr>
<tr><td>Y</td><td>Y</td><td>Y</td><td>N</td><td>Y</td><td>Y</td><td>N</td></tr>
<tr><td>Y</td><td>Y</td><td>N</td><td>Y</td><td>N</td><td>N</td><td>N</td></tr>
<tr><td>Y</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>Y</td><td>N</td></tr>
<tr><td>Y</td><td>Y</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td></tr>
<tr><td>N</td><td>N</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td></tr>
<tr><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td><td>Y</td><td>N</td></tr>
</tbody>
</table>
<aside class="notes">
Why ten?
Because that's what fits on the screen
in a decently readable font.
If I were doing this for real,
with a much more complex domain,
I might use a hundred or a thousand.
And how did we get from random numbers to those combinations?
Behind the scenes, that translation might look like this:
</aside>
</section>
<section>
<img src="images/code/class-item.png" class="autofix code">
<aside class="notes">
We have a list of items we <i>can</i> take,
as instances of an inner class describing them.
To check what's in our cargo manifest,
represented by our Truckload's contents value,
we can iterate through the list of possible items,
checking whether the corresponding bit is on.
So now we're done with the Initialization step.
The next step is . . .
</aside>
</section>
<section>
<div class="cycling-steps">
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="current-step center cycle-stage cycle-stage-assess">Assess</span>
<span class="cycle-arrow-assess">⬅️</span>
<span class="cycle-stage cycle-stage-done">Done?</span>
<span class="cycle-arrow-done">⬇️</span>
<span class="cycle-stage cycle-stage-select">Select</span>
<span class="center cycle-arrow-select">➡️</span>
<span class="cycle-stage cycle-stage-breed">Breed</span>
<span class="cycle-arrow-breed">⬆️</span>
<span class="cycle-stage cycle-stage-mutate">Mutate</span>
<span class="cycle-arrow-mutate">⬅️</span>
<span class="center cycle-ptr cycle-ptr-assess">↑</span>
</div>
<aside class="notes">
. . . to
assess how "fit" each of these truckloads is.
We do this with what's called a "fitness function".
Just like how biological creatures might be perfectly fit
for one environment but a lousy fit for another,
this should reflect how fit a candidate is
<i>for some particular purpose</i>.
In this case, we already know we want the total value,
BUT, any load that's too heavy for the truck, is worthless.
In Ruby, that would look like this:
</aside>
</section>
<section>
<img src="images/code/def-truckload-fitness.png" class="autofix code">
<aside class="notes">
We iterate through the possible items,
summing up the weights of the ones we want to take.
(Writing the bit_on? function is left as an exercise,
to keep this code simple.)
If that exceeds the truck's capacity, we return zero,
else we use the same technique
to sum up the <i>values</i> of those same items.
<br/><br/>
Once again,
we could get as complex as we want in this function,
and NASA's antenna fitness function
certainly must have been.
For instance, if we also had the <i>volume</i> of each item,
and the truck were smaller,
we could also total up the volume,
and make sure it all fits.
<!-- We could even take into account very long or flat shapes,
so that for instance
a flagpole or a large sheet of plywood might not fit,
even though its weight and total volume
may be relatively small. -->
<br/><br/>
Anyway, if we run this fitness function on our population,
we get this:
</aside>
</section>
<section>
<div style="font-size: 0.8em">
<table class="centered">
<thead>
<tr>
<th>Cow</th>
<th>Milk</th>
<th>Cheese</th>
<th>Butter</th>
<th>Ice Cream</th>
<th>Meat</th>
<th>Leather</th>
<th>Fitness</th>
</tr>
</thead>
<tbody>
<tr><td>Y</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>Y</td><td>Y</td><td class="numeric">0</td></tr>
<tr><td>N</td><td>N</td><td>N</td><td>Y</td><td>Y</td><td>N</td><td>N</td><td class="numeric">5,000</td></tr>
<tr><td>N</td><td>Y</td><td>N</td><td>N</td><td>N</td><td>Y</td><td>N</td><td class="numeric">8,800</td></tr>
<tr><td>N</td><td>Y</td><td>Y</td><td>N</td><td>Y</td><td>N</td><td>N</td><td class="numeric">14,800</td></tr>
<tr><td>Y</td><td>Y</td><td>Y</td><td>N</td><td>Y</td><td>Y</td><td>N</td><td class="numeric">0</td></tr>
<tr><td>Y</td><td>Y</td><td>N</td><td>Y</td><td>N</td><td>N</td><td>N</td><td class="numeric">0</td></tr>
<tr><td>Y</td><td>N</td><td>N</td><td>Y</td><td>N</td><td>Y</td><td>N</td><td class="numeric">13,000</td></tr>
<tr><td>Y</td><td>Y</td><td>N</td><td>N</td><td>N</td><td>N</td><td>N</td><td class="numeric">2,800</td></tr>
<tr><td>N</td><td>N</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td class="numeric">0</td></tr>
<tr><td>N</td><td>N</td><td>Y</td><td>N</td><td>N</td><td>Y</td><td>N</td><td class="numeric">20,000</td></tr>
</tbody>
</table>
</div>
<aside class="notes">
So now we're done with the Assessment step.
The next step is to see if . . .
</aside>
</section>
<section>
<div class="cycling-steps">
<span class="center cycle-stage cycle-stage-init">Initialize</span>
<span class="center cycle-arrow-init">⬇️</span>
<span class="center cycle-stage cycle-stage-assess">Assess</span>
<span class="cycle-arrow-assess">⬅️</span>
<span class="current-step cycle-stage cycle-stage-done">Done?</span>
<span class="cycle-arrow-done">⬇️</span>
<span class="cycle-stage cycle-stage-select">Select</span>
<span class="center cycle-arrow-select">➡️</span>
<span class="cycle-stage cycle-stage-breed">Breed</span>
<span class="cycle-arrow-breed">⬆️</span>
<span class="cycle-stage cycle-stage-mutate">Mutate</span>
<span class="cycle-arrow-mutate">⬅️</span>
<span class="center cycle-ptr cycle-ptr-done">↑</span>
</div>
<aside class="notes">
. . . we're done.
So what are our criteria?
The function can be simple,
but it can take some thinking to figure out
what the function should <i>do</i>.
With a knapsack problem,
a good solution can be made totally worthless by
adding just one more item, and exceeding the capacity.
So, we're going to record the best we've seen,
and stop if we haven't seen anything better
within 100 generations.
Why 100?
Pretty much random.
It seems like enough for a good chance for improvement,
and since what we're doing is so simple,
using lots of generations isn't very slow.
In Ruby, that would look like this:
</aside>
</section>
<section>
<img src="images/code/def-truckload-done.png" class="autofix code">
<aside class="notes">
When the code is initially parsed,
we set the initial best combo as empty,
and we set how many generations it's been since we saw that,
as zero,
both as class-variables.
(Remember, this is within the definition of class Truckload.)
When the function is called,
we increment the number of generations,
look at the fitness of each member of the current population,