Genetic Algorithms are a fascinating way of solving problems in computer science which lie in between programming and biology. I implemented one in Swift to solve Rubik's Cubes, and you won't believe what happened next.
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
struct EdgePiece { enum Orientation { case correct case flipped } var location: EdgeLocation var orientation: Orientation } 20 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi
rotatedClockwise case rotatedCounterClockwise } var location: CornerLocation var orientation: Orientation } 22 — "Rubik's Cubes and Genetic Algorithms In Swift" - @Javi
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
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
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