Evolving JavaScript: Cultivating Genetic Algorithms for Creative Coding

A presentation at JSConf Budapest 2024 in June 2024 in Budapest, Hungary by Kevin Maes

Slide 1

Slide 1

Evolving JavaScript Cultivating Genetic Algorithms for Creative Coding Kevin Maes | JSConf Budapest - June 28, 2024

Slide 2

Slide 2

Evolving JavaScript Cultivating Genetic Algorithms for Creative Coding Jó napot! Kevin Maes | JSConf Budapest - June 28, 2024

Slide 3

Slide 3

From the way way way back machine

Slide 4

Slide 4

Slide 5

Slide 5

Slide 6

Slide 6

Slide 7

Slide 7

Kevin Maes @kvmaes

Slide 8

Slide 8

Slide 9

Slide 9

Evolutionary Computation Genetic Programming (GP) Lawrence Fogel

Slide 10

Slide 10

Evolutionary Computation Genetic Programming (GP) Lawrence Fogel Genetic Algorithms (GA) John H. Holland

Slide 11

Slide 11

Evolutionary Computation Genetic Programming (GP) Evolutionary Programming (EP) Genetic Algorithms (GA) Evolutionary Strategies (ES) Lawrence Fogel John H. Holland Evolutionary Algorithms

Slide 12

Slide 12

John H. Holland

Slide 13

Slide 13

John H. Holland Adaptation in Natural and Arti cial Systems fi “general theories of adaptive processes apply across biological, cognitive, social, and computational systems”

Slide 14

Slide 14

Why genetic algorithms? Desired behavior Optimal design Solve complex search problems

Slide 15

Slide 15

There’s a GA for that! • Designing ef cient network topologies • Automated software testing • Evolving game strategies • Scheduling Problems fi • Complex hardware design

Slide 16

Slide 16

Evolved antenna First generation Middle generations Last generation

Slide 17

Slide 17

= ST5-33-142-7

Slide 18

Slide 18

States of a genetic algorithm

Slide 19

Slide 19

Population Initialization Many “individuals” Each with a potential solution Stored in their “DNA”

Slide 20

Slide 20

Evaluation & Fitness Evaluate performance Reward behavior Punishment Weighted criteria

Slide 21

Slide 21

Selection Roulette Wheel Selection 4% 28% 3% 1% 6% R 22% 8% 11% 17%

Slide 22

Slide 22

Crossover x total population size

Slide 23

Slide 23

Mutation • Need to maintain variation • Avoid “local optima” • Strive for the “global optimum” • Mutation rate • Mutation amount

Slide 24

Slide 24

States of a genetic algorithm

Slide 25

Slide 25

Canvas, easy as 1, 2, 3. 1. // index.html <canvas id=“canvas” width=“500” height=“500”></canvas> 2. // Access the canvas element const canvas = document.getElementById(‘canvas’); 3. // Get the 2d canvas context const ctx = canvas.getContext(‘2d’); See Canvas API docs for how to draw shapes, text, styles, etc.

Slide 26

Slide 26

Slide 27

Slide 27

Population Initialization

Slide 28

Slide 28

draw()

Slide 29

Slide 29

draw()

Slide 30

Slide 30

Slide 31

Slide 31

Evaluation

Slide 32

Slide 32

Evaluation + fitness function

Slide 33

Slide 33

Slide 34

Slide 34

Slide 35

Slide 35

Selection

Slide 36

Slide 36

Selection by roulette wheel

Slide 37

Slide 37

Selection by roulette wheel

Slide 38

Slide 38

Crossover

Slide 39

Slide 39

Crossover

Slide 40

Slide 40

Mutation

Slide 41

Slide 41

Mutation

Slide 42

Slide 42

Slide 43

Slide 43

Slide 44

Slide 44

Slide 45

Slide 45

Slide 46

Slide 46

Slide 47

Slide 47

Slide 48

Slide 48

Slide 49

Slide 49

Slide 50

Slide 50

Slide 51

Slide 51

Slide 52

Slide 52

Slide 53

Slide 53

Slide 54

Slide 54

Slide 55

Slide 55

Slide 56

Slide 56

Slide 57

Slide 57

Slide 58

Slide 58

Slide 59

Slide 59

Slide 60

Slide 60

Slide 61

Slide 61

Slide 62

Slide 62

Demo Repo github.com/kevinmaes/ga Circles Particles

Slide 63

Slide 63

Creative Coding Tips •Don’t forget to clear the canvas before drawing •Use requestAnimationFrame •Crank up the frame rate and be impressed •Consider OOP for particle systems

Slide 64

Slide 64

fi •Simpli ed syntax •Built-in animation loop •Event handling •Extensive library •Community resources •Playground environment

Slide 65

Slide 65

Things I’ve learned fi •Population size •Tweak the tness function •Try other selection methods •Adjust mutation rates and amounts

Slide 66

Slide 66

Can GAs help us? •Optimizing UI Layouts •Automating A/B Testing •Personalized content recommendations •Code optimization or readability •Dynamic pricing models •Improve website accessibility

Slide 67

Slide 67

Resources Daniel Shiffman thecodingtrain.com/ New JavaScript version coming September 2024!

Slide 68

Slide 68

EDGE OF TOMORROW

Slide 69

Slide 69

LIVE DIE REPEAT

Slide 70

Slide 70

Slide 71

Slide 71

kevin@kevinmaes.com linkedin.com/in/kevinmaes @kvmaes Kevin Maes github.com/kevinmaes Thank you!