Process terminated with status -1073741819
using namespace std;
int main(int argc, char* argv[])
{
const int N=100; // 基础群大小
const int s=4; //公母比例nf/nm
const int no=8; //每头母畜每胎所产后代数
const double P1=0.25,P2=0.50; //基因型频率,11,12,22(概率P3为1-P1-P2=0.25)
const int g=20; //考察世代数
vector<vector<int> > A(N, vector<int>(3,0)); // 用来存放个体号,性别(1为雄,2为雌)和基因型(11,12,22)
int nm=0,nf=0; //F0代公母畜的个体数
double u2;
//产生服从(0,1)之间均匀分布的随机数
boost::mt19937 rng;
boost::uniform_real<> one(0,1);
boost::variate_generator<boost::mt19937&, boost::uniform_real<> > die(rng, one);
// 确定个体性别(非随机法)
for (int i=0;i<N/(1+s);i++)
A[i][1]=1; //公畜个体
for (int i=(N/(1+s));i<N;i++)
A[i][1]=2; //母畜个体
//个体基因型确定(随机法)
for (int i=0;i<N;i++){
A[i][0]=i; //个体号
u2=die();
if (u2<P1)
A[i][2]=11; //11型个体
else{
if (u2>=P1&&u2<P1+P2)
A[i][2]=12; //12型个体
else
A[i][2]=22; //22型个体
}
}
//确定雌雄个体数
nm=N/(1+s); // 公畜个体数
nf=N-nm; //母畜个体数
for(int i=1;i<=g;i++){
// 交配组合的确定
int u;
// 随机交配,结果记录到vector B中
vector<vector<int> > B(nf*no, vector<int>(5,0)); // 用来存放F1代个体号,父亲个体号,母亲个体号,性别,基因型
for(int i=0;i<nm;i++){
for(int j=0;j<s;j++){
static int b=0; //使母畜号逐渐往后推移
static int c=0; //记录F1代的个体号
double a= die(); //0到1的随机数
u = int(nf*a)+nm+b; //随机抽取的母畜个体号
A[nm].swap(A[u]); //交换第nm行和第u行
B[c][0]=c; //F1代个体号
B[c][1]=i; //F1代个体父亲
B[c][2]=u; //F1代个体母亲
b++;
c++; //F1个体号累加
}
}
//后代性别和基因型的确定
for(int i=0;i<nf*no;i++){
//性别确定
double u1;
u1=die();
if (u1<0.5)
B[i][3]=1;
else
B[i][3]=2;
//基因型确定
int m,f;
double u3,u4;
//父本
if (A[B[i][1]][2]=11)
m=1;
else {
if(A[B[i][1]][2]=22)
m=2;
else{
u3=die();
if(u3<0.5)
m=1;
else
m=2;
}
}
//母本
if (A[B[i][1]][2]=11)
f=1;
else {
if(A[B[i][2]][2]=22)
f=2;
else{
u3=die();
if(u4<0.5)
f=1;
else
f=2;
}
}
B[i][4]=f*10+m;
}
//随机选留nm个公畜,nf个母畜
//计算基因频率
int n11,n12,n22; //各种基因型的个体的数目
n11=n12=n22=0;
double p1,p2; //等位基因1和2的频率
double p11,p12,p22; //基因型频率
for(int i=0;i<no*nf;i++){ //计算F1代各种基因型的个体数
switch(B[i][4]){
case 11:
n11++;
case 12:
n12++;
case 22:
n22++;
}
}
p1=(n11*2+n12)/(2*no*nf); //等位基因1的基因频率
p2=1-p1; //等位基因2的基因频率
p11=double(n11)/double(no*nf); //基因型11的基因型频率
p12=double(n12)/double(no*nf); //基因型12的基因型频率
p22=double(n22)/double(no*nf); //基因型22的基因型频率
//输出每代的基因型频率
ofstream outfile("./drift.txt");
outfile << "代数:"<<g<<endl;
outfile << "p11:"<<p11<< endl;
outfile << "p12:"<<p12<< endl;
outfile << "p22:"<<p22<< endl;
outfile.close();
}
return 0;
}