-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulation.cpp
More file actions
executable file
·195 lines (174 loc) · 4.92 KB
/
population.cpp
File metadata and controls
executable file
·195 lines (174 loc) · 4.92 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Class automatically generated by Dev-C++ New Class wizard
//#include "mtrand.h"
#include "population.h" // class's header file
#include "BRand.hpp"
#include <math.h>
#include <vector>
#include <iostream>
#include <cstdlib>
#define PI 3.141592654
double population::s,population::h,population::hs;
void population::initialize(double sel, double dom)
{
s=sel;
h=dom;
hs=h*s;
}
// class constructor
population::population(int N)
{
alleleholders[0]=N;
alleleholders[1]=0;
alleleholders[2]=0;
size=N;
}
void population::mutateup(int n)
{
for (int i=0;i<n;i++)
{
if ((alleleholders[0]!=0)&&(BRand::Controller.nextOpened()>((0.5*alleleholders[1])/alleleholders[0])))
{alleleholders[0]-=1;
alleleholders[1]+=1;}
else if (alleleholders[1]!=0)
{alleleholders[1]-=1;
alleleholders[2]+=1;}
}
}
void population::mutatedown(int n)
{
for (int i=0;i<n;i++)
{
if ((alleleholders[2]!=0)&&(BRand::Controller.nextOpened()>((0.5*alleleholders[1])/alleleholders[2])))
{alleleholders[2]-=1;
alleleholders[1]+=1;}
else if (alleleholders[1]!=0)
{alleleholders[1]-=1;
alleleholders[0]+=1;}
}
}
void population::clear()
{
alleleholders[0]=size;
alleleholders[1]=0;
alleleholders[2]=0;
}
void population::fix()
{
alleleholders[0]=0;
alleleholders[1]=0;
alleleholders[2]=size;
}
double population::prob()
{
return (alleleholders[0]+(1.-hs)*alleleholders[1]*0.5)/(alleleholders[0]+(1.-hs)*alleleholders[1]+(1.-s)*alleleholders[2]);
}
void population::populate_from(population &p, int N)
{
populate_from(p.prob(),N);
}
void population::populate_from(double prob, int N)
{
alleleholders[0]=0;
alleleholders[1]=0;
alleleholders[2]=0;
if (N!=0) size=N;
if (prob<0.5) //drawing a binomial variate works faster when p<0.5
{alleleholders[0]=binom(size,prob*prob);
alleleholders[1]=binom(size-alleleholders[0],2*prob/(1+prob));
alleleholders[2]=size-alleleholders[0]-alleleholders[1];
}
else //drawing a binomial variate works faster when p<0.5
{prob=1-prob;
alleleholders[2]=binom(size,prob*prob);
alleleholders[1]=binom(size-alleleholders[2],2*prob/(1+prob));
alleleholders[0]=size-alleleholders[2]-alleleholders[1];
}
}
int population::allelenum()
{
return (alleleholders[1]+2*alleleholders[2]);
}
/*int population::binom(int n, double p)
{int x=0;
double y=0,c=log(1-p);
if (c==0)
{//std::cout<<"c is zero!\n";
return 0;}
while (1)
{
y+=ceil(log(BRand::Controller.nextOpened())/c);
if (y>n) return x;
x++;
}
}*/
int population::binom(int n, double pp)
{
int j;
static int nold=(-1);
double am,em,g,angle,p,bnl,sq,t,y;
static double pold=(-1.0),pc,plog,pclog,en,oldg;
p=(pp <= 0.5 ? pp : 1.0-pp);
am=n*p;
if (n < 25)
{bnl=0.0;
for (j=1;j<=n;j++)
if (BRand::Controller.nextOpened() < p) ++bnl; }
else if (am < 1.0) //Note to self: tried to change this 1.0 limit. Simulation perofrmance not senstive to small changes, eg 3.0.
{g=exp(-am);
t=1.0;
for (j=0;j<=n;j++)
{t *= BRand::Controller.nextOpened();
if (t < g) break;}
bnl=(j <= n ? j : n); }
else
{if (n != nold)
{ en=n;
oldg=lgamma(en+1.0); //was gammaln
nold=n;}
if (p != pold)
{pc=1.0-p;
plog=log(p);
pclog=log(pc);
pold=p;}
sq=sqrt(2.0*am*pc);
do
{do
{angle=PI*BRand::Controller.nextOpened();
y=tan(angle);
em=sq*y+am;
}
while (em < 0.0 || em >= (en+1.0));
em=floor(em);
t=1.2*sq*(1.0+y*y)*exp(oldg-lgamma(em+1.0)-lgamma(en-em+1.0)+em*plog+(en-em)*pclog);//was gammaln
// exp(oldg-gammln(em+1.0)-gammln(en-em+1.0)+em*plog+(en-em)*pclog) is p(em|n,p) and acts as the target PDF for em which a continuous variable. Therefore floor(em) is distributed binomially.
// sq*(1.0+y*y) is the actual PDF from which we're drawing em and we're using acception/rejection to get the target PDF.
}
while ( BRand::Controller.nextOpened() > t);
bnl=em;
}
if (p != pp) bnl=n-bnl;
return (int) bnl;
}
population& population::operator= (const population &Source)
{
// do the copy
size = Source.size;
alleleholders[0]=Source.alleleholders[0];
alleleholders[1]=Source.alleleholders[1];
alleleholders[2]=Source.alleleholders[2];
// return the existing object
return *this;
}
int population::choose_allele()
{double d=BRand::Controller.nextClosed();
if (d<(double(alleleholders[0])/size))
return 0;
else if (d>(1-double(alleleholders[2])/size))
return 2;
else
return 1;
}
population::~population()
{
std::cout<<"destructor called\n";
}