-
Notifications
You must be signed in to change notification settings - Fork 3
/
Worm.cpp
executable file
·481 lines (409 loc) · 12.9 KB
/
Worm.cpp
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//
// Worm.cpp
// one
//
// Created by Eduardo Izquierdo on 9/25/15.
// Copyright © 2015 Eduardo Izquierdo. All rights reserved.
//
#include "Worm.h"
int nn(int neuronNumber, int unitNumber)
{
return neuronNumber+((unitNumber-1)*N_neuronsperunit);
}
// The constructor
Worm::Worm(TVector<double> &v,double output)
{
// Muscles
m.SetMuscleParams(N_muscles, T_muscle);
// Nervous system
n.SetCircuitSize(N_units*N_neuronsperunit, 3, 2);
int da, db, dd, vd, vb, va;
int ddNext, vdNext, vbNext, dbNext;
// Stretch receptor
sr.SetStretchReceptorParams(N_segments, N_stretchrec, v(1), v(2));
for (int u = 1; u <= N_units; u++){
// Find the numbers that identify each neuron within a certain repeating unit
da = nn(DA, u);
db = nn(DB, u);
dd = nn(DD, u);
vd = nn(VD, u);
va = nn(VA, u);
vb = nn(VB, u);
// neurons for interunit connections
ddNext = nn(DD, u+1);
vdNext = nn(VD, u+1);
vbNext = nn(VB, u+1);
dbNext = nn(DB, u+1);
// Biases
n.SetNeuronBias(da, v(3));
n.SetNeuronBias(va, v(3));
n.SetNeuronBias(db, v(4));
n.SetNeuronBias(vb, v(4));
n.SetNeuronBias(dd, v(5));
n.SetNeuronBias(vd, v(5));
// Time-constants fixed to 1.0
for (int i = 1; i <= N_neuronsperunit; i++){
n.SetNeuronTimeConstant(nn(i,u), 1.0);
}
// Self-connections
n.SetChemicalSynapseWeight(da,da, v(6));
n.SetChemicalSynapseWeight(va,va, v(6));
n.SetChemicalSynapseWeight(db,db, v(7));
n.SetChemicalSynapseWeight(vb,vb, v(7));
n.SetChemicalSynapseWeight(dd,dd, v(8));
n.SetChemicalSynapseWeight(vd,vd, v(8));
// Cross-connections
// Excitatory Chemical Synapses intraunit
n.SetChemicalSynapseWeight(da, vd, v(9));
n.SetChemicalSynapseWeight(va, dd, v(9));
n.SetChemicalSynapseWeight(vb, dd, v(10));
n.SetChemicalSynapseWeight(db, vd, v(10));
// Inhibitory Chemical Synapses intraunit
n.SetChemicalSynapseWeight(vd, va, v(11));
n.SetChemicalSynapseWeight(dd, da, v(11));
n.SetChemicalSynapseWeight(vd, vb, v(12));
n.SetChemicalSynapseWeight(dd, db, v(12));
// Electrical Synapse Intersegment connections
if (u < N_units){
n.SetElectricalSynapseWeight(dd, ddNext, v(13));
n.SetElectricalSynapseWeight(vd, vdNext, v(13));
n.SetElectricalSynapseWeight(vb, vbNext, v(14));
n.SetElectricalSynapseWeight(db, dbNext, v(14));
}
}
// Excitatory VNC NMJ Weight
NMJ_DA = v(15);
NMJ_VA = v(15);
NMJ_DB = v(16);
NMJ_VB = v(16);
// Inhibitory VNC NMJ Weight
NMJ_DD = v(17);
NMJ_VD = v(17);
AVA_output = 0.0;
AVB_output = 0.0;
}
void Worm::InitializeState(RandomState &rs)
{
t = 0.0;
//n.RandomizeCircuitState(-1.0, 1.0, rs);
for (int u = 1; u <= N_units; u++)
{
// Dorsal neurons
n.SetNeuronOutput(nn(DA,u), 0.1);
n.SetNeuronOutput(nn(DB,u), 0.1);
n.SetNeuronOutput(nn(DD,u), 0.9);
// Ventral neurons
n.SetNeuronOutput(nn(VA,u), 0.9);
n.SetNeuronOutput(nn(VB,u), 0.9);
n.SetNeuronOutput(nn(VD,u), 0.1);
}
b.InitializeBodyState();
m.InitializeMuscleState();
}
void Worm::Step(double StepSize, double output)
{
int mi;
int mt = 0;
double ds, vs;
TVector<double> dorsalInput(1, N_units);
TVector<double> ventralInput(1, N_units);
// Update Body
b.StepBody(StepSize);
// Set input to Stretch Receptors from Body
// Input to SR only if the segment stretch
double d_sr,v_sr;
for(int i = 1; i <= N_segments; ++i){
ds = (b.DorsalSegmentLength(i) - b.RestingLength(i))/b.RestingLength(i);
vs = (b.VentralSegmentLength(i) - b.RestingLength(i))/b.RestingLength(i);
#ifdef SR_TRANS_STRETCH
ds = ds < 0.0 ? 0.0 : ds;
vs = vs < 0.0 ? 0.0 : vs;
#endif
#ifdef SR_TRANS_CONTRACT
ds = ds < 0.0 ? ds : 0.0;
vs = vs < 0.0 ? vs : 0.0;
#endif
#ifdef SR_TRANS_ABS
ds = ds < 0.0 ? -ds : ds;
vs = vs < 0.0 ? -vs : vs;
#endif
#ifdef SR_TRANS_NEG
ds = -ds;
vs = -vs;
#endif
sr.SetDorsalInput(i, ds);
sr.SetVentralInput(i, vs);
}
// Update Stretch Receptors
sr.Update();
// Set input to Nervous System (Ventral Cord) from Stretch Receptors AND Command Interneurons
//// To A_class motorneurons
for (int i = 1; i <= N_units; i++){
n.SetNeuronExternalInput(nn(DA,i), sr.A_D_sr(i) + AVA_output);
n.SetNeuronExternalInput(nn(VA,i), sr.A_V_sr(i) + AVA_output);
}
//// To B_class motorneurons
for (int i = 1; i <= N_units; i++){
n.SetNeuronExternalInput(nn(DB,i), sr.B_D_sr(i) + AVB_output);
n.SetNeuronExternalInput(nn(VB,i), sr.B_V_sr(i) + AVB_output);
}
// Update Nervous System
n.EulerStep(StepSize);
// Set input to Muscles
// Each motor neuron innervates four muscles, overlap in muscles 4, 6-19 and 21)
// Load motorneuron activity
for (int i=1; i<=N_units; i++){
dorsalInput(i) = NMJ_DA*n.NeuronOutput(nn(DA,i)) + NMJ_DB*n.NeuronOutput(nn(DB,i)) + NMJ_DD*n.NeuronOutput(nn(DD,i));
ventralInput(i) = NMJ_VD*n.NeuronOutput(nn(VD,i)) + NMJ_VA*n.NeuronOutput(nn(VA,i)) + NMJ_VB*n.NeuronOutput(nn(VB,i));
}
// Muscles 1-3
for (int mi=1; mi<=3; mi++){
m.SetVentralMuscleInput(mi, ventralInput(1));
m.SetDorsalMuscleInput(mi, dorsalInput(1));
}
mi = 4; // 4th muscle
m.SetVentralMuscleInput(mi, (ventralInput(1)+ventralInput(2)));
m.SetDorsalMuscleInput(mi, (dorsalInput(1)+dorsalInput(2)));
mi = 5; // 5th muscle
m.SetVentralMuscleInput(mi, ventralInput(2));
m.SetDorsalMuscleInput(mi, dorsalInput(2));
mt = 2; // Muscles 6-19
for (int mi=6; mi<=19; mi++){
m.SetVentralMuscleInput(mi, (ventralInput(mt)+ventralInput(mt+1)));
m.SetDorsalMuscleInput(mi, (dorsalInput(mt)+dorsalInput(mt+1)));
mt += mi%2; // increment the index for the innervating unit each two muscles, starting from mi = 7
}
mi = 20; // 20th muscle
m.SetVentralMuscleInput(mi, ventralInput(9));
m.SetDorsalMuscleInput(mi, dorsalInput(9));
mi = 21; // 21st muscle
m.SetVentralMuscleInput(mi, (ventralInput(9)+ventralInput(10)));
m.SetDorsalMuscleInput(mi, (dorsalInput(9)+dorsalInput(10)));
// Muscles 22-24
for (int mi=22; mi<=24; mi++){
m.SetVentralMuscleInput(mi, ventralInput(10));
m.SetDorsalMuscleInput(mi, dorsalInput(10));
}
// Update Muscle activation
m.EulerStep(StepSize);
// Set input to Mechanical Body
// First two segments receive special treatment because they are only affected by a single muscle
b.SetDorsalSegmentActivation(1, m.DorsalMuscleOutput(1)/2);
b.SetVentralSegmentActivation(1, m.VentralMuscleOutput(1)/2);
b.SetDorsalSegmentActivation(2, m.DorsalMuscleOutput(1)/2);
b.SetVentralSegmentActivation(2, m.VentralMuscleOutput(1)/2);
// All other segments receive force from two muscles
for (int i = 3; i <= N_segments-2; i++)
{
mi = (int) ((i-1)/2);
b.SetDorsalSegmentActivation(i, (m.DorsalMuscleOutput(mi) + m.DorsalMuscleOutput(mi+1))/2);
b.SetVentralSegmentActivation(i, (m.VentralMuscleOutput(mi) + m.VentralMuscleOutput(mi+1))/2);
}
// Last two segments receive special treatment because they are only affected by a single muscle
b.SetDorsalSegmentActivation(N_segments-1, m.DorsalMuscleOutput(N_muscles)/2);
b.SetVentralSegmentActivation(N_segments-1, m.VentralMuscleOutput(N_muscles)/2);
b.SetDorsalSegmentActivation(N_segments, m.DorsalMuscleOutput(N_muscles)/2);
b.SetVentralSegmentActivation(N_segments, m.VentralMuscleOutput(N_muscles)/2);
// Time
t += StepSize;
}
double Worm::CoMx()
{
double temp = 0.0;
for (int i = 1; i <= N_rods; i++) {
temp += b.X(i);
}
return temp/N_rods;
}
double Worm::CoMy()
{
double temp = 0.0;
for (int i = 1; i <= N_rods; i++) {
temp += b.Y(i);
}
return temp/N_rods;
}
void Worm::Curvature(TVector<double> &c)
{
double dx1,dy1,dx2,dy2,a,a1,a2,seg;
int k=1;
for (int i = 3; i < N_segments-1; i+=2)
{
dx1 = b.X(i) - b.X(i-2);
dy1 = b.Y(i) - b.Y(i-2);
dx2 = b.X(i+2) - b.X(i);
dy2 = b.Y(i+2) - b.Y(i);
a1 = atan2(dy1,dx1);
a2 = atan2(dy2,dx2);
if (a1 > PI/2 and a2 < -PI/2)
a = (a1 - 2*PI) - a2;
else
if (a1 < -PI/2 and a2 > PI/2)
a = a1 - (a2 - 2*PI);
else
a = a1-a2;
seg = sqrt(pow(b.X(i-2)-b.X(i+2),2) + pow(b.Y(i-2)-b.Y(i+2),2));
c(k) = (2*sin(a)/seg)/1000;
k++;
}
}
void Worm::AngleCurvature(TVector<double> &c)
{
double dx1,dy1,dx2,dy2,a,a1,a2,seg;
int k=1;
for (int i = 3; i < N_segments-1; i+=2)
{
dx1 = b.X(i) - b.X(i-2);
dy1 = b.Y(i) - b.Y(i-2);
dx2 = b.X(i+2) - b.X(i);
dy2 = b.Y(i+2) - b.Y(i);
a1 = atan2(dy1,dx1);
a2 = atan2(dy2,dx2);
if (a1 > PI/2 and a2 < -PI/2)
a = (a1 - 2*PI) - a2;
else
if (a1 < -PI/2 and a2 > PI/2)
a = a1 - (a2 - 2*PI);
else
a = a1-a2;
c(k) = a;
k++;
}
}
double Worm::Orientation()
{
return atan2(b.Y(Head)-b.Y(Tail),b.X(Head)-b.X(Tail));
}
// Dump the state to OFS if SKIPS steps have been performed
void Worm::DumpBodyState(ofstream &ofs, int skips)
{
static int tt = skips;
if (++tt >= skips) {
tt = 0;
ofs << t;
// Body
for (int i = 1; i <= N_rods; i++)
{
ofs << " " << b.X(i) << " " << b.Y(i) << " " << b.Phi(i);
}
ofs << "\n";
}
}
void Worm::DumpActState(ofstream &ofs, int skips)
{
static int tt = skips;
if (++tt >= skips) {
tt = 0;
ofs << t;
//ofs << "\nSR: ";
// Stretch receptors
for (int i = 1; i <= N_stretchrec; i++) {
ofs << " " << sr.A_D_sr(i) << " " << sr.A_V_sr(i) << " " << sr.B_D_sr(i) << " " << sr.B_V_sr(i);
}
// Ventral Cord Motor Neurons
//ofs << "\nV: ";
for (int i = 1; i <= N_units; i++) {
for (int j = 1; j <= N_neuronsperunit; j++) {
ofs << " " << n.NeuronOutput(nn(j,i));
}
}
// Muscles
//ofs << "\nM: ";
for (int i = 1; i <= N_muscles; i++) {
ofs << " " << m.DorsalMuscleOutput(i) << " " << m.VentralMuscleOutput(i);
}
ofs << "\n";
}
}
void Worm::DumpCurvature(ofstream &ofs, int skips)
{
double dx1,dy1,dx2,dy2,a,a1,a2,seg;
static int tt = skips;
if (++tt >= skips) {
tt = 0;
//time
ofs << t;
for (int i = 3; i < N_segments-1; i+=2)
{
dx1 = b.X(i) - b.X(i-2);
dy1 = b.Y(i) - b.Y(i-2);
dx2 = b.X(i+2) - b.X(i);
dy2 = b.Y(i+2) - b.Y(i);
a1 = atan2(dy1,dx1);
a2 = atan2(dy2,dx2);
if (a1 > PI/2 and a2 < -PI/2)
a = (a1 - 2*PI) - a2;
else
if (a1 < -PI/2 and a2 > PI/2)
a = a1 - (a2 - 2*PI);
else
a = a1-a2;
seg = sqrt(pow(b.X(i-2)-b.X(i+2),2) + pow(b.Y(i-2)-b.Y(i+2),2));
ofs << " " << (2*sin(a)/seg)/1000;
}
ofs << "\n";
}
}
void Worm::DumpVoltage(ofstream &ofs, int skips)
{
static int tt = skips;
if (++tt >= skips) {
tt = 0;
ofs << t;
// Ventral Cord Motor Neurons
for (int i = 1; i <= N_units; i++) {
for (int j = 1; j <= N_neuronsperunit; j++) {
ofs << " " << n.NeuronState(nn(j,i));
}
}
ofs << "\n";
}
}
void Worm::DumpParams(ofstream &ofs) {
ofs << "Time-constants: \n" <<
"\n DA: " << n.NeuronTimeConstant(DA) <<
"\n DB: " << n.NeuronTimeConstant(DB) <<
"\n DD: " << n.NeuronTimeConstant(DD) <<
"\n VD: " << n.NeuronTimeConstant(VD) <<
"\n VA: " << n.NeuronTimeConstant(VA) <<
"\n VB: " << n.NeuronTimeConstant(VB) << endl;
ofs << "Biases: \n" <<
"\n DA: " << n.NeuronBias(DA) <<
"\n DB: " << n.NeuronBias(DB) <<
"\n DD: " << n.NeuronBias(DD) <<
"\n VD: " << n.NeuronBias(VD) <<
"\n VA: " << n.NeuronBias(VA) <<
"\n VB: " << n.NeuronBias(VB) << endl;
ofs << "Self conns: \n" <<
"\n DA: " << n.ChemicalSynapseWeight(DA, DA) <<
"\n DB: " << n.ChemicalSynapseWeight(DB, DB) <<
"\n DD: " << n.ChemicalSynapseWeight(DD, DD) <<
"\n VD: " << n.ChemicalSynapseWeight(VD, VD) <<
"\n VA: " << n.ChemicalSynapseWeight(VA, VA) <<
"\n VB: " << n.ChemicalSynapseWeight(VB, VB) << endl;
ofs << "Interneuron propierties: \n AVA active state: " << AVA_act <<
"\n AVB active state: " << AVB_act <<
"\n AVA inactive state: " << AVA_inact <<
"\n AVB inactive state: " << AVB_inact << endl;
ofs << "Chem Conns: \n" <<
"\n DA->VD: " << n.ChemicalSynapseWeight(DA, VD) <<
"\n DB->VD: " << n.ChemicalSynapseWeight(DB, VD) <<
"\n VD->VA: " << n.ChemicalSynapseWeight(VD, VA) <<
"\n VD->VB: " << n.ChemicalSynapseWeight(VD, VB) <<
"\n VA->DD: " << n.ChemicalSynapseWeight(VA, DD) <<
"\n VA->VD: " << n.ChemicalSynapseWeight(VA, VD) <<
"\n VB->DD: " << n.ChemicalSynapseWeight(VB, DD) <<
"\n VB->VD: " << n.ChemicalSynapseWeight(VB, VD) << endl;
ofs << "Gap Juncs: \n DD-DD+1: " << n.ElectricalSynapseWeight(DD, DD+N_neuronsperunit) <<
"\n VB-VB+1: " << n.ElectricalSynapseWeight(VB, VB+N_neuronsperunit) <<
"\n VD-VD+1: " << n.ElectricalSynapseWeight(VD, VD+N_neuronsperunit) << endl;
ofs << "Stretch Receptors Gains: \n A-class SR: " << sr.SR_A_gain <<
"\n B-class SR: " << sr.SR_B_gain << endl;
ofs << "NMJ weights: \n" <<
"\n DA: " << NMJ_DA <<
"\n DB: " << NMJ_DB <<
"\n DD: " << NMJ_DD <<
"\n VD: " << NMJ_VD <<
"\n VA: " << NMJ_VA <<
"\n VB: " << NMJ_VB << endl;
}