forked from abbywlsn/miniphylotrees
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullModel.cpp
More file actions
158 lines (124 loc) · 5.42 KB
/
NullModel.cpp
File metadata and controls
158 lines (124 loc) · 5.42 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
#include <iostream>
#include <vector>
#include <memory>
#include <cassert>
#include <assert.h>
#include "Evolve/Systematics.hpp"
#include "config/config.hpp"
#include "config/command_line.hpp"
#include "config/ArgManager.hpp"
//need to go through each generation 1000 times
//increment generations by 1
//change populaltion to 10, 100, 1000
EMP_BUILD_CONFIG(NullModelConfig,
GROUP(MAIN, "Tree Settings"),
VALUE(MUTATION_RATE, double, 0.05, "This is my mutation rate."),
VALUE(POP_SIZE, int, 10, "This is my population size."),
VALUE(GENERATION_COUNT, int, 10, "This is my number of generations."),
VALUE(RANDOM_SEED, int, -1, "Seed for random number generator"),
);
NullModelConfig config;
// double config.MUTATION_RATE() = config.MUTATION_RATE();
// int config.POP_SIZE() = config.POP_SIZE();
int parentNum;
// int gen = gen;
// int TenGens = 10;
class Organism {
public:
int genotype = 0;
Organism() { //default constructor sets genotype to 0
}
Organism(int _genotype) { //this constructor sets genotype to the parent genotype
genotype = _genotype;
}
int MutateGenotype(emp::Random &RandNum) {
double randMutation = RandNum.GetDouble(0, 1);
assert(randMutation != 0 && randMutation > 0);
if (randMutation < config.MUTATION_RATE()) {
int MutatedGenotype = genotype - RandNum.GetInt(-3, 3);
genotype = MutatedGenotype;
//cout << "mutated genotype = " << genotype << endl;
}
//cout << "GENOTYPE: " << genotype << endl;
return genotype;
}
};
int chooseOrg(std::vector<Organism> ¤tGen, emp::Random &randNum){
parentNum = randNum.GetInt((int) currentGen.size()); //chooses random spot in array for parent
return parentNum;
}
void switchGens(std::vector<Organism> ¤tGen, std::vector<Organism> &childGen, emp::Systematics<Organism, int> &sys){
currentGen.swap(childGen);
childGen.clear();
sys.Update();
}
bool writeToFile(std::string filename, int field_one, bool skip_line){
if(skip_line == true){
std::ofstream file;
file.open(filename, std::ios_base::app);
file << " " << std::endl;
file.close();
}else{
std::ofstream file;
file.open(filename, std::ios_base::app);
file << field_one << ",";
file.close();
}
return true;
}
int main(int argc, char * argv[]) {
//NullModelConfig config;
auto args = emp::cl::ArgManager(argc, argv);
if (args.ProcessConfigOptions(config, std::cout, "NullModelConfig.cfg", "My-macros.h") == false) exit(0);
if(args.TestUnknown() == false) exit(0); //if there are leftover args throw an error
std::cout << "==============================" << std::endl;
std::cout << "| How am I configured? |" << std::endl;
std::cout << "==============================" << std::endl;
config.Write(std::cout);
std::cout << "==============================\n" << std::endl;
emp::Random randNum(config.RANDOM_SEED());
for(int gen = 1; gen <= 5000; gen++){
for(int run = 1; run <= 1000; run++){
//std::cout << config.POP_SIZE() << std::endl;
std::function<int(Organism)> taxonFunc = [](Organism org){return org.genotype;};
emp::Systematics<Organism, int> sys(taxonFunc); //optional 3rd arg
sys.SetTrackSynchronous(true);
//current gen (vector)
std::vector<Organism> currentGen; //begins with currentGen
//child gen (vector)
std::vector<Organism> childGen;
for (int i = 0; i < config.POP_SIZE(); i++) {
currentGen.emplace_back(); //currentGen is filled with 10 organism
sys.AddOrg(currentGen[i], i); //parent is null (removed brackets)
}
// for(int i = 0; i < currentGen.size(); i++){
// cout << currentGen[i] . printVect() << " " << endl;
// }
for (int i = 0; i < gen; i++) {
for(int r = 0; r < config.POP_SIZE(); r++){
chooseOrg(currentGen, randNum);
sys.SetNextParent(parentNum);
//currentGen[parentNum].reproduce(childGen, sys); //fills childGen with 10 Organisms
childGen.emplace_back(currentGen[parentNum].genotype); //fills childGen vector with Organisms
childGen[r].MutateGenotype(randNum);
emp::WorldPosition pos(r, 1);
sys.AddOrg(childGen[r], pos); //removed brackets childGen[i], {i, 0}
}
//sys.PrintStatus();
if(i == gen - 1){
int phylogenetic_diversity = sys.GetPhylogeneticDiversity();
writeToFile("NullModelResults_10.csv", phylogenetic_diversity, false);
//so we want to record 10, 100, and 1000 organisms
//we want to have 0 through 5000 generations
}
for(int j = 0; j < (int) currentGen.size(); j++){
sys.RemoveOrg(j);
}
switchGens(currentGen,childGen, sys); //puts contents of child vector into current vector and deletes content of child vector
}
int total_orgs = gen * config.POP_SIZE();
// std::cout << "generations: " << gen << " / total organisms: " << total_orgs << std::endl;
}
writeToFile("NullModelResults_10.csv", 0, true);
}
};