Skip to content

Commit

Permalink
Move int val code to new DefaultOrganism class
Browse files Browse the repository at this point in the history
  • Loading branch information
naalit committed Dec 1, 2022
1 parent ebf9b1a commit 857fca0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 144 deletions.
88 changes: 88 additions & 0 deletions source/default_mode/DefaultOrganism.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef DEFAULT_ORG_H
#define DEFAULT_ORG_H

#include "../Organism.h"

class DefaultOrganism : public virtual 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;

public:
DefaultOrganism(double interaction_val) : interaction_val(interaction_val) {
if (interaction_val == -2) {
interaction_val = random->GetDouble(-1, 1);
}
if (interaction_val > 1 || interaction_val < -1) {
// Exception for invalid interaction value
throw "Invalid interaction value. Must be between -1 and 1";
};
}

/**
* Input: None
*
* Output: The double representing the organism's interaction value
*
* Purpose: To get a organism's interaction value.
*/
double GetIntVal() const { return interaction_val; }

int GetPhyloBin() const override {
size_t num_phylo_bins = my_config->NUM_PHYLO_BINS();
// classify orgs into bins base on interaction values,
// inclusive of lower bound, exclusive of upper
float size_of_bin = 2.0 / num_phylo_bins;
double int_val = GetIntVal();
float prog = (int_val + 1);
prog = (prog / size_of_bin) + (0.0000000000001);
size_t bin = (size_t)prog;
if (bin >= num_phylo_bins)
bin = num_phylo_bins - 1;
return bin;
}

/**
* Input: The double representing the new interaction value of an organism
*
* Output: None
*
* Purpose: To set an organism's interaction value
*/
void SetIntVal(double _in) {
if (_in > 1 || _in < -1) {
// Exception for invalid interaction value
throw "Invalid interaction value. Must be between -1 and 1";
} else {
interaction_val = _in;
}
}

/**
* Input: None
*
* Output: None
*
* Purpose: To mutate an organism's interaction value. The mutation value is
* chosen from a normal distribution centered on 0 with the mutation size as
* the standard deviation.
*/
void Mutate() override {
double local_rate = my_config->MUTATION_RATE();
double local_size = my_config->MUTATION_SIZE();

if (random->GetDouble(0.0, 1.0) <= local_rate) {
interaction_val += random->GetRandNormal(0.0, local_size);
if (interaction_val < -1)
interaction_val = -1;
else if (interaction_val > 1)
interaction_val = 1;
}
}
};

#endif
80 changes: 3 additions & 77 deletions source/default_mode/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@
#include <sstream> // stringstream
#include <string>
#include "../Organism.h"
#include "DefaultOrganism.h"
#include "SymWorld.h"


