-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finalized the example with the self-driving car.
- Loading branch information
Showing
30 changed files
with
37,627 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "SwiftyJSON"] | ||
path = SwiftyJSON | ||
url = https://github.com/SwiftyJSON/SwiftyJSON.git | ||
[submodule "Swift-AI"] | ||
path = Swift-AI | ||
url = https://github.com/collinhundley/Swift-AI.git |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Cocoa | ||
|
||
@NSApplicationMain | ||
class AppDelegate: NSObject, NSApplicationDelegate { | ||
|
||
|
||
|
||
func applicationDidFinishLaunching(aNotification: NSNotification) { | ||
// Insert code here to initialize your application | ||
} | ||
|
||
func applicationWillTerminate(aNotification: NSNotification) { | ||
// Insert code here to tear down your application | ||
} | ||
|
||
|
||
} | ||
|
58 changes: 58 additions & 0 deletions
58
ExampleCar/ExampleCar/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "mac", | ||
"size" : "16x16", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "16x16", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "32x32", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "32x32", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "128x128", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "128x128", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "256x256", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "256x256", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "512x512", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"size" : "512x512", | ||
"scale" : "2x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Revolver | ||
|
||
struct NeuralNetWeight: Randomizable, CustomStringConvertible, CustomDebugStringConvertible { | ||
let weight: Float | ||
|
||
init(_ weight: Float) { | ||
self.weight = weight | ||
} | ||
|
||
init(generator: EntropyGenerator) { | ||
// Generate Float between +/- 1/sqrt(numInputNodes). | ||
// This interval is optimal for sigmoid activation. | ||
|
||
let bound = Float(1) / sqrt(Float(5)) | ||
self.weight = generator.nextInRange(min: -bound, max: bound) | ||
} | ||
|
||
var description: String { return weight.description } | ||
var debugDescription: String { return weight.debugDescription } | ||
} | ||
|
||
final class CarChromosome: RangeInitializedArray { | ||
typealias Element = NeuralNetWeight | ||
static let initializationRange = 82...82 | ||
|
||
let array: [NeuralNetWeight] | ||
|
||
required init(array: [NeuralNetWeight]) { | ||
self.array = array | ||
} | ||
|
||
var description: String { return array.description } | ||
var debugDescription: String { return array.debugDescription } | ||
|
||
func toFFNN() -> FFNN { | ||
return FFNN(inputs: 5, | ||
hidden: 10, | ||
outputs: 2, | ||
weights: array.map { $0.weight }, | ||
activationFunction: .Sigmoid) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
enum CarSteering { | ||
|
||
case HardLeft | ||
case Left | ||
case Neutral | ||
case Right | ||
case HardRight | ||
|
||
} | ||
|
||
struct CarControlParameters { | ||
|
||
let steering: CarSteering | ||
let acceleration: Double | ||
|
||
static let neutral = CarControlParameters(steering: .Neutral, acceleration: 0) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
typealias CarDetectorOutput = Bool | ||
|
||
struct CarDetectorMeasurements { | ||
|
||
let frontLeft: CarDetectorOutput | ||
let frontMiddle: CarDetectorOutput | ||
let frontRight: CarDetectorOutput | ||
|
||
let under: CarDetectorOutput | ||
|
||
let back: CarDetectorOutput | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
protocol CarDriver { | ||
|
||
func drive(data: CarDetectorMeasurements) -> CarControlParameters | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Revolver | ||
|
||
class CarEvaluator: SequentialEvaluator<CarChromosome> { | ||
static let maxDuration = NSTimeInterval(60 * 60) // 1 hour | ||
static let maxDistance = CarSimulation.maxVelocity * CarEvaluator.maxDuration | ||
static let attempts = 3 | ||
|
||
let generator = MersenneTwister(seed: 4242) | ||
let sim = CarSimulation() | ||
|
||
override func evaluateChromosome(individual: CarChromosome) -> Fitness { | ||
sim.reset() | ||
sim.randomizeTrack(generator) | ||
sim.controlProgram = NetDriver(net: individual.toFFNN()) | ||
|
||
var results = [Fitness]() | ||
results.reserveCapacity(CarEvaluator.attempts) | ||
for _ in 1...CarEvaluator.attempts { | ||
sim.randomizeCar(generator) | ||
|
||
let outcome = sim.run(maxDuration: CarEvaluator.maxDuration) | ||
results.append(Fitness(outcome.distanceTraveledOnTrack) / Fitness(CarEvaluator.maxDistance)) | ||
} | ||
|
||
return results.reduce(0, combine: +) / Fitness(CarEvaluator.attempts) | ||
} | ||
} |
Oops, something went wrong.