From 857fca084265dc3b43128e33b616fb98cd9c8692 Mon Sep 17 00:00:00 2001 From: tolziplohu Date: Thu, 1 Dec 2022 17:34:10 -0600 Subject: [PATCH] Move int val code to new DefaultOrganism class --- source/default_mode/DefaultOrganism.h | 88 +++++++++++++++++++++++++++ source/default_mode/Host.h | 80 +----------------------- source/default_mode/Symbiont.h | 72 ++-------------------- 3 files changed, 96 insertions(+), 144 deletions(-) create mode 100644 source/default_mode/DefaultOrganism.h diff --git a/source/default_mode/DefaultOrganism.h b/source/default_mode/DefaultOrganism.h new file mode 100644 index 00000000..ed39a45a --- /dev/null +++ b/source/default_mode/DefaultOrganism.h @@ -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 \ No newline at end of file diff --git a/source/default_mode/Host.h b/source/default_mode/Host.h index 894282f2..f62d4273 100644 --- a/source/default_mode/Host.h +++ b/source/default_mode/Host.h @@ -7,20 +7,12 @@ #include // stringstream #include #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. @@ -45,16 +37,9 @@ class Host: public BaseHost { Host(emp::Ptr _random, emp::Ptr _world, emp::Ptr _config, double _intval =0.0, emp::vector> _syms = {}, emp::vector> _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 - }; } /** @@ -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 @@ -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. @@ -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. diff --git a/source/default_mode/Symbiont.h b/source/default_mode/Symbiont.h index 84bf2188..d9dd208d 100644 --- a/source/default_mode/Symbiont.h +++ b/source/default_mode/Symbiont.h @@ -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 #include // setprecision #include // 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 _random, emp::Ptr _world, emp::Ptr _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 _random, emp::Ptr _world, emp::Ptr _config, double _intval=0.0, double _points = 0.0) : + DefaultOrganism(_intval), Organism(_config, _world, _random, _points) {} /** @@ -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 * @@ -127,35 +102,6 @@ class Symbiont: public BaseSymbiont { // std::set 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 _in) {res_types = _in;} @@ -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(); } /**