Skip to content

Commit 75692b9

Browse files
committed
Move int val code to new DefaultOrganism class
1 parent 5d62302 commit 75692b9

3 files changed

Lines changed: 96 additions & 144 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#ifndef DEFAULT_ORG_H
2+
#define DEFAULT_ORG_H
3+
4+
#include "../Organism.h"
5+
6+
class DefaultOrganism : public virtual Organism {
7+
protected:
8+
/**
9+
* Purpose: Represents the interaction value between the host and symbiont.
10+
* A negative interaction value represent antagonism, while a positive
11+
* one represents mutualism. Zero is a neutral value.
12+
*/
13+
double interaction_val = 0;
14+
15+
public:
16+
DefaultOrganism(double interaction_val) : interaction_val(interaction_val) {
17+
if (interaction_val == -2) {
18+
interaction_val = random->GetDouble(-1, 1);
19+
}
20+
if (interaction_val > 1 || interaction_val < -1) {
21+
// Exception for invalid interaction value
22+
throw "Invalid interaction value. Must be between -1 and 1";
23+
};
24+
}
25+
26+
/**
27+
* Input: None
28+
*
29+
* Output: The double representing the organism's interaction value
30+
*
31+
* Purpose: To get a organism's interaction value.
32+
*/
33+
double GetIntVal() const { return interaction_val; }
34+
35+
int GetPhyloBin() const override {
36+
size_t num_phylo_bins = my_config->NUM_PHYLO_BINS();
37+
// classify orgs into bins base on interaction values,
38+
// inclusive of lower bound, exclusive of upper
39+
float size_of_bin = 2.0 / num_phylo_bins;
40+
double int_val = GetIntVal();
41+
float prog = (int_val + 1);
42+
prog = (prog / size_of_bin) + (0.0000000000001);
43+
size_t bin = (size_t)prog;
44+
if (bin >= num_phylo_bins)
45+
bin = num_phylo_bins - 1;
46+
return bin;
47+
}
48+
49+
/**
50+
* Input: The double representing the new interaction value of an organism
51+
*
52+
* Output: None
53+
*
54+
* Purpose: To set an organism's interaction value
55+
*/
56+
void SetIntVal(double _in) {
57+
if (_in > 1 || _in < -1) {
58+
// Exception for invalid interaction value
59+
throw "Invalid interaction value. Must be between -1 and 1";
60+
} else {
61+
interaction_val = _in;
62+
}
63+
}
64+
65+
/**
66+
* Input: None
67+
*
68+
* Output: None
69+
*
70+
* Purpose: To mutate an organism's interaction value. The mutation value is
71+
* chosen from a normal distribution centered on 0 with the mutation size as
72+
* the standard deviation.
73+
*/
74+
void Mutate() override {
75+
double local_rate = my_config->MUTATION_RATE();
76+
double local_size = my_config->MUTATION_SIZE();
77+
78+
if (random->GetDouble(0.0, 1.0) <= local_rate) {
79+
interaction_val += random->GetRandNormal(0.0, local_size);
80+
if (interaction_val < -1)
81+
interaction_val = -1;
82+
else if (interaction_val > 1)
83+
interaction_val = 1;
84+
}
85+
}
86+
};
87+
88+
#endif

source/default_mode/Host.h

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@
77
#include <sstream> // stringstream
88
#include <string>
99
#include "../Organism.h"
10+
#include "DefaultOrganism.h"
1011
#include "SymWorld.h"
1112

1213

