-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathHost.h
More file actions
983 lines (854 loc) · 27.2 KB
/
Copy pathHost.h
File metadata and controls
983 lines (854 loc) · 27.2 KB
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
#ifndef HOST_H
#define HOST_H
#include "../../Empirical/include/emp/math/Random.hpp"
#include "../../Empirical/include/emp/tools/string_utils.hpp"
#include <iomanip> // setprecision
#include <sstream> // stringstream
#include <string>
#include "../Organism.h"
#include "SymWorld.h"
class Host: public Organism {
protected:
/**
*
* Purpose: Represents the interaction value between the host and symbiont.
* A negative interaction value represent antagonism, while a positive
* one represents mutualism. Zero is a neutral value.
*
*/
double interaction_val = 0;
/**
*
* Purpose: Represents the number of updates the host
* has lived through; at birth is set to 0.
*
*/
int age = 0;
/**
*
* Purpose: Tracks the number of reproductive events in this host's lineage.
*
*/
size_t reproductions = 0;
/**
*
* Purpose: Tracks the number of tag flips towards partner in this host's lineage.
*
*/
size_t towards_partner_count = 0;
/**
*
* Purpose: Tracks the number of tag flips away from partner in this host's lineage.
*
*/
size_t from_partner_count = 0;
/**
*
* Purpose: Tracks the number of tag flips away from partner in this host's lineage.
*
*/
double tag_permissiveness = 0;
/**
*
* Purpose: Represents the set of symbionts belonging to a host.
* This can be set with SetSymbionts(), and symbionts can be
* added with AddSymbiont(). This can be cleared with ClearSyms()
*
*/
emp::vector<emp::Ptr<Organism>> syms = {};
/**
*
* Purpose: Represents the set of in-progress "reproductive" symbionts belonging to a host. These are symbionts that aren't yet active.
* Symbionts can be added with AddReproSym(). This can be cleared with ClearSyms()
*
*/
emp::vector<emp::Ptr<Organism>> repro_syms = {};
/**
*
* Purpose: Represents the resource points possessed by a host.
* This is what hosts must collect to reproduce.
*
*/
double points = 0;
/**
*
* Purpose: Represents the resources that could be in the process of
* being stolen.
*
*/
double res_in_process = 0;
/**
*
* Purpose: Represents an instance of random.
*
*/
emp::Ptr<emp::Random> random = NULL;
/**
*
* Purpose: Represents the world that the hosts are living in.
*
*/
emp::Ptr<SymWorld> my_world = NULL;
/**
*
* Purpose: Represents the configuration settings for a particular run.
*
*/
emp::Ptr<SymConfigBase> my_config = NULL;
/**
*
* Purpose: Represents if a host is alive. This is set to true when a host is killed.
*
*/
bool dead = false;
/**
*
* Purpose: Represents the tag for this organism
*
*/
emp::BitSet<TAG_LENGTH> tag;
/**
*
* Purpose: Tracks the taxon of this organism.
*
*/
emp::Ptr<taxon_t::base_taxon_t> my_taxon = NULL;
/**
* Purpose: To track location in the world
*
*/
emp::WorldPosition location;
public:
/**
* The constructor for the host class
*/
Host(
emp::Ptr<emp::Random> _random,
emp::Ptr<SymWorld> _world,
emp::Ptr<SymConfigBase> _config,
double _intval =0.0,
emp::vector<emp::Ptr<Organism>> _syms = {},
emp::vector<emp::Ptr<Organism>> _repro_syms = {},
double _points = 0.0
) :
interaction_val(_intval),
syms(_syms),
repro_syms(_repro_syms),
points(_points),
random(_random),
my_world(_world),
my_config(_config)
{
if (_intval == -2) {
interaction_val = random->GetDouble(-1, 1);
}
if (interaction_val > 1 || interaction_val < -1) {
throw "Invalid interaction value. Must be between -1 and 1"; // Exception for invalid interaction value
}
if (my_config->TAG_MATCHING() && my_config->HOST_TAG_PERMISSIVENESS_EVOLVES()) {
tag_permissiveness = my_config->TAG_PERMISSIVENESS();
}
}
/**
* Input: None
*
* Output: None
*
* Purpose: To delete the memory used by a host's symbionts when the host is deleted.
*/
~Host() {
for(size_t i = 0; i < syms.size(); i++) {
syms[i].Delete();
}
for(size_t j = 0; j < repro_syms.size(); j++) {
repro_syms[j].Delete();
}
}
/**
* Input: None
*
* Output: None
*
* Purpose: To force a copy constructor to be generated by the compiler.
*/
Host(const Host &) = default;
/**
* Input: None
*
* Output: None
*
* Purpose: To force a move constructor to be generated by the compiler
*/
Host(Host &&) = default;
/**
* Input: None
*
* Output: None
*
* Purpose: To tell the compiler to use its default generated variants of the constructor
*/
Host() = default;
/**
* Input: None
*
* Output: None
*
* Purpose: To force a copy assignment operator to be generated by the compiler.
*/
Host & operator=(const Host &) = default;
/**
* Input: None
*
* Output: None
*
* Purpose: To force a move assignment operator to be generated by the compiler.
*/
Host & operator=(Host &&) = default;
/**
* Input: An object of host to be compared to the current host.
*
* Output: To boolean representing if thing1 == &thing2
*
* Purpose: To override the bool operator == to return (thing1 == &thing2)
*/
bool operator==(const Host& other) const { return (this == &other); }
/**
* Input: An object of host, and the address of the thing it is being
* compared to.
*
* Output: To boolean representing if *thing1 == thing2
*
* Purpose: To override the bool operator != to return !(*thing1 == thing2)
*/
bool operator!=(const Host &other) const {return !(*this == other);}
/**
* Input: None
*
* Output: Name of class as string, Host
*
* Purpose: To know which subclass the object is
*/
std::string const GetName() const {
return "Host";
}
/**
* Input: Set the reproduction counter
*
* Output: None
*
* Purpose: To set the count of reproductions in this lineage.
*/
void LineageLength(size_t _in) { reproductions = _in; }
/**
* Input: None.
*
* Output: The reproduction count
*
* Purpose: To get the count of reproductions in this lineage.
*/
size_t GetReproCount() const { return reproductions; }
/**
* Input: Set the flips towards a partner counter
*
* Output: None
*
* Purpose: To set the count of flips towards a partner in this lineage.
*/
void SetTowardsPartnerCount(size_t _in) { towards_partner_count = _in; }
/**
* Input: None.
*
* Output: The flips towards a partner count
*
* Purpose: To get the count of flips towards a partner in this lineage.
*/
size_t GetTowardsPartnerCount() const { return towards_partner_count; }
/**
* Input: Set the flips from a partner counter
*
* Output: None
*
* Purpose: To set the count of flips from a partner in this lineage.
*/
void SetFromPartnerCount(size_t _in) { from_partner_count = _in; }
/**
* Input: None.
*
* Output: The flips from a partner count
*
* Purpose: To get the count of flips from a partner in this lineage.
*/
size_t GetFromPartnerCount() const { return from_partner_count; }
/**
* Input: None
*
* Output: The double representing host's interaction value
*
* Purpose: To get the double representing host's interaction value
*/
double GetIntVal() const { return interaction_val; }
emp::Ptr<taxon_t::base_taxon_t> GetTaxon() {
return my_taxon;
}
virtual void SetTaxon(emp::Ptr<taxon_t::base_taxon_t> _in) {
my_taxon = _in;
}
/**
* Input: None
*
* Output: A vector of pointers to the organisms that are the host's syms.
*
* Purpose: To get the vector containing pointers to the host's symbionts.
*/
emp::vector<emp::Ptr<Organism>>& GetSymbionts() { return syms; }
/**
* Input: Symbiont index
*
* Output: Reference to symbiont at given index in host's syms vector.
*
* Purpose: Increase ease of access to individual symbionts.
*/
Organism& GetSymbiont(size_t sym_id) {
emp_assert(sym_id < syms.size());
return *(syms[sym_id]);
}
/**
* Input: Symbiont index
*
* Output: const reference to symbiont at given index in host's syms vector.
*
* Purpose: Increase ease of access to individual symbionts.
*/
const Organism& GetSymbiont(size_t sym_id) const {
emp_assert(sym_id < syms.size());
return *syms[sym_id];
}
/**
* Input: None
*
* Output: A vector of pointers to the organisms that are the host's repro syms.
*
* Purpose: To get the vector containing pointers to the host's repro syms.
*/
emp::vector<emp::Ptr<Organism>>& GetReproSymbionts() { return repro_syms; }
/**
* Input: None
*
* Output: The double representing a host's points.
*
* Purpose: To get the host's points.
*/
double GetPoints() const { return points; }
/**
* Input: None
*
* Output: The double representing res_in_process
*
* Purpose: To get the value of res_in_process
*/
double GetResInProcess() const { return res_in_process; }
/**
* Input: None
*
* Output: The world position of the organism
*
* Purpose: To get the world position of the organism
*/
emp::WorldPosition GetLocation() const { return location; }
/**
* Input: None
*
* Output: The bool representing if an organism is a host.
*
* Purpose: To determine if an organism is a host.
*/
bool IsHost() const { return true; }
/**
* Input: A double representing the host's new interaction value.
*
* Output: None
*
* Purpose: To set a host's interaction value.
*/
void SetIntVal(double _in) {
emp_assert(_in <= 1 && _in >= -1, "Invalid interaction value. Must be between -1 and 1");
interaction_val = _in;
}
/**
* Input: A vector of pointers to organisms that will become a host's symbionts.
*
* Output: None
*
* Purpose: To set a host's symbionts to the input vector of organisms.
*/
void SetSymbionts(emp::vector<emp::Ptr<Organism>> _in) {
ClearSyms();
for (size_t i = 0; i < _in.size(); i++) {
AddSymbiont(_in[i]);
}
}
/**
* Input: A double representing a host's new point value.
*
* Output: None
*
* Purpose: To set a host's points.
*/
void SetPoints(double _in) { points = _in; }
/**
* Input: A new world position
*
* Output: None
*
* Purpose: To set the organism's world position
*/
virtual void SetLocation(emp::WorldPosition _in) { location = _in; }
/**
* Input: None
*
* Output: None
*
* Purpose: To clear a host's symbionts.
*/
void ClearSyms() {
syms.resize(0);
}
/**
* Input: None
*
* Output: None
*
* Purpose: To clear a host's repro symbionts.
*/
void ClearReproSyms() { repro_syms.resize(0); }
/**
* Input: The new tag
*
* Output: None
*
* Purpose: To set a host's tag.
*/
void SetTag(const emp::BitSet<TAG_LENGTH>& _in) { tag.Import(_in); }
/**
* Input: None
*
* Output: The host's tag.
*
* Purpose: To get a host's tag.
*/
emp::BitSet<TAG_LENGTH>& GetTag() { return tag; }
/**
* Input: None
*
* Output: const reference to the host's tag.
*
* Purpose: To get a host's tag.
*/
const emp::BitSet<TAG_LENGTH>& GetTag() const { return tag; }
/**
* Input: The tag permissiveness value to set for this host
*
* Output: None
*
* Purpose: To set the tag permissiveness value of this host
*/
void SetTagPermissiveness(double _in) { tag_permissiveness = _in; }
/**
* Input: None
*
* Output: The tag permissiveness value of this host
*
* Purpose: To get the tag permissiveness value of this host
*/
double GetTagPermissiveness() const { return tag_permissiveness; }
/**
* Input: None
*
* Output: None
*
* Purpose: To kill a host.
*/
void SetDead() { dead = true; }
/**
* Input: The double to be set as res_in_process
*
* Output: None
*
* Purpose: To set the value of res_in_process
*/
void SetResInProcess(double _in) { res_in_process = _in; }
/**
* Input: None
*
* Output: boolean
*
* Purpose: To determine if a host is dead.
*/
bool GetDead() const { return dead; }
/**
* Input: None
*
* Output: an int representing the current age of the Host
*
* Purpose: To get the Host's age.
*/
int GetAge() const { return age; }
/**
* Input: An int of what age the Host should be set to
*
* Output: None
*
* Purpose: To set the Host's age for testing purposes.
*/
void SetAge(int _in) { age = _in; }
/**
* Input: None
*
* Output: None
*
* Purpose: Increments age by one and kills it if too old.
*/
void GrowOlder() {
age = age + 1;
if (age > my_config->HOST_AGE_MAX() && my_config->HOST_AGE_MAX() > 0) {
SetDead();
}
}
/**
* Input: The interaction value of the symbiont that
* is eligible to steal resources from the host.
*
* Output: The double representing the amount of resources
* that are actually stolen from the host.
*
* Purpose: To determine if a host's symbiont is eligible to
* steal resources from the host.
*/
double StealResources(double _intval) {
double host_int_val = GetIntVal();
double res_in_process = GetResInProcess();
// calculate how many resources another organism can steal from this host
if (host_int_val > 0) { // cooperative hosts shouldn't be over punished by StealResources
host_int_val = 0;
}
if (_intval < host_int_val) {
//organism trying to steal can overcome host's defense
double stolen = (host_int_val - _intval) * res_in_process;
double remaining_resources = res_in_process - stolen;
SetResInProcess(remaining_resources);
return stolen;
} else {
//defense cannot be overcome, no resources are stolen
return 0;
}
}
/**
* Input: The double representing the number of points to be incremented onto a host's points.
*
* Output: None
*
* Purpose: To increment a host's points by the input value.
*/
void AddPoints(double _in) { points += _in; }
/**
* Input: The symbiont index position to remove (remember it should be 1-indexed)
*
* Output: The removed symbiont or null if invalid index given
*
* Purpose: To allow removal of a symbiont
*/
emp::Ptr<Organism> RemoveSymbiont(int index) {
int num_syms = syms.size();
if (index < 1 || index > num_syms) {
return nullptr;
} else {
emp::Ptr<Organism> to_remove = syms[index-1];
syms.erase(syms.begin() + (index-1));
to_remove->SetHost(nullptr);
to_remove->SetLocation(emp::WorldPosition::invalid_id);
return to_remove;
}
}
/**
* Input: The pointer to the organism that is to be added to the host's symbionts.
*
* Output: The int describing the symbiont's position ID, or 0 if it did not successfully
* get added to the host's list of symbionts.
*
* Purpose: To add a symbionts to a host's symbionts
*/
int AddSymbiont(emp::Ptr<Organism> _in) {
bool allowed_in = SymAllowedIn();
if (my_config->OUSTING() && allowed_in && (int)syms.size() == my_config->SYM_LIMIT()) {
// if there's more than one sym, randomly choose one to replace, otherwise replace the one sym
const int new_sym_pos = (syms.size() > 1) ? random->GetInt(syms.size()) : 0;
emp::Ptr<Organism> old_sym = syms[new_sym_pos];
my_world->SendToGraveyard(old_sym);
syms[new_sym_pos] = _in;
_in->SetHost(this);
_in->UponInjection();
_in->SetLocation(emp::WorldPosition(new_sym_pos+1, location.GetIndex())); // +1 because 0 is reserved for free-living symbionts
return new_sym_pos+1;
} else if ((int)syms.size() < my_config->SYM_LIMIT() && allowed_in) {
syms.push_back(_in);
_in->SetHost(this);
_in->UponInjection();
_in->SetLocation(emp::WorldPosition(syms.size(), location.GetIndex()));
return syms.size();
} else {
_in.Delete();
return 0;
}
}
/**
* Input: None
*
* Output: A bool representing if a symbiont will be allowed to enter a host.
*
* Purpose: To determine if a symbiont will be allowed into a host. If phage exclusion is off, this function will
* always return true. If phage exclusion is on, then there is a 1/2^n chance of a new phage being allowed in,
* where n is the number of existing phage.
*/
bool SymAllowedIn() {
if (!my_config->PHAGE_EXCLUDE()) {
return true;
} else {
int num_syms = syms.size();
//essentially imitates a 1/ 2^n chance, with n = number of symbionts
int enter_chance = random->GetUInt((int)pow(2.0, num_syms));
if (enter_chance == 0) { return true; }
return false;
}
}
/**
* Input: A pointer to the organism to be added to the host's symbionts.
*
* Output: None
*
* Purpose: To add a repro sym to the host's symbionts.
*/
void AddReproSym(emp::Ptr<Organism> _in) { repro_syms.push_back(_in); }
/**
* Input: None
*
* Output: A bool representing if a host has any symbionts.
*
* Purpose: To determine if a host has any symbionts, though they might be corpses that haven't been removed yet.
*/
bool HasSym() const {
return syms.size() != 0;
}
/**
* Input: None.
*
* Output: A new host with same properties as this host.
*
* Purpose: To avoid creating an organism via constructor in other methods.
*/
emp::Ptr<Organism> MakeNew() {
emp::Ptr<Host> new_host = emp::NewPtr<Host>(random, my_world, my_config, GetIntVal());
if (my_config->TAG_MATCHING()) {
new_host->SetTag(GetTag());
if (my_config->HOST_TAG_PERMISSIVENESS_EVOLVES()) new_host->SetTagPermissiveness(tag_permissiveness);
}
return new_host;
}
/**
* Input: None.
*
* Output: A new host baby of the current host, mutated.
*
* Purpose: To create a new baby host and reset this host's points to 0.
*/
emp::Ptr<Organism> Reproduce() {
emp::Ptr<Organism> host_baby = MakeNew();
host_baby->Mutate();
host_baby->LineageLength(reproductions + 1);
SetPoints(0);
if (my_config->TAG_MATCHING() && HasSym()) {
// do not xor to get 1 where bits are matching
emp::BitSet<TAG_LENGTH> sym_host_parent_matching = syms[0]->GetTag().XOR(tag).NOT();
emp::BitSet<TAG_LENGTH> sym_host_baby_matching = syms[0]->GetTag().XOR(host_baby->GetTag()).NOT();
// difference in matching-ness, with match in child
emp::BitSet<TAG_LENGTH> child_towards = sym_host_baby_matching.XOR(sym_host_parent_matching).AND(sym_host_baby_matching);
// difference in matching-ness, with match in parent
emp::BitSet<TAG_LENGTH> child_from = sym_host_baby_matching.XOR(sym_host_parent_matching).AND(sym_host_parent_matching);
host_baby->SetTowardsPartnerCount(child_towards.CountOnes() + towards_partner_count);
host_baby->SetFromPartnerCount(child_from.CountOnes() + from_partner_count);
}
return host_baby;
}
/**
* Input: None
*
* Output: None
*
* Purpose: To mutate a host's interaction value. This is called on newly generated
* hosts to allow for evolution to occur.
*/
void Mutate() {
double mutation_size = my_config->HOST_MUTATION_SIZE();
if (mutation_size == -1) mutation_size = my_config->MUTATION_SIZE();
double mutation_rate = my_config->HOST_MUTATION_RATE();
if (mutation_rate == -1) mutation_rate = my_config->MUTATION_RATE();
if (random->GetDouble(0.0, 1.0) <= mutation_rate) {
interaction_val += random->GetNormal(0.0, mutation_size);
if (interaction_val < -1) interaction_val = -1;
else if (interaction_val > 1) interaction_val = 1;
}
if (my_config->TAG_MATCHING()) {
if (my_config->HOST_TAG_PERMISSIVENESS_EVOLVES()) {
double permissiveness_mutation_rate = my_config->HOST_TAG_PERMISSIVENESS_MUTATION_RATE();
if (permissiveness_mutation_rate == -1) permissiveness_mutation_rate = mutation_rate;
if (random->GetDouble(0.0, 1.0) <= permissiveness_mutation_rate) {
tag_permissiveness += random->GetNormal(0.0, my_config->HOST_TAG_PERMISSIVENESS_MUTATION_SIZE());
}
}
tag.FlipRandom(my_world->GetRandom(), my_config->TAG_MUTATION_SIZE());
}
}
/**
* Input: The double representing the number of resources to be distributed to the host and its symbionts and the position of the host in the world.
*
* Output: None
*
* Purpose: To distribute resources to a host and its symbionts. In the event that the host has no symbionts,
* the host gets all resources not allocated to defense or given to absent partner. Otherwise, the resource
* is split into equal chunks for each symbiont
*/
void DistribResources(double resources) {
double hostIntVal = interaction_val; //using private variable because we can
//do ectosymbiosis if the config setting is on, there is a parallel sym
//In the event that the host has no symbionts, the host gets all resources not allocated to defense or
// given to absent partner.
if (syms.empty()) {
if (hostIntVal >= 0) {
double spent = resources * hostIntVal;
this->AddPoints(resources - spent);
}
else {
double hostDefense = -1.0 * hostIntVal * resources;
this->AddPoints(resources - hostDefense);
}
return; //This concludes resource distribution for a host without symbionts
}
size_t num_sym = syms.size();
double sym_piece = (double) resources / num_sym;
for(size_t i=0; i < syms.size(); i++) {
DistribResToSym(syms[i], sym_piece);
}
} //end DistribResources
/**
* Input: The total resources received by the host and its location in the world.
*
* Output: The resources remaining after the host maybe does ectosymbiosis.
*
* Purpose: To handle ectosymbiosis.
*/
double HandleEctosymbiosis(double resources, size_t location) {
double leftover_resources = resources;
if (GetDoEctosymbiosis(location)) {
double sym_piece = leftover_resources / (syms.size() + 1); //if there are no endo syms, the ecto sym will handle all the resources
DistribResToSym(my_world->GetSymAt(location), sym_piece);
leftover_resources = leftover_resources - sym_piece; //leave the leftover resources to be split by other syms
}
return leftover_resources;
}
/**
* Input: The location of this host in the world.
*
* Output: A bool value representing whether this host should interact with a parallel sym
*
* Purpose: To determine whether a host should interact with a parallel sym
*/
bool GetDoEctosymbiosis(size_t location) {
//a host is immune to ectosymbiosis if immunity is on and it has a sym.
if (!my_config->ECTOSYMBIOSIS()) return false; //if the config setting is off, we immediately know that ectosymbiosis won't happen
else {
bool is_immune = my_config->ECTOSYMBIOTIC_IMMUNITY() && HasSym();
bool valid_sym = my_world->GetSymAt(location) != nullptr && !my_world->GetSymAt(location)->GetDead();
return (valid_sym == true) && (is_immune == false);
}
}
/**
* Input: The sym to whom resources are distributed and the resources it might recieve.
*
* Output: None
*
* Purpose: To distribute resources between sym and host depending on their interaction values.
*/
void DistribResToSym(emp::Ptr<Organism> sym, double sym_piece) {
double host_int_val = interaction_val;
double host_donation = 0;
if (host_int_val < 0) {
double host_defense = host_int_val * sym_piece * -1.0;
host_donation = 0;
SetResInProcess(sym_piece - host_defense);
}
else if (host_int_val >= 0) {
host_donation = host_int_val * sym_piece;
SetResInProcess(sym_piece - host_donation);
}
double sym_return = sym->ProcessResources(host_donation, this);
this->AddPoints(sym_return + GetResInProcess());
SetResInProcess(0);
}
/**
* Input: The size_t value representing the location of the host.
*
* Output: None
*
* Purpose: To process the host, meaning determining eligibility for reproduction, checking for vertical
* transmission, removing dead syms, and processing alive syms.
*/
void Process(emp::WorldPosition pos) {
// tracking int val for tag and individual phylogenies
if (my_config->PHYLOGENY() && my_world->GetPhylogenyTaxonType() == SymWorld::PHYLO_TAXON_TYPE::TAG) {
my_taxon->GetData().RecordIntVal(GetIntVal());
}
size_t location = pos.GetIndex();
//Currently just wrapping to use the existing function
double desired_resources = my_config->RES_DISTRIBUTE();
double world_resources = my_world->PullResources(desired_resources); //receive resources from the world
double resources = HandleEctosymbiosis(world_resources, location);
if (resources > 0) DistribResources(resources); //if there are enough resources left, distribute them.
// Check reproduction
if (GetPoints() >= my_config->HOST_REPRO_RES() && repro_syms.size() == 0) { // if host has more points than required for repro
// will replicate & mutate a random offset from parent values
// while resetting resource points for host and symbiont to zero
emp::Ptr<Organism> host_baby = Reproduce();
//Now check if symbionts get to vertically transmit
for(size_t j = 0; j< (GetSymbionts()).size(); j++) {
emp::Ptr<Organism> parent = GetSymbionts()[j];
parent->VerticalTransmission(host_baby);
}
my_world->DoBirth(host_baby, location); //Automatically deals with grid
}
if (GetDead()) {
return; //If host is dead, return
}
if (HasSym()) { //let each sym do whatever they need to do
emp::vector<emp::Ptr<Organism>>& syms = GetSymbionts();
for(size_t j = 0; j < syms.size(); j++) {
emp::Ptr<Organism> cur_sym = syms[j];
if (GetDead()) {
return; //If previous symbiont killed host, we're done
}
//sym position should have host index as pop_id and
//position in syms list + 1 as index (0 as fls index)
emp::WorldPosition sym_pos = emp::WorldPosition(j+1, location);
if (!cur_sym->GetDead()) {
cur_sym->Process(sym_pos);
}
if (cur_sym->GetDead()) {
//if the symbiont dies during their process, remove from syms list
//UNLESS they died by getting ousted
syms.erase(syms.begin() + j);
cur_sym.Delete();
}
} //for each sym in syms
} //if org has syms
GrowOlder();
}
}; //Host
#endif