-
Notifications
You must be signed in to change notification settings - Fork 0
/
Colony.cpp
4251 lines (3838 loc) · 132 KB
/
Colony.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
// Colony.cpp : implementation file
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Colony.h"
#include "ColdStorageSimulator.h"
#include "GlobalOptions.h"
#include "VarroaPop.h"
#include <cmath>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// List Class implementations
/////////////////////////////////////////////////////////////////////////////
//
// CBeelist - this is the base class for all bee list classes. Contains the functions and attributes common to all
//
int CBeelist::DroneCount = 0;
int CBeelist::ForagerCount = 0;
int CBeelist::WorkerCount = 0;
CBeelist::~CBeelist()
{
while (!IsEmpty())
delete (RemoveHead());
RemoveAll();
}
int CBeelist::GetQuantity()
{
int TotalCount = 0;
POSITION pos = GetHeadPosition();
BOOL mt = IsEmpty();
int count = GetCount();
while (pos != NULL)
{
CBee* theBee = (CBee*)GetNext(pos);
if (theBee->IsAlive()) TotalCount += theBee->number;
}
return TotalCount;
}
void CBeelist::KillAll()
{
POSITION pos = GetHeadPosition();
while (pos != NULL)
{
CBee* theBee = (CBee*)GetNext(pos);
theBee->Alive = false;
theBee->SetNumber(0);
}
}
// Returns the quantity of bees in this boxcar. Note this is zero-based so the first boxcar is index = 0
int CBeelist::GetQuantityAt(int index)
{
int Count = 0;
if ((index < GetCount()) && (index >= 0))
{
CBee* theBee = (CBee*)GetAt(FindIndex(index));
Count = theBee->GetNumber();
}
return Count;
}
void CBeelist::SetQuantityAt(int index, int Quan)
{
if ((index < GetCount()) && (index >= 0))
{
CBee* theBee = (CBee*)GetAt(FindIndex(index));
theBee->SetNumber(Quan);
}
}
// Returns the sum of quantity of bees in the "from" boxcar thru the "to" boxcar, inclusive. If "to" is greater than
// the length of the list, the sum stops at the end of the list (last boxcar). If from == to, the quantity at that
// point is returned.
int CBeelist::GetQuantityAt(int from, int to)
{
int Count = 0;
int ListLength = GetCount();
if ((from >= 0) && (from < ListLength))
{
for (int i = from; i <= to; i++)
{
if (i >= ListLength) break; // Exceeded the length of the list
Count += GetQuantityAt(i);
}
}
return Count;
}
// Evenly divides Quan bees between boxcars from -> to inclusive
void CBeelist::SetQuantityAt(int from, int to, int Quan)
{
ASSERT(from <= to);
int ListLength = GetCount();
if (to > ListLength - 1) to = ListLength - 1;
int QuanPerBoxcar = Quan / (1 + (to - from)); // divide by number of boxcars
if ((from >= 0) && (from <= to))
{
for (int i = from; i <= to; i++)
SetQuantityAt(i, QuanPerBoxcar);
}
}
// Sets the quantity of bees in boxcars between from -> to = Number*Proportion.
void CBeelist::SetQuantityAtProportional(int from, int to, double Proportion)
{
ASSERT(from <= to);
int ListLength = GetCount();
if (to > ListLength - 1) to = ListLength - 1;
if ((from >= 0) && (from <= to))
{
int BoxcarQuant = 0;
for (int i = from; i <= to; i++)
{
BoxcarQuant = GetQuantityAt(i);
SetQuantityAt(i, (int)(BoxcarQuant * Proportion));
}
}
}
// Factor quantity increases or decreases the number of bees
// in each age group of the list such that New Quantity = Old Quantity * factor
void CBeelist::FactorQuantity(double factor)
{
POSITION pos = GetHeadPosition();
CBee* pBee;
while (pos != NULL)
{
pBee = (CBee*)GetNext(pos);
pBee->number = int(pBee->number * factor);
}
}
CString CBeelist::Status()
{
POSITION pos = GetHeadPosition();
CString StatStg, InterStg;
int count, boxcar = 1;
StatStg.Format("Tot BC: %d, ", m_ListLength);
while (pos != NULL)
{
count = ((CBee*)GetNext(pos))->number;
InterStg.Format("BC%d: %d, ", boxcar, count);
StatStg += InterStg;
boxcar++;
}
return StatStg;
}
/////////////////////////////////////////////////////////////////////////////
//
// CForagerlist - this is a type of CAdultList implementing Forager behaviors
//
CForagerlist::CForagerlist() : CAdultlist::CAdultlist()
{
PendingForagers.RemoveAll();
m_PropActualForagers = .8;
}
CForagerlist::~CForagerlist()
{
while (!PendingForagers.IsEmpty())
delete (PendingForagers.RemoveHead());
PendingForagers.RemoveAll();
}
void CForagerlist::ClearPendingForagers()
{
while (!PendingForagers.IsEmpty())
{
CAdult* temp = (CAdult*)PendingForagers.RemoveHead();
ASSERT(temp);
delete (temp);
}
}
int CForagerlist::GetQuantity()
{
int quan = CBeelist::GetQuantity();
POSITION pos = PendingForagers.GetHeadPosition();
CAdult* temp;
while (pos != NULL)
{
temp = (CAdult*)PendingForagers.GetNext(pos);
quan += temp->GetNumber();
}
quan += m_UnemployedForagers.GetNumber();
return quan;
}
int CForagerlist::GetActiveQuantity()
{
return (GetQuantity() - m_UnemployedForagers.GetNumber());
}
int CForagerlist::GetUnemployedQuantity()
{
return m_UnemployedForagers.GetNumber();
}
void CForagerlist::KillAll()
{
CBeelist::KillAll();
SetUnemployedForagerQuantity(0);
// Caboose->SetNumber(0);
}
void CForagerlist::Update(CAdult* theAdult, CEvent* theDay)
// The Adult Workers coming in are added to the Forager list.
// If the day is a foraging day, the new adult is pushed onto
// the Pending Forager list and the list is aged one forage increment(.25, .5, .75, or 1.0)
// Then the Pending Forager list is scanned and any elements that have accumulated
// at least one foraging day are moved to the Forager list. If the day is not a foraging
// day, the number of number of bees coming in are just added to the number of
// bees in the first boxcar. Also, a check is
// done on the foragers to see if they have had their lifetime reduced
// due to varroa infestation.
//
// March 2017: Update to limit active forager population to a proportion of total colony size.
//
// Reference Gloria DeGrandi-Hoffman "Establishing the foraging population in VARROAPOP" 3/23/2017
//
// Incoming foragers are all added to the UnemployedForager bucket. Then a quantity of foragers = PropActiveForagers *
// ColonySize is moved from this bucket and added to the first forager boxcar. If not enough foragers are available in
// UnemployedForager then move all the foragers in UnemployedForager and be done.
{
// We will either add adults to the existing first boxcar or we will add a new boxcar
bool AddToFirstBoxcar = true;
// 10% of foragers die over the winter 11/1 - 4/1
if (theDay->GetTime() > COleDateTime(2000, 4, 15, 0, 0, 0))
{
int i = 0;
}
#define WINTER_MORTALITY_PER_DAY 0.10 / 152 // Reduce 10% over the 152 days of winter
ASSERT(theAdult);
// Change lifespan from that of Worker to that of forager
WorkerCount--;
ForagerCount++;
theAdult->SetLifespan(GetColony()->m_CurrentForagerLifespan);
if (theDay->IsForageDay())
{
/**** Bypass the Pending Forager logic - too complicated for not much value. All foraging days age one day
PendingForagers.AddHead(theAdult);
POSITION pos = PendingForagers.GetHeadPosition();
CAdult* pendingAdult;
while (pos != NULL) // Increment the forageIncrement for Pending List
{
pendingAdult = (CAdult*)PendingForagers.GetNext(pos);
pendingAdult->SetForageInc(theAdult->GetForageInc()+theDay->GetForageInc());
}
pos = PendingForagers.GetTailPosition();
POSITION oldpos;
while (pos != NULL)
{
oldpos = pos; // Save old position for possible deletion
pendingAdult = (CAdult*)PendingForagers.GetPrev(pos);
if (pendingAdult->GetForageInc() >= 1.0) // Ready to move some forward now
{
pendingAdult->SetForageInc(0.0);
//AddHead(pendingAdult); // Add the appropriate incoming adults to the first boxcar in forager list.
m_UnemployedForagers.SetNumber(m_UnemployedForagers.GetNumber() + pendingAdult->GetNumber());
AddToFirstBoxcar = false;
PendingForagers.RemoveAt(oldpos);
if (GetCount() >= m_ListLength+1)
{
Caboose = (CAdult*)RemoveTail();
delete Caboose;
ForagerCount--;
Caboose = NULL;
}
else Caboose = NULL;
}
}
********** End of Pending Forager logic */
m_UnemployedForagers.SetNumber(m_UnemployedForagers.GetNumber() + theAdult->GetNumber());
AddToFirstBoxcar = false;
}
else // Non-forage day
{
if (IsEmpty())
{
if (GetLength() > 0) // In special case of 0 lifespan, don't add head
{
// AddHead(theAdult);
m_UnemployedForagers.SetNumber(m_UnemployedForagers.GetNumber() + theAdult->GetNumber());
AddToFirstBoxcar = false;
}
}
else
{
m_UnemployedForagers.SetNumber(m_UnemployedForagers.GetNumber() + theAdult->GetNumber());
AddToFirstBoxcar = true;
}
}
// Calculate how many new foragers we have. Total DesiredForager count is ColonySize*m_PropActualForagers. If
// The NewForagerNumber is the difference between the current active forager quantity and the Desired Forager
// quantity
int NewForagerNumber = (int)(m_pColony->GetColonySize() * m_PropActualForagers) - GetActiveQuantity();
if (NewForagerNumber < 0) NewForagerNumber = 0;
if (NewForagerNumber > m_UnemployedForagers.GetNumber())
NewForagerNumber = m_UnemployedForagers.GetNumber(); // Limit to available potential foragers
int uf = m_UnemployedForagers.GetNumber() - NewForagerNumber;
m_UnemployedForagers.SetNumber(uf);
if (m_UnemployedForagers.GetNumber() > 0) // test
{
int i = 1;
}
if (AddToFirstBoxcar)
{
CAdult* ForagerHead = (CAdult*)GetHead();
ForagerHead->SetNumber(ForagerHead->GetNumber() + NewForagerNumber);
}
else
{
CAdult* NewBoxcar = new CAdult(NewForagerNumber);
AddHead(NewBoxcar);
delete theAdult;
Caboose = (CAdult*)RemoveTail();
delete Caboose;
Caboose = NULL;
}
TRACE("Unemployed Foragers %s --- %d\n", theDay->GetDateStg(), m_UnemployedForagers.GetNumber());
// Check for lifespan reduction
if (GlobalOptions::Get().ShouldApplyLifespanReduction()) // Turn on or off
{
CAdult* ListAdult;
double PropRedux;
int NumMites;
POSITION pos = GetHeadPosition();
int day = WADLLIFE + 1;
while (pos != NULL)
{
ListAdult = (CAdult*)GetNext(pos);
NumMites = ListAdult->GetMites();
if (ListAdult->IsAlive() && (NumMites > 0) && (ListAdult->GetNumber() > 0))
// If already killed, don't kill again, if no mites, ignore
{
PropRedux = m_pColony->LongRedux[int(NumMites / ListAdult->GetNumber())];
if (day > (1 - PropRedux) * (WADLLIFE + GetColony()->m_CurrentForagerLifespan))
{
ListAdult->Kill();
}
}
day++;
}
}
// Is this a winter mortality day?
if ((theDay->GetTime().GetMonth() >= 11) || (theDay->GetTime().GetMonth() < 4))
{
POSITION pos = GetHeadPosition();
CAdult* theForager;
int Number;
while (pos != NULL)
{
theForager = (CAdult*)GetNext(pos);
Number = theForager->GetNumber();
Number = int(Number * (1 - WINTER_MORTALITY_PER_DAY));
theForager->SetNumber(Number);
}
// Mortality for Unemployed Foragers
Number = m_UnemployedForagers.GetNumber();
Number = (int)(Number * (1.0 - WINTER_MORTALITY_PER_DAY));
m_UnemployedForagers.SetNumber(Number);
}
}
void CForagerlist::SetLength(int len)
{
// Because forager lifespan can change as a function of Date/Time spans, SetLength is different than all the other
// life stages If the desired length is greater than the current length, new, empty boxcars are added to the list.
// If the desired length is shorter than the current length, the excess tail boxcars are removed.
CAdult* pForager;
if (len < GetCount())
{
while ((GetCount() > len) && (GetCount() > 0)) // Delete all boxcars over new age value
{
pForager = (CAdult*)RemoveTail();
delete pForager;
}
}
else if (len > GetCount())
{
while (GetCount() < len)
{
pForager = new CAdult(0);
AddTail(pForager);
}
}
m_ListLength = len;
}
/////////////////////////////////////////////////////////////////////////////
//
// CForagerlistA - An update from CForagerlist (created in version 3.2.8.0) to implement a new data structure logic for
// Foragers()-> The design intent is to maintain the same interface as CForagerlist but implement to the new data
// structure which is a longer CAdultlist.
//
CForagerlistA::CForagerlistA() : CAdultlist::CAdultlist()
{
PendingForagers.RemoveAll();
m_PropActualForagers = .3;
}
CForagerlistA::~CForagerlistA()
{
while (!PendingForagers.IsEmpty())
delete (PendingForagers.RemoveHead());
PendingForagers.RemoveAll();
}
void CForagerlistA::ClearPendingForagers()
{
while (!PendingForagers.IsEmpty())
{
CAdult* temp = (CAdult*)PendingForagers.RemoveHead();
ASSERT(temp);
delete (temp);
}
}
int CForagerlistA::GetQuantity()
{
int quan = CBeelist::GetQuantity();
POSITION pos = PendingForagers.GetHeadPosition();
CAdult* temp;
while (pos != NULL)
{
temp = (CAdult*)PendingForagers.GetNext(pos);
quan += temp->GetNumber();
}
return quan;
}
int CForagerlistA::GetActiveQuantity()
{
int Quan = GetQuantity();
if (Quan > 0)
{
if (Quan > m_pColony->GetColonySize() * m_PropActualForagers)
Quan = (int)(m_pColony->GetColonySize() * m_PropActualForagers);
}
return Quan;
}
int CForagerlistA::GetPendingQuantity()
{
int quan = 0;
POSITION pos = PendingForagers.GetHeadPosition();
CAdult* temp;
while (pos != NULL)
{
temp = (CAdult*)PendingForagers.GetNext(pos);
quan += temp->GetNumber();
}
return quan;
}
int CForagerlistA::GetUnemployedQuantity()
{
return (GetQuantity() - GetActiveQuantity());
}
void CForagerlistA::KillAll()
{
CBeelist::KillAll();
// Caboose->SetNumber(0);
}
void CForagerlistA::Update(CAdult* theAdult, CColony* theColony, CEvent* theDay)
// The Adult Workers coming in are added to the Forager list.
// If the day is a foraging day, the new adult is pushed onto
// the Pending Forager list and the list is aged one forage increment(.25, .5, .75, or 1.0)
// Then the Pending Forager list is scanned and any elements that have accumulated
// at least one foraging day are moved to the Forager list. If the day is not a foraging
// day, the number of number of bees coming in are just added to the number of
// bees in the first boxcar. Also, a check is
// done on the foragers to see if they have had their lifetime reduced
// due to varroa infestation.
//
// March 2017: Update to limit active forager population to a proportion of total colony size.
//
// Reference Gloria DeGrandi-Hoffman "Establishing the foraging population in VARROAPOP" 3/23/2017
//
// Incoming foragers are all added to the UnemployedForager bucket. Then a quantity of foragers = PropActiveForagers *
// ColonySize is moved from this bucket and added to the first forager boxcar. If not enough foragers are available in
// UnemployedForager then move all the foragers in UnemployedForager and be done.
//
// June 2017: Eliminating the UnemployedForager bucket - reverting back to original data structure. Will keep track of
// Active forager population as not to exceed the specified percentage of colony population size
{
// 10% of foragers die over the winter 11/1 - 4/1
#define WINTER_MORTALITY_PER_DAY 0.10 / 152 // Reduce 10% over the 152 days of winter
if (theAdult == NULL)
{
// make sure we have an adult box card used to age the foragers list even during winter
theAdult = new CAdult(0);
}
// Change lifespan from that of Worker to that of forager
WorkerCount--;
ForagerCount++;
theAdult->SetLifespan(GetColony()->m_CurrentForagerLifespan);
theAdult->SetCurrentAge(0.0);
const bool pendingForagersFirst = GlobalOptions::Get().ShouldForagersAgeBasedOnForageInc();
if (pendingForagersFirst)
{
// Add the Adult to the Pending Foragers list
if (PendingForagers.GetCount() == 0)
{
PendingForagers.AddHead(theAdult);
}
else
{
// check if the latest pending adult is still 0 day old if yes add the numbers
POSITION pos = PendingForagers.GetHeadPosition();
CAdult* pendingAdult = (CAdult*)PendingForagers.GetNext(pos);
if (pendingAdult->GetForageInc() == 0.0)
{
pendingAdult->SetNumber(pendingAdult->GetNumber() + theAdult->GetNumber());
delete theAdult;
ForagerCount--;
}
else
{
PendingForagers.AddHead(theAdult);
}
}
}
if (theDay->IsForageDay())
{
if (!pendingForagersFirst)
{
PendingForagers.AddHead(theAdult);
}
POSITION pos = PendingForagers.GetHeadPosition();
CAdult* pendingAdult;
while (pos != NULL) // Increment the forageIncrement for Pending List
{
pendingAdult = (CAdult*)PendingForagers.GetNext(pos);
pendingAdult->SetForageInc(pendingAdult->GetForageInc() + theDay->GetForageInc());
}
pos = PendingForagers.GetTailPosition();
// Here we can have several hives of Foragers that Reach 1 day of age,
// we should not make others age more than 2 days
// Keep information either we should age the foragers by 1 day
bool addHead = false;
CAdult* foragersHead = new CAdult();
POSITION oldpos;
while (pos != NULL)
{
oldpos = pos; // Save old position for possible deletion
pendingAdult = (CAdult*)PendingForagers.GetPrev(pos);
if (pendingAdult->GetForageInc() >= 1.0)
{
// At least one hive reached 1 day
addHead = true;
// Update the bees count on the foragers head to be added
foragersHead->SetNumber(foragersHead->GetNumber() + pendingAdult->GetNumber());
// Remove and delete the current pending adult
PendingForagers.RemoveAt(oldpos);
delete pendingAdult;
}
}
// In case we need to age the Foragers, let's add the new head
if (addHead)
{
AddHead(foragersHead);
}
else
delete foragersHead;
// If the foragers list is full let's remove the oldest hive
if (GetCount() == m_ListLength + 1)
{
Caboose = (CAdult*)RemoveTail();
ForagerCount--;
// Update stats for dead Foragers
theColony->m_InOutEvent.m_DeadForagers = Caboose ? Caboose->GetNumber() : 0;
}
else
Caboose = NULL;
}
else if (!pendingForagersFirst)
{
if (IsEmpty())
{
if (GetLength() > 0) AddHead(theAdult); // In special case of 0 lifespan, don't add head
}
else
{
CAdult* ForagerHead = (CAdult*)GetHead();
ForagerHead->SetNumber(ForagerHead->GetNumber() + theAdult->GetNumber());
delete theAdult;
ForagerCount--;
}
}
// Check for lifespan reduction
if (GlobalOptions::Get().ShouldApplyLifespanReduction()) // Turn on or off
{
CAdult* ListAdult;
double PropRedux;
int NumMites;
POSITION pos = GetHeadPosition();
int day = WADLLIFE + 1;
while (pos != NULL)
{
ListAdult = (CAdult*)GetNext(pos);
NumMites = ListAdult->GetMites();
if (ListAdult->IsAlive() && (NumMites > 0) && (ListAdult->GetNumber() > 0))
// If already killed, don't kill again, if no mites, ignore
{
PropRedux = m_pColony->LongRedux[int(NumMites / ListAdult->GetNumber())];
if (day > (1 - PropRedux) * (WADLLIFE + GetColony()->m_CurrentForagerLifespan))
{
ListAdult->Kill();
}
}
day++;
}
}
const bool isWinterMortalityActivated = GlobalOptions::Get().ShouldApplyWinterMortality();
if (isWinterMortalityActivated)
{
// Is this a winter mortality day?
if ((theDay->GetTime().GetMonth() >= 11) || (theDay->GetTime().GetMonth() < 4))
{
POSITION pos = GetHeadPosition();
CAdult* theForager;
int Number;
while (pos != NULL)
{
theForager = (CAdult*)GetNext(pos);
Number = theForager->GetNumber();
int NewNumber = Number * (1 - WINTER_MORTALITY_PER_DAY);
// Update stats for Foragers killed due to winter mortality
theColony->m_InOutEvent.m_WinterMortalityForagersLoss += Number - NewNumber;
Number = NewNumber;
theForager->SetNumber(Number);
}
}
}
}
void CForagerlistA::SetLength(int len)
{
// Because forager lifespan can change as a function of Date/Time spans, SetLength is different than all the other
// life stages If the desired length is greater than the current length, new, empty boxcars are added to the list.
// If the desired length is shorter than the current length, the excess tail boxcars are removed.
CAdult* pForager;
if (len < GetCount())
{
while ((GetCount() > len) && (GetCount() > 0)) // Delete all boxcars over new age value
{
pForager = (CAdult*)RemoveTail();
delete pForager;
}
}
else if (len > GetCount())
{
while (GetCount() < len)
{
pForager = new CAdult(0);
AddTail(pForager);
}
}
m_ListLength = len;
}
/////////////////////////////////////////////////////////////////////////////
//
// CForageBasedAgingForagersList - Drones and Workers
//
void CForageBasedAgingForagersList::Update(
CForageBasedAgingAdultList::CabooseQueue& theAdult, CColony* theColony, CEvent* theEvent)
{
// Age foragers
if (theEvent->IsForageDay())
{
POSITION pos = GetHeadPosition();
while (pos != nullptr)
{
auto forager = (CAdult*)GetNext(pos);
forager->SetCurrentAge(forager->GetCurrentAge() + theEvent->GetForageInc());
}
KillOldForagers();
}
// Add new foragers
while (!theAdult.empty())
{
// Change lifespan from that of Worker to that of forager
WorkerCount--;
ForagerCount++;
auto adult = theAdult.back();
adult->SetLifespan(GetColony()->m_CurrentForagerLifespan);
AddHead(adult);
theAdult.pop_back();
}
// Check for lifespan reduction
if (GlobalOptions::Get().ShouldApplyLifespanReduction()) // Turn on or off
{
CAdult* ListAdult;
double PropRedux;
int NumMites;
double MaxAge;
POSITION pos = GetHeadPosition();
while (pos != NULL)
{
ListAdult = (CAdult*)GetNext(pos);
NumMites = ListAdult->GetMites();
if (ListAdult->IsAlive() && (NumMites > 0) && (ListAdult->GetNumber() > 0))
// If already killed, don't kill again, if no mites, ignore
{
PropRedux = m_pColony->LongRedux[int(NumMites / ListAdult->GetNumber())];
MaxAge = ((1 - PropRedux) * (WADLLIFE + GetColony()->m_CurrentForagerLifespan)) - WADLLIFE;
if (ListAdult->GetCurrentAge() > MaxAge)
{
ListAdult->Kill();
}
}
}
}
const bool isWinterMortalityActivated = GlobalOptions::Get().ShouldApplyWinterMortality();
if (isWinterMortalityActivated)
{
// Is this a winter mortality day?
if ((theEvent->GetTime().GetMonth() >= 11) || (theEvent->GetTime().GetMonth() < 4))
{
POSITION pos = GetHeadPosition();
CAdult* theForager;
int Number;
while (pos != NULL)
{
theForager = (CAdult*)GetNext(pos);
Number = theForager->GetNumber();
int NewNumber = Number * (1 - WINTER_MORTALITY_PER_DAY);
// Update stats for Foragers killed due to winter mortality
theColony->m_InOutEvent.m_WinterMortalityForagersLoss += Number - NewNumber;
Number = NewNumber;
theForager->SetNumber(Number);
}
}
}
}
void CForageBasedAgingForagersList::SetLength(int len)
{
m_ListLength = len;
KillOldForagers();
}
void CForageBasedAgingForagersList::KillOldForagers()
{
const float foragersLifespan = float(GetColony()->m_CurrentForagerLifespan);
POSITION pos = GetTailPosition();
while (pos != nullptr)
{
auto forager = (CAdult*)GetPrev(pos);
// end loop no forager can be older than the current one
if (forager->GetCurrentAge() < foragersLifespan) break;
Caboose = (CAdult*)RemoveTail();
ForagerCount--;
// Update stats for dead Foragers
m_pColony->m_InOutEvent.m_DeadForagers += Caboose->GetNumber();
delete Caboose;
Caboose = NULL;
}
}
/////////////////////////////////////////////////////////////////////////////
//
// CAdultlist - Drones and Workers
//
CAdultlist::CAdultlist()
{
Caboose = nullptr;
PendingAdults = nullptr;
}
CAdultlist::~CAdultlist()
{
}
void CAdultlist::Add(CBrood* theBrood, CColony* theColony, CEvent* theEvent, bool bWorker)
{
ASSERT(theBrood);
{
CAdult* theAdult = new CAdult(theBrood->GetNumber());
if (bWorker) WorkerCount++;
else
DroneCount++;
theAdult->SetMites(theBrood->m_Mites);
theAdult->SetPropVirgins(theBrood->m_PropVirgins);
theAdult->SetLifespan(WADLLIFE);
delete theBrood; // These brood are now gone
POSITION pos = GetHeadPosition();
CAdult* adult = (CAdult*)GetNext(pos);
if (adult != NULL)
{
adult->SetNumber(adult->GetNumber() + theAdult->GetNumber());
adult->SetMites(adult->GetMites() + theAdult->GetMites());
adult->SetPropVirgins(adult->GetPropVirgins() * theAdult->GetPropVirgins());
}
else
{
AddHead(theAdult);
}
}
}
void CAdultlist::Update(CBrood* theBrood, CColony* theColony, CEvent* theEvent, bool bWorker)
// The Capped Brood coming in are converted to Adults and pushed onto the list.
// If the list is now greater than the specified number of days, the
// bottom of the list is removed and assigned to the Caboose for Workers or the
// bottom of the list is deleted for drones. We retain the number of mites from
// the Brood so they can be retrieved and released back into the colony. We also
// establish the maximum lifetime for the new adult worker and see if any other
// workers in the list have extended beyone their lifetime as reduced by varroa infestation
//
// TODO:
//
// When the following are all true (non-foraging day, no brood, no larvae), do not age the adults
{
ASSERT(theBrood);
// int NumberOfNonAdults = theColony->Wlarv.GetQuantity() + theColony->Dlarv.GetQuantity() +
// theColony->CapDrn.GetQuantity() + theColony->CapWkr.GetQuantity() + theBrood->GetNumber();
// if (Caboose != NULL) Caboose->SetNumber(0); // Initialize the Caboose to zero - will pass 0 Adults to foragers
// unless aging occurs if (( theBrood->GetNumber() > 0) || (NumberOfNonAdults > 0) || (theEvent->IsForageDay())) //
// Age if any of these are true
CAdult* theAdult = new CAdult(theBrood->GetNumber());
if (bWorker) WorkerCount++;
else
DroneCount++;
theAdult->SetMites(theBrood->m_Mites);
theAdult->SetPropVirgins(theBrood->m_PropVirgins);
theAdult->SetLifespan(WADLLIFE);
delete theBrood; // These brood are now gone
const bool pendingAdults = GlobalOptions::Get().ShouldAdultsAgeBasedOnForageInc();
if (pendingAdults && bWorker)
{
if (PendingAdults == nullptr) PendingAdults.reset(new CAdultlist);
// Add the Adult to the Pending Foragers list
if (PendingAdults->GetCount() == 0)
{
PendingAdults->AddHead(theAdult);
}
else
{
// check if the latest pending adult is still 0 day old if yes add the numbers
POSITION pos = PendingAdults->GetHeadPosition();
CAdult* pendingAdult = (CAdult*)PendingAdults->GetNext(pos);
if (pendingAdult->GetForageInc() == 0.0)
{
pendingAdult->SetMites(pendingAdult->GetMites() + theAdult->GetMites());
pendingAdult->SetNumber(pendingAdult->GetNumber() + theAdult->GetNumber());
pendingAdult->SetPropVirgins((pendingAdult->GetPropVirgins() + theAdult->GetPropVirgins()) * 0.5);
delete theAdult;
theAdult = nullptr;
if (bWorker) WorkerCount--;
else
DroneCount--;
}
else
{
PendingAdults->AddHead(theAdult);
}
}
Caboose = NULL;
if (theEvent->IsForageDay())
{
POSITION pos = PendingAdults->GetHeadPosition();
CAdult* pendingAdult;
while (pos != NULL) // Increment the forageIncrement for Pending List
{
pendingAdult = (CAdult*)PendingAdults->GetNext(pos);
pendingAdult->SetForageInc(pendingAdult->GetForageInc() + theEvent->GetForageInc());
}
pos = PendingAdults->GetTailPosition();
// Here we can have several hives of Foragers that Reach 1 day of age,
// we should not make others age more than 2 days
// Keep information either we should age the foragers by 1 day
bool addHead = false;
CAdult* adultHead = new CAdult();
POSITION oldpos;
while (pos != NULL)
{
oldpos = pos; // Save old position for possible deletion
pendingAdult = (CAdult*)PendingAdults->GetPrev(pos);
if (pendingAdult->GetForageInc() >= 1.0)
{
// At least one hive reached 1 day
addHead = true;
// Update the bees count on the foragers head to be added
adultHead->SetMites(pendingAdult->GetMites() + adultHead->GetMites());
adultHead->SetNumber(pendingAdult->GetNumber() + adultHead->GetNumber());
adultHead->SetPropVirgins((pendingAdult->GetPropVirgins() + adultHead->GetPropVirgins()) * 0.5);
// Remove and delete the current pending adult
PendingAdults->RemoveAt(oldpos);
delete pendingAdult;
}
}
// In case we need to age the Foragers, let's add the new head
if (addHead)
{
AddHead(adultHead);
}
else
delete adultHead;
// If the foragers list is full let's remove the oldest hive
if (GetCount() == m_ListLength + 1)
{
Caboose = (CAdult*)RemoveTail();
if (bWorker) WorkerCount--;
else
DroneCount--;
// Update stats for dead Foragers
theColony->m_InOutEvent.m_DeadDAdults = Caboose ? Caboose->GetNumber() : 0;
}
}
}
else
{
AddHead(theAdult);
int count = GetCount();
if (GetCount() == m_ListLength + 1) // All Boxcars are full - put workers in caboose, drones die off
{
Caboose = (CAdult*)RemoveTail();
Caboose->number *= GetPropTransition();
if (!bWorker)
{
// Update stats for dead drones
theColony->m_InOutEvent.m_DeadDAdults = Caboose->GetNumber();
delete Caboose;
DroneCount--;
Caboose = NULL;
}
}