13-
class Host: public BaseHost {
14+
class Host: public BaseHost, public DefaultOrganism {
1415
protected:
15-
/**
16-
*
17-
* Purpose: Represents the interaction value between the host and symbiont.
18-
* A negative interaction value represent antagonism, while a positive
19-
* one represents mutualism. Zero is a neutral value.
20-
*
21-
*/
22-
double interaction_val = 0;
23-
2416
/**
2517
*
2618
* 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 {
4537
Host(emp::Ptr<emp::Random> _random, emp::Ptr<SymWorld> _world, emp::Ptr<SymConfigBase> _config,
4638
double _intval =0.0, emp::vector<emp::Ptr<BaseSymbiont>> _syms = {},
4739
emp::vector<emp::Ptr<BaseSymbiont>> _repro_syms = {},
48-
double _points = 0.0) : interaction_val(_intval), repro_syms(_repro_syms), Organism(_config, _world, _random, _points) {
40+
double _points = 0.0) : DefaultOrganism(_intval), repro_syms(_repro_syms), Organism(_config, _world, _random, _points) {
4941
// Base class members can't be initialized in the initializer list; later should add BaseHost ctor or remove _syms
5042
syms = _syms;
51-
52-
if (_intval == -2) {
53-
interaction_val = random->GetDouble(-1, 1);
54-
}
55-
if (interaction_val > 1 || interaction_val < -1) {
56-
throw "Invalid interaction value. Must be between -1 and 1"; // Exception for invalid interaction value
57-
};
5843
}
5944

6045
/**
@@ -146,15 +131,6 @@ class Host: public BaseHost {
146131
return "Host";
147132
}
148133

149-
/**
150-
* Input: None
151-
*
152-
* Output: The double representing host's interaction value
153-
*
154-
* Purpose: To get the double representing host's interaction value
155-
*/
156-
double GetIntVal() const { return interaction_val;}
157-
158134

159135
/**
160136
* Input: None
@@ -175,35 +151,6 @@ class Host: public BaseHost {
175151
*/
176152
double GetResInProcess() { return res_in_process;}
177153

178-
int GetPhyloBin() const override {
179-
size_t num_phylo_bins = my_config->NUM_PHYLO_BINS();
180-
//classify orgs into bins base on interaction values,
181-
//inclusive of lower bound, exclusive of upper
182-
float size_of_bin = 2.0 / num_phylo_bins;
183-
double int_val = GetIntVal();
184-
float prog = (int_val + 1);
185-
prog = (prog/size_of_bin) + (0.0000000000001);
186-
size_t bin = (size_t) prog;
187-
if (bin >= num_phylo_bins) bin = num_phylo_bins - 1;
188-
return bin;
189-
}
190-
191-
/**
192-
* Input: A double representing the host's new interaction value.
193-
*
194-
* Output: None
195-
*
196-
* Purpose: To set a host's interaction value.
197-
*/
198-
void SetIntVal(double _in) {
199-
if ( _in > 1 || _in < -1) {
200-
throw "Invalid interaction value. Must be between -1 and 1"; // Exception for invalid interaction value
201-
}
202-
else {
203-
interaction_val = _in;
204-
}
205-
}
206-
207154

208155
/**
209156
* Input: A vector of pointers to organisms that will become a host's symbionts.
@@ -304,27 +251,6 @@ class Host: public BaseHost {
304251
return host_baby;
305252
}
306253

307-
/**
308-
* Input: None
309-
*
310-
* Output: None
311-
*
312-
* Purpose: To mutate a host's interaction value. This is called on newly generated
313-
* hosts to allow for evolution to occur.
314-
*/
315-
void Mutate() override {
316-
double mutation_size = my_config->HOST_MUTATION_SIZE();
317-
if (mutation_size == -1) mutation_size = my_config->MUTATION_SIZE();
318-
double mutation_rate = my_config->HOST_MUTATION_RATE();
319-
if (mutation_rate == -1) mutation_rate = my_config->MUTATION_RATE();
320-
321-
if(random->GetDouble(0.0, 1.0) <= mutation_rate){
322-
interaction_val += random->GetRandNormal(0.0, mutation_size);
323-
if(interaction_val < -1) interaction_val = -1;
324-
else if (interaction_val > 1) interaction_val = 1;
325-
}
326-
}
327-
328254

329255
/**
330256
* 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.

source/default_mode/Symbiont.h

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,20 @@
33

44
#include "../../Empirical/include/emp/math/Random.hpp"
55
#include "../../Empirical/include/emp/tools/string_utils.hpp"
6+
#include "DefaultOrganism.h"
67
#include "SymWorld.h"
78
#include <set>
89
#include <iomanip> // setprecision
910
#include <sstream> // stringstream
1011

1112

12-
class Symbiont: public BaseSymbiont {
13-
protected:
14-
/**
15-
*
16-
* Purpose: Represents the interaction value between the host and symbiont.
17-
* A negative interaction value represent antagonism, while a positive
18-
* one represents mutualism. Zero is a neutral value.
19-
*
20-
*/
21-
double interaction_val = 0;
22-
13+
class Symbiont: public BaseSymbiont, public DefaultOrganism {
2314
public:
2415
/**
2516
* The constructor for symbiont
2617
*/
27-
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) {
28-
if (_intval == -2) {
29-
interaction_val = random->GetDouble(-1, 1);
30-
}
31-
if (interaction_val > 1 || interaction_val < -1) {
32-
throw "Invalid interaction value. Must be between -1 and 1"; // Exception for invalid interaction value
33-
};
34-
}
18+
Symbiont(emp::Ptr<emp::Random> _random, emp::Ptr<SymWorld> _world, emp::Ptr<SymConfigBase> _config, double _intval=0.0, double _points = 0.0) :
19+
DefaultOrganism(_intval), Organism(_config, _world, _random, _points) {}
3520

3621

3722
/**
@@ -96,16 +81,6 @@ class Symbiont: public BaseSymbiont {
9681
}
9782

9883

99-
/**
100-
* Input: None
101-
*
102-
* Output: The double representing the symbiont's interaction value
103-
*
104-
* Purpose: To get a symbiont's interaction value.
105-
*/
106-
double GetIntVal() const {return interaction_val;}
107-
108-
10984
/**
11085
* Input: None
11186
*
@@ -127,35 +102,6 @@ class Symbiont: public BaseSymbiont {
127102

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

130-
int GetPhyloBin() const override {
131-
size_t num_phylo_bins = my_config->NUM_PHYLO_BINS();
132-
//classify orgs into bins base on interaction values,
133-
//inclusive of lower bound, exclusive of upper
134-
float size_of_bin = 2.0 / num_phylo_bins;
135-
double int_val = GetIntVal();
136-
float prog = (int_val + 1);
137-
prog = (prog/size_of_bin) + (0.0000000000001);
138-
size_t bin = (size_t) prog;
139-
if (bin >= num_phylo_bins) bin = num_phylo_bins - 1;
140-
return bin;
141-
}
142-
143-
/**
144-
* Input: The double representing the new interaction value of a symbiont
145-
*
146-
* Output: None
147-
*
148-
* Purpose: To set a symbiont's interaction value
149-
*/
150-
void SetIntVal(double _in) {
151-
if ( _in > 1 || _in < -1) {
152-
throw "Invalid interaction value. Must be between -1 and 1"; // Exception for invalid interaction value
153-
}
154-
else {
155-
interaction_val = _in;
156-
}
157-
}
158-
159105
//void SetResTypes(std::set<int> _in) {res_types = _in;}
160106

161107

@@ -182,15 +128,7 @@ class Symbiont: public BaseSymbiont {
182128
*/
183129
void Mutate() override {
184130
BaseSymbiont::Mutate();
185-
186-
double local_rate = my_config->MUTATION_RATE();
187-
double local_size = my_config->MUTATION_SIZE();
188-
189-
if (random->GetDouble(0.0, 1.0) <= local_rate) {
190-
interaction_val += random->GetRandNormal(0.0, local_size);
191-
if(interaction_val < -1) interaction_val = -1;
192-
else if (interaction_val > 1) interaction_val = 1;
193-
}
131+
DefaultOrganism::Mutate();
194132
}
195133

196134
/**

0 commit comments

Comments
 (0)