-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.rs
203 lines (164 loc) · 6.24 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use specs::prelude::*;
use log::info;
use rand::distributions::{
Distribution,
Uniform,
Bernoulli
};
pub mod components;
pub mod models;
pub mod learning;
pub mod systems;
use self::components::neuron::*;
use self::components::synapse::*;
use self::learning::stdp::*;
use crate::{
simulation::models::{
izhikevich::{
IzhikevichModel,
IzhikevichMorphology,
IzhikevichIntegrator
},
hindmarsh_rose::{
HindmarshRoseModel,
HindmarshRoseMorphology,
HindmarshRoseIntegrator
},
},
simulation::systems::{
synaptic_transmission::SynapticTransmissionSystem,
csv_writer::CSVWriterSystem
},
};
use std::collections::BinaryHeap;
pub type Time = usize;
#[derive(Default)]
pub struct SimulationTime(pub Time);
pub fn run() {
info!("Setting up simulation...");
let mut world = World::new();
world.register::<Neuron>();
world.register::<ColumnCoordinates>();
world.register::<Spiking>();
world.register::<ActionPotential>();
world.register::<HindmarshRoseModel>();
world.register::<HindmarshRoseMorphology>();
world.register::<IzhikevichModel>();
world.register::<IzhikevichMorphology>();
world.register::<Synapse>();
world.register::<STDPLearningRule>();
world.add_resource(SimulationTime::default());
info!("Generating network...");
let synaptic_delay = Uniform::new(1, 20);
let synaptic_strength = Uniform::new(0.5, 4.0);
let excitory = Bernoulli::new(0.8); // what fraction of synapses are excitory
let regular_spiking = IzhikevichMorphology {
a: 0.02,
b: 0.2,
c: -65.,
d: 2.
};
info!(" - Neurons");
// create a bunch of neurons
{
let mut neurons_created = 0;
let num_columns = 20;
for column in 0..num_columns {
for layer in Layer::iter() {
for _ in 0..match layer {
Layer::Motor => 10,
Layer::Sensory => 10,
Layer::Afferent => 10,
Layer::Efferent => 10,
Layer::Internal => 60
} {
world.create_entity()
.with(Neuron::default())
.with(ColumnCoordinates {
column,
layer: *layer
})
.with(IzhikevichModel { v: -65., u: 0.02 * -65. })
.with(regular_spiking.clone())
.build();
neurons_created += 1;
}
}
}
info!("Created {} neurons", neurons_created);
}
info!(" - Synapses");
// wire them up with synapses
{
let mut synapses_created = 0;
let entities = world.entities();
let neurons = world.read_storage::<Neuron>();
let coordinates = world.read_storage::<ColumnCoordinates>();
let mut synapses = world.write_storage::<Synapse>();
let mut stdps = world.write_storage::<STDPLearningRule>();
let mut rng = rand::thread_rng();
for (pre_neuron, _, pre_coord) in (&entities, &neurons, &coordinates).join() {
for (post_neuron, _, post_coord) in (&entities, &neurons, &coordinates).join() {
let probability = match (pre_coord.column == post_coord.column, pre_coord.layer, post_coord.layer) {
// intra-column, intra-layer connections
(true, Layer::Internal, Layer::Internal) => 0.8,
(true, x, y) if x == y => 0.4,
// intra-column, cross-layer connections
(true, Layer::Sensory, Layer::Internal) => 0.8,
(true, Layer::Afferent, Layer::Internal) => 0.8,
(true, Layer::Internal, Layer::Motor) => 0.8,
(true, Layer::Internal, Layer::Efferent) => 0.8,
// cross-column connections
(false, Layer::Efferent, Layer::Afferent) => 0.3,
// everything else is not connected
(_, _, _) => 0.
};
if probability > 0. {
let has_synapse = Bernoulli::new(probability);
if has_synapse.sample(&mut rng) {
let delay = synaptic_delay.sample(&mut rng);
let is_excitory = excitory.sample(&mut rng);
let strength = match is_excitory {
true => synaptic_strength.sample(&mut rng),
false => -synaptic_strength.sample(&mut rng)
};
entities.build_entity()
.with(Synapse {
pre_neuron,
post_neuron,
delay,
strength,
pending_spikes: BinaryHeap::new()
}, &mut synapses)
.with(STDPLearningRule::default(), &mut stdps)
.build();
synapses_created += 1;
}
}
}
}
info!("Created {} synapses", synapses_created);
}
info!("Starting simulation...");
let mut dispatcher = DispatcherBuilder::new()
.with(HindmarshRoseIntegrator, "hindmarsh_rose_integrator", &[])
.with(IzhikevichIntegrator, "izhikevich_integrator", &[])
.with(SynapticTransmissionSystem, "synaptic_transmission", &["hindmarsh_rose_integrator", "izhikevich_integrator"])
.with(STDPLearningSystem, "stdp_learning", &["synaptic_transmission"])
.with(CSVWriterSystem::new(), "csv_writer", &["synaptic_transmission"])
.build();
loop {
{
let mut time = world.write_resource::<SimulationTime>();
time.0 += 1;
if time.0 > 20000 {
break;
}
if time.0 % 1000 == 0 {
info!("Time {}", time.0);
}
}
dispatcher.dispatch(&mut world);
world.maintain();
}
}