Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit918d5ac

Browse files
author
Guled
committed
Reimplemented Flappy Bird Example with new ANN architecture
1 parent9320042 commit918d5ac

File tree

4 files changed

+29
-83
lines changed

4 files changed

+29
-83
lines changed

‎Example/MLKit/GameScene.swift‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import SpriteKit
1010
import MachineLearningKit
11-
/*
11+
import Upsurge
12+
1213
classGameScene:SKScene,SKPhysicsContactDelegate{
1314

1415
// ADDITIONS
@@ -45,7 +46,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
4546

4647
/// SKLabel
4748
vargenerationLabel:SKLabelNode!
48-
var fitnessLabel:SKLabelNode!
49+
varfitnessLabel:SKLabelNode!
4950

5051

5152

@@ -451,12 +452,13 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
451452
*/
452453

453454
// Decision AI makes
454-
let decision = (currentBird?.brain?.forward(input: [Float(1), Float(normalizedDistanceOfNextPipe), Float(normalizedPosToGap), Float(birdYPos), Float(normalizedDistanceFromBottomPipe)]))!
455+
letinput=Matrix<Float>(rows:4, columns:1, elements:[Float(normalizedDistanceOfNextPipe),Float(normalizedPosToGap),Float(birdYPos),Float(normalizedDistanceFromBottomPipe)])
456+
letdecision= currentBird?.brain?.feedforward(input: input)
455457

456458
print("FLAPPY BIRD DECISION:\(decision)")
457459

458460
// 0.95 was arbitrary, tweaking is recommended
459-
if decision[0] >= Float(0.89) {
461+
if(decision?.elements[0])!>=Float(0.5){
460462

461463
if moving.speed>0{
462464

@@ -537,4 +539,3 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
537539
}
538540
}
539541
}
540-
*/

‎Example/MLKit/GameViewController.swift‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import UIKit
1010
import SpriteKit
1111
import MachineLearningKit
1212
import Upsurge
13-
/*
13+
1414
extensionSKNode{
1515
classfunc unarchiveFromFile(_ file:String)->SKNode?{
1616

@@ -41,9 +41,9 @@ public class FlappyGenome: Genome {
4141

4242
publicvarfitness:Float=0
4343

44-
public var brain:NeuralNet?
44+
publicvarbrain:NeuralNetwork?
4545

46-
public init(genotype: [Float], network:NeuralNet) {
46+
publicinit(genotype:[Float], network:NeuralNetwork){
4747

4848
self.genotypeRepresentation= genotype
4949
self.brain= network
@@ -67,11 +67,9 @@ class GameViewController: UIViewController {
6767
for_in1...20{
6868

6969
// Bias already included
70-
let brain = NeuralNet(numberOfInputNeurons: 4, hiddenLayers: [4], numberOfOutputNeurons: 1)
71-
72-
brain.activationFuncType = .siglog
73-
74-
brain.activationFuncTypeOfOutputLayer = .siglog
70+
letbrain=NeuralNetwork(size:(4,1))
71+
brain.addLayer(layer:Layer(size:(4,4), activationType:.siglog))
72+
brain.addLayer(layer:Layer(size:(4,1), activationType:.siglog))
7573

7674
letnewBird=FlappyGenome(genotype:GeneticOperations.encode(network: brain), network: brain)
7775

@@ -117,4 +115,3 @@ class GameViewController: UIViewController {
117115
}
118116

119117
}
120-
*/

‎Example/MLKit/GeneticOperations.swift‎

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,29 @@ import MachineLearningKit
1111
import Upsurge
1212

