Skip to content

Commit

Permalink
Merge pull request #188 from K-Johnson-Horrigan/dev-branch
Browse files Browse the repository at this point in the history
SymWorld Setup() method
  • Loading branch information
anyaevostinar committed Nov 1, 2022
2 parents 1d8ab3d + ed83fff commit 108a1c3
Show file tree
Hide file tree
Showing 29 changed files with 852 additions and 286 deletions.
2 changes: 1 addition & 1 deletion source/SymAnimate.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class SymAnimate : public UI::Animate {
random.ResetSeed(config.SEED());
world.SetRandom(random);

worldSetup(&world, &config);
world.Setup();

p = world.GetPop();

Expand Down
24 changes: 16 additions & 8 deletions source/default_mode/DataNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,11 @@ emp::DataMonitor<int>& SymWorld::GetHostCountDataNode() {
data_node_hostcount.New();
OnUpdate([this](size_t){
data_node_hostcount -> Reset();
for (size_t i = 0; i< pop.size(); i++)
if(IsOccupied(i))
for (size_t i = 0; i< pop.size(); i++){
if (IsOccupied(i)){
data_node_hostcount->AddDatum(1);
}
}
});
}
return *data_node_hostcount;
Expand Down Expand Up @@ -289,9 +291,11 @@ emp::DataMonitor<int>& SymWorld::GetCountHostedSymsDataNode(){
data_node_hostedsymcount.New();
OnUpdate([this](size_t){
data_node_hostedsymcount->Reset();
for (size_t i = 0; i< pop.size(); i++)
if (IsOccupied(i))
for (size_t i = 0; i< pop.size(); i++){
if (IsOccupied(i)){
data_node_hostedsymcount->AddDatum(pop[i]->GetSymbionts().size());
}
}
});
}
return *data_node_hostedsymcount;
Expand All @@ -312,9 +316,11 @@ emp::DataMonitor<int>& SymWorld::GetCountFreeSymsDataNode(){
data_node_freesymcount.New();
OnUpdate([this](size_t){
data_node_freesymcount->Reset();
for (size_t i = 0; i< pop.size(); i++)
if (sym_pop[i])
for (size_t i = 0; i< pop.size(); i++){
if (sym_pop[i]){
data_node_freesymcount->AddDatum(1);
}
}
});
}
return *data_node_freesymcount;
Expand Down Expand Up @@ -366,9 +372,11 @@ emp::DataMonitor<double, emp::data::Histogram>& SymWorld::GetHostIntValDataNode(
data_node_hostintval.New();
OnUpdate([this](size_t){
data_node_hostintval->Reset();
for (size_t i = 0; i< pop.size(); i++)
if (IsOccupied(i))
for (size_t i = 0; i< pop.size(); i++){
if (IsOccupied(i)){
data_node_hostintval->AddDatum(pop[i]->GetIntVal());
}
}
});
}
data_node_hostintval->SetupBins(-1.0, 1.1, 21);
Expand Down
5 changes: 4 additions & 1 deletion source/default_mode/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ class Host: public Organism {
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 > 1 || _intval < -1) {
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
};
}
Expand Down
29 changes: 27 additions & 2 deletions source/default_mode/SymWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,29 @@ class SymWorld : public emp::World<Organism>{


/**
* Input: The pointer to an organism that will be injected into a host.
* Input: The pointer to a host that will be added to the world. This function assumes that the
* pop vector has not been resized to fit the world yet.
*
* Output: None
*
* Purpose: To add a host to the world.
*/
void InjectHost(emp::Ptr<Organism> new_host) {
if (my_config->GRID()) {
AddOrgAt(new_host, emp::WorldPosition(GetRandomCellID()));
}
else {
AddOrgAt(new_host, pop.size());
}
}


/**
* Input: The pointer to an organism that will be injected into the world.
*
* Output: None
*
* Purpose: To add a symbiont to a host's symbionts.
* Purpose: To add a symbiont to the world, either into a host or into a sym world cell.
*/
void InjectSymbiont(emp::Ptr<Organism> new_sym){
size_t new_loc;
Expand Down Expand Up @@ -449,6 +467,13 @@ class SymWorld : public emp::World<Organism>{
emp::DataMonitor<double,emp::data::Histogram>& GetFreeSymInfectChanceDataNode();
emp::DataMonitor<double,emp::data::Histogram>& GetHostedSymInfectChanceDataNode();

/**
* Definitions of setup functions, expanded in WorldSetup.cc
*/
virtual void Setup();
virtual void SetupHosts(long unsigned int* POP_SIZE);
virtual void SetupSymbionts(long unsigned int* total_syms);

/**
* Input: The pointer to the symbiont that is moving, the WorldPosition of its
* current location.
Expand Down
6 changes: 4 additions & 2 deletions source/default_mode/Symbiont.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ class Symbiont: public Organism {
infection_chance = my_config->SYM_INFECTION_CHANCE();
if (infection_chance == -2) infection_chance = random->GetDouble(0,1); //randomized starting infection chance
if (infection_chance > 1 || infection_chance < 0) throw "Invalid infection chance. Must be between 0 and 1"; //exception for invalid infection chance

if ( _intval > 1 || _intval < -1) {
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
};
}
Expand Down
96 changes: 45 additions & 51 deletions source/default_mode/WorldSetup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,65 @@
#define WORLD_SETUP_C

#include "SymWorld.h"
#include "../ConfigSetup.h"
#include "Host.h"
#include "Symbiont.h"

/**
* Input: The number of hosts.
*
* Output: None.
*
* Purpose: To populate the world with hosts with appropriate phenotypes.
*/
void SymWorld::SetupHosts(long unsigned int* POP_SIZE){
for (size_t i = 0; i < *POP_SIZE; i++) {
emp::Ptr<Host> new_org;
new_org.New(&GetRandom(), this, my_config, my_config->HOST_INT());
InjectHost(new_org);
}

void worldSetup(emp::Ptr<SymWorld> world, emp::Ptr<SymConfigBase> my_config) {
// params
emp::Random& random = world->GetRandom();
}


/**
* Input: The number of symbionts.
*
* Output: None.
*
* Purpose: To populate the world with symbionts with appropriate phenotypes.
*/
void SymWorld::SetupSymbionts(long unsigned int *total_syms) {
for (size_t j = 0; j < *total_syms; j++) {
emp::Ptr<Symbiont> new_sym = emp::NewPtr<Symbiont>(&GetRandom(), this, my_config, my_config->SYM_INT(), 0);
InjectSymbiont(new_sym);
}
}

/**
* Input: None.
*
* Output: None.
*
* Purpose: Prepare the world for an experiment by applying the configuration settings
* and populating the world with hosts and symbionts.
*/
void SymWorld::Setup() {
double start_moi = my_config->START_MOI();
long unsigned int POP_SIZE;
if (my_config->POP_SIZE() == -1) {
POP_SIZE = my_config->GRID_X() * my_config->GRID_Y();
} else {
POP_SIZE = my_config->POP_SIZE();
}
bool random_phen_host = false;
bool random_phen_sym = false;
if(my_config->HOST_INT() == -2 && !my_config->COMPETITION_MODE()) random_phen_host = true;
if(my_config->SYM_INT() == -2) random_phen_sym = true;

if (my_config->GRID() == 0) {world->SetPopStruct_Mixed(false);}
else world->SetPopStruct_Grid(my_config->GRID_X(), my_config->GRID_Y(), false);


double comp_host_1 = 0;
double comp_host_2 = 0.95;



//inject hosts
for (size_t i = 0; i < POP_SIZE; i++){
emp::Ptr<Host> new_org;

if (random_phen_host) {new_org.New(&random, world, my_config, random.GetDouble(-1, 1));
} else if (my_config->COMPETITION_MODE() && i%2==0) {
new_org.New(&random, world, my_config, comp_host_1);
} else if (my_config->COMPETITION_MODE() && i%2==1) {
new_org.New(&random, world, my_config, comp_host_2);
} else { new_org.New(&random, world, my_config, my_config->HOST_INT());
}
//Currently hacked because there isn't an AddOrg function, but there probably should be
if(my_config->GRID()) {
world->AddOrgAt(new_org, emp::WorldPosition(world->GetRandomCellID()));
} else {
world->AddOrgAt(new_org, world->size());
}
//world.Inject(*new_org);
}

//sets up the world size
world->Resize(my_config->GRID_X(), my_config->GRID_Y());
// set world structure (either mixed or a grid with some dimensions) and set synchronous generations to false
if (my_config->GRID() == 0) {SetPopStruct_Mixed(false);}
else SetPopStruct_Grid(my_config->GRID_X(), my_config->GRID_Y(), false);

//This loop must be outside of the host generation loop since otherwise
//syms try to inject into mostly empty spots at first
int total_syms = POP_SIZE * start_moi;
for (int j = 0; j < total_syms; j++){
double sym_int = 0;
if (random_phen_sym) {sym_int = random.GetDouble(-1,1);}
else {sym_int = my_config->SYM_INT();}
SetupHosts(&POP_SIZE);

emp::Ptr<Symbiont> new_sym = emp::NewPtr<Symbiont>(&random, world, my_config,
sym_int, 0);
world->InjectSymbiont(new_sym);
}
Resize(my_config->GRID_X(), my_config->GRID_Y());
long unsigned int total_syms = POP_SIZE * start_moi;
SetupSymbionts(&total_syms);
}

#endif
9 changes: 9 additions & 0 deletions source/efficient_mode/EfficientWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class EfficientWorld : public SymWorld {
if (data_node_efficiency) data_node_efficiency.Delete();
}


/**
* Definitions of setup functions, expanded in EfficientWorldSetup.cc
*/
void Setup();
void SetupHosts(long unsigned int* POP_SIZE);
void SetupSymbionts(long unsigned int* total_syms);


/**
* Input: None.
*
Expand Down
94 changes: 35 additions & 59 deletions source/efficient_mode/EfficientWorldSetup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,48 @@
#define EFFWORLD_SETUP_C

#include "EfficientWorld.h"
#include "../ConfigSetup.h"
#include "EfficientSymbiont.h"
#include "EfficientHost.h"
#include "../default_mode/WorldSetup.cc"

void efficientWorldSetup(emp::Ptr<EfficientWorld> world, emp::Ptr<SymConfigBase> my_config) {
// params
emp::Random& random = world->GetRandom();

double start_moi = my_config->START_MOI();
long unsigned int POP_SIZE;
if (my_config->POP_SIZE() == -1) {
POP_SIZE = my_config->GRID_X() * my_config->GRID_Y();
} else {
POP_SIZE = my_config->POP_SIZE();
}
bool random_phen_host = false;
bool random_phen_sym = false;
if(my_config->HOST_INT() == -2 && !my_config->COMPETITION_MODE()) random_phen_host = true;
if(my_config->SYM_INT() == -2) random_phen_sym = true;

if(my_config->EFFICIENCY_MUT_RATE() == -1) my_config->EFFICIENCY_MUT_RATE(my_config->HORIZ_MUTATION_RATE());

if (my_config->GRID() == 0) {world->SetPopStruct_Mixed(false);}
else world->SetPopStruct_Grid(my_config->GRID_X(), my_config->GRID_Y(), false);
// settings

double comp_host_1 = 0;
double comp_host_2 = 0.95;



//inject hosts
for (size_t i = 0; i < POP_SIZE; i++){
/**
* Input: The number of efficient hosts.
*
* Output: None.
*
* Purpose: To populate the world with efficient hosts with appropriate phenotypes.
*/
void EfficientWorld::SetupHosts(long unsigned int* POP_SIZE) {
for (size_t i = 0; i < *POP_SIZE; i++) {
emp::Ptr<EfficientHost> new_org;

if (random_phen_host) {new_org.New(&random, world, my_config, random.GetDouble(-1, 1));
} else if (my_config->COMPETITION_MODE() && i%2==0) {
new_org.New(&random, world, my_config, comp_host_1);
} else if (my_config->COMPETITION_MODE() && i%2==1) {
new_org.New(&random, world, my_config, comp_host_2);
} else { new_org.New(&random, world, my_config, my_config->HOST_INT());
}
//Currently hacked because there isn't an AddOrg function, but there probably should be
if(my_config->GRID()) {
world->AddOrgAt(new_org, emp::WorldPosition(world->GetRandomCellID()));
} else {
world->AddOrgAt(new_org, world->size());
}
//world.Inject(*new_org);
new_org.New(&GetRandom(), this, my_config, my_config->HOST_INT());
InjectHost(new_org);
}
}

//sets up the world size
world->Resize(my_config->GRID_X(), my_config->GRID_Y());

//This loop must be outside of the host generation loop since otherwise
//syms try to inject into mostly empty spots at first
int total_syms = POP_SIZE * start_moi;
for (int j = 0; j < total_syms; j++){
double sym_int = 0;
if (random_phen_sym) {sym_int = random.GetDouble(-1,1);}
else {sym_int = my_config->SYM_INT();}

emp::Ptr<EfficientSymbiont> new_sym = emp::NewPtr<EfficientSymbiont>(&random, world, my_config, sym_int, 0, 1);
world->InjectSymbiont(new_sym);
/**
* Input: The number of efficient symbionts.
*
* Output: None.
*
* Purpose: To populate the world with efficient symbionts with appropriate phenotypes.
*/
void EfficientWorld::SetupSymbionts(long unsigned int* total_syms) {
for (size_t j = 0; j < *total_syms; j++) {
emp::Ptr<EfficientSymbiont> new_sym = emp::NewPtr<EfficientSymbiont>(&GetRandom(), this, my_config, my_config->SYM_INT(), 0, 1);
InjectSymbiont(new_sym);
}
}

/**
* Input: None.
*
* Output: None.
*
* Purpose: Prepare the world for a simulation by applying the configuration settings
* and populating the world with efficient hosts and efficient symbionts.
*/
void EfficientWorld::Setup() {
if (my_config->EFFICIENCY_MUT_RATE() == -1) my_config->EFFICIENCY_MUT_RATE(my_config->HORIZ_MUTATION_RATE());
SymWorld::Setup();
}
#endif
8 changes: 8 additions & 0 deletions source/lysis_mode/LysisWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class LysisWorld : public SymWorld {
if (data_node_cfu) data_node_cfu.Delete();
}


/**
* Definitions of setup functions, expanded in LysisWorldSetup.cc
*/
void SetupHosts(long unsigned int* POP_SIZE);
void SetupSymbionts(long unsigned int* total_syms);


/**
* Input: None.
*
Expand Down

0 comments on commit 108a1c3

Please sign in to comment.