-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimGroup.java
1386 lines (1206 loc) · 44.3 KB
/
SimGroup.java
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
/**
* SimGroup.java
*
* Created on 10-Mar-2005
* City University
* BSc Computing with Distributed Systems
* Project title: Simulating Animal Learning
* Project supervisor: Dr. Eduardo Alonso
* @author Dionysios Skordoulis
*
* Modified in October-2009
* The Centre for Computational and Animal Learning Research
* @supervisor Dr. Esther Mondragon
* email: [email protected]
* @author Rocio Garcia Duran
*
* Modified in July-2011
* The Centre for Computational and Animal Learning Research
* @supervisor Dr. Esther Mondragon
* email: [email protected]
* @author Dr. Alberto Fernandez
* email: [email protected]
*
*/
package simulator;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.HTreeMap;
import org.mapdb.Serializer;
import simulator.configurables.ContextConfig;
import simulator.configurables.ContextConfig.Context;
import simulator.configurables.ITIConfig;
import simulator.configurables.TimingConfiguration;
import simulator.util.USNames;
import extra166y.ParallelArray;
/**
* SimGroup is the class which models a group from the experiment. It will
* process any new sequences of stimuli and adds them to its ArrayList of
* phases. It is an intermediate between the Controller and the Phase itself. It
* contains all the necessary variables need to run a simulation and it keeps a
* record of the results but in a global view, meaning that the results will be
* an extract from all phases together.
*/
public class SimGroup implements Runnable {
public static String getKeyByValue(Map<String, String> configCuesNames,
String value) {
String key = null;
int count = 0;
if (configCuesNames != null) {for (Map.Entry<String, String> entry : configCuesNames.entrySet()) {
if (entry.getValue().equals(value)) {
key = entry.getKey();
count++;
}
}
}
return key;
}
/**
* Helper function for configurals - produce the powerset of the set of
* cues, this is the possible set of configural cues.
*
* @param originalSet
* Set of cues in a trial
* @return the powerset, excluding null
*/
public static <T> Set<Set<T>> powerSet(Set<T> originalSet) {
Set<Set<T>> sets = new HashSet<Set<T>>();
if (originalSet.isEmpty()) {
sets.add(new HashSet<T>());
return sets;
}
List<T> list = new ArrayList<T>(originalSet);
T head = list.get(0);
Set<T> rest = new HashSet<T>(list.subList(1, list.size()));
for (Set<T> set : powerSet(rest)) {
Set<T> newSet = new HashSet<T>();
newSet.add(head);
newSet.addAll(set);
sets.add(newSet);
sets.add(set);
}
return sets;
}
// Alberto Fernández August-2011
// Added boolean parameters isConfiguralCompounds and
// configuralCompoundsMapping.
protected ArrayList<SimPhase> phases;
private Map<String, Stimulus> cues;
private TreeMap<String,TreeMap<String,Float>> sharedMap;
private TreeMap<String,Integer> shorter;
private TreeMap<String,Integer> numberShared;
private TreeMap<String,ArrayList<StimulusElement>> sharedElements;
private String nameOfGroup;
private int noOfPhases, noOfCombinations, count;
private int[] phaseTrials;
/** Threaded array. **/
private ParallelArray<SimPhase> phasePool;
/** All sim cues used in all phases of this group**/
private ArrayList<StimulusElement> simCues;
/** At least one random phase indicator. **/
private boolean hasRandom;
/** The model this group belongs to. **/
private SimModel model;
private int totalTrials = 0;
private int totalStimuli = 0;
private int hasSet = 0;
private float common = 0.2f;
private boolean esther;
private DB db;
private DB db2;
private HTreeMap<Long, Float> map;
private HTreeMap<Long, Float> map2;
private int maxDuration = 0;
private int maxMicros = 0;
ArrayList totalKeySet;
private boolean hashSet;
private int bytes1;
private int bytes2;
private int bytes3;
private int bytes4;
private int bytes5;
private int trialTypes;
private int maxSessions;
private ArrayList names;
private DB dbDisk;
private DB dbDisk2;
private HTreeMap<Long, Float> onDisk;
private HTreeMap<Long, Float> onDisk2;
private long key;
private int s1;
private int s2;
private int s3;
private int s4;
private int s5;
private int rule;
private float totalMax;
private TreeMap<String,Stimulus> commons;
private TreeMap<String,HTreeMap> maps;
private TreeMap<String,HTreeMap> disks;
private Object map3;
private Set<String> set1;
private TreeMap<String,TreeMap<String,Object>> cache;
private TreeMap<Long,Float> cache1;
private TreeMap<Long,Float> cache2;
private Runtime runtime;
private File f1;
private File f2;
private ArrayList<File> files;
private ArrayList<String> trialStrings;
private ArrayList<String> trialStrings2;
private TreeMap<String,Integer> appearanceList;
private TreeMap<String,ArrayList<Integer>> compoundAppearanceList;
private long memory;
/**
* Create a group
*
* @param n
* name of the Group
* @param np
* number of phases
* @param rn
* number of combinations
* @return true if the method completed without any errors and false
* otherwise.
*/
public SimGroup(String n, int np, int rn, SimModel model) {
files = new ArrayList();
runtime = Runtime.getRuntime();
nameOfGroup = n;
noOfPhases = np;
noOfCombinations = rn;
count = 1;
simCues = new ArrayList<StimulusElement>();
compoundAppearanceList = new TreeMap();
maps = new TreeMap();
disks = new TreeMap();
commons = new TreeMap();
cache = new TreeMap();
cache1 = new TreeMap();
cache2 = new TreeMap();
cues = new TreeMap<String, Stimulus>();
phases = new ArrayList<SimPhase>(noOfPhases);
phaseTrials = new int[noOfPhases];
trialStrings = new ArrayList();
trialStrings2 = new ArrayList();
appearanceList = new TreeMap();
this.setModel(model);
}
/**
* Adds a new phase in the group's arraylist. The stimuli sequence of the
* given is being processed mainly so it could be added as a new SimPhase
* object and secondary it might produce new cues which weren't on previous
* phases. This new cues are added on the group's cue list as well.
*
* @param seqOfStimulus
* the stimuli sequence of the given phase.
* @param boolean to know if the phase is going to be randomly executed
* @param int the number of the current phase
* @param boolean to know if the phase is going to use configural cues
* @param mapping
* with configural compounds from their "virtual" name
* @return true if the method completed without any errors and false
* otherwise.
*/
public boolean addPhase(String seqOfStimulus, boolean isRandom,
int phaseNum, boolean isConfiguralCompounds,
TreeMap<String, String> configuralCompoundsMapping,
TimingConfiguration timings, ITIConfig iti, ContextConfig context,boolean vartheta) {
seqOfStimulus = seqOfStimulus.toUpperCase(); // Sequence is always
// stored in upper case.
// Alberto Fernández
// August-2011
files = new ArrayList();
List<Trial> order = new ArrayList<Trial>(50);
Set<Trial> trials = new HashSet<Trial>();
String sep = "/";
String[] listedStimuli = seqOfStimulus.toUpperCase().split(sep);
// CSC of cues. J Gray
Stimulus cscCues;
if (!cues.containsKey(context.getSymbol())
&& !context.getContext().equals(Context.EMPTY)) {
// Modified by J Gray to add CSC cues.
cscCues = new Stimulus(this,context.getSymbol(), context.getAlphaR(),totalTrials,totalStimuli);
cues.put(context.getSymbol(), cscCues);
}
// Added by Alberto Fernández
// Introduce "virtual" cues (lower case letters) in case of configural
// compounds.
int noStimuli = listedStimuli.length;
Map<String, SimStimulus> stimuli = new HashMap<String, SimStimulus>();
Set<String> configuralsAddedThisGroup = new HashSet<String>();
for (int i = 0; i < noStimuli; i++) {
String selStim = listedStimuli[i], repStim = "", cuesName = "", stimName = "";
boolean reinforced = false;
boolean oktrials = false, okcues = false, okreinforced = false;
boolean probe = false; // Is this a probe trial
if (model.isUseContext()) {
cuesName = context.getSymbol();
}
String compound = "";
int noStimRep = 1;
for (int n = 0; n < selStim.length(); n++) {
char selChar = selStim.charAt(n);
if (Character.isDigit(selChar) && !oktrials) {
repStim += selChar;
} else if (selChar == '^') { // This is a probe trial
probe = true;
} else if (Character.isLetter(selChar) && !okcues) {
oktrials = true;
cuesName += selChar;
if (!cues.containsKey(selChar + "")) {
// Modified by J Gray to add CSC cues.
cscCues = new Stimulus(this,selChar + "", 0.25f,totalTrials,totalStimuli);
cues.put(selChar + "", cscCues);
}
compound += selChar;
} else if ((USNames.isUS(selChar + ""))) {
cuesName += selChar;
oktrials = true;
okcues = true;
reinforced = (USNames.isReinforced(selChar + ""));
okreinforced = true;
if (reinforced && !cues.containsKey(selChar + "")) {
//Modified by Niklas 2014 to treat US like a CS
Stimulus usCue = new Stimulus(this,selChar + "", 0.25f,totalTrials,totalStimuli);
cues.put(selChar + "", usCue);
} else if (!reinforced && !USNames.hasReinforced(cues.keySet().toArray(new String[cues.size()]))) {
Stimulus usCue = new Stimulus(this,"+" + "", 0.25f,totalTrials,totalStimuli);
cues.put("+" + "", usCue);
}
} else
return false;
}
cscCues = new Stimulus(this,compound + "", 0.25f,totalTrials,totalStimuli);
//if (model.isCompound()) { cues.put(compound + "", cscCues);}
// Added by Alberto Fernández August-2011
if ((model.isUseContext() || compound.length() > 1)
&& isConfiguralCompounds) {
Set<String> compoundSet = new HashSet<String>();
for (char c : compound.toCharArray()) {
compoundSet.add(c + "");
}
if (!model.isSerialConfigurals()) {
for (Set<String> set : powerSet(compoundSet)) {
String s = "";
List<String> bits = new ArrayList<String>(set);
Collections.sort(bits);
for (String str : bits) {
s += str;
}
// Add configural cue as a "virtual" cue (lower
// case letter)
s = model.isUseContext() ? context.getSymbol()
+ s : s;
if (s.length() > 1) {
String virtualCueName = getKeyByValue(
configuralCompoundsMapping, s);
if (virtualCueName == null) {
if (configuralCompoundsMapping
.isEmpty()) {
virtualCueName = "a";
} else {
char c = configuralCompoundsMapping
.lastKey().charAt(0);
c = (char) (c + 1);
while(!Character.isLetter(c) || Character.toUpperCase(c) == c || Context.isContext(c+"")) {
c++;
//System.out.println("loop simgroup");
}
virtualCueName = "" + c;
}
configuralCompoundsMapping.put(
virtualCueName, s);
}
cuesName += virtualCueName;
configuralsAddedThisGroup.add(virtualCueName);
if (!cues.containsKey(virtualCueName + "")) {
// Modified to use CSCs. J Gray
cscCues = new Stimulus(this,virtualCueName + "", 0.25f,totalTrials,totalStimuli);
cues.put(virtualCueName + "", cscCues);
}
String compoundName = s + virtualCueName;
cscCues = new Stimulus(this,compoundName + "", 0.25f,totalTrials,totalStimuli);
cues.put(compoundName + "", cscCues);
}
}
}
}
int stringPos = 0;
//Find start position of this trial string
for(int s = 0; s < i; s++) {
stringPos += listedStimuli[s].length() + 1;
}
//Determine how many trial strings identical with this
//precede it
int trialNum = 0;
for(int s = i - 1; s >= 0; s--) {
if(listedStimuli[s].equals(listedStimuli[i])) {
trialNum++;
}
}
stimName = cuesName;
Trial trial = new Trial(stimName, probe,
(model.isTimingPerTrial() ? i : 0), selStim, stringPos, trialNum);
trials.add(trial);
if (repStim.length() > 0)
noStimRep = Integer.parseInt(repStim);
if (stimuli.containsKey(stimName))
stimuli.get(stimName).addTrials(noStimRep);
else
stimuli.put(stimName, new SimStimulus(stimName, noStimRep,
cuesName, reinforced));
for (int or = 0; or < noStimRep; or++)
order.add(trial);
}
for (Stimulus cue : cues.values()) {
for (Stimulus cue2 : cues.values()) {
if (!cue.isCS || !cue2.isCS || cue2.getName().equals(cue.getName()) || cue.isCommon() || cue2.isCommon()) {}
else {
if (commons.containsKey("c"+cue.getName()+cue2.getName()) || commons.containsKey("c"+cue2.getName()+cue.getName())) {}
else {
cscCues = new Stimulus(this,"c"+cue.getName()+cue2.getName(), 0.25f,totalTrials,totalStimuli);
commons.put("c"+cue.getName()+cue2.getName(), cscCues);
}
}
}
}
for (String key : commons.keySet()) {
String first =key.charAt(1)+"";
String second = key.charAt(2)+"";
Stimulus stim1 = cues.get(first);
Stimulus stim2 = cues.get(second);
Stimulus stim3 = commons.get(key);
stim1.addCommon(second,stim3);
stim2.addCommon(first,stim3);
if (!cues.containsKey(key))cues.put(key,stim3);
}
timings.setTrials(order.size());
timings.restartOnsets();
List<List<List<CS>>> sequences = timings.sequences(new HashSet<Trial>(
order));
if(true) {
sequences = timings.compounds(new ArrayList<Trial>(order));
}
TreeMap<String, Float> tm = model.getAlphaCues();
Iterator<String> it = tm.keySet().iterator();
while(it.hasNext()) {
String stim = it.next();
if (!USNames.isUS(stim) && cues.containsKey(stim) && stim.length() == 1) {
float alphaValue = (tm.containsKey(stim)) ? tm.get(stim) : -1;
if (stim.length() > 1) {
char[] characters = stim.toCharArray();
for (char character : characters) {
alphaValue += tm.get(character +"");
}
alphaValue /= stim.length();
}
if (alphaValue != -1 && alphaValue != 0) cues.get(stim).setRAlpha(alphaValue);
}
}
// Set indicator that this group has at least one randomised phase
hasRandom = hasRandom ? hasRandom : isRandom;
timings.restartOnsets();
//sortStimuli();
for (int j = 0; j < order.size(); j++ ){
Map<CS, int[]> timing = timings.makeTimings(order.get(j).getCues());
totalMax = (int) Math.round(timing.get(CS.TOTAL)[1]);
for (Stimulus stim : cues.values()) {
char[] names = stim.getName().toCharArray();
CS[] css = new CS[names.length];
int onset = -1;
int offset = 200;
int counter = 0;
for (char character : names) {
for (CS cs : order.get(j).getCues()) {
if (cs.getName().equals((String) (character + ""))) {
css[counter] = cs;
}
}
int tempOnset = (css[counter] != null && timing.containsKey(css[counter])) ? timing.get(css[counter])[0]: -1;
onset = Math.max(tempOnset,onset);
int tempOffset = (css[counter] != null && timing.containsKey(css[counter])) ? timing.get(css[counter])[1]: -1;
offset = Math.min(tempOffset,offset);
counter++;
}
stim.setTiming(onset,offset);
int duration = (!Context.isContext(stim.getName())) ? (offset-onset) : (int) (Math.round(timing.get(CS.TOTAL)[1]));
if ((duration) > stim.getMaxDuration()) { stim.setMaxDuration(duration);}
if (duration > totalMax) {totalMax = duration;}
if ( stim.getMaxDuration() > totalMax) {totalMax = stim.getMaxDuration();}
}
for (Stimulus s : cues.values()) {
if (s.getName().length() > 1) {
int onset = Math.max(0, Math.min(cues.get(s.getName().charAt(1)+"").getTheOnset(),cues.get(s.getName().charAt(2)+"").getTheOnset()));
int offset = Math.max(cues.get(s.getName().charAt(1)+"").getTheOffset(),cues.get(s.getName().charAt(2)+"").getTheOffset());
s.setMaxDuration(offset-onset);
s.setTiming(onset, offset);
}
s.setAllMaxDuration((int)totalMax);
}
for (String name: USNames.getNames()) {
if (cues.containsKey(name)) {cues.get(name).setAllMaxDuration((int)totalMax);}
if (cues.containsKey(name) && cues.get(name).getMaxDuration() < (timing.get(CS.US)[1] - timing.get(CS.US)[0])) {cues.get(name).setMaxDuration((timing.get(CS.US)[1] - timing.get(CS.US)[0]));}
}
maxMicros = (int) totalMax;
}
float totalMax = 0;
for (int j = 0; j < order.size(); j++ ){
Map<CS, int[]> timing = timings.makeTimings(order.get(j).getCues());
totalMax = (float) Math.max(totalMax,(int) Math.round(this.totalMax + iti.getMinimum()));
}
maxDuration = (int) totalMax;
for (Stimulus s : cues.values()) {
if (s.getAllMaxDuration() < totalMax) s.setAllMaxDuration((int)totalMax);
}
names = new ArrayList(cues.keySet());
return addPhaseToList(phaseNum,seqOfStimulus, order, stimuli, isRandom, timings,
iti, context,totalTrials,listedStimuli,vartheta);
}
public float getTotalMax() {return maxDuration;}
public int getSubElementNumber() {return model.getSetSize();}
/**
* Add a phase to this groups list of phases.
*
* @param seqOfStimulus
* @param order
* @param stimuli
* @param isRandom
* @param timings
* @param iti
* @return
*/
protected boolean addPhaseToList(int phaseNum,String seqOfStimulus, List<Trial> order,
Map<String, SimStimulus> stimuli, boolean isRandom,
TimingConfiguration timings, ITIConfig iti, ContextConfig context, int totalTrials,String[] listedStimuli,boolean vartheta) {
return phases.add(new SimPhase(phaseNum,1,seqOfStimulus, order, stimuli, this,
isRandom, timings, iti, context,totalTrials,listedStimuli,vartheta));
}
/**
* Empties every phase's results. It iterates through the phases and calls
* the SimPhase.emptyResults() method. This method cleans up the results
* variable.
*
*/
public void clearResults() {
for (int i = 0; i < noOfPhases; i++) {
SimPhase sp = phases.get(i);
//sp.emptyResults();
}
count = 1;
}
/**
* Checks if this is the name of a configural cue (i.e. contains lowercase
* characters)
*
* @param cueName
* the cue to check
* @return true if this is a configural cue
*/
protected boolean configuralCue(String cueName) {
return !cueName.equals(cueName.toUpperCase());
}
/**
*
* @return a count of how many phases are random in this group
*/
public int countRandom() {
int count = 0;
for (SimPhase phase : phases) {
if (phase.isRandom()
|| phase.getTimingConfig().hasVariableDurations()) {
count++;
}
}
return count;
}
/**
*
* @return a map of context names to their configurations.
*/
public Map<String, ContextConfig> getContexts() {
Map<String, ContextConfig> contexts = new HashMap<String, ContextConfig>();
for (SimPhase phase : phases) {
contexts.put(phase.getContextConfig().getSymbol(),
phase.getContextConfig());
}
return contexts;
}
/**
* Returns the number of trials that have been produced so far.
*
* @return the number of trials so far.
*/
public int getCount() {
return count;
}
/**
* Returns the TreeMap which contains the cues and their values. An
* important object on overall group result processing.
*
* @return the group's cues.
*/
public Map<String, Stimulus> getCuesMap() {
return cues;
}
/**
* @return the model
*/
public SimModel getModel() {
return model;
}
/**
* Returns the group's current name. By default shall be "Group n" where n
* is the position that has on the table.
*
* @return the name of the group.
*/
public String getNameOfGroup() {
return nameOfGroup;
}
/**
* @return the noOfPhases
*/
public int getNoOfPhases() {
return noOfPhases;
}
/**
* Returns the ArrayList which contains the SimPhases, the phases that run
* on this specific group.
*
* @return the group's phases.
*/
public List<SimPhase> getPhases() {
return phases;
}
/**
*
* @return a boolean indicating that this group has at least one random
* phase.
*/
public boolean hasRandom() {
return hasRandom;
}
/**
* Adds one more value to the count variable which represents the trials so
* far.
*/
public void nextCount() {
count++;
}
/**
* Returns the number of combinations that shall be run if the user has
* chosen a random sequence.
*
* @return the number of combinations.
*/
public int noOfCombin() {
return noOfCombinations;
}
/**
* @return the number of runs of the algorithm in this group
*/
public int numRandom() {
int count = 0;
for (SimPhase phase : phases) {
int increment = phase.isRandom() ? model.getCombinationNo() : 0;
increment = phase.getTimingConfig().hasVariableDurations() ? Math
.max(increment, 1) * model.getVariableCombinationNo()
: increment;
count += increment;
}
return count;
}
/**
* The Runnable's run method. This starts a new Thread. It actually runs
* every SimPhases.runSimulator() method which is the method that uses the
* formula on the phases.
*/
@Override
public void run() {
// Add to phasepool so we can still cancel them quickly if required
createDBs();
addMicrostimuli();
phasePool = ParallelArray.createEmpty(noOfPhases, SimPhase.class,
Simulator.fjPool);
phasePool.asList().addAll(phases);
int trialCount = 0;
for (int i = 0; i < noOfPhases; i++) {
trialCount += phases.get(i).getNoTrials();
phaseTrials[i] = trialCount;
}
for (int i = 0; i < noOfPhases; i++) {
if (model.contextAcrossPhase()) {
// Deal with different omega per phase
for (Entry<String, Stimulus> entry : cues.entrySet()) {
String realName = model.getConfigCuesNames().get(
entry.getKey());
realName = realName == null ? "" : realName;
}
}
if (i > 0) {
//phases.get(i).setDopamine(phases.get(i-1).getDopamine());
for (Stimulus s : phases.get(i).getCues().values()) {
for (Stimulus s2 : phases.get(i-1).getCues().values()) {
if (s.getName().equals(s2.getName())) {
for (StimulusElement se : s.getList()) {
for (StimulusElement se2 : s2.getList()) {
if (se.getMicroIndex() == se2.getMicroIndex()) {
se.setVariableSalience(se2.getVariableSalience());
se.setCSVariableSalience(se2.getCSVariableSalience());
}
}
}
}
}
}
}
phases.get(i).runSimulator();
}
}
public int[] getPhaseTrials() {return phaseTrials;}
public void setCommon(float common) {this.common = common;}
public float getCommon() {return common;}
public void addMicrostimuli () {
float currentCommon = 0;
float allCommon = 0;
numberShared = new TreeMap();
shorter = new TreeMap();
int shortest = 0;
String allString = "";
for (Stimulus s: cues.values()) {
shortest = Math.max(shortest,s.getAllMaxDuration());
}
for (Stimulus cl : cues.values()) {
cl.addMicrostimuli(esther);
}
for (Stimulus s: cues.values()) {
if (s.getMaxDuration() != 0 && s.isCS) {
if (!allString.equals("")) {allString += ", ";}
allString += s.getName();
shortest = (int) Math.min(s.getMaxDuration(),shortest);
}
}/*
if (sharedMap != null && sharedMap.containsKey(allString)) {
allCommon = sharedMap.get(allString).get("N/A");
}
else {
allCommon = common;
}
for (Stimulus s: cues.values()) {
for (Stimulus s2: cues.values()) {
if (s!= s2) {
if (sharedMap != null && sharedMap.containsKey(s.getName()) && sharedMap.get(s.getName()).containsKey(s2.getName())) {
currentCommon = (Float) sharedMap.get(s.getName()).get(s2.getName());
}
else {
currentCommon = common;
}
if (!s.isCS || !s2.isCS) {
s.setCommon(s2, 0f);
} else {
s.setCommon(s2, currentCommon);
}
float duration1 = s.getMaxDuration();
float duration2 = s2.getMaxDuration();
float smallest = Math.min(duration1, duration2);
if (!(numberShared.containsKey(s.getName()+", " + s2.getName()) || numberShared.containsKey(s2.getName()+", " + s.getName()))) {
numberShared.put(s.getName()+", " + s2.getName(), (int) Math.round(smallest*currentCommon));//sharedMap.get(s.getName()).get(s2.getName())));
shorter.put(s.getName()+", " + s2.getName(), (int) Math.round(smallest));
}
} else {
s.setCommon(s,1);
}
}
}
for (Stimulus s: cues.values()) {
for (Stimulus s2: cues.values()) {
if (s!= s2 && (s.isCS && s2.isCS)) {
ArrayList<Integer> sharedIndexes = new ArrayList();
int duration = 0;
int number = 0;
if (shorter.containsKey(s.getName() + ", " + s2.getName())) {
duration = shorter.get(s.getName() + ", " + s2.getName());
number = numberShared.get(s.getName() + ", " + s2.getName());
}
if (shorter.containsKey(s2.getName()+", "+s.getName())) {
duration = shorter.get(s2.getName() + ", " + s.getName());
number = numberShared.get(s2.getName() + ", " + s.getName());
}
int count = 0;
while (count < number) {
int nextRandom = (int) Math.round(Math.random()*(duration-1));
if (!sharedIndexes.contains(nextRandom)) {
//s2.getList()[nextRandom] = s.getList()[nextRandom];
sharedIndexes.add(nextRandom);
count++;
}
}
}
}
}
boolean commonToAll = true;
int allShared = (int) Math.round(shortest*allCommon);
int count = 0;
ArrayList<Integer> sharedIndexes = new ArrayList();
if (commonToAll && sharedMap != null) {
while (count < allShared) {
int nextRandom = (int) Math.round(Math.random()*(shortest-1));
if (!sharedIndexes.contains(nextRandom)) {
StimulusElement commonToAllElement = null;
while (commonToAllElement == null) {
for (Stimulus s: cues.values()) {
if (s.isCS) {
if (Math.random() > 0.9) {
commonToAllElement = s.getList()[nextRandom];
}
}
}
//System.out.println("loop simgroup");
}
sharedIndexes.add(nextRandom);
count++;
}
}
//
}*/
}
public void setControl(ModelControl control) {
for (SimPhase phase : phases) {
phase.setControl(control);
}
}
/**
* @param model
* the model to set
*/
public void setModel(SimModel model) {
this.model = model;
}
public void setEsther(boolean b) {
esther = b;
}
/**
* @param noOfPhases
* the noOfPhases to set
*/
public void setNoOfPhases(int noOfPhases) {
this.noOfPhases = noOfPhases;
}
public int trialCount() {
int count = 0;
for(SimPhase p : phases) {
int multiplier = p.isRandom() ? Simulator.getController().getModel().getCombinationNo() : 1;
multiplier *= p.getTimingConfig().hasVariableDurations() ? Simulator.getController().getModel().getVariableCombinationNo() : 1;
count += p.getTimingConfig().getTrials() * multiplier;
}
return count;
}
public void clearMap(String map) {
maps.get(map).clear();
}
public void removeMap(String map) {
//dbDisk.
//dbDisk.
//disks.get(map).clear();
//maps.get(map).clear();
maps.remove(map); disks.remove(map); cache.remove(map);}
public TreeMap<String,HTreeMap> getMaps() {return maps;}
public int getCombinationNo() {
return Simulator.getController().getModel().getCombinationNo();
}
public ArrayList<StimulusElement> getSimCues() {
return simCues;
}
public boolean getEsther() {return esther;}
public void setSharedElements(TreeMap<String,TreeMap<String,Float>> data) {
for (String s : data.keySet()) {
TreeMap<String,Float> tm = data.get(s);
TreeMap<String,Float> tm2 = new TreeMap();
for (String s2 : tm.keySet()) {
tm2.put(s2 + "", tm.get(s2) + 0f);
}
if (sharedMap == null) {
sharedMap = new TreeMap();
hasSet = 1;}
sharedMap.put(s,tm2);
}
}
public void setTotalTrials(int trials) {totalTrials = trials;}
public int getTotalTrials() {return totalTrials;}
public void setTotalStimuli(int stimuli) {totalStimuli = stimuli;}
public int getTotalStimuli() {return totalStimuli;}
public void initializeDBKeys() {
maxSessions = 1;
totalKeySet = new ArrayList();
for (int i = 0; i < noOfPhases; i++) {
maxSessions = Math.max(maxSessions, model.getGroupSession(nameOfGroup, i+1));
for (int j = 0; j < getPhases().get(i).getOrderedSeq().size(); j++) {
String s = getPhases().get(i).getOrderedSeq().get(j).toString();
if (!totalKeySet.contains(s)) { totalKeySet.add(s);}
}
}
trialTypes = totalKeySet.size();
bytes1 = getC(totalStimuli*totalStimuli*noOfPhases);
s1 = bytes1;
bytes2 = getC(maxMicros);
s2 = bytes1+bytes2+1;
bytes3 = getC(totalTrials);
s3 = bytes1+bytes2+bytes3+2;
bytes4 = getC(maxDuration);
s4 = bytes1+bytes2+bytes3+bytes4+3;
bytes5 = getC(maxSessions*trialTypes);
s5 = bytes1+bytes2+bytes3+bytes4+bytes5+4;
//System.out.println(s5);
/*System.out.println("stims: " + stims + " // " + getC(stims));
System.out.println("micros: " + micros + " // " + getC(micros));
System.out.println("trials: " + trials + " // " + getC(trials));
System.out.println("timepoints: " + timepoints + " // " + getC(timepoints));
System.out.println("phases: " + phases + " // " + getC(phases));
System.out.println("maxSessions: " + maxSessions + " // " + getC(maxSessions));
System.out.println("trialTypes: " + trialTypes + " // " + getC(trialTypes));*/
}
public int getC(int val) {
if (val == 1 || val == 0) {return 1;}
else return (int) Math.ceil(Math.log(val)/Math.log(2));