Slide 1

Slide 1 text

RUBIK'S CUBES AND GENETIC ALGORITHMS IN SWIFT @JAVI 1 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 2

Slide 2 text

EVOLUTION BY NATURAL SELECTION 2 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 3

Slide 3 text

EVOLUTION BY NATURAL SELECTION 3 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 4

Slide 4 text

4 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 5

Slide 5 text

EVOLUTION BY NATURAL SELECTION 1. ? 2. Random mutations in genome 3. Survival of the fittest 4. (Rinse and repeat) 5 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 6

Slide 6 text

GENETIC ALGORITHMS 6 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 7

Slide 7 text

GENETIC ALGORITHMS 7 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 8

Slide 8 text

GENETIC ALGORITHMS - EXAMPLE 1. GOAL: FIND A NUMBER 'X' 2. START WITH 0000000000000000 3. MUTATE EACH RANDOMLY, BIT BY BIT 4. CALC FITNESS COMPARING WITH X'S BITS 8 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 9

Slide 9 text

GENETIC ALGORITHMS - EXAMPLE final class Individual { let number: UInt init(number: UInt) { self.number = number } } 9 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 10

Slide 10 text

GENETIC ALGORITHMS - EXAMPLE typealias Fitness = Int extension Individual { func fitness(withGoal goal: UInt) -> Fitness { return Fitness(self.number.numberOfBitsEqual(in: goal)) } } 10 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 11

Slide 11 text

GENETIC ALGORITHMS - EXAMPLE extension Individual { let likelyhoodOfMutatingBit = 5 func mutate() -> Individual { let mutatedBits = self.number.bits.map { bit -> Bool in let shouldMutate = Bool.random(percentage: likelyhoodOfMutatingBit) return shouldMutate ? !bit : bit } return Individual(number: UInt(bits: mutatedBits)) } } 11 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 12

Slide 12 text

GENETIC ALGORITHMS - EXAMPLE final class GeneticSolver { var population: [Individual] init(numberToSolve: UInt, populationSize: Int) func runGeneration() { // 1: Natural selection (survival of the fittest) population.removeLast(Int(Double(population.count) * 0.8)) // 2: Random mutations population += population.map { $0.mutate() } // 3: Sort by fitness population.sort { $0.fitness(towards: numberToSolve) > $1.fitness(towards: numberToSolve) } } } 12 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 13

Slide 13 text

GENETIC ALGORITHMS - EXAMPLE 13 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 14

Slide 14 text

RUBIK'S CUBES 14 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 15

Slide 15 text

RUBIK'S CUBES 15 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 16

Slide 16 text

RUBIK'S CUBES 16 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 17

Slide 17 text

RUBIK'S CUBES 43,252,003,274,489,856,000 17 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 18

Slide 18 text

RUBIK'S CUBES 18 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 19

Slide 19 text

RubikSwift struct Cube { struct Pieces { var edges: EdgePieceCollection var corners: CornerPieceCollection } var pieces: Pieces } 19 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 20

Slide 20 text

RubikSwift enum EdgeLocation { case topRight, topFront, topLeft, topBack... } struct EdgePiece { enum Orientation { case correct case flipped } var location: EdgeLocation var orientation: Orientation } 20 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 21

Slide 21 text

RubikSwift 21 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 22

Slide 22 text

RubikSwift struct CornerPiece { enum Orientation { case correct case rotatedClockwise case rotatedCounterClockwise } var location: CornerLocation var orientation: Orientation } 22 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 23

Slide 23 text

RubikSwift struct Cube { struct Pieces { var edges: EdgePieceCollection var corners: CornerPieceCollection } var pieces: Pieces } 23 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 24

Slide 24 text

RubikSwift enum Face { case top, bottom, left, right, front, back } struct Move { enum Magnitude { case clockwiseQuarterTurn case halfTurn case counterClockwiseQuarterTurn } let face: Face let magnitude: Magnitude } 24 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 25

Slide 25 text

RubikSwift 25 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 26

Slide 26 text

RubikSwift extension Cube { mutating func apply(_ move: Move) } 26 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 27

Slide 27 text

RubikSwift final class Individual { let moves: [Move] init(moves: [Move]) { self.moves = moves } } 27 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 28

Slide 28 text

RubikSwift typealias Fitness = Int extension Individual { func fitness(solvingCube cube: Cube) -> Fitness { var cubeAfterApplyingMoves = cube cubeAfterApplyingMoves.apply(self.moves) return cubeAfterApplyingMoves.numberOfSolvedPieces } } extension Cube { // Number of pieces in the correct location and orientation var numberOfSolvedPieces: Int } 28 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 29

Slide 29 text

RubikSwift extension Individual { func mutate() -> Individual { var moves = self.moves let randomMoves = Move.randomMoves(count: Int.random(in: 5...20)) moves += randomMoves return Individual(moves: moves) } } 29 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 30

Slide 30 text

RubikSwift final class Solver { private(set) var population: [Individual] init(scrambledCube: Cube, populationSize: Int) func runGeneration() { population.removeLast(Int(Double(population.count) * 0.8)) population += population.map { $0.mutate() } population.sort { $0.fitness(solvingCube: scrambledCube) > $1.fitness(solvingCube: scrambledCube) } } } 30 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 31

Slide 31 text

RubikSwift DOES IT ACTUALLY WORK? 31 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 32

Slide 32 text

RubikSwift Cube: R' F' B D2 L' R' D2 R' F2 B2 D F L' R2 F' U2 R' U' D F 1: New best: B2 (3.0) 3: New best: B2 U D' U2 F2 D2 B U2 D R B' [...] (6) 7: New best: B2 L2 U2 L' B2 F' U' U' D L2 [...] (7) 100 (avg fitness 3.6012, 23806 algs/sec, 0 min elapsed) 3465: New best: B2 D2 U R2 B2 U2 B' F2 D [...] (13) 10000 (avg fitness 6.462, 11217 algs/sec, 74 min elapsed) 25781: New best: L' D' B2 L2 B' D R2 F R' [...] (**16**) 80800 (avg fitness 9.2612, 7676 algs/sec, 877 min elapsed) 32 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 33

Slide 33 text

RubikSwift Fitness: 16 33 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 34

Slide 34 text

LINKS - http://github.com/JaviSoto/talks - http://github.com/JaviSoto/RubikSwift - https://rogeralsing.com/2008/12/07/genetic-programming- evolution-of-mona-lisa/ @JAVI 34 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi

Slide 35

Slide 35 text

Thank you ! 35 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi