-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmm_9_haga_withFD.cpp
executable file
·1478 lines (1283 loc) · 40.2 KB
/
bmm_9_haga_withFD.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Original model by Naoki Hiratani (N.Hiratani@gmail.com)
// Python-friendly interactive implementation by Roman Koshkin (roman.koshkin@gmail.com)
#include <algorithm>
#include <boost/format.hpp>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iostream>
#include <random>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// to complile
// g++ -std=gnu++11 -Ofast -shared -fPIC -ftree-vectorize -march=native -mavx bmm_9_haga_grid.cpp -o ./bmm.dylib
// g++ -std=gnu++11 -O3 -dynamiclib -ftree-vectorize -march=native -mavx bmm_7_haga.cpp -o ./bmm.dylib
// sudo /usr/bin/g++ -std=gnu++11 -Ofast -shared -fPIC -ftree-vectorize -march=native -mavx bmm_8_haga.cpp -o ./bmm.dylib
// icc -std=gnu++11 -O3 -shared -fPIC bmm_5_haga.cpp -o ./bmm.dylib
// g++ -std=gnu++11 -Ofast -shared -fPIC -ftree-vectorize -march=native -mavx -fopenmp bmm_9_haga.cpp -o ./bmm.dylib
struct Timer {
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
std::chrono::duration<float> duration;
Timer() {
start = std::chrono::high_resolution_clock::now();
}
/* when the function where this object is created returns,
this object must be destroyed, hence this destructor is called */
~Timer() {
end = std::chrono::high_resolution_clock::now();
duration = end - start;
float ms = duration.count() * 1000.0f;
std::cout << "Elapsed (c++ timer): " << ms << " ms." << std::endl;
}
};
// class definition
class Model {
public:
// input struct
typedef struct
{
double alpha;
double usd;
double JEI;
double T;
double h;
// int NE;
// int NI;
// probability of connection
double cEE;
double cIE;
double cEI;
double cII;
// Synaptic weights
double JEE;
double JEEinit;
double JIE;
double JII;
//initial conditions of synaptic weights
double JEEh;
double sigJ;
double Jtmax;
double Jtmin;
// Thresholds of update
double hE;
double hI;
double IEex;
double IIex;
double mex;
double sigex;
// Average intervals of update, ms
double tmE;
double tmI;
//Short-Term Depression
double trec;
double Jepsilon;
// Time constants of STDP decay
double tpp;
double tpd;
double twnd;
// Coefficients of STDP
double g;
//homeostatic
int itauh;
double hsd;
double hh;
double Ip;
double a;
double xEinit;
double xIinit;
double tinit;
double U;
double taustf;
double taustd;
double Cp;
double Cd;
bool HAGA;
bool asym;
double stimIntensity;
} ParamsStructType;
// returned struct
typedef struct
{
double alpha;
double usd;
double JEI;
double T;
double h;
int NE;
int NI;
// probability of connection
double cEE;
double cIE;
double cEI;
double cII;
// Synaptic weights
double JEE;
double JEEinit;
double JIE;
double JII;
//initial conditions of synaptic weights
double JEEh;
double sigJ;
double Jtmax;
double Jtmin;
// Thresholds of update
double hE;
double hI;
double IEex;
double IIex;
double mex;
double sigex;
// Average intervals of update, ms
double tmE;
double tmI;
//Short-Term Depression
double trec;
double Jepsilon;
// Time constants of STDP decay
double tpp;
double tpd;
double twnd;
// Coefficients of STDP
double g;
//homeostatic
int itauh;
double hsd;
double hh;
double Ip;
double a;
double xEinit;
double xIinit;
double tinit;
double Jmin;
double Jmax;
double Cp;
double Cd;
int SNE;
int SNI;
int NEa;
double t;
double U;
double taustf;
double taustd;
bool HAGA;
bool asym;
double stimIntensity;
int cell_id;
} retParamsStructType;
// ostringstream ossSTP;
// ofstream ofsSTP;
double accum;
int nstim;
int NE, NI, N;
int SNE, SNI; //how many neurons get updated per time step
int NEa; // Exact number of excitatory neurons stimulated externally
int pmax;
vector<vector<double>> Jo;
vector<vector<double>> Ji;
double alpha = 50.0; // Degree of log-STDP (50.0)
double usd = 0.1; // Release probability of a synapse (0.05 - 0.5)
double JEI = 0.15; // 0.15 or 0.20
double pi = 3.14159265;
double e = 2.71828182;
double T = 1800 * 1000.0; // simulation time, ms
double h = 0.01; // time step, ms ??????
// probability of connection
double cEE = 0.2; //
double cIE = 0.2; //
double cEI = 0.5; //
double cII = 0.5; //
// Synaptic weights
double JEE = 0.15; //
double JEEinit = 0.15; // ?????????????
double JIE = 0.15; //
double JII = 0.06; //
//initial conditions of synaptic weights
double JEEh = 0.15; // Standard synaptic weight E-E
double sigJ = 0.3; //
double Jtmax = 0.25; // J_maxˆtot
double Jtmin = 0.01; // J_minˆtot // ??? NOT IN THE PAPER
// WEIGHT CLIPPING // ???
double Jmax = 5.0 * JEE; // ???
double Jmin = 0.01 * JEE; // ????
// Thresholds of update
double hE = 1.0; // Threshold of update of excitatory neurons
double hI = 1.0; // Threshold of update of inhibotory neurons
double IEex = 2.0; // Amplitude of steady external input to excitatory neurons
double IIex = 0.5; // Amplitude of steady external input to inhibitory neurons
double mex = 0.3; // mean of external input
double sigex = 0.1; // variance of external input
// Average intervals of update, ms
double tmE = 5.0; //t_Eud EXCITATORY
double tmI = 2.5; //t_Iud INHIBITORY
//Short-Term Depression
double trec = 600.0; // recovery time constant (tau_sd, p.13 and p.12)
//double usyn = 0.1;
double Jepsilon = 0.001; // BEFORE UPDATING A WEIGHT, WE CHECK IF IT IS GREATER THAN
// Jepsilon. If smaller, we consider this connection as
// non-existent, and do not update the weight.
// Time constants of STDP decay
double tpp = 20.0; // tau_p
double tpd = 40.0; // tau_d
double twnd = 500.0; // STDP window lenght, ms
// Coefficients of STDP
double Cp = 0.1 * JEE; // must be 0.01875 (in the paper)
double Cd = Cp * tpp / tpd; // must be 0.0075 (in the paper)
//homeostatic
//double hsig = 0.001*JEE/sqrt(10.0);
double hsig = 0.001 * JEE; // i.e. 0.00015 per time step (10 ms)
int itauh = 100; // decay time of homeostatic plasticity, (100s)
double hsd = 0.1; // release probability
double hh = 10.0; // SOME MYSTERIOUS PARAMETER
double Ip = 1.0; // External current applied to randomly chosen excitatory neurons
double a = 0.20; // Fraction of neurons to which this external current is applied
double xEinit = 0.02; // the probability that an excitatory neurons spikes at the beginning of the simulation
double xIinit = 0.01; // the probability that an inhibitory neurons spikes at the beginning of the simulation
double tinit = 100.0; // period of time after which STDP kicks in
vector<double> dvec;
vector<int> ivec;
deque<double> ideque; // <<<< !!!!!!!!
vector<deque<double>> dspts; // <<<< !!!!!!!!
vector<int> x;
set<int> spts;
int saveflag = 0;
double t = 0;
int tidx = -1;
// bool trtof = true; // ?????? some flag
double u;
int j;
vector<int> smpld;
set<int>::iterator it;
double k1, k2, k3, k4;
bool Iptof = true;
vector<vector<int>> Jinidx; /* list (len=3000) of lists. Each
list lists indices of excitatory neurons whose weights are > Jepsilon */
// we initialize (only excitatory) neurons with values of synaptic efficiency
vector<double> ys;
// classes to stream data to files
ofstream ofsr;
double tauh; // decay time of homeostatic plasticity, in ms
double g;
// method declarations
Model(int, int, int); // construction
double dice();
double ngn();
void sim(int);
void setParams(ParamsStructType);
retParamsStructType getState();
vector<vector<double>> calc_J(double, double);
vector<int> rnd_sample(int, int);
double fd(double, double);
vector<double> F;
vector<double> D;
double U = 0.6; // default release probability for HAGA
double taustf;
double taustd;
bool HAGA;
bool asym;
void STPonSpike(int);
void STPonNoSpike();
void updateMembranePot(int);
void checkIfStim(int);
void STDP(int);
void saveSTP();
void reinitFD();
void saveX();
void saveDSPTS();
void loadX(string);
void loadDSPTS(string);
deque<double> SplitString(string);
deque<int> SplitString_int(string);
void reinit_Jinidx();
void saveSpts();
void loadSpts(string);
double acc0;
double acc1;
double acc2;
double c0;
double c1;
double c2;
double c;
double acc;
vector<int> hStim;
double stimIntensity;
int cell_id;
// ostringstream ossa;
// ofstream ofsa;
// string fstra;
// diagnostics:
// ostringstream ossb;
// ofstream ofsb;
// string fstrb;
// void dumpClusters();
// here you just declare pointers, but you must
// ALLOCATE space on the heap for them in the class constructor
double *ptr_Jo;
double *ptr_F;
double *ptr_D;
double *ptr_ys;
// flexible arrays can only be declared at the end of the class !!
// double sm[];
// double* sm;
private:
// init random number generator
std::random_device m_randomdevice;
std::mt19937 m_mt;
};
// double Model::dice(){
// return rand()/(RAND_MAX + 1.0);
// }
double Model::dice() {
std::uniform_real_distribution<double> dist(0.0, 1.0);
return dist(m_mt);
}
double Model::ngn() {
// sample from a normal distribution based on two uniform distributions
// WHY IS IT SO?
double u = Model::dice();
double v = Model::dice();
return sqrt(-2.0 * log(u)) * cos(2.0 * pi * v);
}
// choose the neuron ids that will be updated at the current time step
vector<int> Model::rnd_sample(int ktmp, int Ntmp) { // when ktmp << Ntmp
vector<int> smpld;
int xtmp;
bool tof;
while (smpld.size() < ktmp) {
xtmp = (int)floor(Ntmp * Model::dice());
tof = true;
// make sure that the sampled id isn't the same as any of the previous ones
for (int i = 0; i < smpld.size(); i++) {
if (xtmp == smpld[i]) {
tof = false;
}
}
if (tof)
smpld.push_back(xtmp);
}
return smpld;
}
double Model::fd(double x, double alpha) {
return log(1.0 + alpha * x) / log(1.0 + alpha);
}
void Model::STPonSpike(int i) {
if (HAGA == 0) {
ys[i] -= usd * ys[i];
} else {
F[i] += U * (1 - F[i]); // U = 0.6
D[i] -= D[i] * F[i];
}
// remove it from the set of spiking neurons
it = spts.find(i);
if (it != spts.end()) {
spts.erase(it++);
}
// and turn it OFF
x[i] = 0;
}
void Model::STPonNoSpike() {
// EVERY 10 ms Becuase STD is slow, we can apply STD updates every 10th step
if (((int)floor(t / h)) % 10 == 0) {
for (int i = 0; i < NE; i++) {
if (HAGA == 0) { // @@
if (t < 2000000) {
ys[i] += hsd * (1.0 - ys[i]) / trec;
} else {
// 4th order Runge-Kutta
k1 = (1.0 - ys[i]) / trec;
k2 = (1.0 - (ys[i] + 0.5 * hsd * k1)) / trec;
k3 = (1.0 - (ys[i] + 0.5 * hsd * k2)) / trec;
k4 = (1.0 - (ys[i] + hsd * k3)) / trec;
ys[i] += hsd * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
}
} else {
if (t < 2000000) {
F[i] += hsd * (U - F[i]) / taustf; // @@ don't forget about hsd!!!
D[i] += hsd * (1.0 - D[i]) / taustd;
} else {
// hsd = 1.0; // @@ don't forget about hsd!!!
k1 = (U - F[i]) / taustf;
k2 = (U - (F[i] + 0.5 * hsd * k1)) / taustf; // @@ don't forget about hsd!!!
k3 = (U - (F[i] + 0.5 * hsd * k2)) / taustf;
k4 = (U - (F[i] + hsd * k3)) / taustf;
F[i] += hsd * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
k1 = (1.0 - D[i]) / taustd;
k2 = (1.0 - (D[i] + 0.5 * hsd * k1)) / taustd;
k3 = (1.0 - (D[i] + 0.5 * hsd * k2)) / taustd;
k4 = (1.0 - (D[i] + hsd * k3)) / taustd;
D[i] += hsd * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
}
}
}
}
}
void Model::saveDSPTS() {
ofstream ofsDSPTS;
// ofsDSPTS.open("DSPTS_" + std::to_string(cell_id) + "_" + std::to_string(t));
ofsDSPTS.open("DSPTS_" + std::to_string(cell_id));
ofsDSPTS.precision(10);
for (int i = 0; i < NE; i++) {
ofsDSPTS << i;
for (int sidx = 0; sidx < dspts[i].size(); sidx++) {
ofsDSPTS << " " << dspts[i][sidx];
}
ofsDSPTS << endl;
}
}
void Model::saveX() {
ofstream ofsX;
// ofsX.open("X_" + std::to_string(cell_id) + "_" + std::to_string(t));
ofsX.open("X_" + std::to_string(cell_id));
ofsX.precision(10);
ofsX << x[0];
for (int i = 1; i < N; i++) {
ofsX << " " << x[i];
}
ofsX << endl;
}
void Model::loadDSPTS(string tt) {
dspts.clear();
deque<double> iideque;
// ifstream file("DSPTS_" + std::to_string(cell_id) + "_" + tt);
ifstream file("DSPTS_" + std::to_string(cell_id));
if (!file.is_open()) {
throw "DSPTS file not found.";
}
string line;
while (getline(file, line)) {
iideque = SplitString(line.c_str());
iideque.pop_front();
dspts.push_back(iideque);
// cout << i << endl;
// cout << dspts[i].back() << endl;
}
file.close();
cout << "DSPTS loaded" << endl;
}
void Model::loadX(string tt) {
x.clear();
deque<double> iideque;
// ifstream file("X_" + std::to_string(cell_id) + "_" + tt);
ifstream file("X_" + std::to_string(cell_id));
if (!file.is_open()) {
throw "X file not found.";
}
string line;
while (getline(file, line)) {
iideque = SplitString(line.c_str());
for (int i = 0; i < N; i++) {
x.push_back(iideque[i]);
}
}
file.close();
cout << "X loaded" << endl;
}
deque<double> Model::SplitString(string line) {
deque<double> iideque;
string temp = "";
for (int i = 0; i < line.length(); ++i) {
if (line[i] == ' ') {
iideque.push_back(stod(temp));
temp = "";
} else {
temp.push_back(line[i]);
}
}
iideque.push_back(stod(temp));
return iideque;
}
deque<int> Model::SplitString_int(string line) {
deque<int> iideque;
string temp = "";
for (int i = 0; i < line.length(); ++i) {
if (line[i] == ' ') {
iideque.push_back(stoi(temp));
temp = "";
} else {
temp.push_back(line[i]);
}
}
return iideque;
}
void Model::updateMembranePot(int i) {
// WE update the membrane potential of the chosen excitatory neuron (eq.4, p.12)
// -threshold of update + steady exc input * mean/var of external stim input
u = -hE + IEex * (mex + sigex * ngn()); // pentagon, p.12
it = spts.begin();
//we go over all POSTsynaptic neurons that are spiking now
while (it != spts.end()) {
//if a postsynaptic spiking neuron happens to be excitatory,
if (*it < NE) {
if (HAGA == 0) {
u += ys[*it] * Jo[i][*it];
} else {
u += F[*it] * D[*it] * Jo[i][*it];
}
//if a postsynaptic spiking neuron happens to be inhibitory,
} else {
u += Jo[i][*it];
}
++it;
}
}
void Model::checkIfStim(int i) {
if (hStim[i] == 1) {
if (dice() < stimIntensity) {
u += Ip;
}
// std::cout << "stim @ t=" << t << " on neuron " << i << std::endl;
}
}
void Model::STDP(int i) {
// if( ( (int)floor(t/h) )%1000 == 0 ){
// std::cout << "USING STDP" << std::endl;
// }
// if the POSTsynaptic neuron chosen for update exceeds the threshold, we save its ID in the set "spts"
spts.insert(i);
/* dspts saves the TIME of this spike to a DEQUE, such that each row id of this deque
corresponds to the id of the spiking neuron. The row records the times at which that
neuron emitted at spike. */
dspts[i].push_back(t); // SHAPE: (n_postsyn x pytsyn_sp_times)
x[i] = 1;
// // record a spike on an EXCITATORY neuron (because STDP is only on excitatory neurons)
if (saveflag == 1){
ofsr << t << " " << i << endl; // record a line to file
}
// BEGIN STDP ******************************************************************************
// **************** CHECK THIS IMPLEMENTATION, I DON'T LIKE IT ****************************
// *****************************************************************************************
/* First (LTD), we treat the chosen neuron as PREsynaptic and loop over all the POSTSYNAPTIC excitatory
neurons that THE CHOSEN NEURON synapses on. Since we're at time t (and this is the latest time),
the spikes recorded on those "POSTsynaptic" neurons will have an earlier timing than the spike
recorded on the currently chosen neuron (that we treat as PREsynaptic). This indicates that
the synaptic weight between this chosen neuron (presynaptic) and all the other neurons
(postsynaptic) will decrease. */
for (int ip = 0; ip < NE; ip++) {
if (Jo[ip][i] > Jepsilon && t > tinit) {
// dspts is a deque of spiking times on the ith POSTSYNAPTIC neurons
for (int sidx = 0; sidx < dspts[ip].size(); sidx++) {
// { ?????? Eq.7 p.12 }
/* depression happens if presynaptic spike time (t) happens AFTER
a postsynaptic spike time (dspts[ip][sidx])
BY THE WAY: HERE WE HAVE ASYMMETRIC STDP */
if (HAGA == 0) {
if (asym == 1) {
Jo[ip][i] -= Cd * fd(Jo[ip][i] / JEE, alpha) * exp(-(t - dspts[ip][sidx]) / tpd); /// !!! <<<<<<<<<<<<<<<<<<<<< !!!!!!
} else {
// old (and apparently wrong) way
// Jo[ip][i] += Cd*fd(Jo[ip][i]/JEE, alpha)*exp( -(t-dspts[ip][sidx])/tpd );
// new (and hopefully right) way. See STDP_explained.ipynb
Jo[ip][i] += Cp * exp((dspts[ip][sidx] - t) / tpp) - fd(Jo[ip][i] / JEE, alpha) * Cd * exp((dspts[ip][sidx] - t) / tpd);
}
} else {
if (asym == 1) {
Jo[ip][i] -= F[i] * D[i] * Cd * fd(Jo[ip][i] / JEE, alpha) * exp(-(t - dspts[ip][sidx]) / tpd);
} else {
// old (and apparently wrong) way
// Jo[ip][i] += F[i] * D[i] * Cd*fd(Jo[ip][i]/JEE, alpha)*exp( -(t-dspts[ip][sidx])/tpd );
// new (and hopefully right) way. See STDP_explained.ipynb
// STP-dependent (new HAGA)
Jo[ip][i] += F[i] * D[i] * (Cp * exp((dspts[ip][sidx] - t) / tpp) - fd(Jo[ip][i] / JEE, alpha) * Cd * exp((dspts[ip][sidx] - t) / tpd));
// STP-independent (new Hiratani)
// Jo[ip][i] += (Cp * exp((dspts[ip][sidx] - t) / tpp) - fd(Jo[ip][i] / JEE, alpha) * Cd * exp((dspts[ip][sidx] - t) / tpd));
}
}
}
// we force the weights to be no less than Jmin, THIS WAS NOT IN THE PAPER
if (Jo[ip][i] > Jmax)
Jo[ip][i] = Jmax;
if (Jo[ip][i] < Jmin)
Jo[ip][i] = Jmin;
}
}
// (LTP)
/* Jinidx is a list of lists (shape (2500 POSTsyn, n PREsyn)). E.g. if in row 15 we have number 10,
it means that the weight between POSTsynaptic neuron 15 and presynaptc neuron 10 is greater than Jepsilon */
/* we treat the currently chosen neuron as POSTtsynaptic, and we loop over all the presynaptic
neurons that synapse on the current postsynaptic neuron. At time t (the latest time, and we don't
yet know any spikes that will happen in the future) all the spikes on the presynaptic neurons with
id j will have an earlier timing that the spike on the currently chosen neuron i (that we treat
as postsynaptic for now). This indicates that the weights between the chosen neuron treated as post-
synaptic for now and all the other neurons (treated as presynaptic for now) will be potentiated. */
for (int jidx = 0; jidx < Jinidx[i].size(); jidx++) {
j = Jinidx[i][jidx]; // at each loop we get the id of the jth presynaptic neuron with J > Jepsilon
if (t > tinit) {
for (int sidx = 0; sidx < dspts[j].size(); sidx++) {
// we loop over all the spike times on the jth PRESYNAPTIC neuron
if (HAGA == 0) {
if (asym == 1) {
Jo[i][j] += g * Cp * exp(-(t - dspts[j][sidx]) / tpp);
} else {
Jo[i][j] += Cp * exp(-(t - dspts[j][sidx]) / tpp) - fd(Jo[i][j] / JEE, alpha) * Cd * exp(-(t - dspts[j][sidx]) / tpd);
}
} else {
if (asym == 1) {
Jo[i][j] += F[j] * D[j] * g * Cp * exp(-(t - dspts[j][sidx]) / tpp);
} else {
// STP-dependent (по новому определению Haga)
Jo[i][j] += F[j] * D[j] * (Cp * exp(-(t - dspts[j][sidx]) / tpp) - fd(Jo[i][j] / JEE, alpha) * Cd * exp(-(t - dspts[j][sidx]) / tpd));
// STP-independent (по новому определению Hiratani: не так как было у Хиратани в статье, а просто без F*D)
// Jo[i][j] += (Cp * exp(-(t - dspts[j][sidx]) / tpp) - fd(Jo[i][j] / JEE, alpha) * Cd * exp(-(t - dspts[j][sidx]) / tpd));
}
}
// as per eq. (7) p.12, it actually should be
// Jo[i][j] += g * Cp * exp((dspts[j][sidx] - t)/tpp)
// because dspts[j][sidx] is t_pre, and t is t_post
}
// we force the weights to be no more than Jmax, THIS WAS NOT IN THE PAPER
if (Jo[i][j] > Jmax)
Jo[i][j] = Jmax;
if (Jo[i][j] < Jmin)
Jo[i][j] = Jmin;
}
}
}
// void Model::saveSTP() {
// if (HAGA == false) {
// accum = 0;
// ofsSTP << t;
// for (int i = 0; i < 5; i++){
// accum = 0;
// for(int j = 0; j < 500; j++){
// accum += ys[j+500*i];
// }
// ofsSTP << " " << accum/500;
// }
// ofsSTP << std::endl;
// } else {
// accum = 0;
// ofsSTP << t;
// for (int i = 0; i < 5; i++){
// accum = 0;
// for(int j = 0; j < 500; j++){
// accum += F[j+500*i] * D[j+500*i];
// }
// ofsSTP << " " << accum/500;
// }
// ofsSTP << std::endl;
// }
// }
// void Model::saveSTP() {
// if (HAGA == false) {
// ofsSTP << t;
// for (int i = 0; i < NE; i++){
// ofsSTP << " " << ys[i];
// }
// ofsSTP << std::endl;
// } else {
// ofsSTP << t;
// for (int i = 0; i < NE; i++){
// ofsSTP << " " << F[i] * D[i];
// }
// ofsSTP << std::endl;
// }
// }
void Model::sim(int interval) {
// dumpClusters();
// std::cout << "SNE = " << SNE << ", SNI = " << SNI << std::endl;
std::cout << "t = " << t << std::endl;
Timer timer;
// HAVING INITIALIZED THE NETWORK, WE go time step by time step
while (interval > 0) {
// // save STP states every millisecond
// if( (int)floor(t/h) % 1000 == 0) {
// saveSTP();
// }
t += h;
interval -= 1;
// we decide which EXCITATORY neurons will be updated
// they may or may not be spiking at the current step
smpld = rnd_sample(SNE, NE);
// to log the distribution of sampled neurons
// ofsb << smpld[0] << " " << smpld[1] << " " << smpld[2] << " " << smpld[3] << " " << smpld[4] << " " << std::endl;
// we cycle through those chosen neurons
for (int iidx = 0; iidx < smpld.size(); iidx++) {
int i = smpld[iidx];
// STP (empty square, eq. 6 p.12)
// if a chosen neuron is ALREADY on
if (x[i] == 1) {
STPonSpike(i);
}
// either way
updateMembranePot(i);
checkIfStim(i);
// WE PERFORM AN STDP on the chosen neuron if it spikes u > 0
if (u > 0) {
STDP(i);
}
}
// we sample INHIBITORY neurons to be updated at the current step
smpld = rnd_sample(SNI, NI);
for (int iidx = 0; iidx < smpld.size(); iidx++) {
int i = NE + smpld[iidx];
/* if this inhibitory neuron is spiking we set it to zero
in the binary vector x and remove its index from the set
of currently spiking neurons */
// crazy optimization, but in fact eq.(5) p.12 (filled circle)
if (x[i] == 1) {
it = spts.find(i);
if (it != spts.end()) {
// removing a spike time from the SET of spikes on inhibitory neurons is the same as
// subtracting them
spts.erase(it++);
}
x[i] = 0;
}
//update the membrane potential on a chosen inhibitory neuron
u = -hI + IIex * (mex + sigex * ngn()); // hexagon, eq.5, p.12
it = spts.begin();
while (it != spts.end()) {
u += Jo[i][*it];
++it;
}
// if the membrane potential on the currently chosen INHIBITORy neuron
// is greater than the threshold, we record a spike on this neuron.
if (u > 0) {
spts.insert(i);
x[i] = 1;
// // record a spike on an INHIBITORY NEURON
if (saveflag == 1) {
ofsr << t << " " << i << endl;
}
}
}
STPonNoSpike();
// EVERY 10 ms Homeostatic plasticity, weight clipping, boundary conditions, old spike removal
if (((int)floor(t / h)) % 1000 == 0) {
//Homeostatic Depression
for (int i = 0; i < NE; i++) {
for (int jidx = 0; jidx < Jinidx[i].size(); jidx++) {
j = Jinidx[i][jidx];
// ?????????? THAT'S NOT EXACTLY WHAT THE PAPER SAYS
k1 = (JEEh - Jo[i][j]) / tauh;
k2 = (JEEh - (Jo[i][j] + 0.5 * hh * k1)) / tauh;
k3 = (JEEh - (Jo[i][j] + 0.5 * hh * k2)) / tauh;
k4 = (JEEh - (Jo[i][j] + hh * k3)) / tauh;
Jo[i][j] += hh * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0 + hsig * ngn();
// we clip the weights from below and above
if (Jo[i][j] < Jmin)
Jo[i][j] = Jmin; // ????? Jmin is zero, not 0.0015, as per Table 1
if (Jo[i][j] > Jmax)
Jo[i][j] = Jmax;
}
}
//boundary condition
for (int i = 0; i < NE; i++) {
double Jav = 0.0;
for (int jidx = 0; jidx < Jinidx[i].size(); jidx++) {
// find the total weight per each postsynaptic neuron
Jav += Jo[i][Jinidx[i][jidx]];
}
// find mean weight per each postsynaptic neuron
Jav = Jav / ((double)Jinidx[i].size());
if (Jav > Jtmax) {
for (int jidx = 0; jidx < Jinidx[i].size(); jidx++) {
j = Jinidx[i][jidx];
// if the total weight exceeds Jtmax, we subtract the excess value
Jo[i][j] -= (Jav - Jtmax);
// but if a weight is less that Jmin, we set it to Jmin (clip from below)
if (Jo[i][j] < Jmin) {
Jo[i][j] = Jmin;
}
}
// if the total weight is less that Jtmin
} else if (Jav < Jtmin) {
for (int jidx = 0; jidx < Jinidx[i].size(); jidx++) {
j = Jinidx[i][jidx];
/* ???????? we top up each (!!!???) weight by the difference
between the total min and current total weight */
Jo[i][j] += (Jtmin - Jav);
// but if a weight is more that Jmax, we clip it to Jmax
if (Jo[i][j] > Jmax) {
Jo[i][j] = Jmax;
}
}
}
}
// remove spikes older than 500 ms
for (int i = 0; i < NE; i++) {
for (int sidx = 0; sidx < dspts[i].size(); sidx++) {
//if we have spike times that are occured more than 500 ms ago, we pop them from the deque
if (t - dspts[i][0] > twnd) {
dspts[i].pop_front();
}
}
}
}
// EVERY 1s
if (((int)floor(t / h)) % (1000 * 100) == 0) {
tidx += 1; // we count the number of 1s cycles
int s = 0;
it = spts.begin();
while (it != spts.end()) {
++s;
++it;
}
// exit if either no neurons are spiking or too many spiking after t > 200 ms
if (s == 0 || (s > 1.0 * NE && t > 200.0)) {
std::cout << "Exiting because either 0 or too many spikes at t =" << t << std::endl;
break;
}
}
}
}
// initialize the weight matrix
vector<vector<double>> Model::calc_J(double JEEinit, double JEI) {
vector<vector<double>> J;
int mcount = 0;
for (int i = 0; i < NE; i++) {
J.push_back(dvec);
for (int j = 0; j < NE; j++) {
J[i].push_back(0.0);
// first E-E weights consistent with the E-E connection probability
if (i != j && dice() < cEE) {
// @@ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
J[i][j] = JEEinit * (1.0 + sigJ * ngn());
// if (HAGA == 1) {
// J[i][j] = JEEinit * (1.0 + sigJ*ngn()); // should be JEEinit, but the matrix is initialized together with the model, but the parameters are set later
// } else {
// J[i][j] += JEEinit*(1.0 + sigJ*ngn());
// }
// if some weight is out of range, we clip it
if (J[i][j] < Jmin)
J[i][j] = Jmin;
if (J[i][j] > Jmax)
J[i][j] = Jmax;
}
}
// then the E-I weights
for (int j = NE; j < N; j++) {
J[i].push_back(0.0); // here the matrix J is at first of size 2500, we extend it
if (dice() < cEI) {
J[i][j] -= JEI; /* becuase jth presynaptic inhibitory synapsing on
an ith excitatory postsynaptic neuron should inhibit it. Hence the minus */
}
}
}
// then the I-E and I-I weights