forked from Marxan-source-code/marxan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.cpp
1338 lines (1107 loc) · 50 KB
/
output.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
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include "output.hpp"
#include "computation.hpp"
#include "defines.hpp"
namespace marxan {
// Constant maps for file writing/headers
map<int, string> clumptypeMap = {
{0, "Clumping - default step function"},
{1, "Clumping - two level step function."},
{2, "Clumping - rising benefit function"}
};
map<int, string> heurotypeMap = {
{0,"Richness"},
{1,"Greedy"},
{2,"Maximum Rarity"},
{3,"Best Rarity"},
{4,"Average Rarity"},
{5,"Summation Rarity"},
{6,"Product Irreplaceability"},
{7,"Summation Irreplaceability"}
};
// Helper function to return delimiter and file pointer for a file.
pair<char, FILE*> GetFileAndDelimiter(string filename, int delimiterMode, bool append = false) {
FILE* fp;
char sDelimiter;
fp = append ? fopen(filename.c_str(), "a") : fopen(filename.c_str(), "w");
if (!fp)
displayErrorMessage("Cannot save output to %s \n", filename.c_str());
if (delimiterMode > 1)
sDelimiter = ',';
else
sDelimiter = '\t';
return pair<char, FILE*>(sDelimiter, fp);
}
// debug output for probability 1D
void writeProb1DDebugTable(int spno, string savename, const vector<double>& ExpectedAmount1D, const vector<double>& VarianceInExpectedAmount1D, const vector<sspecies>& spec)
{
FILE* fp;
int i, iHeavisideStepFunction;
double rZ, rRawP, rP, rShortfallPenalty;
fp = fopen(savename.c_str(), "w");
fprintf(fp, "SPID,EA,VIEA,Z,RawP,ptarget1d,HeavisideSF,ShortfallP,P\n");
for (i = spno - 1; i >= 0; i--)
{
computeProbMeasures(VarianceInExpectedAmount1D[i], spec[i].target, spec[i].ptarget1d, ExpectedAmount1D[i],
rZ, rRawP, iHeavisideStepFunction, rShortfallPenalty, rP);
fprintf(fp, "%i,%f,%f,%f,%f,%f,%i,%f,%f\n", spec[i].name, ExpectedAmount1D[i], VarianceInExpectedAmount1D[i], rZ, rRawP, spec[i].ptarget1d, iHeavisideStepFunction, rShortfallPenalty, rP);
}
fclose(fp);
}
// debug output for probability 2D
void writeProb2DDebugTable(int spno, string savename, const vector<double>& ExpectedAmount2D, const vector<double>& VarianceInExpectedAmount2D, vector<sspecies> spec)
{
FILE* fp;
int i, iHeavisideStepFunction;
double rZ, rRawP, rP, rShortfallPenalty;
fp = fopen(savename.c_str(), "w");
fprintf(fp, "SPID,CA,EA,VIEA,Z,RawP,ptarget2d,HeavisideSF,ShortfallP,P\n");
for (i = spno - 1; i >= 0; i--)
{
computeProbMeasures(VarianceInExpectedAmount2D[i], spec[i].target, spec[i].ptarget2d, ExpectedAmount2D[i],
rZ, rRawP, iHeavisideStepFunction, rShortfallPenalty, rP);
fprintf(fp, "%i,%f,%f,%f,%f,%f,%f,%i,%f,%f\n",
spec[i].name, spec[i].amount, ExpectedAmount2D[i], VarianceInExpectedAmount2D[i], rZ, rRawP, spec[i].ptarget2d, iHeavisideStepFunction, rShortfallPenalty, rP);
}
fclose(fp);
}
// debug output for probability 1D
void writeProb1DDetailDebugTable(string savename, int puno, int spno, const vector<spustuff>& pu, const vector<spu>& SM, const vector<int>& R)
{
FILE* fp;
int i, ipu, ism, isp;
double rAmount;
fp = fopen(savename.c_str(), "w");
fprintf(fp, "PUID,R,richness,PROB");
for (i = 1; i <= spno; i++)
fprintf(fp, ",A%i", i);
for (i = 1; i <= spno; i++)
fprintf(fp, ",EA%i", i);
for (i = 1; i <= spno; i++)
fprintf(fp, ",VIEA%i", i);
fprintf(fp, "\n");
for (ipu = puno - 1; ipu >= 0; ipu--)
{
vector<double> AMOUNT(spno, 0.0), EA(spno, 0.0), VIEA(spno, 0.0);
if (pu[ipu].richness)
{
for (i = 0; i < pu[ipu].richness; i++)
{
ism = pu[ipu].offset + i;
isp = SM[ism].spindex;
rAmount = SM[ism].amount;
AMOUNT[isp] = rAmount;
if (rAmount)
{
if (R[ipu] == 1 || R[ipu] == 2)
{
EA[isp] = rAmount * (1 - pu[ipu].prob);
VIEA[isp] = rAmount * rAmount * pu[ipu].prob * (1 - pu[ipu].prob);
}
}
}
}
fprintf(fp, "%i,%i,%i,%f", 100 - ipu, R[ipu], pu[ipu].richness, pu[ipu].prob);
for (i = spno - 1; i >= 0; i--)
fprintf(fp, ",%f", AMOUNT[i]);
for (i = spno - 1; i >= 0; i--)
fprintf(fp, ",%f", EA[i]);
for (i = spno - 1; i >= 0; i--)
fprintf(fp, ",%f", VIEA[i]);
fprintf(fp, "\n");
}
fclose(fp);
}
// debug output for probability 2D
void writeProb2DDetailDebugTable(string savename, int puno, const vector<spustuff>& pu, const vector<spu>& SM, const vector<int>& R)
{
FILE* fp;
int i, ipu, ism, isp;
double AMOUNT[9], EA[9], VIEA[9], PROB[9], rAmount;
fp = fopen(savename.c_str(), "w");
fprintf(fp, "PUID,R,richness,P1,P2,P3,P4,P5,P6,P7,P8,P9,A1,A2,A3,A4,A5,A6,A7,A8,A9,EA1,EA2,EA3,EA4,EA5,EA6,EA7,EA8,EA9,VIEA1,VIEA2,VIEA3,VIEA4,VIEA5,VIEA6,VIEA7,VIEA8,VIEA9\n");
for (ipu = puno - 1; ipu >= 0; ipu--)
{
for (i = 0; i < 9; i++)
{
AMOUNT[i] = 0;
EA[i] = 0;
VIEA[i] = 0;
PROB[i] = 0;
}
if (pu[ipu].richness)
{
for (i = 0; i < pu[ipu].richness; i++)
{
ism = pu[ipu].offset + i;
isp = SM[ism].spindex;
rAmount = SM[ism].amount;
AMOUNT[isp] = rAmount;
PROB[isp] = SM[ism].prob;
if (rAmount)
{
if (R[ipu] == 1 || R[ipu] == 2)
{
EA[isp] = rAmount * PROB[isp];
VIEA[isp] = rAmount * rAmount * PROB[isp] * (1 - PROB[isp]);
}
}
}
}
fprintf(fp, "%i,%i,%i,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",
107 - ipu, R[ipu], pu[ipu].richness,
PROB[8], PROB[7], PROB[6], PROB[5], PROB[4], PROB[3], PROB[2], PROB[1], PROB[0],
AMOUNT[8], AMOUNT[7], AMOUNT[6], AMOUNT[5], AMOUNT[4], AMOUNT[3], AMOUNT[2], AMOUNT[1], AMOUNT[0],
EA[8], EA[7], EA[6], EA[5], EA[4], EA[3], EA[2], EA[1], EA[0],
VIEA[8], VIEA[7], VIEA[6], VIEA[5], VIEA[4], VIEA[3], VIEA[2], VIEA[1], VIEA[0]);
}
fclose(fp);
}
// display startup message: the program title and authors
void displayStartupMessage(void)
{
printf(" %s \n\n Spatial Prioritization via Annealing\n\n", sVersionString.c_str());
printf(" Coded by Ian Ball, modified by Matthew Watts\n");
printf(" Written by Ian Ball and Hugh Possingham\n\n");
printf(" Marxan website\n\n");
printf("%s\n\n", sMarxanWebSite.c_str());
}
// display shutdown message when verbosity > 0
void displayShutdownMessage(chrono::high_resolution_clock::time_point start)
{
if (verbosity > 0)
{
printf("\n");
displayTimePassed(start);
printf("\n The End \n");
if (savelog)
{
fprintf(fsavelog, "\n The End \n");
}
}
}
// write a sync file: tell calling app we've completed a run
void writeSecondarySyncFileRun(int iSyncRun)
{
FILE* fsync;
char sSyncFileName[80];
sprintf(sSyncFileName, "sync%i", iSyncRun);
fsync = fopen(sSyncFileName, "w");
fprintf(fsync, sSyncFileName, "%s");
fclose(fsync);
}
// write a sync file: tell calling app we've completed all runs
void writeSecondarySyncFile(void)
{
FILE* fsync;
fsync = fopen("sync", "w");
fprintf(fsync, "sync");
fclose(fsync);
}
// displays an error message for any verbosity
// program is then terminated
void displayErrorMessage(string sMess, ...)
{
va_list args;
va_start(args, sMess);
vprintf(sMess.c_str(), args);
if (savelog) vfprintf(fsavelog, sMess.c_str(), args);
va_end(args);
throw runtime_error("Error message was generated.\n");
}
// displays a warning message when verbosity > 0
void displayWarningMessage(string sMess, ...)
{
va_list args;
if (verbosity > 0)
{
va_start(args, sMess);
vprintf(sMess.c_str(), args);
if (savelog) vfprintf(fsavelog, sMess.c_str(), args);
va_end(args);
}
}
// display a progress message when verbosity > 0
void displayProgress(string sMess, ...)
{
va_list args;
if (verbosity > 0)
{
va_start(args, sMess);
vprintf(sMess.c_str(), args);
if (savelog)
{
vfprintf(fsavelog, sMess.c_str(), args);
fflush(fsavelog);
}
va_end(args);
}
}
// display a progress message when verbosity > 1
void displayProgress1(string sMess, ...)
{
va_list args;
if (verbosity > 1)
{
va_start(args, sMess);
vprintf(sMess.c_str(), args);
if (savelog)
{
vfprintf(fsavelog, sMess.c_str(), args);
fflush(fsavelog);
}
va_end(args);
}
}
// display a progress message when verbosity > 2 :(was 2, now 5)
void displayProgress2(string sMess, ...)
{
va_list args;
if (verbosity > 5)
{
va_start(args, sMess);
vprintf(sMess.c_str(), args);
if (savelog)
{
vfprintf(fsavelog, sMess.c_str(), args);
fflush(fsavelog);
}
va_end(args);
}
}
// display a progress message when verbosity > 3 :(was 3, now 5)
void displayProgress3(string sMess, ...)
{
va_list args;
if (verbosity > 5)
{
va_start(args, sMess);
vprintf(sMess.c_str(), args);
if (savelog)
{
vfprintf(fsavelog, sMess.c_str(), args);
fflush(fsavelog);
}
va_end(args);
}
}
// create a trace file
void createTraceFile(void)
{
FILE* fdebugtrace;
if (verbosity > 2)
{
if (fnames.savedebugtracefile)
{
fdebugtrace = fopen(sTraceFileName.c_str(), "w");
fflush(fdebugtrace);
fclose(fdebugtrace);
}
}
}
// append message to a trace file when verbosity > 2
void appendTraceFile(string sMess, ...)
{
FILE* fdebugtrace;
va_list args;
if (verbosity > 2)
{
if (fnames.savedebugtracefile)
{
va_start(args, sMess);
fdebugtrace = fopen(sTraceFileName.c_str(), "a");
vfprintf(fdebugtrace, sMess.c_str(), args);
fclose(fdebugtrace);
va_end(args);
}
}
}
// create a debug file
void createDebugFile(string sFileName, string sHeader, const sfname& fnames)
{
FILE* fdebugtrace;
string writename = fnames.outputdir + sFileName;
fdebugtrace = fopen(writename.c_str(), "w");
fprintf(fdebugtrace, sHeader.c_str(), "%s");
fflush(fdebugtrace);
fclose(fdebugtrace);
}
// append message to a debug file
void appendDebugFile(string sFileName, string sLine, const sfname& fnames)
{
FILE* fdebugtrace;
string writename = fnames.outputdir + sFileName;
fdebugtrace = fopen(writename.c_str(), "a");
fprintf(fdebugtrace, sLine.c_str(), "%s");
fclose(fdebugtrace);
}
// display how many seconds since program started
void displayTimePassed(chrono::high_resolution_clock::time_point start)
{
auto end = chrono::high_resolution_clock::now();
uint64_t itemp = chrono::duration_cast<std::chrono::seconds>(end - start).count();
printf("Time passed so far is ");
if (itemp >= 60 * 60)
{
printf(" %i hour%c,%i min%c and %i secs \n",
(int)(itemp / 3600), ((itemp / 3600 == 1) ? ' ' : 's'),
(int)((itemp / 60) % 60), ((itemp / 60 == 1) ? ' ' : 's'), (int)(itemp % 60));
}
else {
if (itemp >= 60)
printf(" %i min%c and %i secs \n", (int)(itemp / 60), ((itemp / 60 == 1) ? ' ' : 's'), (int)(itemp % 60));
else
printf("%i secs \n", (int)(itemp));
}
if (savelog)
{
fprintf(fsavelog, "Time passed so far is ");
if (itemp >= 60 * 60)
{
fprintf(fsavelog, " %i hour%c,%i min%c and %i secs \n",
(int)(itemp / 3600), ((itemp / 3600 == 1) ? ' ' : 's'),
(int)((itemp / 60) % 60), ((itemp / 60 == 1) ? ' ' : 's'), (int)(itemp % 60));
}
else {
if (itemp >= 60)
fprintf(fsavelog, " %i min%c and %i secs \n", (int)(itemp / 60), ((itemp / 60 == 1) ? ' ' : 's'), (int)(itemp % 60));
else
fprintf(fsavelog, "%i secs \n", (int)(itemp));
}
}
}
// create a log file, or reset a log file
void createLogFile(int my_savelog, string my_savelogname)
{
if (savelog)
{ // close and delete old savelog info
fclose(fsavelog);
}
savelog = my_savelog;
if (savelog)
{
savelogname = my_savelogname;
// Try to open file and complain if it don't work
fsavelog = fopen(savelogname.c_str(), "w");
if (fsavelog == NULL)
{
savelog = 0;
displayErrorMessage("Error: Cannot save to log file %s \n", savelogname.c_str());
} // open failed
// Header printing
fprintf(fsavelog, " %s \n\n Spatial Prioritization via Annealing\n\n", sVersionString.c_str());
fprintf(fsavelog, " Coded by Ian Ball, modified by Matthew Watts\n");
fprintf(fsavelog, " Written by Ian Ball and Hugh Possingham\n\n");
fprintf(fsavelog, " Marxan website\n\n");
fprintf(fsavelog, "%s\n\n", sMarxanWebSite.c_str());
} // save log has just been turned on
}
// write an asymmetric connection file
void writeAsymmetricConnectionFile(int puno, const vector<sconnections>& connections, const vector<spustuff>& pu, sfname fnames)
{
int i;
FILE* fp;
string writename = fnames.outputdir + "debug_asymmetric_connectivity.csv";
if ((fp = fopen(writename.c_str(), "w")) == NULL)
{
displayProgress1("Warning: Cannot create file %s", writename.c_str());
}
fprintf(fp, "idA,idB,connectionorigon\n");
for (i = 0; i < puno; i++)
{
for (const sneighbour& p : connections[i].first)
fprintf(fp, "%i,%i,%i,%lf\n", pu[i].id, pu[p.nbr].id, p.connectionorigon, p.cost);
}
fclose(fp);
}
// write an output file from the loaded sparse matrix
void writeSparseMatrix(int iSMno, int puno, vector<spustuff>& PU, vector<sspecies>& spec, vector<spu>& SM, sfname& fnames)
{
FILE* fp;
string writename = fnames.inputdir + "sm.csv";
if ((fp = fopen(writename.c_str(), "w")) == NULL)
displayErrorMessage("cannot create PUvSpecies file %s\n", writename.c_str());
fputs("species,pu,amount,prob\n", fp);
for (int i = puno - 1; i >= 0; i--)
{
if (PU[i].richness > 0)
{
for (int j = 0; j < PU[i].richness; j++)
fprintf(fp, "%i,%i,%g,%g\n",
spec[SM[PU[i].offset + j].spindex].name,
PU[i].id,
SM[PU[i].offset + j].amount,
SM[PU[i].offset + j].prob);
}
}
fclose(fp);
}
double computeTotalConnection2(int puno, const vector<int>& R, const vector<sconnections>& connections) {
double connectiontemp = 0.0;
for (int i = 0; i < puno; i++)
{
if (R[i] == 1 || R[i] == 2)
{
connectiontemp += ConnectionCost2(connections[i], R, 1, 0, 1, asymmetricconnectivity, fOptimiseConnectivityIn);
}
}
return connectiontemp;
}
// write a summary file
void writeSummary(string savename, const vector<string>& summaries, int imode)
{
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename, imode);
FILE* fp = fileInfo.second; // Imode = 1, REST output, Imode = 2, Arcview output
char sDelimiter = fileInfo.first;
fprintf(fp, "\"Run_Number\"%c\"Score\"%c\"Cost\"%c\"Planning_Units\"%c\"Connectivity\"%c\"Connectivity_Total\"%c\"Connectivity_In\"%c\"Connectivity_Edge\"%c\"Connectivity_Out\"%c\"Connectivity_In_Fraction\"%c\"Penalty\"",
sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter);
if (fProb1D == 1)
fprintf(fp, "%c\"Probability1D\"", sDelimiter);
if (fProb2D == 1)
fprintf(fp, "%c\"Probability2D\"", sDelimiter);
fprintf(fp, "%c\"Shortfall\"%c\"Missing_Values\"%c\"MPM\"\n", sDelimiter, sDelimiter, sDelimiter);
for (const string& line : summaries) {
fprintf(fp, "%s", line.c_str());
}
fclose(fp);
} // writeSummary
// caculate and format summary text for a run
string computeSummary(int puno, int spno, const vector<int>& R, const vector<sspecies>& spec, const scost& reserve,
int itn, double misslevel, int imode)
{
stringstream ss;
char del = imode > 1 ? ',' : '\t';
int ino = 0, isp = 0;
double shortfall, connectiontemp, rMPM, rConnectivityFraction,
rConnectivityTotal = 0, rConnectivityIn = 0, rConnectivityEdge = 0, rConnectivityOut = 0;
// Ouput the Summary Statistics
for (int i = 0; i <= puno - 1; i++)
if (R[i] == 1 || R[i] == 2)
ino++;
isp = computeRepresentationMISSLEVEL(spno, spec, misslevel, shortfall, rMPM);
#ifdef DEBUG_COUNTMISSING
appendTraceFile("computeSummary shortfall %g\n", shortfall);
#endif
computeConnectivityIndices(rConnectivityTotal, rConnectivityIn, rConnectivityEdge, rConnectivityOut,
puno, R, connections);
if (rConnectivityTotal > 0)
rConnectivityFraction = rConnectivityIn / rConnectivityTotal;
else
rConnectivityFraction = 0;
connectiontemp = computeTotalConnection2(puno, R, connections);
ss << itn << del << reserve.total << del << reserve.cost << del << ino << del <<
connectiontemp << del << rConnectivityTotal << del << rConnectivityIn << del << rConnectivityEdge << del <<
rConnectivityOut << del << rConnectivityFraction << del << reserve.penalty;
if (fProb1D == 1)
ss << del << reserve.probability1D;
if (fProb2D == 1)
ss << del << reserve.probability2D;
ss << del << shortfall << del << isp << del << rMPM << "\n";
return ss.str();
} // computeSummary
// write the contents of the spec data structure.
// used to validate if the input spec file has been read as intended.
void writeSpec(int spno, const vector<sspecies>& spec, string savename)
{
FILE* fp = GetFileAndDelimiter(savename, 0).second;
fprintf(fp, "id,name,target,prop,type,spf,target2,targetocc,sepdistance,sepnum,ptarget1d,ptarget2d\n");
for (int i = 0; i < spno; i++)
fprintf(fp, "%i,%s,%f,%f,%i,%f,%f,%i,%f,%i,%f,%f\n",
spec[i].name,
spec[i].sname.c_str(), spec[i].target,
spec[i].prop, spec[i].type,
spec[i].spf, spec[i].target2,
spec[i].targetocc, spec[i].sepdistance,
spec[i].sepnum, spec[i].ptarget1d,
spec[i].ptarget2d);
fclose(fp);
}
// write the contents of the pu data structure. used to validate if the input pu file has been read as intended
void writePu(int puno, const vector<spustuff>& pu, string savename)
{
FILE* fp = GetFileAndDelimiter(savename, 0).second; // 0 is placeholder
fprintf(fp, "id,status,cost,prob,xloc,yloc\n");
for (int i = 0; i < puno; i++)
fprintf(fp, "%i,%i,%f,%f,%f,%f\n",
pu[i].id, pu[i].status, pu[i].cost, pu[i].prob, pu[i].xloc, pu[i].yloc);
fclose(fp);
}
// write the penalty calculated for each species
void writePenalty(int spno, const vector<sspecies>& spec, string savename, int iOutputType)
{
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename, iOutputType);
FILE* fp = fileInfo.second; // Imode = 1, REST output, Imode = 2, Arcview output
char sDelimiter = fileInfo.first;
fprintf(fp, "spid%cpenalty\n", sDelimiter);
// Ouput the Summary Statistics
for (int i = 0; i < spno; i++)
fprintf(fp, "%i%c%g\n", spec[i].name, sDelimiter, spec[i].penalty);
fclose(fp);
}
// write the set of planning units used to calculate penalty
void writePenaltyPlanningUnits(int puno, const vector<spustuff>& pu, const vector<int>& Rtemp, string savename, int iOutputType)
{
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename, iOutputType);
FILE* fp = fileInfo.second; // Imode = 1, REST output, Imode = 2, Arcview output
char sDelimiter = fileInfo.first;
fprintf(fp, "puid%cR\n", sDelimiter);
// Ouput the Summary Statistics
for (int i = 0; i < puno; i++)
fprintf(fp, "%i%c%i\n", pu[i].id, sDelimiter, Rtemp[i]);
fclose(fp);
}
// create a solutions matrix file
void createSolutionsMatrix(int puno, const vector<spustuff>& pu, string savename_ism, int iOutputType, int iIncludeHeaders)
{
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename_ism, iOutputType);
FILE* fp = fileInfo.second;
char sDelimiter = fileInfo.first;
if (iIncludeHeaders == 1)
{
fprintf(fp, "SolutionsMatrix");
for (int i = 0; i < puno; i++)
fprintf(fp, "%cP%i", sDelimiter, pu[i].id);
fprintf(fp, "\n");
}
fclose(fp);
}
// append an entry to a solutions matrix file
void appendSolutionsMatrix(int iRun, int puno, const vector<int>& R, string savename, int iOutputType, int iIncludeHeaders)
{
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename, iOutputType, true);
FILE* fp = fileInfo.second;
char sDelimiter = fileInfo.first;
int i, iStatus;
if (iIncludeHeaders == 1)
{
fprintf(fp, "S%i", iRun);
}
for (i = 0; i < puno; i++)
{
iStatus = R[i];
if (R[i] == 3)
iStatus = 0;
if (R[i] == 2)
iStatus = 1;
fprintf(fp, "%c%i", sDelimiter, iStatus);
}
fprintf(fp, "\n");
fclose(fp);
}
// create a solution file: output_r0001.csv, output_best.csv
void writeSolution(int puno, const vector<int>& R, const vector<spustuff>& pu, string savename, int imode, const sfname& fnames)
{
int i;
FILE* fp = GetFileAndDelimiter(savename, imode).second; /* Imode = 1, REST output, Imode = 2, Arcview output */
if (imode == 3)
{
fprintf(fp, "PUID,%s\n", fnames.bestfieldname.c_str());
}
else {
if (imode == 2)
fprintf(fp, "\"planning_unit\",\"solution\"\n");
}
for (i = 0; i < puno; i++)
{
if (R[i] == 1 || R[i] == 2)
{
fprintf(fp, "%i", pu[i].id);
if (imode > 1)
fprintf(fp, ",1");
fprintf(fp, "\n");
}
else {
fprintf(fp, "%i,0\n", pu[i].id);
}
}
fclose(fp);
}
// write scenario file: a text file with input parameters
void writeScenario(int puno, int spno, double prop, double cm,
sanneal& anneal, int seedinit, long int repeats, int clumptype,
srunoptions runoptions, int heurotype, double costthresh, double tpf1, double tpf2,
string savename)
{
FILE* fp;
string temp;
fp = fopen(savename.c_str(), "w");
if (!fp)
displayErrorMessage("Cannot save output to %s \n", savename.c_str());
fprintf(fp, "Number of Planning Units %i\n", puno);
fprintf(fp, "Number of Conservation Values %i\n", spno);
fprintf(fp, "Starting proportion %.2f\n", prop);
fprintf(fp, "Connection modifier %.2f\n\n", cm);
// print clump type
fprintf(fp, "%s\n", clumptypeMap[clumptype].c_str());
fprintf(fp, "Algorithm Used: %s\n", runoptions.algorithm_description().c_str());
if (runoptions.HeuristicOn)
{
if (heurotypeMap.find(heurotype) == heurotypeMap.end()) {
temp = "Unkown Heuristic Type";
}
else {
temp = heurotypeMap[heurotype];
}
fprintf(fp, "Heuristic type : %s\n", temp.c_str());
}
else {
fprintf(fp, "No Heuristic used \n");
}
if (runoptions.ThermalAnnealingOn)
{
fprintf(fp, "Number of iterations %lld\n", anneal.iterations);
if (anneal.Tinit >= 0)
{
fprintf(fp, "Initial temperature %.2f\n", anneal.Tinit);
fprintf(fp, "Cooling factor %.6f\n", anneal.Tcool);
}
else
{
fprintf(fp, "Initial temperature set adaptively\n");
fprintf(fp, "Cooling factor set adaptively\n");
}
fprintf(fp, "Number of temperature decreases %li\n\n", anneal.Titns);
}
else {
fprintf(fp, "Number of iterations N/A\nInitial temperature N/A\nCooling Factor N/A\n");
fprintf(fp, "Number of temperature decreases N/A\n\n");
}
if (costthresh)
{
fprintf(fp, "Cost Threshold Enabled: %f\n", costthresh);
fprintf(fp, "Threshold penalty factor A %.2f\n", tpf1);
fprintf(fp, "Threshold penalty factor B %.2f\n\n", tpf2);
}
else {
fprintf(fp, "Cost Threshold Disabled\nThreshold penalty factor A N/A\n");
fprintf(fp, "Threshold penalty factor B N/A\n\n");
}
fprintf(fp, "Random Seed %i\n", seedinit);
fprintf(fp, "Number of runs %ld\n", repeats);
fclose(fp);
} // writeScenario
// write a species file - the missing values file: output_mv1.csv output_mvbest.csv
void writeSpecies(int spno, vector<sspecies>& spec, string savename, int imode, double misslevel)
{
int isp, iHeavisideStepFunction;
string temp = "";
double rMPM, rTestMPM, rRawP, rShortfallPenalty, placeholder;
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename, imode);
FILE* fp = fileInfo.second; // Imode = 1, Tab Delimitted Text output, Imode = 2, Arcview output
char sDelimiter = fileInfo.first;
fprintf(fp, "\"Conservation Feature\"%c\"Feature Name\"%c\"Target\"%c", sDelimiter, sDelimiter, sDelimiter);
fprintf(fp, "\"Amount Held\"%c\"Occurrence Target \"%c\"Occurrences Held\"%c", sDelimiter, sDelimiter, sDelimiter);
fprintf(fp, "\"Separation Target \"%c\"Separation Achieved\"%c\"Target Met\"%c\"MPM\"", sDelimiter, sDelimiter, sDelimiter);
if (fProb1D == 1)
fprintf(fp, "%cptarget1d%cEA1D%cVIEA1D%cZ1D%crawP1D%cheavisideSF1D%cshortfallP1D%cP1D", sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter);
if (fProb2D == 1)
fprintf(fp, "%cptarget2d%cEA2D%cVIEA2D%cZ2D%crawP2D%cheavisideSF2D%cshortfallP2D%cP2D", sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter);
fprintf(fp, "\n");
for (isp = spno - 1 ; isp >= 0; isp--)
{
rMPM = 1;
fprintf(fp, "%i%c%s%c", spec[isp].name, sDelimiter, spec[isp].sname.c_str(), sDelimiter);
fprintf(fp, "%lf%c%lf%c%i%c%i%c", spec[isp].target, sDelimiter, spec[isp].amount, sDelimiter,
spec[isp].targetocc, sDelimiter, spec[isp].occurrence, sDelimiter);
fprintf(fp, "%i%c%i", spec[isp].sepnum, sDelimiter, spec[isp].separation);
if (spec[isp].target)
{
temp = "yes";
if (spec[isp].amount / spec[isp].target < misslevel)
temp = "no";
rTestMPM = spec[isp].amount / spec[isp].target;
if (rTestMPM < rMPM)
rMPM = rTestMPM;
}
if (spec[isp].targetocc)
{
temp = "yes";
if (spec[isp].occurrence / spec[isp].targetocc < misslevel)
temp = "no";
rTestMPM = spec[isp].occurrence / spec[isp].targetocc;
if (rTestMPM < rMPM)
rMPM = rTestMPM;
}
if (spec[isp].sepnum)
{
temp = "yes";
if (spec[isp].separation / spec[isp].sepnum < misslevel)
temp = "no";
}
fprintf(fp, "%c%s", sDelimiter, temp.c_str());
fprintf(fp, "%c%lf", sDelimiter, rMPM);
if (fProb1D == 1)
{
computeProbMeasures(spec[isp].variance1D, spec[isp].target, spec[isp].ptarget1d, spec[isp].expected1D,
spec[isp].Zscore1D, rRawP, iHeavisideStepFunction, rShortfallPenalty, placeholder);
// "ptarget1d EA1D VIEA1D Z1D rawP1D heavisideSF1D shortfallP1D P1D"
fprintf(fp, "%c%lf%c%lf%c%lf%c%lf%c%lf%c%i%c%lf%c%lf",
sDelimiter, spec[isp].ptarget1d,
sDelimiter, spec[isp].expected1D,
sDelimiter, spec[isp].variance1D,
sDelimiter, spec[isp].Zscore1D,
sDelimiter, rRawP,
sDelimiter, iHeavisideStepFunction,
sDelimiter, rShortfallPenalty,
sDelimiter, spec[isp].probability1D);
}
if (fProb2D == 1)
{
computeProbMeasures(spec[isp].variance2D, spec[isp].target, spec[isp].ptarget2d, spec[isp].expected2D,
spec[isp].Zscore2D, rRawP, iHeavisideStepFunction, rShortfallPenalty, placeholder);
// "ptarget2d EA2D VIEA2D Z2D rawP1D heavisideSF1D shortfallP1D P2D"
fprintf(fp, "%c%lf%c%lf%c%lf%c%lf%c%lf%c%i%c%lf%c%lf",
sDelimiter, spec[isp].ptarget2d,
sDelimiter, spec[isp].expected2D,
sDelimiter, spec[isp].variance2D,
sDelimiter, spec[isp].Zscore2D,
sDelimiter, rRawP,
sDelimiter, iHeavisideStepFunction,
sDelimiter, rShortfallPenalty,
sDelimiter, spec[isp].probability2D);
}
fprintf(fp, "\n");
}
fclose(fp);
} // Output missing species information with new information
// write summed solution file output_ssoln.csv
void writeSumSoln(int puno, const vector<int>& sumsoln, const vector<spustuff>& pu, string savename, int imode)
{
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename, imode);
FILE* fp = fileInfo.second; // Imode = 1, REST output, Imode = 2, Arcview output
char sDelimiter = fileInfo.first;
if (imode > 1)
{
fprintf(fp, "\"planning_unit\",\"number\"\n");
}
for (int i = puno - 1 ; i >= 0; i--)
fprintf(fp, "%i%c%i\n", pu[i].id, sDelimiter, sumsoln[i]);
fclose(fp);
}
// write planning unit richness to a file output_richness.csv
void writeRichness(int puno, const vector<spustuff>& pu, string savename, int iOutputType)
{
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename, iOutputType);
FILE* fp = fileInfo.second;
char sDelimiter = fileInfo.first;
fprintf(fp, "puid%crichness\n", sDelimiter);
for (int i = 0; i < puno; i++)
fprintf(fp, "%i%c%i\n", pu[i].id, sDelimiter, pu[i].richness);
fclose(fp);
}
// compute total area available, reserved, excluded. write it to a file if verbosity > 3.
void computeTotalAreas(int puno, int spno, const vector<spustuff>& pu, const vector<sspecies>& spec, const vector<spu>& SM)
{
vector<int> TotalOccurrences(spno, 0), TO_2(spno, 0), TO_3(spno, 0);
vector<double> TotalAreas(spno, 0), TA_2(spno, 0), TA_3(spno, 0);
FILE *TotalAreasFile;
computeOccurrencesAndAreas(puno, pu, SM,
TotalOccurrences, TO_2, TO_3,
TotalAreas, TA_2, TA_3);
TotalAreasFile = fopen("MarOptTotalAreas.csv", "w");
fprintf(TotalAreasFile, "spname,spindex,totalarea,reservedarea,excludedarea,targetarea,totalocc,reservedocc,excludedocc,targetocc\n");
for (int i = 0; i < spno; i++)
fprintf(TotalAreasFile, "%i,%i,%g,%g,%g,%g,%i,%i,%i,%i\n", spec[i].name, i, TotalAreas[i], TA_2[i], TA_3[i], spec[i].target, TotalOccurrences[i], TO_2[i], TO_3[i], spec[i].targetocc);
fclose(TotalAreasFile);
}
// compute total area available, reserved, excluded. write it to a file output_totalareas.csv
void writeTotalAreas(int puno, int spno, const vector<spustuff>& pu, const vector<sspecies>& spec, const vector<spu>& SM, string savename, int iOutputType)
{
vector<int> TotalOccurrences(spno, 0), TO_2(spno, 0), TO_3(spno, 0);
vector<double> TotalAreas(spno, 0), TA_2(spno, 0), TA_3(spno, 0);
pair<char, FILE*> fileInfo = GetFileAndDelimiter(savename, iOutputType);
FILE* TotalAreasFile = fileInfo.second;
char sDelimiter = fileInfo.first;
computeOccurrencesAndAreas(puno, pu, SM,
TotalOccurrences, TO_2, TO_3,
TotalAreas, TA_2, TA_3);
fprintf(TotalAreasFile, "spname%ctotalarea%creservedarea%cexcludedarea%ctargetarea%ctotalocc%creservedocc%cexcludedocc%ctargetocc\n",
sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter, sDelimiter);
for (int i = 0; i < spno; i++)
fprintf(TotalAreasFile, "%i%c%g%c%g%c%g%c%g%c%i%c%i%c%i%c%i\n",
spec[i].name, sDelimiter, TotalAreas[i], sDelimiter, TA_2[i], sDelimiter, TA_3[i], sDelimiter,
spec[i].target, sDelimiter, TotalOccurrences[i], sDelimiter, TO_2[i], sDelimiter, TO_3[i], sDelimiter, spec[i].targetocc);
fclose(TotalAreasFile);
}
// write vector R (status of each planning unit) to file. debug aid for annealing algorithms
void writeR(int iMessage, string sMessage, int puno, const vector<int>& R, const vector<spustuff>& pu, const sfname& fnames)
{
FILE* fp;
string messagebuffer = sMessage + to_string(iMessage);