1313
/// The GeneticOperations class manages encoding genes into weights for the neural network and decoding neural network weights into genes. These methods are not provided in the framework itself, rather it was for the game example.
14-
/*
14+
1515
finalclassGeneticOperations{
1616
/**
17-
The encode method converts aNueralNet object to an array of floats by taking the weights of each layer and placing them into an array.
17+
The encode method converts aNueralNetwork object to an array of floats by taking the weights of each layer and placing them into an array.
1818

1919
- parameter network: A NeuralNet Object.
2020

2121
- returns: An array of Float values.
2222
*/
23-
public static func encode(network: NeuralNet) -> [Float] {
24-
25-
let inputLayerNeurons = network.inputLayer.listOfNeurons
26-
let outputLayerNeurons = network.outputLayer.listOfNeurons
23+
publicstaticfunc encode(network:NeuralNetwork)->[Float]{
2724

2825
vargenotypeRepresentation:[Float]=[]
2926

30-
for neuron in inputLayerNeurons {
31-
genotypeRepresentation += neuron.weightsComingIn
32-
}
27+
forlayerin network.layers{
3328

34-
for hiddenLayer in network.listOfHiddenLayers {
35-
for neuron in hiddenLayer.listOfNeurons {
36-
genotypeRepresentation += neuron.weightsComingIn
37-
}
38-
39-
for neuron in hiddenLayer.listOfNeurons {
40-
genotypeRepresentation += neuron.weightsGoingOut
41-
}
29+
genotypeRepresentation+=Array<Float>(layer.weights!.elements)
4230
}
4331

44-
forneuron inoutputLayerNeurons {
45-
genotypeRepresentation +=neuron.weightsGoingOut
32+
forlayerinnetwork.layers{
33+
genotypeRepresentation+=Array<Float>(layer.bias!.elements)
4634
}
4735

36+
4837
return genotypeRepresentation
4938
}
5039

@@ -55,59 +44,18 @@ final class GeneticOperations {
5544

5645
- returns: An array of Float values.
5746
*/
58-
public static func decode(genotype: [Float]) ->NeuralNet {
47+
publicstaticfunc decode(genotype:[Float])->NeuralNetwork{
5948

6049
// Create a new NueralNet
61-
let brain = NeuralNet(numberOfInputNeurons: 4, hiddenLayers: [4], numberOfOutputNeurons: 1)
62-
63-
brain.activationFuncType = .siglog
64-
65-
brain.activationFuncTypeOfOutputLayer = .siglog
66-
67-
// Convert genotype back to weights for each layer
68-
let inputLayerWeights: [Float] = Array<Float>(genotype[0...4])
69-
70-
// First is bias neuron
71-
let hiddenLayerWeightsComingInForNeuron1: [Float] = Array<Float>(genotype[5...8])
72-
let hiddenLayerWeightsComingInForNeuron2: [Float] = Array<Float>(genotype[9...12])
73-
let hiddenLayerWeightsComingInForNeuron3: [Float] = Array<Float>(genotype[13...16])
74-
let hiddenLayerWeightsComingInForNeuron4: [Float] = Array<Float>(genotype[17...20])
75-
let hiddenLayerWeightsComingInForNeuron5: [Float] = Array<Float>(genotype[21...24])
76-
let hiddenLayerWeightsGoingOut: [Float] = Array<Float>(genotype[25...29])
77-
let outputLayerWeightGoingOut: Float = genotype[30]
78-
79-
for (i, neuron) in brain.inputLayer.listOfNeurons.enumerated() {
80-
81-
neuron.weightsComingIn = ValueArray<Float>([inputLayerWeights[i]])
82-
}
83-
84-
for hiddenLayer in brain.listOfHiddenLayers {
85-
for (i, neuron) in hiddenLayer.listOfNeurons.enumerated() {
86-
87-
if i == 0 {
88-
neuron.weightsComingIn = ValueArray<Float>(hiddenLayerWeightsComingInForNeuron1)
89-
} else if i == 1 {
90-
neuron.weightsComingIn = ValueArray<Float>(hiddenLayerWeightsComingInForNeuron2)
91-
} else if i == 2 {
92-
neuron.weightsComingIn = ValueArray<Float>(hiddenLayerWeightsComingInForNeuron3)
93-
} else if i == 3 {
94-
neuron.weightsComingIn = ValueArray<Float>(hiddenLayerWeightsComingInForNeuron4)
95-
} else if i == 4 {
96-
neuron.weightsComingIn = ValueArray<Float>(hiddenLayerWeightsComingInForNeuron5)
97-
}
98-
}
99-
100-
for (i, neuron) in hiddenLayer.listOfNeurons.enumerated() {
101-
102-
neuron.weightsGoingOut = ValueArray<Float>([hiddenLayerWeightsGoingOut[i]])
103-
104-
}
105-
106-
}
50+
letbrain=NeuralNetwork(size:(4,1))
51+
brain.addLayer(layer:Layer(size:(4,4), activationType:.siglog))
52+
brain.addLayer(layer:Layer(size:(4,1), activationType:.siglog))
10753

108-
brain.outputLayer.listOfNeurons[0].weightsGoingOut = ValueArray<Float>([outputLayerWeightGoingOut])
54+
brain.layers[0].weights=Matrix<Float>(rows:4, columns:4, elements:ValueArray<Float>(Array<Float>(genotype[0...15])))
55+
brain.layers[0].bias=Matrix<Float>(rows:4, columns:1, elements:ValueArray<Float>(Array<Float>(genotype[20...23])))
56+
brain.layers[1].weights=Matrix<Float>(rows:1, columns:4, elements:ValueArray<Float>(Array<Float>(genotype[16...19])))
57+
brain.layers[1].bias=Matrix<Float>(rows:1, columns:1, elements:ValueArray<Float>([genotype[24]]))
10958

11059
return brain
11160
}
11261
}
113-
*/

‎Example/Tests/NeuralNetworkSpec.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class NeuralNetworkSpec: QuickSpec {
1616

1717
overridefunc spec(){
1818

19-
it(""){
19+
it("Should be able to run a simple AND example."){
2020

2121

2222
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp