-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulatedAnnealing.c
1216 lines (1152 loc) · 33.6 KB
/
simulatedAnnealing.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
/*
Random number library function
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
*/
#include "mt19937ar.h"
#ifdef _MSC_VER
#define C_DIR_DELIMITER '\\'
#include <direct.h>
#include <process.h>
#else
#define C_DIR_DELIMITER '/'
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
/*
Stop Visual C++ from warning about thread safety when asked to compile idiomatic ANSI
*/
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
#define _MAX_STATIC_BUFF_LEN 1024
#define MY_REALLOC_BLOCK_SIZE 1024
#define BRK_ENDLINE "\r\n"
#define TOK_WHITESPACE " \t,"
#define _MY_TINY_EPS 1e-10
#define _SCREEN_PRINT_STEP 100
#define OUT_DIR "samplingPattern"
#define HOST_INFO_FILE "activeLandscape.txt" /* This file is created by the landscape scale model when it runs */
#define DEBUG_DUMP_INFO 0 /* Whether (1) or not (0) to dump information at the end to check the calculation */
#define PARAM_OBJ_FUNC_TYPE 0 /*
= 0 means average of detection probabilities over runs
= 1 means simulate and count up number of successes
= 2 means maximise average of expected detections/run
*/
/*
Global variables storing global configuration options
*/
char INPUT_DIR[_MAX_STATIC_BUFF_LEN];
char SIM_OUTPUT_STUB[_MAX_STATIC_BUFF_LEN];
char OBJ_FUNC_OUT[_MAX_STATIC_BUFF_LEN];
int NUM_RUNS;
int PARAM_N;
int PARAM_n;
double PARAM_WITHIN_CELL_R;
double PARAM_WITHIN_CELL_S0;
int PARAM_TRUE_MIN_FLAG;
double TEST_SENS;
double DET_LAG;
double PARAM_DELTA;
double PARAM_COOL;
double PARAM_ALPHA;
int SIMANN_N;
int B_ALLOW_DUPLICATES;
typedef struct
{
int hostID;
int hostX;
int hostY;
double hostDensity;
} t_HostInfo;
typedef struct
{
int hostID;
int hostPos;
} t_HostLookup;
typedef struct
{
double maxTimeInf;
int numInf;
t_HostLookup *aHostLookup;
double *aTimeInf;
double *aHostDensity;
double *aPDetect;
} t_RunInfo;
typedef struct
{
int hostID;
int numSims;
} t_InfInfo;
typedef struct
{
int numRuns;
t_RunInfo *aRunInfo; /* this stores times of infection and p(detect) for infected hosts in the individual runs */
int numHosts;
t_HostInfo *aHostInfo; /* this stores the information on location of hosts */
int numInf;
t_InfInfo *aInfInfo; /* this stores information on how frequently hosts infected (used to avoid ever choosing non-infected hosts) */
} t_SSAInfo;
/*
Utility functions for reading configuration options
*/
int getCfgFileName(char *szProgName, char *szCfgFile)
{
/*
Work out configuration file name from that of the executable
and check whether it exists by attempting to read it
*/
char *pPtr;
FILE *fp;
szCfgFile[0] = '\0';
{
if ((pPtr = strrchr(szProgName, C_DIR_DELIMITER)) != NULL)
{
strcpy(szCfgFile, pPtr + 1);
}
else
{
strcpy(szCfgFile, szProgName);
}
if ((pPtr = strstr(szCfgFile, ".exe")) != NULL)
{
*pPtr = '\0';
}
strcat(szCfgFile, ".cfg");
}
/* check file exists */
fp = fopen(szCfgFile, "rb");
if (fp)
{
fclose(fp);
return 1;
}
return 0;
}
/*
Following set of routines find values of parameters from the command line options,
or, failing that, from the cfg file
*/
int findKey(int argc, char **argv, char*szCfgFile, char *szKey, char *szValue)
{
char *pVal;
int bRet, i;
FILE *fp;
char *pThisPair;
char *szArgvCopy;
bRet = 0;
i = 0;
/* try to find the relevant key on the command line */
while (bRet == 0 && i<argc)
{
szArgvCopy = strdup(argv[i]);
if (szArgvCopy)
{
pThisPair = strtok(szArgvCopy, " \t");
while (pThisPair)
{
if (strncmp(pThisPair, szKey, strlen(szKey)) == 0)
{
pVal = strchr(pThisPair, '=');
if (pVal)
{
/* make sure isn't just start of string matching the key */
if (pThisPair[strlen(szKey)] == '=')
{
strcpy(szValue, pVal + 1);
fprintf(stdout, "extracted %s->%s from command line\n", szKey, szValue);
bRet = 1;
}
}
}
pThisPair = strtok(NULL, " \t");
}
free(szArgvCopy);
}
i++;
}
/* otherwise, look in the cfg file */
if (bRet == 0)
{
fp = fopen(szCfgFile, "rb");
if (fp)
{
char szLine[_MAX_STATIC_BUFF_LEN];
while (!bRet && fgets(szLine, _MAX_STATIC_BUFF_LEN, fp))
{
char *pPtr;
if ((pPtr = strchr(szLine, '=')) != NULL)
{
*pPtr = '\0';
if (strcmp(szKey, szLine) == 0)
{
strcpy(szValue, pPtr + 1);
/* strip off newline (if any) */
if ((pPtr = strpbrk(szValue, "\r\n")) != NULL)
*pPtr = '\0';
bRet = 1;
}
}
}
fclose(fp);
}
}
return bRet;
}
int readStringFromCfg(int argc, char **argv, char *szCfgFile, char *szKey, char *szValue)
{
return(findKey(argc, argv, szCfgFile, szKey, szValue));
}
int readDoubleFromCfg(int argc, char **argv, char *szCfgFile, char *szKey, double *pdValue)
{
char szValue[_MAX_STATIC_BUFF_LEN];
if (findKey(argc, argv, szCfgFile, szKey, szValue))
{
*pdValue = atof(szValue);
return 1;
}
return 0;
}
int readIntFromCfg(int argc, char **argv, char *szCfgFile, char *szKey, int *pnValue)
{
char szValue[_MAX_STATIC_BUFF_LEN];
if (findKey(argc, argv, szCfgFile, szKey, szValue))
{
*pnValue = atoi(szValue);
return 1;
}
return 0;
}
/*
Return uniform random number between 0 and 1
Encapsulated to allow easy replacement if necessary
*/
double uniformRandom()
{
#if 0
int nRet;
nRet = RAND_MAX;
while(nRet == RAND_MAX || nRet == 0)
{
nRet = rand();
}
return ((double)nRet/(double)(RAND_MAX));
#else
return genrand_real3();
#endif
}
/*
seed random number generator
encapsulated to allow easy replacement if necessary
*/
void seedRandom()
{
unsigned long ulnSeed;
unsigned long myPID;
ulnSeed =(unsigned long) time(NULL);
#ifndef _WIN32 /* make sure different processes started at same time have different seeds */
myPID = (unsigned long) getpid();
#else
myPID = (unsigned long) _getpid();
#endif
ulnSeed += myPID;
#if 0
srand((unsigned int)ulnSeed);
#else
init_genrand(ulnSeed);
#endif
}
int readHostInfo(t_SSAInfo *pSSAInfo)
{
int bRet;
FILE *fIn;
char szBuff[_MAX_STATIC_BUFF_LEN];
char szHostInf[_MAX_STATIC_BUFF_LEN];
char *pPtr;
int thisTok;
int numAlloc;
bRet = 0;
sprintf(szHostInf, "%s%s", INPUT_DIR, HOST_INFO_FILE);
fIn = fopen(szHostInf, "rb");
if(fIn)
{
bRet = 1;
numAlloc = 0;
while(bRet && fgets(szBuff, _MAX_STATIC_BUFF_LEN, fIn))
{
if(pSSAInfo->numHosts == numAlloc)
{
numAlloc += MY_REALLOC_BLOCK_SIZE;
pSSAInfo->aHostInfo = realloc(pSSAInfo->aHostInfo, sizeof(*pSSAInfo->aHostInfo) * numAlloc);
if(!pSSAInfo->aHostInfo)
{
bRet = 0;
}
}
if(bRet)
{
pSSAInfo->aHostInfo[pSSAInfo->numHosts].hostID = pSSAInfo->numHosts;
thisTok = 0;
pPtr = strpbrk(szBuff, BRK_ENDLINE);
if(*pPtr)
{
*pPtr = '\0';
}
pPtr = strtok(szBuff, TOK_WHITESPACE);
while(bRet && pPtr)
{
switch(thisTok)
{
case 0:
/* x */
pSSAInfo->aHostInfo[pSSAInfo->numHosts].hostX = atoi(pPtr);
break;
case 1:
/* y */
pSSAInfo->aHostInfo[pSSAInfo->numHosts].hostY = atoi(pPtr);
break;
case 2:
/* density */
pSSAInfo->aHostInfo[pSSAInfo->numHosts].hostDensity = atof(pPtr);
break;
case 3:
if(pSSAInfo->numHosts != atoi(pPtr))
{
fprintf(stderr, "host mismatch when parsing landscape\n");
bRet = 0;
}
break;
default:
break;
}
pPtr = strtok(NULL, TOK_WHITESPACE);
thisTok++;
}
}
pSSAInfo->numHosts++;
}
fclose(fIn);
if(bRet && pSSAInfo->numHosts)
{
fprintf(stdout, "readHostInfo():\n\tread %d hosts\n", pSSAInfo->numHosts);
}
else
{
bRet = 0;
fprintf(stderr, "couldn't find hosts\n");
}
}
else
{
fprintf(stderr, "couldn't read host info file (%s)\n", szHostInf);
}
return bRet;
}
static int cmpHostLookup(const void *p1, const void *p2)
{
t_HostLookup *pHL1 = (t_HostLookup *)p1;
t_HostLookup *pHL2 = (t_HostLookup *)p2;
return pHL1->hostID - pHL2->hostID;
}
static int cmpInfInfo(const void *p1, const void *p2)
{
t_InfInfo *pHL1 = (t_InfInfo *)p1;
t_InfInfo *pHL2 = (t_InfInfo *)p2;
return pHL1->hostID - pHL2->hostID;
}
int readSims(t_SSAInfo *pSSAInfo)
{
int bRet;
int i;
char szInputFile[_MAX_STATIC_BUFF_LEN];
char szEndTimeFile[_MAX_STATIC_BUFF_LEN];
FILE *fIn,*fEnd;
char szBuffer[_MAX_STATIC_BUFF_LEN];
char *pPtr;
int thisTok;
int numAlloc;
int hostID;
double readMaxTime;
int everInfAlloc;
int infFrom,infTo;
fprintf(stdout, "readSims()\n");
everInfAlloc = 0;
bRet = 1;
pSSAInfo->numRuns = NUM_RUNS;
/* Infer number of runs from the input files themselves */
if (pSSAInfo->numRuns < 0)
{
int foundGoodFile,lastRunNumber;
fprintf(stdout, "Inferring number of runs\n");
pSSAInfo->numRuns = 0;
do
{
foundGoodFile = 0;
sprintf(szInputFile, "%s%s_%d.txt", INPUT_DIR, SIM_OUTPUT_STUB, pSSAInfo->numRuns);
if (fIn = fopen(szInputFile, "rb"))
{
fprintf(stdout, "\tFound input %s\n", szInputFile);
foundGoodFile = 1;
fclose(fIn);
pSSAInfo->numRuns++;
}
} while (foundGoodFile);
fprintf(stdout, "\tInferred %d input files\n", pSSAInfo->numRuns);
/* Check that this is correct based on lastRunNumber file */
sprintf(szInputFile, "%slastRunNumber.txt", INPUT_DIR);
fIn = fopen(szInputFile, "rb");
lastRunNumber = -1;
if (fIn)
{
fgets(szBuffer, _MAX_STATIC_BUFF_LEN, fIn);
lastRunNumber = atoi(szBuffer);
fclose(fIn);
}
if (pSSAInfo->numRuns == lastRunNumber)
{
fprintf(stderr, "\t\tCorrectly...");
}
else
{
fprintf(stderr, "\t\tIncorrect number inferred...exiting");
pSSAInfo->numRuns = 0;
}
}
if (pSSAInfo->numRuns)
{
pSSAInfo->aRunInfo = malloc(sizeof(*pSSAInfo->aRunInfo)*pSSAInfo->numRuns);
if (pSSAInfo->aRunInfo)
{
for (i = 0; bRet && i < pSSAInfo->numRuns; i++)
{
sprintf(szInputFile, "%s%s_%d.txt", INPUT_DIR, SIM_OUTPUT_STUB, i);
fprintf(stdout, "\t%s_%d\n", SIM_OUTPUT_STUB, i);
fIn = fopen(szInputFile, "rb");
if (fIn)
{
pSSAInfo->aRunInfo[i].numInf = 0;
pSSAInfo->aRunInfo[i].aHostLookup = NULL;
pSSAInfo->aRunInfo[i].aPDetect = pSSAInfo->aRunInfo[i].aTimeInf = pSSAInfo->aRunInfo[i].aHostDensity = NULL;
numAlloc = 0;
while (bRet && fgets(szBuffer, _MAX_STATIC_BUFF_LEN, fIn))
{
if (pSSAInfo->aRunInfo[i].numInf == numAlloc)
{
numAlloc += MY_REALLOC_BLOCK_SIZE;
pSSAInfo->aRunInfo[i].aHostLookup = realloc(pSSAInfo->aRunInfo[i].aHostLookup, sizeof(*pSSAInfo->aRunInfo[i].aHostLookup) * numAlloc);
pSSAInfo->aRunInfo[i].aPDetect = realloc(pSSAInfo->aRunInfo[i].aPDetect, sizeof(*pSSAInfo->aRunInfo[i].aPDetect) * numAlloc);
pSSAInfo->aRunInfo[i].aTimeInf = realloc(pSSAInfo->aRunInfo[i].aTimeInf, sizeof(*pSSAInfo->aRunInfo[i].aTimeInf) * numAlloc);
pSSAInfo->aRunInfo[i].aHostDensity = realloc(pSSAInfo->aRunInfo[i].aHostDensity, sizeof(*pSSAInfo->aRunInfo[i].aHostDensity) * numAlloc);
if (!(pSSAInfo->aHostInfo && pSSAInfo->aRunInfo[i].aPDetect && pSSAInfo->aRunInfo[i].aTimeInf && pSSAInfo->aRunInfo[i].aHostDensity))
{
bRet = 0;
}
}
if (bRet)
{
thisTok = 0;
pPtr = strpbrk(szBuffer, BRK_ENDLINE);
if (*pPtr)
{
*pPtr = '\0';
}
pPtr = strtok(szBuffer, TOK_WHITESPACE);
while (pPtr)
{
switch (thisTok)
{
case 12:
hostID = atoi(pPtr);
pSSAInfo->aRunInfo[i].aHostLookup[pSSAInfo->aRunInfo[i].numInf].hostID = hostID;
pSSAInfo->aRunInfo[i].aHostLookup[pSSAInfo->aRunInfo[i].numInf].hostPos = pSSAInfo->aRunInfo[i].numInf;
if (pSSAInfo->numInf == everInfAlloc)
{
everInfAlloc += MY_REALLOC_BLOCK_SIZE;
pSSAInfo->aInfInfo = realloc(pSSAInfo->aInfInfo, sizeof(*pSSAInfo->aInfInfo)*everInfAlloc);
if (!pSSAInfo->aInfInfo)
{
bRet = 0;
}
}
if (bRet)
{
pSSAInfo->aInfInfo[pSSAInfo->numInf].hostID = hostID;
pSSAInfo->numInf++;
}
break;
case 2:
/* time */
pSSAInfo->aRunInfo[i].aTimeInf[pSSAInfo->aRunInfo[i].numInf] = atof(pPtr);
break;
case 6:
/* host density */
pSSAInfo->aRunInfo[i].aHostDensity[pSSAInfo->aRunInfo[i].numInf] = atof(pPtr);
break;
default:
break;
}
thisTok++;
pPtr = strtok(NULL, TOK_WHITESPACE);
}
}
pSSAInfo->aRunInfo[i].numInf++;
}
fclose(fIn);
sprintf(szEndTimeFile, "%sendTime_%d.txt", INPUT_DIR, i);
readMaxTime = 0.0;
fEnd = fopen(szEndTimeFile, "rb");
if (fEnd)
{
fgets(szBuffer, _MAX_STATIC_BUFF_LEN, fEnd);
readMaxTime = atof(szBuffer);
pSSAInfo->aRunInfo[i].maxTimeInf = readMaxTime;
fprintf(stdout, "\t\tread %d infections (maxTime=%.4f)\n", pSSAInfo->aRunInfo[i].numInf, pSSAInfo->aRunInfo[i].maxTimeInf);
qsort(pSSAInfo->aRunInfo[i].aHostLookup, pSSAInfo->aRunInfo[i].numInf, sizeof(*pSSAInfo->aRunInfo[i].aHostLookup), cmpHostLookup);
fclose(fEnd);
}
else
{
bRet = 0;
fprintf(stderr, "couldn't read %s\n", szEndTimeFile);
}
}
else
{
bRet = 0;
fprintf(stderr, "couldn't open %s\n", szInputFile);
}
}
if (bRet)
{
fprintf(stdout, "\t%d hosts infected in total\n", pSSAInfo->numInf);
qsort(pSSAInfo->aInfInfo, pSSAInfo->numInf, sizeof(*pSSAInfo->aInfInfo), cmpInfInfo);
pSSAInfo->aInfInfo[0].numSims = 1;
infTo = 0;
for (infFrom = 1; infFrom < pSSAInfo->numInf; infFrom++)
{
if (pSSAInfo->aInfInfo[infTo].hostID == pSSAInfo->aInfInfo[infFrom].hostID)
{
pSSAInfo->aInfInfo[infTo].numSims++;
}
else
{
infTo++;
pSSAInfo->aInfInfo[infTo].hostID = pSSAInfo->aInfInfo[infFrom].hostID;
pSSAInfo->aInfInfo[infTo].numSims = 1;
}
}
pSSAInfo->numInf = infTo + 1;
fprintf(stdout, "\t%d unique hosts infected\n", pSSAInfo->numInf);
}
}
else
{
bRet = 0;
}
}
else
{
bRet = 0;
}
return bRet;
}
/*
Handle build up of detectability
sigma(t) = 1./(1+J.*exp(-r.*t));
where
- t = time since first infection of the cell
- sigma(t) = detectability of cell at time t
- r = (logistic growth) rate at which infectivity increases
- J = (1-w0)/w0;
- detLag = period within which detection is not possible following infection
*/
double detectProbSingleSurvey(double timeSinceInf, int numSamples, double dHostDensity)
{
/*
use 1-p(do not detect)
*/
double pDetectSingleSample;
double J;
double thisWCM;
if (PARAM_TRUE_MIN_FLAG)
{
if (PARAM_WITHIN_CELL_S0 >= dHostDensity)
{
J = 0.0;
}
else
{
thisWCM = PARAM_WITHIN_CELL_S0 / dHostDensity;
J = (1.0 - thisWCM) / thisWCM;
}
}
else
{
J = (1.0 - PARAM_WITHIN_CELL_S0) / PARAM_WITHIN_CELL_S0;
}
if (timeSinceInf < DET_LAG)
{
pDetectSingleSample = 0.0;
}
else
{
pDetectSingleSample = TEST_SENS * (1.0 / (1.0 + J * exp(-PARAM_WITHIN_CELL_R * (timeSinceInf - DET_LAG))));
}
/*
Probability of one or more detections (given numSamples taken)
*/
return 1.0 - pow(1.0 - pDetectSingleSample, numSamples);
}
int calcProbDetect(t_SSAInfo *pSSAInfo)
{
int numToAverage,bRet,i,j,k,numSurveys;
double timeSurvey,timeInf,pDetect,pDontDetect,pDetectThisTime,firstOffset,hostDensity;
fprintf(stdout, "calcProbDetect()\n");
bRet = 1;
for(i=0;i<pSSAInfo->numRuns;i++)
{
fprintf(stdout, "\tdoing simulation %d\n", i);
numSurveys = (int)((pSSAInfo->aRunInfo[i].maxTimeInf+_MY_TINY_EPS)/PARAM_DELTA) + 1;
for(j=0;j<pSSAInfo->aRunInfo[i].numInf;j++)
{
timeInf = pSSAInfo->aRunInfo[i].aTimeInf[j];
hostDensity = pSSAInfo->aRunInfo[i].aHostDensity[j];
pDetect = 0.0;
numToAverage = 0;
firstOffset = 0.0;
/*
This loops over the different times surveying could start relative to time of first infection (step of one day)
*/
while(firstOffset < PARAM_DELTA)
{
#ifdef _EXHAUSTIVE_SURVEY_DIAGNOSTICS
fprintf(stdout, "****\n");
fprintf(stdout, "surveyOffset=%.4f for host number %d to be infected (hostID=%d,firstInf=%.4f) in simulation %d (maxTimeInf=%.4f)\n",firstOffset,j, pSSAInfo->aRunInfo[i].aHostLookup[j].hostID, timeInf, i, pSSAInfo->aRunInfo[i].maxTimeInf);
fprintf(stdout, "****\n");
#endif
pDontDetect = 1.0;
for(k=0;k<numSurveys;k++)
{
timeSurvey = firstOffset + k * PARAM_DELTA;
/*
Avoid doing any sample that is actually too late
*/
if(timeSurvey < timeInf)
{
pDetectThisTime = 0.0;
#ifdef _EXHAUSTIVE_SURVEY_DIAGNOSTICS
fprintf(stdout, "do survey at %f (ignored in calculation since before first infection of this cell at %.4f)\n", timeSurvey, timeInf);
#endif
}
else
{
if(timeSurvey > pSSAInfo->aRunInfo[i].maxTimeInf)
{
pDetectThisTime = 0.0;
#ifdef _EXHAUSTIVE_SURVEY_DIAGNOSTICS
fprintf(stdout, "do survey at %f (ignored in calculation since after maximum incidence reached at %.4f)\n", timeSurvey, pSSAInfo->aRunInfo[i].maxTimeInf);
#endif
}
else
{
pDetectThisTime = detectProbSingleSurvey(timeSurvey - timeInf, PARAM_n, hostDensity);
#ifdef _EXHAUSTIVE_SURVEY_DIAGNOSTICS
fprintf(stdout, "do survey at %f\n", timeSurvey);
#endif
}
}
pDontDetect *= (1.0-pDetectThisTime);
}
pDetect += (1.0-pDontDetect);
firstOffset+=(1.0/365.0);
numToAverage++;
}
pDetect /= numToAverage;
pSSAInfo->aRunInfo[i].aPDetect[j] = pDetect;
}
}
return bRet;
}
double calcObjFunction(t_SSAInfo *pSSAInfo, int numToSurvey, int *anHostID)
{
int thisHost,i,j;
double randomDraw,objFunc,pNotDetectOverall,pDetectOnThisHost,pDetectFromThisPatternInThisRun;
double expectedFindsThisRun; /* Calculating the expected maximum number of finds before the disease reaches a certain incidence */
t_HostLookup sLookup,*pFound;
objFunc = 0.0;
for(i=0; i < pSSAInfo->numRuns;i++)
{
expectedFindsThisRun = 0.0;
pNotDetectOverall = 1.0;
for(j=0;j<numToSurvey;j++)
{
thisHost = anHostID[j];
sLookup.hostID = thisHost;
pFound = bsearch(&sLookup,pSSAInfo->aRunInfo[i].aHostLookup, pSSAInfo->aRunInfo[i].numInf, sizeof(*pSSAInfo->aRunInfo[i].aHostLookup), cmpHostLookup);
if(pFound)
{
pDetectOnThisHost = pSSAInfo->aRunInfo[i].aPDetect[pFound->hostPos];
}
else
{
pDetectOnThisHost = 0.0;
}
pNotDetectOverall *= (1.0 - pDetectOnThisHost); /*PRODUCT OF NO DETECTION PROBABILITIES*/
expectedFindsThisRun += pDetectOnThisHost; /* ADDS UP EXPECTED NUMBER OF FINDS */
}
pDetectFromThisPatternInThisRun = 1 - pNotDetectOverall;
switch(PARAM_OBJ_FUNC_TYPE)
{
case 0:
objFunc += pDetectFromThisPatternInThisRun; /*THIS CALCULATES THE SUM OF THE PROBS OF DETECTION WHEN ALL J SAMPLED, SUMMED OVER ALL RUNS*/
break;
case 1:
randomDraw = uniformRandom();
if(randomDraw < pDetectFromThisPatternInThisRun)
{
objFunc++; /*THIS CALCULATES SUM OF DETECTIONS OR NOT WHEN ALL J SAMPLED, SUMMED OVER ALL RUNS*/
}
break;
case 2:
objFunc += expectedFindsThisRun;
break;
default:
fprintf(stderr, "Not implemented\n");
}
} /*DOES THIS FOR ALL I RUNS*/
objFunc /= (double)pSSAInfo->numRuns; /*THIS TAKES THE MEAN OF WHATEVER FORM THE OF TAKES AS SPECIFIED ABOVE*/
return objFunc;
}
/*
Will only ever find hosts not already being sampled
*/
int randomValidHost(t_SSAInfo *pSSAInfo, int bAllowDups, int numToCheck, int *anPatternToCheck, int newPos)
{
int hostIndex,bHostOK,i;
bHostOK = 0;
do
{
hostIndex = (int)(uniformRandom()*(double)pSSAInfo->numInf);
bHostOK = 1;
/* if host is already the pattern, flag it */
for (i = 0; i < numToCheck; i++)
{
if (pSSAInfo->aInfInfo[hostIndex].hostID == anPatternToCheck[i])
{
/*
fprintf(stdout, "\tChoice of %d for element %d of new pattern clashes with element %d of current pattern [%d]...rechoosing\n", pSSAInfo->aInfInfo[hostIndex].hostID, newPos, i, anPatternToCheck[i]);
*/
bHostOK = 0;
}
}
} while (bHostOK == 0 && bAllowDups == 0);
return pSSAInfo->aInfInfo[hostIndex].hostID;
}
void debugDumpInfo(t_SSAInfo *pSSAInfo)
{
int i,j;
FILE *fOut;
char szOutFile[_MAX_STATIC_BUFF_LEN];
fprintf(stdout, "debugDumpInfo():\n");
sprintf(szOutFile, "%s//debug_HostInfo.txt", OUT_DIR);
fOut = fopen(szOutFile, "wb");
if(!fOut)
{
fprintf(stderr, "couldn't open %s", szOutFile);
return;
}
for(i=0;i<pSSAInfo->numHosts;i++)
{
fprintf(fOut, "%d %d %d %f\n", pSSAInfo->aHostInfo[i].hostID, pSSAInfo->aHostInfo[i].hostX, pSSAInfo->aHostInfo[i].hostY, pSSAInfo->aHostInfo[i].hostDensity);
}
fclose(fOut);
sprintf(szOutFile, "%s//debug_UniqInf.txt", OUT_DIR);
fOut = fopen(szOutFile, "wb");
if(!fOut)
{
fprintf(stderr, "couldn't open %s", szOutFile);
return;
}
for(i=0;i<pSSAInfo->numInf;i++)
{
fprintf(fOut, "%d %d\n", pSSAInfo->aInfInfo[i].hostID, pSSAInfo->aInfInfo[i].numSims);
}
fclose(fOut);
/* Debug files: jused to check that simulation data is being read in processed correctly */
/* One of these is created for each simulation, and the number of lines is equal to the total number of infections */
for(j=0;j<pSSAInfo->numRuns;j++)
{
sprintf(szOutFile, "%s//debug_PDetect_%d.txt", OUT_DIR, j);
fOut = fopen(szOutFile, "wb");
if(!fOut)
{
fprintf(stderr, "couldn't open %s", szOutFile);
return;
}
for(i=0;i<pSSAInfo->aRunInfo[j].numInf;i++)
{
fprintf(fOut, "%d %d %d %d %f %f %f\n", /* Output is "in a sort of random order" */
pSSAInfo->aRunInfo[j].aHostLookup[i].hostPos, /* Order of infection in simulation */
pSSAInfo->aRunInfo[j].aHostLookup[i].hostID, /* Host ID */
pSSAInfo->aHostInfo[pSSAInfo->aRunInfo[j].aHostLookup[i].hostID].hostX, /* Raster column ID */
pSSAInfo->aHostInfo[pSSAInfo->aRunInfo[j].aHostLookup[i].hostID].hostY, /* Raster row ID */
pSSAInfo->aRunInfo[j].aTimeInf[pSSAInfo->aRunInfo[j].aHostLookup[i].hostPos], /* Time of first infection */
pSSAInfo->aRunInfo[j].aHostDensity[pSSAInfo->aRunInfo[j].aHostLookup[i].hostPos], /* Host density */
pSSAInfo->aRunInfo[j].aPDetect[pSSAInfo->aRunInfo[j].aHostLookup[i].hostPos]); /* Probability of detection */
}
fclose(fOut);
}
}
int readParams(int argc, char **argv)
{
int bRet = 1;
char szCfgFile[_MAX_STATIC_BUFF_LEN];
fprintf(stdout, "readParams()\n");
if (!getCfgFileName(argv[0], szCfgFile))
{
fprintf(stderr, "Couldn't find cfg file for program name '%s'\n", argv[0]);
return 0;
}
if (!readStringFromCfg(argc, argv, szCfgFile, "inputDirectory", INPUT_DIR))
{
fprintf(stdout, "Couldn't read inputDirectory\n");
return 0;
}
if (!readStringFromCfg(argc, argv, szCfgFile, "outStub", SIM_OUTPUT_STUB))
{
fprintf(stdout, "Couldn't read outStub\n");
return 0;
}
if (!readIntFromCfg(argc, argv, szCfgFile, "numIts", &NUM_RUNS))
{
fprintf(stdout, "Couldn't read numIts\n");
return 0;
}
if (!readIntFromCfg(argc, argv, szCfgFile, "allowDuplicates", &B_ALLOW_DUPLICATES))
{
fprintf(stdout, "Couldn't read allowDuplicates\n");
return 0;
}
if (!readIntFromCfg(argc, argv, szCfgFile, "numSites", &PARAM_N))
{
fprintf(stdout, "Couldn't read numSites\n");
return 0;
}
if (!readIntFromCfg(argc, argv, szCfgFile, "samplesPerSite", &PARAM_n))
{
fprintf(stdout, "Couldn't read samplesPerSite\n");
return 0;
}
if (!readDoubleFromCfg(argc, argv, szCfgFile, "withinCellBulkUp", &PARAM_WITHIN_CELL_R))
{
fprintf(stdout, "Couldn't read withinCellBulkUp\n");
return 0;
}
if (!readDoubleFromCfg(argc, argv, szCfgFile, "withinCellMin", &PARAM_WITHIN_CELL_S0))
{
fprintf(stdout, "Couldn't read withinCellMin\n");
return 0;
}
if (!readDoubleFromCfg(argc, argv, szCfgFile, "testSens", &TEST_SENS))
{
fprintf(stdout, "Couldn't read testSens\n");
return 0;
}
if (!readDoubleFromCfg(argc, argv, szCfgFile, "detLag", &DET_LAG))
{
fprintf(stdout, "Couldn't read detLag\n");
return 0;
}
if (!readDoubleFromCfg(argc, argv, szCfgFile, "delta", &PARAM_DELTA))
{
fprintf(stdout, "Couldn't read delta\n");
return 0;
}
if (!readDoubleFromCfg(argc, argv, szCfgFile, "cool", &PARAM_COOL))
{
fprintf(stdout, "Couldn't read cool\n");
return 0;
}
if (!readDoubleFromCfg(argc, argv, szCfgFile, "alpha", &PARAM_ALPHA))
{
fprintf(stdout, "Couldn't read alpha\n");
return 0;
}
if (!readIntFromCfg(argc, argv, szCfgFile, "simann_n", &SIMANN_N))
{
fprintf(stdout, "Couldn't read simann_n\n");
return 0;
}
if (!readStringFromCfg(argc, argv, szCfgFile, "objFuncOut", OBJ_FUNC_OUT))
{
fprintf(stdout, "Couldn't read objFuncOut\n");
return 0;
}
if (!readIntFromCfg(argc, argv, szCfgFile, "trueMinFlag", &PARAM_TRUE_MIN_FLAG))
{
fprintf(stdout, "Couldn't read trueMinFlag\n");
return 0;
}
if (bRet)
{
/* dump out parameters as read and understood by the programme */
FILE* paramsOut;
char outFile[_MAX_STATIC_BUFF_LEN];
sprintf(outFile, "%s%c%s_paramsOut.txt", OUT_DIR, C_DIR_DELIMITER, SIM_OUTPUT_STUB);
fprintf(stdout, "\twriting copy of parameters to %s\n", outFile);
paramsOut = fopen(outFile, "wb");
if (paramsOut)
{
fprintf(paramsOut, "inputDirectory=%s\n", INPUT_DIR);
fprintf(paramsOut, "outStub=%s\n", SIM_OUTPUT_STUB);
fprintf(paramsOut, "numIts=%d\n", NUM_RUNS);
fprintf(paramsOut, "allowDuplicates=%d\n", B_ALLOW_DUPLICATES);
fprintf(paramsOut, "numSites=%d\n", PARAM_N);
fprintf(paramsOut, "samplesPerSite=%d\n", PARAM_n);
fprintf(paramsOut, "withinCellBulkUp=%f\n", PARAM_WITHIN_CELL_R);
fprintf(paramsOut, "withinCellMin=%f\n", PARAM_WITHIN_CELL_S0);
fprintf(paramsOut, "testSens=%f\n", TEST_SENS);
fprintf(paramsOut, "detLag=%f\n", DET_LAG);
fprintf(paramsOut, "delta=%f\n", PARAM_DELTA);
fprintf(paramsOut, "cool=%f\n", PARAM_COOL);
fprintf(paramsOut, "alpha=%f\n", PARAM_ALPHA);
fprintf(paramsOut, "simann_n=%d\n", SIMANN_N);
fprintf(paramsOut, "objFuncOut=%s\n", OBJ_FUNC_OUT);
fprintf(paramsOut, "trueMinFlag=%d\n", PARAM_TRUE_MIN_FLAG);
fclose(paramsOut);
}
else
{
fprintf(stderr, "couldn't dump parameters...exiting\n");
return 0;
}
}
return bRet;
}
int main(int argc, char **argv)
{
t_SSAInfo sSSAInfo;
int bContinue;
seedRandom();
memset(&sSSAInfo,0,sizeof(sSSAInfo));
#ifdef _MSC_VER
if(!mkdir(OUT_DIR))
#else
if(!mkdir(OUT_DIR, 0777))
#endif
{
fprintf(stdout, "created directory %s for output\n", OUT_DIR);
}