class Host: public BaseHost {
class Host: public BaseHost, public DefaultOrganism {
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 set of in-progress "reproductive" symbionts belonging to a host. These are symbionts that aren't yet active.
Expand All @@ -45,16 +37,9 @@ class Host: public BaseHost {
Host(emp::Ptr<emp::Random> _random, emp::Ptr<SymWorld> _world, emp::Ptr<SymConfigBase> _config,
double _intval =0.0, emp::vector<emp::Ptr<BaseSymbiont>> _syms = {},
emp::vector<emp::Ptr<BaseSymbiont>> _repro_syms = {},
double _points = 0.0) : interaction_val(_intval), repro_syms(_repro_syms), Organism(_config, _world, _random, _points) {
double _points = 0.0) : DefaultOrganism(_intval), repro_syms(_repro_syms), Organism(_config, _world, _random, _points) {
// Base class members can't be initialized in the initializer list; later should add BaseHost ctor or remove _syms
syms = _syms;

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 Expand Up @@ -146,15 +131,6 @@ class Host: public BaseHost {
return "Host";
}

/**
* 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;}


/**
* Input: None
Expand All @@ -175,35 +151,6 @@ class Host: public BaseHost {
*/
double GetResInProcess() { return res_in_process;}

int GetPhyloBin() const override {
size_t num_phylo_bins = my_config->NUM_PHYLO_BINS();
//classify orgs into bins base on interaction values,
//inclusive of lower bound, exclusive of upper
float size_of_bin = 2.0 / num_phylo_bins;
double int_val = GetIntVal();
float prog = (int_val + 1);
prog = (prog/size_of_bin) + (0.0000000000001);
size_t bin = (size_t) prog;
if (bin >= num_phylo_bins) bin = num_phylo_bins - 1;
return bin;
}

/**
* Input: A double representing the host's new interaction value.
*
* Output: None
*
* Purpose: To set a host's interaction value.
*/
void SetIntVal(double _in) {
if ( _in > 1 || _in < -1) {
throw "Invalid interaction value. Must be between -1 and 1"; // Exception for invalid interaction value
}
else {
interaction_val = _in;
}
}


/**
* Input: A vector of pointers to organisms that will become a host's symbionts.
Expand Down Expand Up @@ -304,27 +251,6 @@ class Host: public BaseHost {
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() override {
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->GetRandNormal(0.0, mutation_size);
if(interaction_val < -1) interaction_val = -1;
else if (interaction_val > 1) interaction_val = 1;
}
}


/**
* 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.
Expand Down
72 changes: 5 additions & 67 deletions source/default_mode/Symbiont.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,20 @@

#include "../../Empirical/include/emp/math/Random.hpp"
#include "../../Empirical/include/emp/tools/string_utils.hpp"
#include "DefaultOrganism.h"
#include "SymWorld.h"
#include <set>
#include <iomanip> // setprecision
#include <sstream> // stringstream


class Symbiont: public BaseSymbiont {
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;

class Symbiont: public BaseSymbiont, public DefaultOrganism {
public:
/**
* The constructor for symbiont
*/
Symbiont(emp::Ptr<emp::Random> _random, emp::Ptr<SymWorld> _world, emp::Ptr<SymConfigBase> _config, double _intval=0.0, double _points = 0.0) : interaction_val(_intval), Organism(_config, _world, _random, _points) {
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
};
}
Symbiont(emp::Ptr<emp::Random> _random, emp::Ptr<SymWorld> _world, emp::Ptr<SymConfigBase> _config, double _intval=0.0, double _points = 0.0) :
DefaultOrganism(_intval), Organism(_config, _world, _random, _points) {}


/**
Expand Down Expand Up @@ -96,16 +81,6 @@ class Symbiont: public BaseSymbiont {
}


/**
* Input: None
*
* Output: The double representing the symbiont's interaction value
*
* Purpose: To get a symbiont's interaction value.
*/
double GetIntVal() const {return interaction_val;}


/**
* Input: None
*
Expand All @@ -127,35 +102,6 @@ class Symbiont: public BaseSymbiont {

// std::set<int> GetResTypes() const {return res_types;}

int GetPhyloBin() const override {
size_t num_phylo_bins = my_config->NUM_PHYLO_BINS();
//classify orgs into bins base on interaction values,
//inclusive of lower bound, exclusive of upper
float size_of_bin = 2.0 / num_phylo_bins;
double int_val = GetIntVal();
float prog = (int_val + 1);
prog = (prog/size_of_bin) + (0.0000000000001);
size_t bin = (size_t) prog;
if (bin >= num_phylo_bins) bin = num_phylo_bins - 1;
return bin;
}

/**
* Input: The double representing the new interaction value of a symbiont
*
* Output: None
*
* Purpose: To set a symbiont's interaction value
*/
void SetIntVal(double _in) {
if ( _in > 1 || _in < -1) {
throw "Invalid interaction value. Must be between -1 and 1"; // Exception for invalid interaction value
}
else {
interaction_val = _in;
}
}

//void SetResTypes(std::set<int> _in) {res_types = _in;}


Expand All @@ -182,15 +128,7 @@ class Symbiont: public BaseSymbiont {
*/
void Mutate() override {
BaseSymbiont::Mutate();

double local_rate = my_config->MUTATION_RATE();
double local_size = my_config->MUTATION_SIZE();

if (random->GetDouble(0.0, 1.0) <= local_rate) {
interaction_val += random->GetRandNormal(0.0, local_size);
if(interaction_val < -1) interaction_val = -1;
else if (interaction_val > 1) interaction_val = 1;
}
DefaultOrganism::Mutate();
}

/**
Expand Down

0 comments on commit 857fca0

Please sign in to comment.