forked from Marxan-source-code/marxan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clumping.cpp
1281 lines (1103 loc) · 50.7 KB
/
clumping.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 <algorithm>
#include <stdexcept>
#include "computation.hpp"
#include "clumping.hpp"
#include "output.hpp"
// functions relating to species clumping
namespace marxan {
// returns the clump number of a species at a planning unit, if the species doesn't occur here, returns 0
int rtnClumpSpecAtPu(const spustuff& pu, const vector<spu>& SM, const vector<spu_out>& SM_out, int iSpecIndex)
{
if (pu.richness > 0)
for (int i = 0; i < pu.richness; i++)
if (SM[pu.offset + i].spindex == iSpecIndex)
return SM_out[pu.offset + i].clump;
return 0;
}
// sets the clump number of a species at a planning unit
void setClumpSpecAtPu(const spustuff& pu, const vector<spu>& SM, vector<spu_out>& SM_out, int iSpecIndex, int iSetClump)
{
if (pu.richness > 0)
for (int i = 0; i < pu.richness; i++)
if (SM[pu.offset + i].spindex == iSpecIndex)
SM_out[pu.offset + i].clump = iSetClump; //TODO verify thread change
}
void ClearClump(int isp, sclumps& target, const vector<spustuff>& pu, const vector<spu>& SM, vector<spu_out>& SM_out) {
/* Remove all links from this clump */
for (int i : target.head) {
if (rtnClumpSpecAtPu(pu[i], SM, SM_out, isp) == target.clumpid) {/* in case pu is in new clump */
setClumpSpecAtPu(pu[i], SM, SM_out, isp, 0);
}
}
target.head.clear();
}
int ClumpCut(int isp, const vector<spustuff>& pu,
const vector<sspecies>& spec, const sclumps& clump,
const int& clumppu, const vector<sconnections>& connections, const vector<spu>& SM, const vector<spu_out>& SM_out,
double& totalamount, int& totalocc,
int& iseparation, int imode, int clumptype) {
int ineighbour = 0, iclumps = 0;
vector<int> newhead, head;
vector<sclumps> spclump, newpclump;
double clumpamount, rAmount;
int iocc;
totalamount = 0;
totalocc = 0;
/* Set up spclump for counting Separation */
if (imode)
{
sclumps temp;
temp.amount = 0;
temp.clumpid = clumppu;
spclump.push_back(temp);
newpclump = spclump;
}
/** Generate list of all neighbours and count them **/
/*First check if there are no neighbours then exit. **/
/* return null for no clump cut done and need to do separation count */
if (connections[clumppu].nbrno == 0)
{
if (imode)
{
iseparation = CountSeparation(isp, spclump, pu, SM, SM_out, spec, 0);
}
return 0;
}
for (const sneighbour& pnbr : connections[clumppu].first)
{
if (rtnClumpSpecAtPu(pu[pnbr.nbr], SM, SM_out, isp) == clump.clumpid)
{
ineighbour++;
newhead.push_back(pnbr.nbr);
} // If neighbour is part of the same clump
} // For cycling through all neighbours
head = newhead;
if (ineighbour <= 1)
{ // One or fewer neighbours
if (imode)
{ // separation distance called
for (int pclumpu : clump.head) {
if (pclumpu != clumppu) {
sclumps temp;
temp.clumpid = pclumpu;
temp.amount = clump.amount - returnAmountSpecAtPu(pu[clumppu], SM, isp).second;
spclump.push_back(temp);
}
}
iseparation = CountSeparation(isp, spclump, pu, SM, SM_out, spec, 0);
}
else {
iseparation = spec[isp].sepnum;
}
rAmount = returnAmountSpecAtPu(pu[clumppu], SM, isp).second;
totalamount = PartialPen4(isp, clump.amount - rAmount, spec, clumptype);
totalocc = (clump.occs - (rAmount > 0)) * (totalamount > 0); // count only if still valid size
return 0;
}
// More than one neighbour. Can they form their own clump?
// Put first neighbour at head of new list
// Since we are using vectors instead of LL, this new logic might be slightly slower.
// Reserve size for clumplist to allow iteration while modifying (without new allocation)
// TODO - revisit.
while (!head.empty()) {
vector<int> clumplist;
clumplist.reserve(head.size());
clumpamount = 0;
iclumps++;
clumplist.push_back(head.front());
head.erase(head.begin()); //remove first element from head
ineighbour--;
for (int id2 : clumplist) {
for (sneighbour pnbr : connections[id2].first)
{
// if neighbour in clump but not cut out one
if (rtnClumpSpecAtPu(pu[pnbr.nbr], SM, SM_out, isp) == clump.clumpid && pnbr.nbr != clumppu)
{
if (find(clumplist.begin(), clumplist.end(), pnbr.nbr) == clumplist.end()) {
// add item to clumplist if not already in
clumplist.push_back(pnbr.nbr);
auto nbrFound = find(head.begin(), head.end(), pnbr.nbr);
if (nbrFound != head.end()) {
ineighbour--;
head.erase(nbrFound);
}
}
}
} // cycling through every neighbour on this clump
}
iocc = 0;
for (int id2 : clumplist) {
rAmount = returnAmountSpecAtPu(pu[id2], SM, isp).second;
clumpamount += rAmount;
iocc += (rAmount > 0);
}
totalamount += PartialPen4(isp, clumpamount, spec, clumptype);
if (PartialPen4(isp, clumpamount, spec, clumptype))
{
totalocc += iocc;
}
if (imode) {
for (int id2 : clumplist) {
sclumps temp;
temp.clumpid = id2;
temp.amount = clumpamount;
spclump.push_back(temp);
}
}
} // Continue clump formation whilst there are members in the list
if (imode) {
iseparation = CountSeparation(isp, spclump, pu, SM, SM_out, spec, 0);
}
else
{
iseparation = spec[isp].sepnum;
}
return iclumps;
}
/*************** Setting Clumps for Species Aggregation Rule**********/
void SetSpeciesClumps(int puno, const vector<int>& R, vector<sspecies>& spec, const vector<spustuff>& pu,
const vector<spu>& SM, vector<spu_out>& SM_out, const vector<sconnections>& connections, int clumptype) {
int isp, ism;
for (int ipu = 0; ipu < puno; ipu++)
{
if (pu[ipu].richness)
{
for (int i = 0; i < pu[ipu].richness; i++)
{
ism = pu[ipu].offset + i;
isp = SM[ism].spindex;
if (spec[isp].target2)
{
spec[isp].clumps = 0;
if ((R[ipu] == 1 || R[ipu] == 2) && SM[ism].amount > 0 && SM_out[ism].clump == 0)
{
AddNewPU(ipu, isp, connections, spec, pu, SM, SM_out, clumptype);
} // Add a New planning unit
} // For each type 4 species
}
}
}
}
/************ Species Amounts Type 4 **************/
/** Assumes Set Species Clumps has been called **/
void SpeciesAmounts4(int isp, vector<sspecies>& spec, int clumptype) {
double ftemp;
struct sclumps* pclump;
for (sclumps pclump : spec[isp].head) {
ftemp = PartialPen4(isp, pclump.amount, spec, clumptype);
spec[isp].amount += ftemp;
spec[isp].occurrence += pclump.occs * (ftemp > 0);
}
}
// Clear Clumps
// This is for clean up purposes
void ClearClumps(int spno, vector<sspecies>& spec, const vector<spustuff>& pu, const vector<spu>& SM, vector<spu_out>& SM_out) {
for (int i = 0; i < spno; i++)
{
for (sclumps& clump : spec[i].head) {
ClearClump(i, clump, pu, SM, SM_out);
}
spec[i].head.clear();
spec[i].clumps = 0;
} // Clear clump for each species
}
// Add New Clump
sclumps AddNewClump(int isp, int ipu, vector<sspecies>& spec, const vector<spustuff>& pu, const vector<spu>& SM, vector<spu_out>& SM_out) {
int iclumpno = 0;
double rAmount;
if (spec[isp].head.empty()) {
iclumpno = 1;
}
int ind = 0;
bool found = false;
sclumps prev;
prev.clumpid = -1; // placeholder initial value
for (const sclumps& pclump : spec[isp].head) {
if (prev.clumpid == -1) {
prev = pclump;
continue;
}
else {
if (pclump.clumpid - prev.clumpid > 1)
{
iclumpno = prev.clumpid + 1;
found = true;
break;
} // Looking for good number
}
ind++;
prev = pclump;
}
if (!found) {
iclumpno = spec[isp].head.back().clumpid + 1;
}
setClumpSpecAtPu(pu[ipu], SM, SM_out, isp, iclumpno);
// Set up new clump to insert
sclumps temp;
temp.clumpid = iclumpno;
temp.head.push_back(ipu);
rAmount = returnAmountSpecAtPu(pu[ipu], SM, isp).second;
temp.amount = rAmount;
temp.occs = (rAmount > 0);
if (!spec[isp].head.empty())
{
spec[isp].head.insert(spec[isp].head.begin() + ind, temp);
} // Stick clump into correct location
else
{
spec[isp].head.push_back(temp);
} // First clump on the block
spec[isp].clumps++;
return temp; // TODO ADBAI - flesh out return value
}
// Add New Planning Unit for a given Species
void AddNewPU(int ipu, int isp, const vector<sconnections>& connections, vector<sspecies>& spec, const vector<spustuff>& pu,
const vector<spu>& SM, vector<spu_out>& SM_out, int clumptype) {
int ineighbours = 0;
int iclumpno, iClump;
sclumps temp;
double ftemp, rAmount;
for (const sneighbour& pnbr : connections[ipu].first) {
// Check all the neighbours to see if any are already in clumps */
iClump = rtnClumpSpecAtPu(pu[pnbr.nbr], SM, SM_out, isp);
if (iClump > 0) {
// Neighbour that is part of clump
int ind;
ineighbours++;
if (ineighbours == 1) {
ind = 0; // reset
for (sclumps tempClump : spec[isp].head) {
if (tempClump.clumpid == iClump) {
temp = tempClump;
break;
}
ind++;
}
setClumpSpecAtPu(pu[ipu], SM, SM_out, isp, iClump);
spec[isp].head[ind].head.push_back(ipu);
// Remove old value for this clump
ftemp = PartialPen4(isp, spec[isp].head[ind].amount, spec, clumptype);
spec[isp].amount -= ftemp;
spec[isp].occurrence -= spec[isp].head[ind].occs * (ftemp > 0);
rAmount = returnAmountSpecAtPu(pu[ipu], SM, isp).second;
spec[isp].head[ind].occs += (rAmount > 0);
spec[isp].head[ind].amount += rAmount;
}
else {
// TODO - revisit this branch logic. Removing LL implementation may have slowed things.
// look at previous found index clump
sclumps& pclump = spec[isp].head[ind];
if (pclump.clumpid != iClump)
{
// Check if this is a different clump
// Join this new clump to the old one
int ind2 = 0;
for (sclumps tempClump : spec[isp].head) {
if (tempClump.clumpid == iClump) {
break;
}
ind2++;
}
sclumps& pnewclump = spec[isp].head[ind2];
for (int puid : spec[isp].head[ind2].head) {
setClumpSpecAtPu(pu[puid], SM, SM_out, isp, pclump.clumpid);
}
// cut out this clump and join it to pclump
// this operation copies over the pu
pnewclump.head.insert(pnewclump.head.end(), pclump.head.begin(), pclump.head.end());
// merge clumps
pclump.head = pnewclump.head; // copy assignment
pclump.amount += pnewclump.amount;
pclump.occs += pnewclump.occs;
ftemp = PartialPen4(isp, pnewclump.amount, spec, clumptype);
spec[isp].amount -= ftemp;
spec[isp].occurrence -= pnewclump.occs * (ftemp > 0);
// Remove pnewclump from spec[isp]
spec[isp].head.erase(spec[isp].head.begin() + ind2);
} // Join the two clumps together
}
}
}
// Adding a New clump
if (!ineighbours)
{
AddNewClump(isp, ipu, spec, pu, SM, SM_out);
ftemp = PartialPen4(isp, rAmount, spec, clumptype);
spec[isp].amount += ftemp;
spec[isp].occurrence += (ftemp > 0);
} // Adding a new clump
// Correcting Amount if new clump not added
if (ineighbours)
{
ftemp = PartialPen4(isp, temp.amount, spec, clumptype);
spec[isp].amount += ftemp;
spec[isp].occurrence += temp.occs * (ftemp > 0);
}
}
/************** REM PU ****************************************/
/*********** Remove a planning unit. Note it is similar to CutClump but actually does action **/
/**************************************************************/
void RemPu(int ipu, int isp, const vector<sconnections>& connections, vector<sspecies>& spec, const vector<spustuff>& pu,
const vector<spu>& SM, vector<spu_out>& SM_out, int clumptype) {
int ineighbours = 0;
sclumps pclump;
int cppu;
struct sneighbour* pnbr;
double oldamount, newamount = 0.0, rAmount;
int newoccs;
/* Find the correct clump to remove */
bool found = false;
int foundInd = 0;
int idToFind = rtnClumpSpecAtPu(pu[ipu], SM, SM_out, isp);
for (sclumps tempClump : spec[isp].head) {
if (tempClump.clumpid == idToFind) {
found = true;
break;
}
foundInd++;
}
sclumps& oldclump = spec[isp].head[foundInd];
if (!found)
displayErrorMessage("Serious error in Remove Type 4 species routine\n");
/* Locate the correct clumppu */
auto cppuIt = find(oldclump.head.begin(), oldclump.head.end(), ipu);
cppu = *cppuIt;
setClumpSpecAtPu(pu[cppu], SM, SM_out, isp, 0);
oldamount = PartialPen4(isp, oldclump.amount, spec, clumptype);
spec[isp].amount -= oldamount;
spec[isp].occurrence -= oldclump.occs * (oldamount > 0);
/* Building the neighbour list */
vector<int> head;
for (const sneighbour& pnbr : connections[cppu].first) {
ineighbours++;
head.push_back(pnbr.nbr);
}
if (ineighbours <= 1) {
rAmount = returnAmountSpecAtPu(pu[ipu], SM, isp).second;
oldclump.amount -= rAmount;
oldclump.occs -= (rAmount > 0);
newamount = PartialPen4(isp, oldclump.amount, spec, clumptype);
newoccs = oldclump.occs * (newamount > 0);
// remove clummpu
oldclump.head.erase(cppuIt);
// remove old clump
spec[isp].head.erase(spec[isp].head.begin() + foundInd);
if (ineighbours < 1)
{
// remove old clump
spec[isp].head.erase(spec[isp].head.begin() + foundInd);
spec[isp].clumps--;
} /* Removing redundant clump */
spec[isp].amount += newamount;
spec[isp].occurrence += newoccs;
return;
} /* I'm not cutting a clump */
/* Else create new clumps */
vector<int> modifiedHead = head; // copy assignment
for (int id : head) {
/* take first element as seed for new clump number */
pclump = AddNewClump(isp, id, spec, pu, SM, SM_out);
// TODO - revisit this logic
/* Continue until you've added every conceivable neighbour */
for (int id2 : pclump.head) {
for (const sneighbour& pnbr : connections[id2].first) {
if (rtnClumpSpecAtPu(pu[pnbr.nbr], SM, SM_out, isp) == oldclump.clumpid) {
auto eraseIt = find(oldclump.head.begin(), oldclump.head.end(), pnbr.nbr);
oldclump.head.erase(eraseIt);
setClumpSpecAtPu(pu[id2], SM, SM_out, isp, pclump.clumpid);
rAmount = returnAmountSpecAtPu(pu[id2], SM, isp).second;
pclump.amount += rAmount;
pclump.occs += (rAmount > 0);
/* Check if it is on neighbours list and if so then remove it from that list*/
auto idIt = find(modifiedHead.begin(), modifiedHead.end(), id2);
if (idIt != modifiedHead.end()) {
modifiedHead.erase(idIt);
}
}
}
}
spec[isp].amount += PartialPen4(isp, pclump.amount, spec, clumptype);
spec[isp].occurrence += pclump.occs * (PartialPen4(isp, pclump.amount, spec, clumptype) > 0);
}
/* Every neighbour in local list has been used and every clump formed*/
/* Remove old clump */
/* Worry about change in amount and hence score */
spec[isp].head.erase(spec[isp].head.begin() + foundInd);
ClearClump(isp, oldclump, pu, SM, SM_out);
}
/*** Remove Clump Check ***/
/** returns 0 if any member of clump is non-removable, Ie status == 2 **/
int RemClumpCheck(const sclumps& pclump, const vector<spustuff>& pu) {
for (int pcpu : pclump.head) {
if (pu[pcpu].status == 2)
return 0;
}
return 1;
}
/********* Set Penalties for a given Type 4 Species ***/
/* Returns 1 if the species is a 'bad species' and -1 if it is a 'good species' */
/* Also sticks the penalty into spec[isp].penalty */
int CalcPenaltyType4(int isp, int puno, const vector<spu>& SM, vector<spu_out>& SM_out, const vector<sconnections>& connections,
vector<sspecies>& spec, const vector<spustuff>& pu, double cm, int clumptype, rng_engine& rngEngine)
{
vector<sclumps> newno;
int j, ipu, iputotal = 0;
int ineighbours = 0, iclumpno, badspecies = 0;
double totalamount = 0, dummy = 0;
int idummy;
double cost = 0.0, connection = 0.0, rAmount = 0.0;
vector<int> R;
R.resize(puno); /* needed for separation */
for (int i = 0; i < puno; i++)
R[i] = pu[i].status;
/*** Step 1. Make a list of all the possible PUs to be included ****/
vector<int> plisthead;
for (int i = 0; i < puno; i++) {
if (returnAmountSpecAtPu(pu[i], SM, isp).second > 0)
{
if (pu[i].status == 3) continue; /* not allowed to consider this one */
if (pu[i].status == 2)
{ /* add to clumps and remove from list */
AddNewPU(i, isp, connections, spec, pu, SM, SM_out, clumptype);
continue;
} /* checking if PU forced into reserve */
iputotal++;
plisthead.push_back(i);
} /** Made list of all sites with this species **/
}
/* Check first to see if I've already satisfied targets for this species */
SpeciesAmounts4(isp, spec, clumptype);
if (spec[isp].sepnum > 0)
spec[isp].separation = CountSeparation2(isp, 0, newno, puno, R, pu, SM, SM_out, spec, 0);
if ((spec[isp].amount >= spec[isp].target) && (spec[isp].occurrence >= spec[isp].targetocc) && (spec[isp].separation >= spec[isp].sepnum))
{
spec[isp].amount = 0;
spec[isp].occurrence = 0;
spec[isp].separation = 0;
/** Clean out all the clump numbers for this species.*/
for (sclumps& c : spec[isp].head) {
ClearClump(isp, c, pu, SM, SM_out);
}
spec[isp].clumps = 0;
return(-1);
} /* return when all targets already met. */
if (iputotal) {
// shuffle vector to emulate randomly choosing
shuffle(plisthead.begin(), plisthead.end(), rngEngine);
do {
int chosenPu = plisthead.back();
plisthead.pop_back();
// Add pu to system
R[chosenPu] = 1;
AddNewPU(chosenPu, isp, connections, spec, pu, SM, SM_out, clumptype);
/*** Check to see if I should continue by calculating current holdings **/
SpeciesAmounts4(isp, spec, clumptype);
if (spec[isp].sepnum > 0)
spec[isp].separation = CountSeparation2(isp, 0, newno, puno, R, pu, SM, SM_out, spec, 0);
} while ((spec[isp].amount < spec[isp].target || spec[isp].separation < spec[isp].sepnum || spec[isp].occurrence < spec[isp].targetocc)
&& iputotal >= 1);
}
if (spec[isp].amount < spec[isp].target || spec[isp].occurrence < spec[isp].targetocc)
{
badspecies = 1;
displayProgress1("Species %i cannot be fully represented!\n", spec[isp].name);
} /*** Record the fact that the species is unrepresentable ***/
if (spec[isp].separation < spec[isp].sepnum && spec[isp].amount >= spec[isp].target && spec[isp].occurrence >= spec[isp].targetocc)
{
badspecies = 1;
displayProgress1("Species %i can only get %i separate valid clumps where %i are wanted!\n",
spec[isp].name, spec[isp].separation, spec[isp].sepnum);
} /*** Record the fact that the species is unrepresentable ***/
/* Search through the clumps looking for any which can be removed */
/* But only do this if occurrence target met. Otherwise every single pu is neccessary*/
if (spec[isp].occurrence >= spec[isp].targetocc)
{
int count = 0;
vector<sclumps> newClumps;
while (count < spec[isp].head.size()) {
sclumps& pclump = spec[isp].head[count];
int i = 0;
if (RemClumpCheck(pclump, pu)) {
i = 1;
}
if (i)
{
if (spec[isp].occurrence - pclump.occs < spec[isp].targetocc)
i = 0;
} /* Check is occurrence decrease ok? */
if (i)
{
if (!((spec[isp].amount - pclump.amount >= spec[isp].target) || (pclump.amount < spec[isp].target2)))
i = 0;
} /* Check is amount decrease OK? */
if (i && spec[isp].sepnum)
{
vector<sclumps> tempSepVector(spec[isp].head.begin() + count, spec[isp].head.end()); // In LL implementation, this would have been current point onwards.
j = CountSeparation2(isp, 0, tempSepVector, puno, R, pu, SM, SM_out, spec, -1); //TODO - revise vector that gets passed in here
if ((j < spec[isp].separation) && (j < spec[isp].sepnum))
i = 0;
if (!spec[isp].target2)
i = 0; /* cannot elegantly remove clumps if species is listed as non-clumping */
}
if (i) /* This is a clump which can be safely removed */
{ /* cut clump if uneccessary or it is too small */
for (int puid : pclump.head) {
setClumpSpecAtPu(pu[puid], SM, SM_out, isp, 0);
}
totalamount -= pclump.amount;
spec[isp].clumps--;
// physically remove clump
spec[isp].head.erase(spec[isp].head.begin() + count);
}
else {
count++; //increment only if we didn't remove
}
}
} /*** Remove unneccesary clumps and links****/
/** Test all PU's to see if any one of them are superfluous **/
/* But only do this if occurrence target met. Otherwise every single pu is neccessary*/
if (spec[isp].occurrence >= spec[isp].targetocc)
{
for (sclumps& pclump : spec[isp].head) {
vector<int> newPu;
for (int oldPu : pclump.head) {
/** Test to see if this oldPu is necessary **/
int i = 0;
if (R[oldPu] != 2)
i = 1;
if (i)
{
rAmount = returnAmountSpecAtPu(pu[oldPu], SM, isp).second;
if ((pclump.amount - rAmount > spec[isp].target2) && (spec[isp].amount - rAmount > spec[isp].target))
i = 1;
else
i = 0;
} /* doesn't drop amount below min clump size or target */
if (i)
{
if (spec[isp].occurrence <= spec[isp].targetocc)
i = 0;
} /* Does it drop occurrences too low? */
if (i)
{
vector<sclumps> temp;
sclumps pnewclump = { oldPu, 0, 0 };
temp.push_back(pnewclump);
j = CountSeparation2(isp, oldPu, temp, puno, R, pu, SM, SM_out, spec, -1);
if ((j < spec[isp].separation) && (j < spec[isp].sepnum))
i = 0;
} /* How does count sep fare? */
if (i)
{
if (ClumpCut(isp, pu, spec, pclump, oldPu, connections, SM, SM_out, dummy, idummy, j, 0, clumptype))
i = 0;
} /* Does it cut the clump? these are not allowed to remove */
if (i) /* Is this removable? */
{ /* remove pcpu */
setClumpSpecAtPu(pu[oldPu], SM, SM_out, isp, 0);
totalamount -= rAmount;
pclump.amount -= rAmount;
// pu effectively removed by not including in newPu
} /** remove unneccessary clumppu **/
else {
newPu.push_back(oldPu); // add to new list so we include it.
}
}
pclump.head = newPu;
}
} /** Cycle over each pclump **/
/*** Now count the cost of this particular reserve ****/
/*** For each clump figure out connection cost ***/
for (sclumps& pclump : spec[isp].head) {
iclumpno = pclump.clumpid;
for (int oldPu : pclump.head) {
if (pu[oldPu].status != 2)
{
cost += pu[oldPu].cost;
connection += connections[oldPu].fixedcost;
} /* only count fixed costs if PU not forced into reserve */
if (connections[oldPu].nbrno)
{
for (const sneighbour& pnbr : connections[oldPu].first)
{
if (rtnClumpSpecAtPu(pu[pnbr.nbr], SM, SM_out, isp) != iclumpno)
connection += pnbr.cost;
} /** Counting each individual connection **/
} /** Counting connection strength if neccessary **/
}
}
/* Finally. Calculate penalty from all of this.*/
spec[isp].penalty = cost + connection * cm;
/* Consider case where targets cannot be met */
totalamount = 0;
if (spec[isp].amount < spec[isp].target)
totalamount = spec[isp].target / spec[isp].amount;
if (spec[isp].occurrence < spec[isp].targetocc)
totalamount += (float)spec[isp].targetocc / (float)spec[isp].occurrence;
if (totalamount)
spec[isp].penalty *= totalamount; /* Scale it up */
if (spec[isp].sepdistance)
spec[isp].separation = 1;
spec[isp].amount = 0; /* because my routines add it in */
spec[isp].occurrence = 0;
/** Clean out all the clump numbers for this species.*/
for (sclumps& clump : spec[isp].head)
{
ClearClump(isp, clump, pu, SM, SM_out);
} /** Remove each clump ***/
spec[isp].clumps = 0;
return(badspecies);
}
/**** Partial Penalty for type 4 species ***/
double PartialPen4(int isp, double amount, const vector<sspecies>& spec, int clumptype) {
if (amount >= spec[isp].target2)
{
return (amount); /* I'm not a partial penalty */
}
else
{
switch (clumptype)
{
case 0:
return(0.0); /* default step function */
case 1:
return(amount / 2.0); /* nicer step function */
case 2:
if (spec[isp].target2)
return (amount / spec[isp].target2 * amount);
default:
return(0.0);
}
}
}
/*** Value for Adding a Planning Unit ****/
double ValueAdd(int isp, int ipu, int puno, const vector<int>& R, const vector<sconnections>& connections, const vector<spustuff>& pu,
const vector<spu>& SM, const vector<spu_out>& SM_out, const vector<sspecies>& spec, int clumptype) {
int ineighbours = 0, iclumpid, iseparation;
vector<sneighbour> pnbr;
vector<sclumps> head;
vector<sclumps> sepclump;
double amount, oldamount = 0.0, shortfall;
int oldoccs = 0, occs, iClump;
/* Count neighbours */
if (connections[ipu].nbrno > 0) {
pnbr = connections[ipu].first;
for (sneighbour& neighbour : pnbr) {
iClump = rtnClumpSpecAtPu(pu[neighbour.nbr], SM, SM_out, isp);
if (iClump) {
iclumpid = 1;
/* Is nbr on my list ?*/
if (!head.empty()) {
for (sclumps tempClump : head) {
if (tempClump.clumpid == iClump)
iclumpid = 0;
}
}
if (iclumpid) {
ineighbours++;
sclumps correctClump;
/* find the right clump */
for (sclumps tempClump2 : spec[isp].head) {
if (tempClump2.clumpid == iClump) {
correctClump = tempClump2;
}
}
head.push_back(correctClump);
if (spec[isp].sepnum) {
for (int ppu : correctClump.head) {
sclumps newClump;
newClump.clumpid = ppu;
sepclump.push_back(newClump); /* glue to sep list. Still need amount */
} /* stick whole clump onto separation clump for later */
}
}
}
}
} /* If There are neighbours */
if (spec[isp].sepnum) {
sclumps psclump;
psclump.clumpid = ipu;
sepclump.push_back(psclump);
} /* Add ipu to my sepclump list */
/* now I know number and names of neighbouring clumps */
amount = returnAmountSpecAtPu(pu[ipu], SM, isp).second;
occs = (amount > 0);
for (sclumps plink : head) {
amount += plink.amount;
occs += plink.occs;
oldamount += PartialPen4(isp, plink.amount, spec, clumptype);
oldoccs += plink.occs * (PartialPen4(isp, plink.amount, spec, clumptype) > 0);
}
/* set the sepclump amounts to this new amount */
if (spec[isp].sepnum)
{
for (auto& psclump : sepclump) {
psclump.amount = amount;
}
}
amount = PartialPen4(isp, amount, spec, clumptype);
occs = occs * (amount > 0);
amount = amount - oldamount; /* amount is change in amount for this species */
occs = occs - oldoccs;
if (spec[isp].sepnum) {
iseparation = CountSeparation2(isp, 0, sepclump, puno, R, pu, SM, SM_out, spec, 1); /* imode = 1 doesn't do anything*/
}
/* Return the effective amount for this species */
/* Old amount + change in amount + separation penalty for changed structure */
amount = spec[isp].amount + amount;
shortfall = 0;
if (spec[isp].target)
shortfall = amount >= spec[isp].target ? 0 : (spec[isp].target - amount) / spec[isp].target;
if (spec[isp].targetocc) {
occs = occs + spec[isp].occurrence;
amount = occs >= spec[isp].targetocc ? 0 :
((double)spec[isp].targetocc - (double)occs) / (double)spec[isp].targetocc;
shortfall += amount;
}
if (spec[isp].target && spec[isp].targetocc)
shortfall /= 2;
return(shortfall + computeSepPenalty(iseparation, spec[isp].sepnum));
}
/** Value Remove. The amount of species loss for removing a single pu */
double ValueRem(int ipu, int isp, const vector<sspecies>& spec, const vector<sconnections>& connections,
const vector<spustuff>& pu, const vector<spu>& SM, const vector<spu_out>& SM_out, int clumptype) {
double newamount = 0, amount, shortfall = 0;
sclumps pclump;
int ppu;
int iseparation;
int newocc = 0;
/* locate the clump and clumppu of the target site ipu */
for (sclumps clumpTemp : spec[isp].head) {
if (clumpTemp.clumpid == rtnClumpSpecAtPu(pu[ipu], SM, SM_out, isp)) {
pclump = clumpTemp;
break;
}
}
/* locate the correct pclump pu */
for (int id : pclump.head) {
if (id == ipu) {
ppu = id;
break;
}
}
/* debugmem2 = debugmem; debugging line */
if (spec[isp].sepnum)
ClumpCut(isp, pu, spec, pclump, ppu, connections, SM, SM_out, newamount, newocc, iseparation, 1, clumptype);
else
ClumpCut(isp, pu, spec, pclump, ppu, connections, SM, SM_out, newamount, newocc, iseparation, 0, clumptype);
if (spec[isp].target)
{
amount = spec[isp].amount + newamount - PartialPen4(isp, pclump.amount, spec, clumptype);
shortfall = amount > spec[isp].target ? 0 : (spec[isp].target - amount) / spec[isp].target;
} /* there is an abundance amount */
/*if (isp == 16) printf("pclump->occs %i targetocc %i shortfall %.2f\n",
pclump->occs,spec[isp].targetocc,shortfall);*/
if (spec[isp].targetocc) { /* Handle the case where there is a targetocc */
amount = spec[isp].occurrence + newocc - pclump.occs * (PartialPen4(isp, pclump.amount, spec, clumptype) > 0);
if (amount < spec[isp].targetocc)
shortfall += ((double)spec[isp].targetocc - amount) / (double)spec[isp].targetocc;
if (spec[isp].target)
shortfall /= 2;
}
/* if (isp ==16) printf("shortfall %.2f occ %i newocc %i pclump->amount %.2f\n",
shortfall, spec[isp].occurrence,newocc,pclump->amount);*/
return(shortfall + computeSepPenalty(iseparation, spec[isp].sepnum));
}
/*************** NewPenalty4 *********************/
/* Calculates the new penalty for adding or removing a PU for species which have
clumping requirements */
double NewPenalty4(int ipu, int isp, int puno, const vector<sspecies>& spec, const vector<spustuff>& pu, const vector<spu>& SM, const vector<spu_out>& SM_out,
const vector<int>& R, const vector<sconnections>& connections, int imode, int clumptype) {
double amount;
if (imode == 1) {
if (spec[isp].penalty == 0)
return (0); /* Targets have all already been met */
amount = ValueAdd(isp, ipu, puno, R, connections, pu, SM, SM_out, spec, clumptype);
}
else {
/* determine change in this amount */
amount = ValueRem(ipu, isp, spec, connections, pu, SM, SM_out, clumptype);
} /** removing a planning unit **/
return amount;
}
int ValidPU(int ipu, int isp, const vector<sclumps>& newno, const vector<sspecies>& spec, const vector<spustuff>& pu,
const vector<spu>& SM, const vector<spu_out>& SM_out, int imode) {
// Returns true if ipu is acceptable as a planning unit
int i = returnAmountSpecAtPu(pu[ipu], SM, isp).first;
sclumps pclump;
bool found = false;
if (!newno.empty())
{
if (imode == -2)
{
if (SM_out[i].clump == newno[0].clumpid)
return(0); // This whole clump is to be removed
}
// iterate through newno to find if there's any clumpId equal to given ipu.
// if there is check if amount meets target.
for (const sclumps& clump : newno) {
if (ipu == clump.clumpid) {
if (clump.amount < spec[isp].target2) {
return 0;
}
else {
return 1;
}
}
} // ipu is on list of changed pus
}
// Find clump
for (const sclumps& clump : spec[isp].head) {
if (SM_out[i].clump == clump.clumpid) {
pclump = clump;
found = true;
}
} // scan through to find clump
if (found)
{
if (pclump.amount < spec[isp].target2)
return 0;
else
return 1;
}
else
{
if (SM[i].amount < spec[isp].target2)
return 0;