Skip to content

Commit 24aa9b6

Browse files
author
Guled
committed
Adjusted Genetic Algor
1 parent 30c095c commit 24aa9b6

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

Example/MLKit/GameScene.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
3434
/// The pipe that is in front of the bird
3535
var currentPipe: Int = 0
3636

37+
var maxFitness: Float = 0
38+
39+
var maxBird: FlappyGenome?
40+
3741
var lastBestGen: [FlappyGenome] = []
3842

3943
// END of ADDITIONS
@@ -213,7 +217,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
213217
goalArea.name = "GOAL"
214218
goalArea.fillColor = SKColor.red
215219
goalArea.position = pipeUp.position
216-
goalArea.position.y += 230
220+
goalArea.position.y += 270
217221
// END of ADDITIONS
218222

219223
pipePair.addChild(pipeUp)
@@ -295,8 +299,14 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
295299
lastBestGen = (flappyBirdGenerationContainer?.filter({$0.fitness >= 4}))!
296300
}
297301

302+
303+
if (currentBird?.fitness)! > maxFitness {
304+
maxFitness = (currentBird?.fitness)!
305+
maxBird = currentBird
306+
}
307+
298308
// If we have hit the 20th bird, we need to move on to the next generation
299-
if currentFlappy == 10 {
309+
if currentFlappy == 20 {
300310

301311
print("GENERATING NEW GEN!")
302312

@@ -306,8 +316,9 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
306316
var newGen: [FlappyGenome] = []
307317

308318
newGen += lastBestGen
319+
newGen.append(maxBird!)
309320

310-
while newGen.count < 10 {
321+
while newGen.count < 20 {
311322

312323
// Select the best parents
313324
let parents = PopulationManager.selectParents(genomes: flappyBirdGenerationContainer!)
@@ -406,7 +417,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
406417
let normalizedDistanceOfNextPipe = (distanceOfNextPipe - 3)/(725-3)
407418

408419
// Bird Y position
409-
let birdYPos = bird.position.y/CGFloat(800)
420+
let birdYPos = bird.position.y/CGFloat(880)
410421

411422
// Measure how close the bird is to the gap between the pipes
412423
let posToGap = pipes.children[0].children[2].position.y - bird.position.y
@@ -459,7 +470,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
459470

460471
if moving.speed > 0 {
461472

462-
if y >= 800 {
473+
if y >= 880 {
463474
moving.speed = 0
464475

465476
bird.physicsBody?.collisionBitMask = worldCategory

Example/MLKit/GameViewController.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ extension SKNode {
3737
// Genome that represents a Flappy Bird
3838
public class FlappyGenome: Genome {
3939

40+
/// Genotype representation of the genome.
41+
public var genotypeRepresentation: [Float]
42+
43+
public var fitness: Float = 0
44+
4045
public var brain: NeuralNet?
4146

4247
public init(genotype: [Float], network: NeuralNet) {
4348

44-
super.init(genotype: genotype)
45-
4649
self.genotypeRepresentation = genotype
4750
self.brain = network
4851
}
@@ -64,7 +67,7 @@ class GameViewController: UIViewController {
6467
// Create First Generation of Flappy Birds
6568
var generation1: [FlappyGenome] = []
6669

67-
for _ in 1...10 {
70+
for _ in 1...20 {
6871

6972
let brain = NeuralNet()
7073

Example/Tests/GeneticSpec.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,27 @@ import Nimble
1515

1616
class GeneticSpec: QuickSpec {
1717

18+
19+
public struct FakeGenome: Genome {
20+
21+
var genotypeRepresentation: [Float]
22+
23+
var fitness: Float
24+
25+
public init(genotype: [Float]) {
26+
27+
self.genotypeRepresentation = genotype
28+
self.fitness = 0
29+
}
30+
31+
}
32+
33+
1834
override func spec() {
1935

2036
it("Should be able to produce a unique genotype after swap mutation process.") {
2137

22-
let fakeGenome: Genome = Genome(genotype: [1.0, 2.0, 3.0])
38+
var fakeGenome: FakeGenome = FakeGenome(genotype: [1.0, 2.0, 3.0])
2339

2440
let oldGenotype = fakeGenome.genotypeRepresentation
2541

@@ -32,7 +48,7 @@ class GeneticSpec: QuickSpec {
3248

3349
it("Should be able to produce a unique genotype after insert mutation process. ") {
3450

35-
let fakeGenome: Genome = Genome(genotype: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
51+
var fakeGenome: FakeGenome = FakeGenome(genotype: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
3652

3753
let oldGenotype = fakeGenome.genotypeRepresentation
3854

@@ -44,7 +60,7 @@ class GeneticSpec: QuickSpec {
4460

4561
it("Should be able to produce a unique genotype after inverse mutation process. ") {
4662

47-
let fakeGenome: Genome = Genome(genotype: [1.0, 2.0, 3.0])
63+
var fakeGenome: FakeGenome = FakeGenome(genotype: [1.0, 2.0, 3.0])
4864

4965
let oldGenotype = fakeGenome.genotypeRepresentation
5066

@@ -55,7 +71,7 @@ class GeneticSpec: QuickSpec {
5571

5672
it("Should be able to produce a unique genotype after scramble mutation process. ") {
5773

58-
let fakeGenome: Genome = Genome(genotype: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
74+
var fakeGenome: FakeGenome = FakeGenome(genotype: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
5975

6076
let oldGenotype = fakeGenome.genotypeRepresentation
6177

MLKit/Classes/Genetic Algorithms/Genome.swift

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,14 @@
88

99
import Foundation
1010

11-
1211
/// Blueprint for a Genome. It is encouraged to inherit from this base class in order to accomodate for your needs.
13-
open class Genome {
12+
public protocol Genome {
1413

1514
/// Genotype representation of the genome.
16-
public var genotypeRepresentation: [Float]
15+
var genotypeRepresentation: [Float] { get set }
1716

1817
/// Fitness of a particular genome.
19-
public var fitness: Float = 0
20-
21-
public init(genotype: [Float]) {
22-
23-
self.genotypeRepresentation = genotype
24-
}
18+
var fitness: Float { get set }
2519

26-
// Method used to assign a fitness value for a Genome
27-
/* This method is not given in the framework as there is no way to tell how you will evaluate fitness within your own project. Please implement your own 'generateFitness' method. An example is given in the Example Flappy Bird Game Project.
28-
open func generateFitness() {
29-
30-
}
31-
*/
3220
}
21+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ MLKit is a simple machine learning framework written in Swift. Currently MLKit f
77
[![License](https://img.shields.io/cocoapods/l/MLKit.svg?style=flat)](https://cocoapods.org/pods/MachineLearningKit)
88
[![Platform](https://img.shields.io/cocoapods/p/MLKit.svg?style=flat)](https://cocoapods.org/pods/MachineLearningKit)
99

10-
[MachineLearningKit Reference](http://cocoadocs.org/docsets/MachineLearningKit/0.1.4/index.html)
10+
[MachineLearningKit Reference](http://cocoadocs.org/docsets/MachineLearningKit/0.1.5/)
1111

1212
## Requirements
1313

0 commit comments

Comments
 (0)