Last active
April 2, 2017 06:17
-
-
Save lizixroy/dfeabeaf329b0df317d5b525e2281f8b to your computer and use it in GitHub Desktop.
Aplysia
This file contains hidden or 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
class Neuron { | |
var incomingNeurons: [Neuron] = [] | |
var outcomingNeurons: [Neuron] = [] | |
var releasableSynapticVesicles: Int = 1000 | |
private weak var timer: Timer? | |
init() { | |
Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { _ in | |
self.replenishSynapticVesicles() | |
}) | |
} | |
func excite(magnitude: Int) { fatalError("Needs to be overriden") } | |
func consumeReleasableSynapticVesicles() { | |
releasableSynapticVesicles = releasableSynapticVesicles * 3/4 | |
} | |
@objc private func replenishSynapticVesicles() { | |
print("replenishing vesicles ...") | |
if self.releasableSynapticVesicles < 1000 { | |
self.releasableSynapticVesicles += 100 | |
} | |
} | |
} | |
class MotorNeuron: Neuron { | |
override func excite(magnitude: Int) { | |
print("Withdraw with magnitude \(magnitude)") | |
} | |
} | |
class SensoryNeuron: Neuron { | |
override func excite(magnitude: Int) { | |
let outputMagnitude = magnitude * self.releasableSynapticVesicles | |
print("Sensory neuron receives touch with magnitude \(magnitude) sends action potential with magnitude: \(outputMagnitude)") | |
for neuron in outcomingNeurons { | |
neuron.excite(magnitude: outputMagnitude) | |
} | |
consumeReleasableSynapticVesicles() | |
} | |
} | |
class InterNeuron: Neuron { | |
override func excite(magnitude: Int) { | |
let outputMagnitude = magnitude * self.releasableSynapticVesicles | |
print("Inter neuron receives action potential with magnitude \(magnitude) sends action potential with magnitude: \(outputMagnitude)") | |
for neuron in outcomingNeurons { | |
neuron.excite(magnitude: outputMagnitude) | |
} | |
consumeReleasableSynapticVesicles() | |
} | |
} | |
protocol Organ { | |
var neurons: [Neuron] { get } | |
} | |
class Siphon: Organ { | |
var neurons = [Neuron]() | |
func touch() { | |
for neuron in neurons { | |
neuron.excite(magnitude: 10) | |
} | |
} | |
} | |
class Gill: Organ { | |
var neurons = [Neuron]() | |
} | |
class Aplysia { | |
let siphon: Siphon | |
let gill: Gill | |
init(siphon: Siphon, gill: Gill) { | |
self.siphon = siphon | |
self.gill = gill | |
grow() | |
} | |
private func grow() { | |
print("Aplysia is growing up ..."); | |
// Wire up neurons in Aplysia, this is what happens when a baby Aplysia grows up :) | |
let interNeuron = InterNeuron() | |
self.siphon.neurons[0].outcomingNeurons.append(interNeuron); | |
interNeuron.outcomingNeurons.append(self.gill.neurons[0]); | |
print("Aplysia has grown up. Neural pathways are fully wired. :)"); | |
// siphon's sensory neuron --> interNeuron --> gill's motor neuron | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment