I'm trying to program the following: create a starting cohort of 1000 age-1 individuals of random lengths simulated from a uniform random on [650, 750]. Then, 1)determine the number that mature at age-1 according to a logistic function (pMat)and place these lengths in an outfile. Place the complement of the 1000 age-1 individuals into a second outfile Iimmatures). 3) determine the number of the immatures that survive to age-2, given a mean survival rate (= 0.75), and place these individuals in a third outfile. The random numbers used are also placed in separate outfiles to check that they are simulalated correctly.
the code runs correctly to the extent of creating the age-1 matures and immatures.
BUT the numbers surviving are not correct. The number of survivors ("Age2) is (wrongly) identical to the number of non-surviving age-1 immatures ("Morts1") and there are duplicate lengths among them (each length should be distinct). I am unsure what I need to do to correct this and obtain the correct coding.
#include <iostream>
#include <random>
#include <fstream>
usingnamespace std;
int main()
{
int nreps = 1000;
double c0 = -7.5;//intercept of logistic prob of maturation at length L
double c1 = 0.01;//logistic slope
double g0 = 0.97; //intercept of annual length growth increment
double g1 = 1.08; //slope of annual growth increment
double cvg = 0.05; //st deviation of normal distribution of annual growth increment.
double surv1 = 0.75; //mean probability of survival of cohort immatures to age2
double L2 = 0;
double L2B = 0;
double Age2 = 0;
double M1 = 0; // age 1 immature morts (non-survivors to age 2)
ofstream fout1("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Cohort.txt");//initial cohort at first age-at-maturity
ofstream fout2("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\R1.txt");//unif randoms for probability of maturation
ofstream fout3("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\R2.txt");// unif randoms for probabiliyt of survival of initial cohort immatures
ofstream fout4("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\pMat.txt");// length-specific maturation probability
ofstream fout5("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Mat1.txt"); //Age1 Matures
ofstream fout6("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Immat1.txt");//Age1 Immatures
ofstream fout7("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Morts1.txt");//Age 1 immatures that do not survive to age 2.
ofstream fout8("C:\\Users\\NickG2\\Documents\\Cpp\\VSProjects\\SimMauration\\Age2.txt");//Age2 survivors, both maturing and immatures
if (!fout1) {
cerr << "Problem opening the file.\n";
return 1;
}
uniform_real_distribution<double> d1(650.0, 750.0);// for simulating the length distribution of the initial cohort
uniform_real_distribution<double> d2(0.0, 1.0);// for probability of maturation of initial cohort
uniform_real_distribution<double> d3(0.0, 1.0);//for probability of cohort+immatures-to-age2 survival
random_device rd{};
mt19937 gen(rd());
for (int i = 0; i < nreps; ++i) {
double L1 = d1(gen);
double R1 = d2(gen);
double R2 = d3(gen);
fout1 << L1 << endl;
fout2 << R1 << endl;
fout3 << R2 << endl;
double pMat = (exp(c0 + c1 * L1) / (1 + exp(c0 + c1 * L1)));
fout4 << pMat << endl;
if (R1 < pMat) { //determine if individual at length L matures
double Mat = L1; // number of individuals in the initial cohort of 1000 age 1 individual that mature at age 1
fout5 << Mat << endl;// copy mature age 1 individualsto file
}
else {
L2 = L1; // number of individuals in the initial cohort that do not mature and remain immature
L2B = L1; // copy L2
fout6 << L2 << endl; //copy immature age 1 individuals to file.
}
if (R2 < surv1) { //probability that an immature of age 1 survives to the next age (age 2)
Age2 = L2B; //number of immature age 1 individuals that survive to age 2
}
else {
M1 = L2B;
fout7 << M1 << endl;
fout8 << Age2 << endl;
}
}
}
PS You might make your code legible and maybe even self documenting too if you choose sensible/non-cryptic variable names. 'L2B' as an example means bugger all to most people.
I suspect all of this undisciplined and un-planned coding is the solution to your otherwise simple problem.
I created a version of your code that just prints a table of the key values. Here are the first few lines:
pMat Len R1 R2 Mature? Survives
0.378 700 0.206 0.305 Y Y
0.406 712 0.27 0.757 Y N
0.335 681 0.519 0.189 N Y
0.305 668 0.642 0.711 N Y
0.284 658 0.837 0.662 N Y
0.419 717 0.346 0.545 Y Y
0.371 697 0.173 0.42 Y Y
0.362 693 0.505 0.594 N Y
0.336 682 0.367 0.428 N Y
Notice anything odd about the underlined output? This reflects a bug in your logic.